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
17 } from '@shared/extra-utils'
18 import { VideoCreateResult } from '@shared/models'
20 const expect = chai.expect
22 describe('Test video comments API validator', function () {
23 let pathThread: string
24 let pathComment: string
25 let server: ServerInfo
26 let video: VideoCreateResult
27 let userAccessToken: string
28 let userAccessToken2: string
31 // ---------------------------------------------------------------
33 before(async function () {
36 server = await flushAndRunServer(1)
38 await setAccessTokensToServers([ server ])
41 const video = await server.videos.upload({ attributes: {} })
42 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
46 const created = await server.comments.createThread({ videoId: video.uuid, text: 'coucou' })
47 commentId = created.id
48 pathComment = '/api/v1/videos/' + video.uuid + '/comments/' + commentId
52 const user = { username: 'user1', password: 'my super password' }
53 await server.users.create({ username: user.username, password: user.password })
54 userAccessToken = await server.login.getAccessToken(user)
58 const user = { username: 'user2', password: 'my super password' }
59 await server.users.create({ username: user.username, password: user.password })
60 userAccessToken2 = await server.login.getAccessToken(user)
64 describe('When listing video comment threads', function () {
65 it('Should fail with a bad start pagination', async function () {
66 await checkBadStartPagination(server.url, pathThread, server.accessToken)
69 it('Should fail with a bad count pagination', async function () {
70 await checkBadCountPagination(server.url, pathThread, server.accessToken)
73 it('Should fail with an incorrect sort', async function () {
74 await checkBadSortPagination(server.url, pathThread, server.accessToken)
77 it('Should fail with an incorrect video', async function () {
78 await makeGetRequest({
80 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads',
81 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
86 describe('When listing comments of a thread', function () {
87 it('Should fail with an incorrect video', async function () {
88 await makeGetRequest({
90 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads/' + commentId,
91 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
95 it('Should fail with an incorrect thread id', async function () {
96 await makeGetRequest({
98 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/156',
99 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
103 it('Should success with the correct params', async function () {
104 await makeGetRequest({
106 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
107 statusCodeExpected: HttpStatusCode.OK_200
112 describe('When adding a video thread', function () {
114 it('Should fail with a non authenticated user', async function () {
118 await makePostBodyRequest({
123 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
127 it('Should fail with nothing', async function () {
129 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
132 it('Should fail with a short comment', async function () {
136 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
139 it('Should fail with a long comment', async function () {
141 text: 'h'.repeat(10001)
143 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
146 it('Should fail with an incorrect video', async function () {
147 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads'
149 text: 'super comment'
151 await makePostBodyRequest({
154 token: server.accessToken,
156 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
160 it('Should succeed with the correct parameters', async function () {
162 text: 'super comment'
164 await makePostBodyRequest({
167 token: server.accessToken,
169 statusCodeExpected: HttpStatusCode.OK_200
174 describe('When adding a comment to a thread', function () {
175 it('Should fail with a non authenticated user', async function () {
179 await makePostBodyRequest({
184 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
188 it('Should fail with nothing', async function () {
190 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
193 it('Should fail with a short comment', async function () {
197 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
200 it('Should fail with a long comment', async function () {
202 text: 'h'.repeat(10001)
204 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
207 it('Should fail with an incorrect video', async function () {
208 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
210 text: 'super comment'
212 await makePostBodyRequest({
215 token: server.accessToken,
217 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
221 it('Should fail with an incorrect comment', async function () {
222 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
224 text: 'super comment'
226 await makePostBodyRequest({
229 token: server.accessToken,
231 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
235 it('Should succeed with the correct parameters', async function () {
237 text: 'super comment'
239 await makePostBodyRequest({
242 token: server.accessToken,
244 statusCodeExpected: HttpStatusCode.OK_200
249 describe('When removing video comments', function () {
250 it('Should fail with a non authenticated user', async function () {
251 await makeDeleteRequest({ url: server.url, path: pathComment, token: 'none', statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
254 it('Should fail with another user', async function () {
255 await makeDeleteRequest({
258 token: userAccessToken,
259 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
263 it('Should fail with an incorrect video', async function () {
264 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
265 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
268 it('Should fail with an incorrect comment', async function () {
269 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
270 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: HttpStatusCode.NOT_FOUND_404 })
273 it('Should succeed with the same user', async function () {
274 let commentToDelete: number
277 const created = await server.comments.createThread({ videoId: video.uuid, token: userAccessToken, text: 'hello' })
278 commentToDelete = created.id
281 const path = '/api/v1/videos/' + video.uuid + '/comments/' + commentToDelete
283 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
284 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 })
287 it('Should succeed with the owner of the video', async function () {
288 let commentToDelete: number
289 let anotherVideoUUID: string
292 const { uuid } = await server.videos.upload({ token: userAccessToken, attributes: { name: 'video' } })
293 anotherVideoUUID = uuid
297 const created = await server.comments.createThread({ videoId: anotherVideoUUID, text: 'hello' })
298 commentToDelete = created.id
301 const path = '/api/v1/videos/' + anotherVideoUUID + '/comments/' + commentToDelete
303 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: HttpStatusCode.FORBIDDEN_403 })
304 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: HttpStatusCode.NO_CONTENT_204 })
307 it('Should succeed with the correct parameters', async function () {
308 await makeDeleteRequest({
311 token: server.accessToken,
312 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
317 describe('When a video has comments disabled', function () {
318 before(async function () {
319 video = await server.videos.upload({ attributes: { commentsEnabled: false } })
320 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
323 it('Should return an empty thread list', async function () {
324 const res = await makeGetRequest({
327 statusCodeExpected: HttpStatusCode.OK_200
329 expect(res.body.total).to.equal(0)
330 expect(res.body.data).to.have.lengthOf(0)
333 it('Should return an thread comments list')
335 it('Should return conflict on thread add', async function () {
337 text: 'super comment'
339 await makePostBodyRequest({
342 token: server.accessToken,
344 statusCodeExpected: HttpStatusCode.CONFLICT_409
348 it('Should return conflict on comment thread add')
351 describe('When listing admin comments threads', function () {
352 const path = '/api/v1/videos/comments'
354 it('Should fail with a bad start pagination', async function () {
355 await checkBadStartPagination(server.url, path, server.accessToken)
358 it('Should fail with a bad count pagination', async function () {
359 await checkBadCountPagination(server.url, path, server.accessToken)
362 it('Should fail with an incorrect sort', async function () {
363 await checkBadSortPagination(server.url, path, server.accessToken)
366 it('Should fail with a non authenticated user', async function () {
367 await makeGetRequest({
370 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
374 it('Should fail with a non admin user', async function () {
375 await makeGetRequest({
378 token: userAccessToken,
379 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
383 it('Should succeed with the correct params', async function () {
384 await makeGetRequest({
387 token: server.accessToken,
391 searchAccount: 'toto',
394 statusCodeExpected: HttpStatusCode.OK_200
399 after(async function () {
400 await cleanupTests([ server ])