1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { VideoCreateResult } from '@shared/models'
6 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
15 setAccessTokensToServers,
18 } from '../../../../shared/extra-utils'
20 checkBadCountPagination,
21 checkBadSortPagination,
22 checkBadStartPagination
23 } from '../../../../shared/extra-utils/requests/check-api-params'
24 import { addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
26 const expect = chai.expect
28 describe('Test video comments API validator', function () {
29 let pathThread: string
30 let pathComment: string
31 let server: ServerInfo
32 let video: VideoCreateResult
33 let userAccessToken: string
34 let userAccessToken2: string
37 // ---------------------------------------------------------------
39 before(async function () {
42 server = await flushAndRunServer(1)
44 await setAccessTokensToServers([ server ])
47 const res = await uploadVideo(server.url, server.accessToken, {})
48 video = res.body.video
49 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
53 const res = await addVideoCommentThread(server.url, server.accessToken, video.uuid, 'coucou')
54 commentId = res.body.comment.id
55 pathComment = '/api/v1/videos/' + video.uuid + '/comments/' + commentId
59 const user = { username: 'user1', password: 'my super password' }
60 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
61 userAccessToken = await userLogin(server, user)
65 const user = { username: 'user2', password: 'my super password' }
66 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
67 userAccessToken2 = await userLogin(server, user)
71 describe('When listing video comment threads', function () {
72 it('Should fail with a bad start pagination', async function () {
73 await checkBadStartPagination(server.url, pathThread, server.accessToken)
76 it('Should fail with a bad count pagination', async function () {
77 await checkBadCountPagination(server.url, pathThread, server.accessToken)
80 it('Should fail with an incorrect sort', async function () {
81 await checkBadSortPagination(server.url, pathThread, server.accessToken)
84 it('Should fail with an incorrect video', async function () {
85 await makeGetRequest({
87 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads',
88 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
93 describe('When listing comments of a thread', function () {
94 it('Should fail with an incorrect video', async function () {
95 await makeGetRequest({
97 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads/' + commentId,
98 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
102 it('Should fail with an incorrect thread id', async function () {
103 await makeGetRequest({
105 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/156',
106 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
110 it('Should success with the correct params', async function () {
111 await makeGetRequest({
113 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
114 statusCodeExpected: HttpStatusCode.OK_200
119 describe('When adding a video thread', function () {
121 it('Should fail with a non authenticated user', async function () {
125 await makePostBodyRequest({
130 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
134 it('Should fail with nothing', async function () {
136 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
139 it('Should fail with a short comment', async function () {
143 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
146 it('Should fail with a long comment', async function () {
148 text: 'h'.repeat(10001)
150 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
153 it('Should fail with an incorrect video', async function () {
154 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads'
156 text: 'super comment'
158 await makePostBodyRequest({
161 token: server.accessToken,
163 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
167 it('Should succeed with the correct parameters', async function () {
169 text: 'super comment'
171 await makePostBodyRequest({
174 token: server.accessToken,
176 statusCodeExpected: HttpStatusCode.OK_200
181 describe('When adding a comment to a thread', function () {
182 it('Should fail with a non authenticated user', async function () {
186 await makePostBodyRequest({
191 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
195 it('Should fail with nothing', async function () {
197 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
200 it('Should fail with a short comment', async function () {
204 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
207 it('Should fail with a long comment', async function () {
209 text: 'h'.repeat(10001)
211 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
214 it('Should fail with an incorrect video', async function () {
215 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
217 text: 'super comment'
219 await makePostBodyRequest({
222 token: server.accessToken,
224 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
228 it('Should fail with an incorrect comment', async function () {
229 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
231 text: 'super comment'
233 await makePostBodyRequest({
236 token: server.accessToken,
238 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
242 it('Should succeed with the correct parameters', async function () {
244 text: 'super comment'
246 await makePostBodyRequest({
249 token: server.accessToken,
251 statusCodeExpected: HttpStatusCode.OK_200
256 describe('When removing video comments', function () {
257 it('Should fail with a non authenticated user', async function () {
258 await makeDeleteRequest({ url: server.url, path: pathComment, token: 'none', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
261 it('Should fail with another user', async function () {
262 await makeDeleteRequest({
265 token: userAccessToken,
266 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
270 it('Should fail with an incorrect video', async function () {
271 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
272 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
275 it('Should fail with an incorrect comment', async function () {
276 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
277 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
280 it('Should succeed with the same user', async function () {
281 let commentToDelete: number
284 const res = await addVideoCommentThread(server.url, userAccessToken, video.uuid, 'hello')
285 commentToDelete = res.body.comment.id
288 const path = '/api/v1/videos/' + video.uuid + '/comments/' + commentToDelete
290 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
291 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 })
294 it('Should succeed with the owner of the video', async function () {
295 let commentToDelete: number
296 let anotherVideoUUID: string
299 const res = await uploadVideo(server.url, userAccessToken, { name: 'video' })
300 anotherVideoUUID = res.body.video.uuid
304 const res = await addVideoCommentThread(server.url, server.accessToken, anotherVideoUUID, 'hello')
305 commentToDelete = res.body.comment.id
308 const path = '/api/v1/videos/' + anotherVideoUUID + '/comments/' + commentToDelete
310 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
311 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 })
314 it('Should succeed with the correct parameters', async function () {
315 await makeDeleteRequest({
318 token: server.accessToken,
319 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
324 describe('When a video has comments disabled', function () {
325 before(async function () {
326 const res = await uploadVideo(server.url, server.accessToken, { commentsEnabled: false })
327 video = res.body.video
328 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
331 it('Should return an empty thread list', async function () {
332 const res = await makeGetRequest({
335 statusCodeExpected: HttpStatusCode.OK_200
337 expect(res.body.total).to.equal(0)
338 expect(res.body.data).to.have.lengthOf(0)
341 it('Should return an thread comments list')
343 it('Should return conflict on thread add', async function () {
345 text: 'super comment'
347 await makePostBodyRequest({
350 token: server.accessToken,
352 statusCodeExpected: HttpStatusCode.CONFLICT_409
356 it('Should return conflict on comment thread add')
359 describe('When listing admin comments threads', function () {
360 const path = '/api/v1/videos/comments'
362 it('Should fail with a bad start pagination', async function () {
363 await checkBadStartPagination(server.url, path, server.accessToken)
366 it('Should fail with a bad count pagination', async function () {
367 await checkBadCountPagination(server.url, path, server.accessToken)
370 it('Should fail with an incorrect sort', async function () {
371 await checkBadSortPagination(server.url, path, server.accessToken)
374 it('Should fail with a non authenticated user', async function () {
375 await makeGetRequest({
378 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
382 it('Should fail with a non admin user', async function () {
383 await makeGetRequest({
386 token: userAccessToken,
387 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
391 it('Should succeed with the correct params', async function () {
392 await makeGetRequest({
395 token: server.accessToken,
399 searchAccount: 'toto',
402 statusCodeExpected: HttpStatusCode.OK_200
407 after(async function () {
408 await cleanupTests([ server ])