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,
20 } from '@shared/extra-utils'
21 import { VideoCreateResult } from '@shared/models'
23 const expect = chai.expect
25 describe('Test video comments API validator', function () {
26 let pathThread: string
27 let pathComment: string
28 let server: ServerInfo
29 let video: VideoCreateResult
30 let userAccessToken: string
31 let userAccessToken2: string
34 // ---------------------------------------------------------------
36 before(async function () {
39 server = await flushAndRunServer(1)
41 await setAccessTokensToServers([ server ])
44 const res = await uploadVideo(server.url, server.accessToken, {})
45 video = res.body.video
46 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
50 const created = await server.commentsCommand.createThread({ videoId: video.uuid, text: 'coucou' })
51 commentId = created.id
52 pathComment = '/api/v1/videos/' + video.uuid + '/comments/' + commentId
56 const user = { username: 'user1', password: 'my super password' }
57 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
58 userAccessToken = await userLogin(server, user)
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)
68 describe('When listing video comment threads', function () {
69 it('Should fail with a bad start pagination', async function () {
70 await checkBadStartPagination(server.url, pathThread, server.accessToken)
73 it('Should fail with a bad count pagination', async function () {
74 await checkBadCountPagination(server.url, pathThread, server.accessToken)
77 it('Should fail with an incorrect sort', async function () {
78 await checkBadSortPagination(server.url, pathThread, server.accessToken)
81 it('Should fail with an incorrect video', async function () {
82 await makeGetRequest({
84 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads',
85 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
90 describe('When listing comments of a thread', function () {
91 it('Should fail with an incorrect video', async function () {
92 await makeGetRequest({
94 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads/' + commentId,
95 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
99 it('Should fail with an incorrect thread id', async function () {
100 await makeGetRequest({
102 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/156',
103 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
107 it('Should success with the correct params', async function () {
108 await makeGetRequest({
110 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
111 statusCodeExpected: HttpStatusCode.OK_200
116 describe('When adding a video thread', function () {
118 it('Should fail with a non authenticated user', async function () {
122 await makePostBodyRequest({
127 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
131 it('Should fail with nothing', async function () {
133 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
136 it('Should fail with a short comment', async function () {
140 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
143 it('Should fail with a long comment', async function () {
145 text: 'h'.repeat(10001)
147 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
150 it('Should fail with an incorrect video', async function () {
151 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads'
153 text: 'super comment'
155 await makePostBodyRequest({
158 token: server.accessToken,
160 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
164 it('Should succeed with the correct parameters', async function () {
166 text: 'super comment'
168 await makePostBodyRequest({
171 token: server.accessToken,
173 statusCodeExpected: HttpStatusCode.OK_200
178 describe('When adding a comment to a thread', function () {
179 it('Should fail with a non authenticated user', async function () {
183 await makePostBodyRequest({
188 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
192 it('Should fail with nothing', async function () {
194 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
197 it('Should fail with a short comment', async function () {
201 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
204 it('Should fail with a long comment', async function () {
206 text: 'h'.repeat(10001)
208 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
211 it('Should fail with an incorrect video', async function () {
212 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
214 text: 'super comment'
216 await makePostBodyRequest({
219 token: server.accessToken,
221 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
225 it('Should fail with an incorrect comment', async function () {
226 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
228 text: 'super comment'
230 await makePostBodyRequest({
233 token: server.accessToken,
235 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
239 it('Should succeed with the correct parameters', async function () {
241 text: 'super comment'
243 await makePostBodyRequest({
246 token: server.accessToken,
248 statusCodeExpected: HttpStatusCode.OK_200
253 describe('When removing video comments', function () {
254 it('Should fail with a non authenticated user', async function () {
255 await makeDeleteRequest({ url: server.url, path: pathComment, token: 'none', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
258 it('Should fail with another user', async function () {
259 await makeDeleteRequest({
262 token: userAccessToken,
263 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
267 it('Should fail with an incorrect video', async function () {
268 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
269 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
272 it('Should fail with an incorrect comment', async function () {
273 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
274 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
277 it('Should succeed with the same user', async function () {
278 let commentToDelete: number
281 const created = await server.commentsCommand.createThread({ videoId: video.uuid, token: userAccessToken, text: 'hello' })
282 commentToDelete = created.id
285 const path = '/api/v1/videos/' + video.uuid + '/comments/' + commentToDelete
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 })
291 it('Should succeed with the owner of the video', async function () {
292 let commentToDelete: number
293 let anotherVideoUUID: string
296 const res = await uploadVideo(server.url, userAccessToken, { name: 'video' })
297 anotherVideoUUID = res.body.video.uuid
301 const created = await server.commentsCommand.createThread({ videoId: anotherVideoUUID, text: 'hello' })
302 commentToDelete = created.id
305 const path = '/api/v1/videos/' + anotherVideoUUID + '/comments/' + commentToDelete
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 })
311 it('Should succeed with the correct parameters', async function () {
312 await makeDeleteRequest({
315 token: server.accessToken,
316 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
321 describe('When a video has comments disabled', function () {
322 before(async function () {
323 const res = await uploadVideo(server.url, server.accessToken, { commentsEnabled: false })
324 video = res.body.video
325 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
328 it('Should return an empty thread list', async function () {
329 const res = await makeGetRequest({
332 statusCodeExpected: HttpStatusCode.OK_200
334 expect(res.body.total).to.equal(0)
335 expect(res.body.data).to.have.lengthOf(0)
338 it('Should return an thread comments list')
340 it('Should return conflict on thread add', async function () {
342 text: 'super comment'
344 await makePostBodyRequest({
347 token: server.accessToken,
349 statusCodeExpected: HttpStatusCode.CONFLICT_409
353 it('Should return conflict on comment thread add')
356 describe('When listing admin comments threads', function () {
357 const path = '/api/v1/videos/comments'
359 it('Should fail with a bad start pagination', async function () {
360 await checkBadStartPagination(server.url, path, server.accessToken)
363 it('Should fail with a bad count pagination', async function () {
364 await checkBadCountPagination(server.url, path, server.accessToken)
367 it('Should fail with an incorrect sort', async function () {
368 await checkBadSortPagination(server.url, path, server.accessToken)
371 it('Should fail with a non authenticated user', async function () {
372 await makeGetRequest({
375 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
379 it('Should fail with a non admin user', async function () {
380 await makeGetRequest({
383 token: userAccessToken,
384 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
388 it('Should succeed with the correct params', async function () {
389 await makeGetRequest({
392 token: server.accessToken,
396 searchAccount: 'toto',
399 statusCodeExpected: HttpStatusCode.OK_200
404 after(async function () {
405 await cleanupTests([ server ])