fix a few typos (#2141)
[oweals/peertube.git] / server / tests / api / check-params / services.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4
5 import {
6   cleanupTests,
7   flushAndRunServer,
8   makeGetRequest,
9   ServerInfo,
10   setAccessTokensToServers,
11   uploadVideo
12 } from '../../../../shared/extra-utils'
13
14 describe('Test services API validators', function () {
15   let server: ServerInfo
16
17   // ---------------------------------------------------------------
18
19   before(async function () {
20     this.timeout(60000)
21
22     server = await flushAndRunServer(1)
23     await setAccessTokensToServers([ server ])
24
25     const res = await uploadVideo(server.url, server.accessToken, { name: 'my super name' })
26     server.video = res.body.video
27   })
28
29   describe('Test oEmbed API validators', function () {
30
31     it('Should fail with an invalid url', async function () {
32       const embedUrl = 'hello.com'
33       await checkParamEmbed(server, embedUrl)
34     })
35
36     it('Should fail with an invalid host', async function () {
37       const embedUrl = 'http://hello.com/videos/watch/' + server.video.uuid
38       await checkParamEmbed(server, embedUrl)
39     })
40
41     it('Should fail with an invalid video id', async function () {
42       const embedUrl = `http://localhost:${server.port}/videos/watch/blabla`
43       await checkParamEmbed(server, embedUrl)
44     })
45
46     it('Should fail with an unknown video', async function () {
47       const embedUrl = `http://localhost:${server.port}/videos/watch/88fc0165-d1f0-4a35-a51a-3b47f668689c`
48       await checkParamEmbed(server, embedUrl, 404)
49     })
50
51     it('Should fail with an invalid path', async function () {
52       const embedUrl = `http://localhost:${server.port}/videos/watchs/${server.video.uuid}`
53
54       await checkParamEmbed(server, embedUrl)
55     })
56
57     it('Should fail with an invalid max height', async function () {
58       const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}`
59
60       await checkParamEmbed(server, embedUrl, 400, { maxheight: 'hello' })
61     })
62
63     it('Should fail with an invalid max width', async function () {
64       const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}`
65
66       await checkParamEmbed(server, embedUrl, 400, { maxwidth: 'hello' })
67     })
68
69     it('Should fail with an invalid format', async function () {
70       const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}`
71
72       await checkParamEmbed(server, embedUrl, 400, { format: 'blabla' })
73     })
74
75     it('Should fail with a non supported format', async function () {
76       const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}`
77
78       await checkParamEmbed(server, embedUrl, 501, { format: 'xml' })
79     })
80
81     it('Should succeed with the correct params', async function () {
82       const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}`
83       const query = {
84         format: 'json',
85         maxheight: 400,
86         maxwidth: 400
87       }
88
89       await checkParamEmbed(server, embedUrl, 200, query)
90     })
91   })
92
93   after(async function () {
94     await cleanupTests([ server ])
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 }