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