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