1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
7 checkBadCountPagination,
8 checkBadSortPagination,
9 checkBadStartPagination,
17 setAccessTokensToServers,
19 } from '@shared/extra-utils'
20 import { VideoCreateResult } from '@shared/models'
22 const expect = chai.expect
24 describe('Test video comments API validator', function () {
25 let pathThread: string
26 let pathComment: string
27 let server: ServerInfo
28 let video: VideoCreateResult
29 let userAccessToken: string
30 let userAccessToken2: string
33 // ---------------------------------------------------------------
35 before(async function () {
38 server = await flushAndRunServer(1)
40 await setAccessTokensToServers([ server ])
43 const res = await uploadVideo(server.url, server.accessToken, {})
44 video = res.body.video
45 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
49 const created = await server.commentsCommand.createThread({ videoId: video.uuid, text: 'coucou' })
50 commentId = created.id
51 pathComment = '/api/v1/videos/' + video.uuid + '/comments/' + commentId
55 const user = { username: 'user1', password: 'my super password' }
56 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
57 userAccessToken = await server.loginCommand.getAccessToken(user)
61 const user = { username: 'user2', password: 'my super password' }
62 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
63 userAccessToken2 = await server.loginCommand.getAccessToken(user)
67 describe('When listing video comment threads', function () {
68 it('Should fail with a bad start pagination', async function () {
69 await checkBadStartPagination(server.url, pathThread, server.accessToken)
72 it('Should fail with a bad count pagination', async function () {
73 await checkBadCountPagination(server.url, pathThread, server.accessToken)
76 it('Should fail with an incorrect sort', async function () {
77 await checkBadSortPagination(server.url, pathThread, server.accessToken)
80 it('Should fail with an incorrect video', async function () {
81 await makeGetRequest({
83 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads',
84 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
89 describe('When listing comments of a thread', function () {
90 it('Should fail with an incorrect video', async function () {
91 await makeGetRequest({
93 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads/' + commentId,
94 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
98 it('Should fail with an incorrect thread id', async function () {
99 await makeGetRequest({
101 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/156',
102 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
106 it('Should success with the correct params', async function () {
107 await makeGetRequest({
109 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
110 statusCodeExpected: HttpStatusCode.OK_200
115 describe('When adding a video thread', function () {
117 it('Should fail with a non authenticated user', async function () {
121 await makePostBodyRequest({
126 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
130 it('Should fail with nothing', async function () {
132 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
135 it('Should fail with a short comment', async function () {
139 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
142 it('Should fail with a long comment', async function () {
144 text: 'h'.repeat(10001)
146 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
149 it('Should fail with an incorrect video', async function () {
150 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads'
152 text: 'super comment'
154 await makePostBodyRequest({
157 token: server.accessToken,
159 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
163 it('Should succeed with the correct parameters', async function () {
165 text: 'super comment'
167 await makePostBodyRequest({
170 token: server.accessToken,
172 statusCodeExpected: HttpStatusCode.OK_200
177 describe('When adding a comment to a thread', function () {
178 it('Should fail with a non authenticated user', async function () {
182 await makePostBodyRequest({
187 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
191 it('Should fail with nothing', async function () {
193 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
196 it('Should fail with a short comment', async function () {
200 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
203 it('Should fail with a long comment', async function () {
205 text: 'h'.repeat(10001)
207 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
210 it('Should fail with an incorrect video', async function () {
211 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
213 text: 'super comment'
215 await makePostBodyRequest({
218 token: server.accessToken,
220 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
224 it('Should fail with an incorrect comment', async function () {
225 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
227 text: 'super comment'
229 await makePostBodyRequest({
232 token: server.accessToken,
234 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
238 it('Should succeed with the correct parameters', async function () {
240 text: 'super comment'
242 await makePostBodyRequest({
245 token: server.accessToken,
247 statusCodeExpected: HttpStatusCode.OK_200
252 describe('When removing video comments', function () {
253 it('Should fail with a non authenticated user', async function () {
254 await makeDeleteRequest({ url: server.url, path: pathComment, token: 'none', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
257 it('Should fail with another user', async function () {
258 await makeDeleteRequest({
261 token: userAccessToken,
262 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
266 it('Should fail with an incorrect video', async function () {
267 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
268 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
271 it('Should fail with an incorrect comment', async function () {
272 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
273 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
276 it('Should succeed with the same user', async function () {
277 let commentToDelete: number
280 const created = await server.commentsCommand.createThread({ videoId: video.uuid, token: userAccessToken, text: 'hello' })
281 commentToDelete = created.id
284 const path = '/api/v1/videos/' + video.uuid + '/comments/' + commentToDelete
286 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
287 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 })
290 it('Should succeed with the owner of the video', async function () {
291 let commentToDelete: number
292 let anotherVideoUUID: string
295 const res = await uploadVideo(server.url, userAccessToken, { name: 'video' })
296 anotherVideoUUID = res.body.video.uuid
300 const created = await server.commentsCommand.createThread({ videoId: anotherVideoUUID, text: 'hello' })
301 commentToDelete = created.id
304 const path = '/api/v1/videos/' + anotherVideoUUID + '/comments/' + commentToDelete
306 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
307 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 })
310 it('Should succeed with the correct parameters', async function () {
311 await makeDeleteRequest({
314 token: server.accessToken,
315 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
320 describe('When a video has comments disabled', function () {
321 before(async function () {
322 const res = await uploadVideo(server.url, server.accessToken, { commentsEnabled: false })
323 video = res.body.video
324 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
327 it('Should return an empty thread list', async function () {
328 const res = await makeGetRequest({
331 statusCodeExpected: HttpStatusCode.OK_200
333 expect(res.body.total).to.equal(0)
334 expect(res.body.data).to.have.lengthOf(0)
337 it('Should return an thread comments list')
339 it('Should return conflict on thread add', async function () {
341 text: 'super comment'
343 await makePostBodyRequest({
346 token: server.accessToken,
348 statusCodeExpected: HttpStatusCode.CONFLICT_409
352 it('Should return conflict on comment thread add')
355 describe('When listing admin comments threads', function () {
356 const path = '/api/v1/videos/comments'
358 it('Should fail with a bad start pagination', async function () {
359 await checkBadStartPagination(server.url, path, server.accessToken)
362 it('Should fail with a bad count pagination', async function () {
363 await checkBadCountPagination(server.url, path, server.accessToken)
366 it('Should fail with an incorrect sort', async function () {
367 await checkBadSortPagination(server.url, path, server.accessToken)
370 it('Should fail with a non authenticated user', async function () {
371 await makeGetRequest({
374 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
378 it('Should fail with a non admin user', async function () {
379 await makeGetRequest({
382 token: userAccessToken,
383 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
387 it('Should succeed with the correct params', async function () {
388 await makeGetRequest({
391 token: server.accessToken,
395 searchAccount: 'toto',
398 statusCodeExpected: HttpStatusCode.OK_200
403 after(async function () {
404 await cleanupTests([ server ])