findById -> findByPk
[oweals/peertube.git] / server / tests / api / check-params / video-captions.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4 import {
5   createUser,
6   flushTests,
7   killallServers,
8   makeDeleteRequest,
9   makeGetRequest,
10   makeUploadRequest,
11   runServer,
12   ServerInfo,
13   setAccessTokensToServers,
14   uploadVideo,
15   userLogin
16 } from '../../../../shared/utils'
17 import { join } from 'path'
18 import { createVideoCaption } from '../../../../shared/utils/videos/video-captions'
19
20 describe('Test video captions API validator', function () {
21   const path = '/api/v1/videos/'
22
23   let server: ServerInfo
24   let userAccessToken: string
25   let videoUUID: string
26
27   // ---------------------------------------------------------------
28
29   before(async function () {
30     this.timeout(30000)
31
32     await flushTests()
33
34     server = await runServer(1)
35
36     await setAccessTokensToServers([ server ])
37
38     {
39       const res = await uploadVideo(server.url, server.accessToken, {})
40       videoUUID = res.body.video.uuid
41     }
42
43     {
44       const user = {
45         username: 'user1',
46         password: 'my super password'
47       }
48       await createUser(server.url, server.accessToken, user.username, user.password)
49       userAccessToken = await userLogin(server, user)
50     }
51   })
52
53   describe('When adding video caption', function () {
54     const fields = { }
55     const attaches = {
56       'captionfile': join(__dirname, '..', '..', 'fixtures', 'subtitle-good1.vtt')
57     }
58
59     it('Should fail without a valid uuid', async function () {
60       await makeUploadRequest({
61         method: 'PUT',
62         url: server.url,
63         path: path + '4da6fde3-88f7-4d16-b119-108df563d0b06/captions',
64         token: server.accessToken,
65         fields,
66         attaches
67       })
68     })
69
70     it('Should fail with an unknown id', async function () {
71       await makeUploadRequest({
72         method: 'PUT',
73         url: server.url,
74         path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions',
75         token: server.accessToken,
76         fields,
77         attaches
78       })
79     })
80
81     it('Should fail with a missing language in path', async function () {
82       const captionPath = path + videoUUID + '/captions'
83       await makeUploadRequest({
84         method: 'PUT',
85         url: server.url,
86         path: captionPath,
87         token: server.accessToken,
88         fields,
89         attaches
90       })
91     })
92
93     it('Should fail with an unknown language', async function () {
94       const captionPath = path + videoUUID + '/captions/15'
95       await makeUploadRequest({
96         method: 'PUT',
97         url: server.url,
98         path: captionPath,
99         token: server.accessToken,
100         fields,
101         attaches
102       })
103     })
104
105     it('Should fail without access token', async function () {
106       const captionPath = path + videoUUID + '/captions/fr'
107       await makeUploadRequest({
108         method: 'PUT',
109         url: server.url,
110         path: captionPath,
111         fields,
112         attaches,
113         statusCodeExpected: 401
114       })
115     })
116
117     it('Should fail with a bad access token', async function () {
118       const captionPath = path + videoUUID + '/captions/fr'
119       await makeUploadRequest({
120         method: 'PUT',
121         url: server.url,
122         path: captionPath,
123         token: 'blabla',
124         fields,
125         attaches,
126         statusCodeExpected: 401
127       })
128     })
129
130     it('Should fail with an invalid captionfile extension', async function () {
131       const attaches = {
132         'captionfile': join(__dirname, '..', '..', 'fixtures', 'subtitle-bad.txt')
133       }
134
135       const captionPath = path + videoUUID + '/captions/fr'
136       await makeUploadRequest({
137         method: 'PUT',
138         url: server.url,
139         path: captionPath,
140         token: server.accessToken,
141         fields,
142         attaches,
143         statusCodeExpected: 400
144       })
145     })
146
147     // We don't check the extension yet
148     // it('Should fail with an invalid captionfile extension and octet-stream mime type', async function () {
149     //   await createVideoCaption({
150     //     url: server.url,
151     //     accessToken: server.accessToken,
152     //     language: 'zh',
153     //     videoId: videoUUID,
154     //     fixture: 'subtitle-bad.txt',
155     //     mimeType: 'application/octet-stream',
156     //     statusCodeExpected: 400
157     //   })
158     // })
159
160     it('Should succeed with a valid captionfile extension and octet-stream mime type', async function () {
161       await createVideoCaption({
162         url: server.url,
163         accessToken: server.accessToken,
164         language: 'zh',
165         videoId: videoUUID,
166         fixture: 'subtitle-good.srt',
167         mimeType: 'application/octet-stream'
168       })
169     })
170
171     // We don't check the file validity yet
172     // it('Should fail with an invalid captionfile srt', async function () {
173     //   const attaches = {
174     //     'captionfile': join(__dirname, '..', '..', 'fixtures', 'subtitle-bad.srt')
175     //   }
176     //
177     //   const captionPath = path + videoUUID + '/captions/fr'
178     //   await makeUploadRequest({
179     //     method: 'PUT',
180     //     url: server.url,
181     //     path: captionPath,
182     //     token: server.accessToken,
183     //     fields,
184     //     attaches,
185     //     statusCodeExpected: 500
186     //   })
187     // })
188
189     it('Should success with the correct parameters', async function () {
190       const captionPath = path + videoUUID + '/captions/fr'
191       await makeUploadRequest({
192         method: 'PUT',
193         url: server.url,
194         path: captionPath,
195         token: server.accessToken,
196         fields,
197         attaches,
198         statusCodeExpected: 204
199       })
200     })
201   })
202
203   describe('When listing video captions', function () {
204     it('Should fail without a valid uuid', async function () {
205       await makeGetRequest({ url: server.url, path: path + '4da6fde3-88f7-4d16-b119-108df563d0b06/captions' })
206     })
207
208     it('Should fail with an unknown id', async function () {
209       await makeGetRequest({ url: server.url, path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions', statusCodeExpected: 404 })
210     })
211
212     it('Should success with the correct parameters', async function () {
213       await makeGetRequest({ url: server.url, path: path + videoUUID + '/captions', statusCodeExpected: 200 })
214     })
215   })
216
217   describe('When deleting video caption', function () {
218     it('Should fail without a valid uuid', async function () {
219       await makeDeleteRequest({
220         url: server.url,
221         path: path + '4da6fde3-88f7-4d16-b119-108df563d0b06/captions/fr',
222         token: server.accessToken
223       })
224     })
225
226     it('Should fail with an unknown id', async function () {
227       await makeDeleteRequest({
228         url: server.url,
229         path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions/fr',
230         token: server.accessToken,
231         statusCodeExpected: 404
232       })
233     })
234
235     it('Should fail with an invalid language', async function () {
236       await makeDeleteRequest({
237         url: server.url,
238         path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions/16',
239         token: server.accessToken
240       })
241     })
242
243     it('Should fail with a missing language', async function () {
244       const captionPath = path + videoUUID + '/captions'
245       await makeDeleteRequest({ url: server.url, path: captionPath, token: server.accessToken })
246     })
247
248     it('Should fail with an unknown language', async function () {
249       const captionPath = path + videoUUID + '/captions/15'
250       await makeDeleteRequest({ url: server.url, path: captionPath, token: server.accessToken })
251     })
252
253     it('Should fail without access token', async function () {
254       const captionPath = path + videoUUID + '/captions/fr'
255       await makeDeleteRequest({ url: server.url, path: captionPath, statusCodeExpected: 401 })
256     })
257
258     it('Should fail with a bad access token', async function () {
259       const captionPath = path + videoUUID + '/captions/fr'
260       await makeDeleteRequest({ url: server.url, path: captionPath, token: 'coucou', statusCodeExpected: 401 })
261     })
262
263     it('Should fail with another user', async function () {
264       const captionPath = path + videoUUID + '/captions/fr'
265       await makeDeleteRequest({ url: server.url, path: captionPath, token: userAccessToken, statusCodeExpected: 403 })
266     })
267
268     it('Should success with the correct parameters', async function () {
269       const captionPath = path + videoUUID + '/captions/fr'
270       await makeDeleteRequest({ url: server.url, path: captionPath, token: server.accessToken, statusCodeExpected: 204 })
271     })
272   })
273
274   after(async function () {
275     killallServers([ server ])
276
277     // Keep the logs if the test failed
278     if (this['ok']) {
279       await flushTests()
280     }
281   })
282 })