rename blacklist to block/blocklist, merge block and auto-block views
[oweals/peertube.git] / server / tests / api / check-params / video-captions.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
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     // We accept any file now
128     // it('Should fail with an invalid captionfile extension', async function () {
129     //   const attaches = {
130     //     'captionfile': join(__dirname, '..', '..', 'fixtures', 'subtitle-bad.txt')
131     //   }
132     //
133     //   const captionPath = path + videoUUID + '/captions/fr'
134     //   await makeUploadRequest({
135     //     method: 'PUT',
136     //     url: server.url,
137     //     path: captionPath,
138     //     token: server.accessToken,
139     //     fields,
140     //     attaches,
141     //     statusCodeExpected: 400
142     //   })
143     // })
144
145     // We don't check the extension yet
146     // it('Should fail with an invalid captionfile extension and octet-stream mime type', async function () {
147     //   await createVideoCaption({
148     //     url: server.url,
149     //     accessToken: server.accessToken,
150     //     language: 'zh',
151     //     videoId: videoUUID,
152     //     fixture: 'subtitle-bad.txt',
153     //     mimeType: 'application/octet-stream',
154     //     statusCodeExpected: 400
155     //   })
156     // })
157
158     it('Should succeed with a valid captionfile extension and octet-stream mime type', async function () {
159       await createVideoCaption({
160         url: server.url,
161         accessToken: server.accessToken,
162         language: 'zh',
163         videoId: videoUUID,
164         fixture: 'subtitle-good.srt',
165         mimeType: 'application/octet-stream'
166       })
167     })
168
169     // We don't check the file validity yet
170     // it('Should fail with an invalid captionfile srt', async function () {
171     //   const attaches = {
172     //     'captionfile': join(__dirname, '..', '..', 'fixtures', 'subtitle-bad.srt')
173     //   }
174     //
175     //   const captionPath = path + videoUUID + '/captions/fr'
176     //   await makeUploadRequest({
177     //     method: 'PUT',
178     //     url: server.url,
179     //     path: captionPath,
180     //     token: server.accessToken,
181     //     fields,
182     //     attaches,
183     //     statusCodeExpected: 500
184     //   })
185     // })
186
187     it('Should success with the correct parameters', async function () {
188       const captionPath = path + videoUUID + '/captions/fr'
189       await makeUploadRequest({
190         method: 'PUT',
191         url: server.url,
192         path: captionPath,
193         token: server.accessToken,
194         fields,
195         attaches,
196         statusCodeExpected: 204
197       })
198     })
199   })
200
201   describe('When listing video captions', function () {
202     it('Should fail without a valid uuid', async function () {
203       await makeGetRequest({ url: server.url, path: path + '4da6fde3-88f7-4d16-b119-108df563d0b06/captions' })
204     })
205
206     it('Should fail with an unknown id', async function () {
207       await makeGetRequest({ url: server.url, path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions', statusCodeExpected: 404 })
208     })
209
210     it('Should success with the correct parameters', async function () {
211       await makeGetRequest({ url: server.url, path: path + videoUUID + '/captions', statusCodeExpected: 200 })
212     })
213   })
214
215   describe('When deleting video caption', function () {
216     it('Should fail without a valid uuid', async function () {
217       await makeDeleteRequest({
218         url: server.url,
219         path: path + '4da6fde3-88f7-4d16-b119-108df563d0b06/captions/fr',
220         token: server.accessToken
221       })
222     })
223
224     it('Should fail with an unknown id', async function () {
225       await makeDeleteRequest({
226         url: server.url,
227         path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions/fr',
228         token: server.accessToken,
229         statusCodeExpected: 404
230       })
231     })
232
233     it('Should fail with an invalid language', async function () {
234       await makeDeleteRequest({
235         url: server.url,
236         path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions/16',
237         token: server.accessToken
238       })
239     })
240
241     it('Should fail with a missing language', async function () {
242       const captionPath = path + videoUUID + '/captions'
243       await makeDeleteRequest({ url: server.url, path: captionPath, token: server.accessToken })
244     })
245
246     it('Should fail with an unknown language', async function () {
247       const captionPath = path + videoUUID + '/captions/15'
248       await makeDeleteRequest({ url: server.url, path: captionPath, token: server.accessToken })
249     })
250
251     it('Should fail without access token', async function () {
252       const captionPath = path + videoUUID + '/captions/fr'
253       await makeDeleteRequest({ url: server.url, path: captionPath, statusCodeExpected: 401 })
254     })
255
256     it('Should fail with a bad access token', async function () {
257       const captionPath = path + videoUUID + '/captions/fr'
258       await makeDeleteRequest({ url: server.url, path: captionPath, token: 'coucou', statusCodeExpected: 401 })
259     })
260
261     it('Should fail with another user', async function () {
262       const captionPath = path + videoUUID + '/captions/fr'
263       await makeDeleteRequest({ url: server.url, path: captionPath, token: userAccessToken, statusCodeExpected: 403 })
264     })
265
266     it('Should success with the correct parameters', async function () {
267       const captionPath = path + videoUUID + '/captions/fr'
268       await makeDeleteRequest({ url: server.url, path: captionPath, token: server.accessToken, statusCodeExpected: 204 })
269     })
270   })
271
272   after(async function () {
273     await cleanupTests([ server ])
274   })
275 })