]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/video-comments.ts
Fix CLI tools
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / video-comments.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
e2e22e40
C
2
3import 'mocha'
d4a8e7a6 4import * as chai from 'chai'
12edc149 5import { HttpStatusCode } from '@shared/core-utils'
11ba2ab3 6import {
12edc149
C
7 checkBadCountPagination,
8 checkBadSortPagination,
9 checkBadStartPagination,
7c3b7976 10 cleanupTests,
4cb6d457 11 createUser,
42e1ec25 12 flushAndRunServer,
42e1ec25
C
13 makeDeleteRequest,
14 makeGetRequest,
15 makePostBodyRequest,
16 ServerInfo,
17 setAccessTokensToServers,
18 uploadVideo,
19 userLogin
12edc149
C
20} from '@shared/extra-utils'
21import { VideoCreateResult } from '@shared/models'
e2e22e40 22
47564bbe
C
23const expect = chai.expect
24
e2e22e40
C
25describe('Test video comments API validator', function () {
26 let pathThread: string
27 let pathComment: string
28 let server: ServerInfo
d4a8e7a6 29 let video: VideoCreateResult
4cb6d457 30 let userAccessToken: string
fde37dc9 31 let userAccessToken2: string
e2e22e40
C
32 let commentId: number
33
34 // ---------------------------------------------------------------
35
36 before(async function () {
e212f887 37 this.timeout(30000)
e2e22e40 38
210feb6c 39 server = await flushAndRunServer(1)
e2e22e40
C
40
41 await setAccessTokensToServers([ server ])
42
e2e22e40
C
43 {
44 const res = await uploadVideo(server.url, server.accessToken, {})
d4a8e7a6
C
45 video = res.body.video
46 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
e2e22e40
C
47 }
48
49 {
12edc149
C
50 const created = await server.commentsCommand.createThread({ videoId: video.uuid, text: 'coucou' })
51 commentId = created.id
d4a8e7a6 52 pathComment = '/api/v1/videos/' + video.uuid + '/comments/' + commentId
e2e22e40 53 }
4cb6d457
C
54
55 {
fde37dc9 56 const user = { username: 'user1', password: 'my super password' }
1eddc9a7 57 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
4cb6d457
C
58 userAccessToken = await userLogin(server, user)
59 }
fde37dc9
C
60
61 {
62 const user = { username: 'user2', password: 'my super password' }
63 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
64 userAccessToken2 = await userLogin(server, user)
65 }
e2e22e40
C
66 })
67
68 describe('When listing video comment threads', function () {
69 it('Should fail with a bad start pagination', async function () {
11ba2ab3 70 await checkBadStartPagination(server.url, pathThread, server.accessToken)
e2e22e40
C
71 })
72
73 it('Should fail with a bad count pagination', async function () {
11ba2ab3 74 await checkBadCountPagination(server.url, pathThread, server.accessToken)
e2e22e40
C
75 })
76
77 it('Should fail with an incorrect sort', async function () {
11ba2ab3 78 await checkBadSortPagination(server.url, pathThread, server.accessToken)
e2e22e40
C
79 })
80
81 it('Should fail with an incorrect video', async function () {
11ba2ab3
C
82 await makeGetRequest({
83 url: server.url,
84 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads',
2d53be02 85 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
11ba2ab3 86 })
e2e22e40
C
87 })
88 })
89
90 describe('When listing comments of a thread', function () {
91 it('Should fail with an incorrect video', async function () {
11ba2ab3
C
92 await makeGetRequest({
93 url: server.url,
94 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads/' + commentId,
2d53be02 95 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
11ba2ab3 96 })
e2e22e40
C
97 })
98
99 it('Should fail with an incorrect thread id', async function () {
11ba2ab3
C
100 await makeGetRequest({
101 url: server.url,
d4a8e7a6 102 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/156',
2d53be02 103 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
11ba2ab3 104 })
e2e22e40
C
105 })
106
107 it('Should success with the correct params', async function () {
11ba2ab3
C
108 await makeGetRequest({
109 url: server.url,
d4a8e7a6 110 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
2d53be02 111 statusCodeExpected: HttpStatusCode.OK_200
11ba2ab3 112 })
e2e22e40
C
113 })
114 })
115
116 describe('When adding a video thread', function () {
117
118 it('Should fail with a non authenticated user', async function () {
119 const fields = {
120 text: 'text'
121 }
2d53be02
RK
122 await makePostBodyRequest({
123 url: server.url,
124 path: pathThread,
125 token: 'none',
126 fields,
127 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
128 })
e2e22e40
C
129 })
130
131 it('Should fail with nothing', async function () {
132 const fields = {}
133 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
134 })
135
136 it('Should fail with a short comment', async function () {
137 const fields = {
53eb90c0 138 text: ''
e2e22e40
C
139 }
140 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
141 })
142
143 it('Should fail with a long comment', async function () {
144 const fields = {
298b3fd3 145 text: 'h'.repeat(10001)
e2e22e40
C
146 }
147 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
148 })
149
150 it('Should fail with an incorrect video', async function () {
151 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads'
152 const fields = {
153 text: 'super comment'
154 }
2d53be02
RK
155 await makePostBodyRequest({
156 url: server.url,
157 path,
158 token: server.accessToken,
159 fields,
160 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
161 })
e2e22e40
C
162 })
163
164 it('Should succeed with the correct parameters', async function () {
165 const fields = {
166 text: 'super comment'
167 }
2d53be02
RK
168 await makePostBodyRequest({
169 url: server.url,
170 path: pathThread,
171 token: server.accessToken,
172 fields,
173 statusCodeExpected: HttpStatusCode.OK_200
174 })
e2e22e40
C
175 })
176 })
177
178 describe('When adding a comment to a thread', function () {
179 it('Should fail with a non authenticated user', async function () {
180 const fields = {
181 text: 'text'
182 }
2d53be02
RK
183 await makePostBodyRequest({
184 url: server.url,
185 path: pathComment,
186 token: 'none',
187 fields,
188 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
189 })
e2e22e40
C
190 })
191
192 it('Should fail with nothing', async function () {
193 const fields = {}
194 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
195 })
196
197 it('Should fail with a short comment', async function () {
198 const fields = {
53eb90c0 199 text: ''
e2e22e40
C
200 }
201 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
202 })
203
204 it('Should fail with a long comment', async function () {
205 const fields = {
298b3fd3 206 text: 'h'.repeat(10001)
e2e22e40
C
207 }
208 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
209 })
210
211 it('Should fail with an incorrect video', async function () {
212 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
213 const fields = {
214 text: 'super comment'
215 }
2d53be02
RK
216 await makePostBodyRequest({
217 url: server.url,
218 path,
219 token: server.accessToken,
220 fields,
221 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
222 })
e2e22e40
C
223 })
224
225 it('Should fail with an incorrect comment', async function () {
d4a8e7a6 226 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
e2e22e40
C
227 const fields = {
228 text: 'super comment'
229 }
2d53be02
RK
230 await makePostBodyRequest({
231 url: server.url,
232 path,
233 token: server.accessToken,
234 fields,
235 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
236 })
e2e22e40
C
237 })
238
239 it('Should succeed with the correct parameters', async function () {
240 const fields = {
241 text: 'super comment'
242 }
2d53be02
RK
243 await makePostBodyRequest({
244 url: server.url,
245 path: pathComment,
246 token: server.accessToken,
247 fields,
248 statusCodeExpected: HttpStatusCode.OK_200
249 })
e2e22e40
C
250 })
251 })
252
4cb6d457
C
253 describe('When removing video comments', function () {
254 it('Should fail with a non authenticated user', async function () {
2d53be02 255 await makeDeleteRequest({ url: server.url, path: pathComment, token: 'none', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
4cb6d457
C
256 })
257
258 it('Should fail with another user', async function () {
2d53be02
RK
259 await makeDeleteRequest({
260 url: server.url,
261 path: pathComment,
262 token: userAccessToken,
263 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
264 })
4cb6d457
C
265 })
266
267 it('Should fail with an incorrect video', async function () {
268 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
2d53be02 269 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
4cb6d457
C
270 })
271
272 it('Should fail with an incorrect comment', async function () {
d4a8e7a6 273 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
2d53be02 274 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
4cb6d457
C
275 })
276
fde37dc9
C
277 it('Should succeed with the same user', async function () {
278 let commentToDelete: number
279
280 {
12edc149
C
281 const created = await server.commentsCommand.createThread({ videoId: video.uuid, text: 'hello' })
282 commentToDelete = created.id
fde37dc9
C
283 }
284
d4a8e7a6 285 const path = '/api/v1/videos/' + video.uuid + '/comments/' + commentToDelete
fde37dc9 286
2d53be02
RK
287 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
288 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 })
fde37dc9
C
289 })
290
291 it('Should succeed with the owner of the video', async function () {
292 let commentToDelete: number
293 let anotherVideoUUID: string
294
295 {
296 const res = await uploadVideo(server.url, userAccessToken, { name: 'video' })
297 anotherVideoUUID = res.body.video.uuid
298 }
299
300 {
12edc149
C
301 const created = await server.commentsCommand.createThread({ videoId: anotherVideoUUID, text: 'hello' })
302 commentToDelete = created.id
fde37dc9
C
303 }
304
305 const path = '/api/v1/videos/' + anotherVideoUUID + '/comments/' + commentToDelete
306
2d53be02
RK
307 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
308 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 })
fde37dc9
C
309 })
310
4cb6d457 311 it('Should succeed with the correct parameters', async function () {
2d53be02
RK
312 await makeDeleteRequest({
313 url: server.url,
314 path: pathComment,
315 token: server.accessToken,
316 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
317 })
4cb6d457
C
318 })
319 })
320
47564bbe
C
321 describe('When a video has comments disabled', function () {
322 before(async function () {
323 const res = await uploadVideo(server.url, server.accessToken, { commentsEnabled: false })
d4a8e7a6
C
324 video = res.body.video
325 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
47564bbe
C
326 })
327
328 it('Should return an empty thread list', async function () {
329 const res = await makeGetRequest({
330 url: server.url,
331 path: pathThread,
2d53be02 332 statusCodeExpected: HttpStatusCode.OK_200
47564bbe
C
333 })
334 expect(res.body.total).to.equal(0)
335 expect(res.body.data).to.have.lengthOf(0)
336 })
337
338 it('Should return an thread comments list')
339
340 it('Should return conflict on thread add', async function () {
341 const fields = {
342 text: 'super comment'
343 }
2d53be02
RK
344 await makePostBodyRequest({
345 url: server.url,
346 path: pathThread,
347 token: server.accessToken,
348 fields,
349 statusCodeExpected: HttpStatusCode.CONFLICT_409
350 })
47564bbe
C
351 })
352
353 it('Should return conflict on comment thread add')
354 })
355
f1273314
C
356 describe('When listing admin comments threads', function () {
357 const path = '/api/v1/videos/comments'
358
359 it('Should fail with a bad start pagination', async function () {
360 await checkBadStartPagination(server.url, path, server.accessToken)
361 })
362
363 it('Should fail with a bad count pagination', async function () {
364 await checkBadCountPagination(server.url, path, server.accessToken)
365 })
366
367 it('Should fail with an incorrect sort', async function () {
368 await checkBadSortPagination(server.url, path, server.accessToken)
369 })
370
371 it('Should fail with a non authenticated user', async function () {
372 await makeGetRequest({
373 url: server.url,
374 path,
2d53be02 375 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
f1273314
C
376 })
377 })
378
379 it('Should fail with a non admin user', async function () {
380 await makeGetRequest({
381 url: server.url,
382 path,
383 token: userAccessToken,
2d53be02 384 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
f1273314
C
385 })
386 })
387
388 it('Should succeed with the correct params', async function () {
389 await makeGetRequest({
390 url: server.url,
391 path,
392 token: server.accessToken,
393 query: {
394 isLocal: false,
395 search: 'toto',
396 searchAccount: 'toto',
397 searchVideo: 'toto'
398 },
2d53be02 399 statusCodeExpected: HttpStatusCode.OK_200
f1273314
C
400 })
401 })
402 })
403
7c3b7976
C
404 after(async function () {
405 await cleanupTests([ server ])
e2e22e40
C
406 })
407})