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