]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/video-captions.ts
e42e8db81ba9c24fcbc5ae9d3a8714688bb793b0
[github/Chocobozzz/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 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
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 server = await flushAndRunServer(1)
33
34 await setAccessTokensToServers([ server ])
35
36 {
37 const res = await uploadVideo(server.url, server.accessToken, {})
38 videoUUID = res.body.video.uuid
39 }
40
41 {
42 const user = {
43 username: 'user1',
44 password: 'my super password'
45 }
46 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
47 userAccessToken = await userLogin(server, user)
48 }
49 })
50
51 describe('When adding video caption', function () {
52 const fields = { }
53 const attaches = {
54 captionfile: join(__dirname, '..', '..', 'fixtures', 'subtitle-good1.vtt')
55 }
56
57 it('Should fail without a valid uuid', async function () {
58 await makeUploadRequest({
59 method: 'PUT',
60 url: server.url,
61 path: path + '4da6fde3-88f7-4d16-b119-108df563d0b06/captions',
62 token: server.accessToken,
63 fields,
64 attaches
65 })
66 })
67
68 it('Should fail with an unknown id', async function () {
69 await makeUploadRequest({
70 method: 'PUT',
71 url: server.url,
72 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions',
73 token: server.accessToken,
74 fields,
75 attaches
76 })
77 })
78
79 it('Should fail with a missing language in path', async function () {
80 const captionPath = path + videoUUID + '/captions'
81 await makeUploadRequest({
82 method: 'PUT',
83 url: server.url,
84 path: captionPath,
85 token: server.accessToken,
86 fields,
87 attaches
88 })
89 })
90
91 it('Should fail with an unknown language', async function () {
92 const captionPath = path + videoUUID + '/captions/15'
93 await makeUploadRequest({
94 method: 'PUT',
95 url: server.url,
96 path: captionPath,
97 token: server.accessToken,
98 fields,
99 attaches
100 })
101 })
102
103 it('Should fail without access token', async function () {
104 const captionPath = path + videoUUID + '/captions/fr'
105 await makeUploadRequest({
106 method: 'PUT',
107 url: server.url,
108 path: captionPath,
109 fields,
110 attaches,
111 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
112 })
113 })
114
115 it('Should fail with a bad access token', async function () {
116 const captionPath = path + videoUUID + '/captions/fr'
117 await makeUploadRequest({
118 method: 'PUT',
119 url: server.url,
120 path: captionPath,
121 token: 'blabla',
122 fields,
123 attaches,
124 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
125 })
126 })
127
128 // We accept any file now
129 // it('Should fail with an invalid captionfile extension', async function () {
130 // const attaches = {
131 // 'captionfile': join(__dirname, '..', '..', 'fixtures', 'subtitle-bad.txt')
132 // }
133 //
134 // const captionPath = path + videoUUID + '/captions/fr'
135 // await makeUploadRequest({
136 // method: 'PUT',
137 // url: server.url,
138 // path: captionPath,
139 // token: server.accessToken,
140 // fields,
141 // attaches,
142 // statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
143 // })
144 // })
145
146 // We don't check the extension yet
147 // it('Should fail with an invalid captionfile extension and octet-stream mime type', async function () {
148 // await createVideoCaption({
149 // url: server.url,
150 // accessToken: server.accessToken,
151 // language: 'zh',
152 // videoId: videoUUID,
153 // fixture: 'subtitle-bad.txt',
154 // mimeType: 'application/octet-stream',
155 // statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
156 // })
157 // })
158
159 it('Should succeed with a valid captionfile extension and octet-stream mime type', async function () {
160 await createVideoCaption({
161 url: server.url,
162 accessToken: server.accessToken,
163 language: 'zh',
164 videoId: videoUUID,
165 fixture: 'subtitle-good.srt',
166 mimeType: 'application/octet-stream'
167 })
168 })
169
170 // We don't check the file validity yet
171 // it('Should fail with an invalid captionfile srt', async function () {
172 // const attaches = {
173 // 'captionfile': join(__dirname, '..', '..', 'fixtures', 'subtitle-bad.srt')
174 // }
175 //
176 // const captionPath = path + videoUUID + '/captions/fr'
177 // await makeUploadRequest({
178 // method: 'PUT',
179 // url: server.url,
180 // path: captionPath,
181 // token: server.accessToken,
182 // fields,
183 // attaches,
184 // statusCodeExpected: HttpStatusCode.INTERNAL_SERVER_ERROR_500
185 // })
186 // })
187
188 it('Should success with the correct parameters', async function () {
189 const captionPath = path + videoUUID + '/captions/fr'
190 await makeUploadRequest({
191 method: 'PUT',
192 url: server.url,
193 path: captionPath,
194 token: server.accessToken,
195 fields,
196 attaches,
197 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
198 })
199 })
200 })
201
202 describe('When listing video captions', function () {
203 it('Should fail without a valid uuid', async function () {
204 await makeGetRequest({ url: server.url, path: path + '4da6fde3-88f7-4d16-b119-108df563d0b06/captions' })
205 })
206
207 it('Should fail with an unknown id', async function () {
208 await makeGetRequest({
209 url: server.url,
210 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions',
211 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
212 })
213 })
214
215 it('Should success with the correct parameters', async function () {
216 await makeGetRequest({ url: server.url, path: path + videoUUID + '/captions', statusCodeExpected: HttpStatusCode.OK_200 })
217 })
218 })
219
220 describe('When deleting video caption', function () {
221 it('Should fail without a valid uuid', async function () {
222 await makeDeleteRequest({
223 url: server.url,
224 path: path + '4da6fde3-88f7-4d16-b119-108df563d0b06/captions/fr',
225 token: server.accessToken
226 })
227 })
228
229 it('Should fail with an unknown id', async function () {
230 await makeDeleteRequest({
231 url: server.url,
232 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions/fr',
233 token: server.accessToken,
234 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
235 })
236 })
237
238 it('Should fail with an invalid language', async function () {
239 await makeDeleteRequest({
240 url: server.url,
241 path: path + '4da6fde3-88f7-4d16-b119-108df5630b06/captions/16',
242 token: server.accessToken
243 })
244 })
245
246 it('Should fail with a missing language', async function () {
247 const captionPath = path + videoUUID + '/captions'
248 await makeDeleteRequest({ url: server.url, path: captionPath, token: server.accessToken })
249 })
250
251 it('Should fail with an unknown language', async function () {
252 const captionPath = path + videoUUID + '/captions/15'
253 await makeDeleteRequest({ url: server.url, path: captionPath, token: server.accessToken })
254 })
255
256 it('Should fail without access token', async function () {
257 const captionPath = path + videoUUID + '/captions/fr'
258 await makeDeleteRequest({ url: server.url, path: captionPath, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
259 })
260
261 it('Should fail with a bad access token', async function () {
262 const captionPath = path + videoUUID + '/captions/fr'
263 await makeDeleteRequest({ url: server.url, path: captionPath, token: 'coucou', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
264 })
265
266 it('Should fail with another user', async function () {
267 const captionPath = path + videoUUID + '/captions/fr'
268 await makeDeleteRequest({
269 url: server.url,
270 path: captionPath,
271 token: userAccessToken,
272 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
273 })
274 })
275
276 it('Should success with the correct parameters', async function () {
277 const captionPath = path + videoUUID + '/captions/fr'
278 await makeDeleteRequest({
279 url: server.url,
280 path: captionPath,
281 token: server.accessToken,
282 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
283 })
284 })
285 })
286
287 after(async function () {
288 await cleanupTests([ server ])
289 })
290 })