findById -> findByPk
[oweals/peertube.git] / server / tests / api / check-params / services.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4
5 import {
6   flushTests,
7   killallServers,
8   makeGetRequest,
9   runServer,
10   ServerInfo,
11   setAccessTokensToServers,
12   uploadVideo
13 } from '../../../../shared/utils'
14
15 describe('Test services API validators', function () {
16   let server: ServerInfo
17
18   // ---------------------------------------------------------------
19
20   before(async function () {
21     this.timeout(60000)
22
23     await flushTests()
24
25     server = await runServer(1)
26     await setAccessTokensToServers([ server ])
27
28     const res = await uploadVideo(server.url, server.accessToken, { name: 'my super name' })
29     server.video = res.body.video
30   })
31
32   describe('Test oEmbed API validators', function () {
33
34     it('Should fail with an invalid url', async function () {
35       const embedUrl = 'hello.com'
36       await checkParamEmbed(server, embedUrl)
37     })
38
39     it('Should fail with an invalid host', async function () {
40       const embedUrl = 'http://hello.com/videos/watch/' + server.video.uuid
41       await checkParamEmbed(server, embedUrl)
42     })
43
44     it('Should fail with an invalid video id', async function () {
45       const embedUrl = 'http://localhost:9001/videos/watch/blabla'
46       await checkParamEmbed(server, embedUrl)
47     })
48
49     it('Should fail with an unknown video', async function () {
50       const embedUrl = 'http://localhost:9001/videos/watch/88fc0165-d1f0-4a35-a51a-3b47f668689c'
51       await checkParamEmbed(server, embedUrl, 404)
52     })
53
54     it('Should fail with an invalid path', async function () {
55       const embedUrl = 'http://localhost:9001/videos/watchs/' + server.video.uuid
56
57       await checkParamEmbed(server, embedUrl)
58     })
59
60     it('Should fail with an invalid max height', async function () {
61       const embedUrl = 'http://localhost:9001/videos/watch/' + server.video.uuid
62
63       await checkParamEmbed(server, embedUrl, 400, { maxheight: 'hello' })
64     })
65
66     it('Should fail with an invalid max width', async function () {
67       const embedUrl = 'http://localhost:9001/videos/watch/' + server.video.uuid
68
69       await checkParamEmbed(server, embedUrl, 400, { maxwidth: 'hello' })
70     })
71
72     it('Should fail with an invalid format', async function () {
73       const embedUrl = 'http://localhost:9001/videos/watch/' + server.video.uuid
74
75       await checkParamEmbed(server, embedUrl, 400, { format: 'blabla' })
76     })
77
78     it('Should fail with a non supported format', async function () {
79       const embedUrl = 'http://localhost:9001/videos/watch/' + server.video.uuid
80
81       await checkParamEmbed(server, embedUrl, 501, { format: 'xml' })
82     })
83
84     it('Should succeed with the correct params', async function () {
85       const embedUrl = 'http://localhost:9001/videos/watch/' + server.video.uuid
86       const query = {
87         format: 'json',
88         maxheight: 400,
89         maxwidth: 400
90       }
91
92       await checkParamEmbed(server, embedUrl, 200, query)
93     })
94   })
95
96   after(async function () {
97     killallServers([ server ])
98
99     // Keep the logs if the test failed
100     if (this['ok']) {
101       await flushTests()
102     }
103   })
104 })
105
106 function checkParamEmbed (server: ServerInfo, embedUrl: string, statusCodeExpected = 400, query = {}) {
107   const path = '/services/oembed'
108
109   return makeGetRequest({
110     url: server.url,
111     path,
112     query: Object.assign(query, { url: embedUrl }),
113     statusCodeExpected
114   })
115 }