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,
16 setAccessTokensToServers,
18 } from '@shared/extra-utils'
19 import { VideoCreateResult } from '@shared/models'
21 const expect = chai.expect
23 describe('Test video comments API validator', function () {
24 let pathThread: string
25 let pathComment: string
26 let server: ServerInfo
27 let video: VideoCreateResult
28 let userAccessToken: string
29 let userAccessToken2: string
32 // ---------------------------------------------------------------
34 before(async function () {
37 server = await flushAndRunServer(1)
39 await setAccessTokensToServers([ server ])
42 const res = await uploadVideo(server.url, server.accessToken, {})
43 video = res.body.video
44 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
48 const created = await server.commentsCommand.createThread({ videoId: video.uuid, text: 'coucou' })
49 commentId = created.id
50 pathComment = '/api/v1/videos/' + video.uuid + '/comments/' + commentId
54 const user = { username: 'user1', password: 'my super password' }
55 await server.usersCommand.create({ username: user.username, password: user.password })
56 userAccessToken = await server.loginCommand.getAccessToken(user)
60 const user = { username: 'user2', password: 'my super password' }
61 await server.usersCommand.create({ username: user.username, password: user.password })
62 userAccessToken2 = await server.loginCommand.getAccessToken(user)
66 describe('When listing video comment threads', function () {
67 it('Should fail with a bad start pagination', async function () {
68 await checkBadStartPagination(server.url, pathThread, server.accessToken)
71 it('Should fail with a bad count pagination', async function () {
72 await checkBadCountPagination(server.url, pathThread, server.accessToken)
75 it('Should fail with an incorrect sort', async function () {
76 await checkBadSortPagination(server.url, pathThread, server.accessToken)
79 it('Should fail with an incorrect video', async function () {
80 await makeGetRequest({
82 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads',
83 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
88 describe('When listing comments of a thread', function () {
89 it('Should fail with an incorrect video', async function () {
90 await makeGetRequest({
92 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads/' + commentId,
93 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
97 it('Should fail with an incorrect thread id', async function () {
98 await makeGetRequest({
100 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/156',
101 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
105 it('Should success with the correct params', async function () {
106 await makeGetRequest({
108 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
109 statusCodeExpected: HttpStatusCode.OK_200
114 describe('When adding a video thread', function () {
116 it('Should fail with a non authenticated user', async function () {
120 await makePostBodyRequest({
125 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
129 it('Should fail with nothing', async function () {
131 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
134 it('Should fail with a short comment', async function () {
138 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
141 it('Should fail with a long comment', async function () {
143 text: 'h'.repeat(10001)
145 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
148 it('Should fail with an incorrect video', async function () {
149 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads'
151 text: 'super comment'
153 await makePostBodyRequest({
156 token: server.accessToken,
158 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
162 it('Should succeed with the correct parameters', async function () {
164 text: 'super comment'
166 await makePostBodyRequest({
169 token: server.accessToken,
171 statusCodeExpected: HttpStatusCode.OK_200
176 describe('When adding a comment to a thread', function () {
177 it('Should fail with a non authenticated user', async function () {
181 await makePostBodyRequest({
186 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
190 it('Should fail with nothing', async function () {
192 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
195 it('Should fail with a short comment', async function () {
199 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
202 it('Should fail with a long comment', async function () {
204 text: 'h'.repeat(10001)
206 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
209 it('Should fail with an incorrect video', async function () {
210 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
212 text: 'super comment'
214 await makePostBodyRequest({
217 token: server.accessToken,
219 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
223 it('Should fail with an incorrect comment', async function () {
224 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
226 text: 'super comment'
228 await makePostBodyRequest({
231 token: server.accessToken,
233 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
237 it('Should succeed with the correct parameters', async function () {
239 text: 'super comment'
241 await makePostBodyRequest({
244 token: server.accessToken,
246 statusCodeExpected: HttpStatusCode.OK_200
251 describe('When removing video comments', function () {
252 it('Should fail with a non authenticated user', async function () {
253 await makeDeleteRequest({ url: server.url, path: pathComment, token: 'none', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
256 it('Should fail with another user', async function () {
257 await makeDeleteRequest({
260 token: userAccessToken,
261 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
265 it('Should fail with an incorrect video', async function () {
266 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
267 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
270 it('Should fail with an incorrect comment', async function () {
271 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
272 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
275 it('Should succeed with the same user', async function () {
276 let commentToDelete: number
279 const created = await server.commentsCommand.createThread({ videoId: video.uuid, token: userAccessToken, text: 'hello' })
280 commentToDelete = created.id
283 const path = '/api/v1/videos/' + video.uuid + '/comments/' + commentToDelete
285 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
286 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 })
289 it('Should succeed with the owner of the video', async function () {
290 let commentToDelete: number
291 let anotherVideoUUID: string
294 const res = await uploadVideo(server.url, userAccessToken, { name: 'video' })
295 anotherVideoUUID = res.body.video.uuid
299 const created = await server.commentsCommand.createThread({ videoId: anotherVideoUUID, text: 'hello' })
300 commentToDelete = created.id
303 const path = '/api/v1/videos/' + anotherVideoUUID + '/comments/' + commentToDelete
305 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
306 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 })
309 it('Should succeed with the correct parameters', async function () {
310 await makeDeleteRequest({
313 token: server.accessToken,
314 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
319 describe('When a video has comments disabled', function () {
320 before(async function () {
321 const res = await uploadVideo(server.url, server.accessToken, { commentsEnabled: false })
322 video = res.body.video
323 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
326 it('Should return an empty thread list', async function () {
327 const res = await makeGetRequest({
330 statusCodeExpected: HttpStatusCode.OK_200
332 expect(res.body.total).to.equal(0)
333 expect(res.body.data).to.have.lengthOf(0)
336 it('Should return an thread comments list')
338 it('Should return conflict on thread add', async function () {
340 text: 'super comment'
342 await makePostBodyRequest({
345 token: server.accessToken,
347 statusCodeExpected: HttpStatusCode.CONFLICT_409
351 it('Should return conflict on comment thread add')
354 describe('When listing admin comments threads', function () {
355 const path = '/api/v1/videos/comments'
357 it('Should fail with a bad start pagination', async function () {
358 await checkBadStartPagination(server.url, path, server.accessToken)
361 it('Should fail with a bad count pagination', async function () {
362 await checkBadCountPagination(server.url, path, server.accessToken)
365 it('Should fail with an incorrect sort', async function () {
366 await checkBadSortPagination(server.url, path, server.accessToken)
369 it('Should fail with a non authenticated user', async function () {
370 await makeGetRequest({
373 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
377 it('Should fail with a non admin user', async function () {
378 await makeGetRequest({
381 token: userAccessToken,
382 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
386 it('Should succeed with the correct params', async function () {
387 await makeGetRequest({
390 token: server.accessToken,
394 searchAccount: 'toto',
397 statusCodeExpected: HttpStatusCode.OK_200
402 after(async function () {
403 await cleanupTests([ server ])