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