]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/video-comments.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / video-comments.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
e2e22e40
C
2
3import 'mocha'
d4a8e7a6 4import * as chai from 'chai'
c55e3d72
C
5import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
6import { HttpStatusCode, VideoCreateResult } from '@shared/models'
11ba2ab3 7import {
7c3b7976 8 cleanupTests,
254d3579 9 createSingleServer,
42e1ec25
C
10 makeDeleteRequest,
11 makeGetRequest,
12 makePostBodyRequest,
254d3579 13 PeerTubeServer,
d23dd9fb 14 setAccessTokensToServers
bf54587a 15} from '@shared/server-commands'
e2e22e40 16
47564bbe
C
17const expect = chai.expect
18
e2e22e40
C
19describe('Test video comments API validator', function () {
20 let pathThread: string
21 let pathComment: string
254d3579 22 let server: PeerTubeServer
d4a8e7a6 23 let video: VideoCreateResult
4cb6d457 24 let userAccessToken: string
fde37dc9 25 let userAccessToken2: string
e2e22e40
C
26 let commentId: number
27
28 // ---------------------------------------------------------------
29
30 before(async function () {
e212f887 31 this.timeout(30000)
e2e22e40 32
254d3579 33 server = await createSingleServer(1)
e2e22e40
C
34
35 await setAccessTokensToServers([ server ])
36
e2e22e40 37 {
08642a76 38 video = await server.videos.upload({ attributes: {} })
d4a8e7a6 39 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
e2e22e40
C
40 }
41
42 {
89d241a7 43 const created = await server.comments.createThread({ videoId: video.uuid, text: 'coucou' })
12edc149 44 commentId = created.id
d4a8e7a6 45 pathComment = '/api/v1/videos/' + video.uuid + '/comments/' + commentId
e2e22e40 46 }
4cb6d457
C
47
48 {
fde37dc9 49 const user = { username: 'user1', password: 'my super password' }
89d241a7
C
50 await server.users.create({ username: user.username, password: user.password })
51 userAccessToken = await server.login.getAccessToken(user)
4cb6d457 52 }
fde37dc9
C
53
54 {
55 const user = { username: 'user2', password: 'my super password' }
89d241a7
C
56 await server.users.create({ username: user.username, password: user.password })
57 userAccessToken2 = await server.login.getAccessToken(user)
fde37dc9 58 }
e2e22e40
C
59 })
60
61 describe('When listing video comment threads', function () {
62 it('Should fail with a bad start pagination', async function () {
11ba2ab3 63 await checkBadStartPagination(server.url, pathThread, server.accessToken)
e2e22e40
C
64 })
65
66 it('Should fail with a bad count pagination', async function () {
11ba2ab3 67 await checkBadCountPagination(server.url, pathThread, server.accessToken)
e2e22e40
C
68 })
69
70 it('Should fail with an incorrect sort', async function () {
11ba2ab3 71 await checkBadSortPagination(server.url, pathThread, server.accessToken)
e2e22e40
C
72 })
73
74 it('Should fail with an incorrect video', async function () {
11ba2ab3
C
75 await makeGetRequest({
76 url: server.url,
77 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads',
c0e8b12e 78 expectedStatus: HttpStatusCode.NOT_FOUND_404
11ba2ab3 79 })
e2e22e40
C
80 })
81 })
82
83 describe('When listing comments of a thread', function () {
84 it('Should fail with an incorrect video', async function () {
11ba2ab3
C
85 await makeGetRequest({
86 url: server.url,
87 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads/' + commentId,
c0e8b12e 88 expectedStatus: HttpStatusCode.NOT_FOUND_404
11ba2ab3 89 })
e2e22e40
C
90 })
91
92 it('Should fail with an incorrect thread id', async function () {
11ba2ab3
C
93 await makeGetRequest({
94 url: server.url,
d4a8e7a6 95 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/156',
c0e8b12e 96 expectedStatus: HttpStatusCode.NOT_FOUND_404
11ba2ab3 97 })
e2e22e40
C
98 })
99
100 it('Should success with the correct params', async function () {
11ba2ab3
C
101 await makeGetRequest({
102 url: server.url,
d4a8e7a6 103 path: '/api/v1/videos/' + video.shortUUID + '/comment-threads/' + commentId,
c0e8b12e 104 expectedStatus: HttpStatusCode.OK_200
11ba2ab3 105 })
e2e22e40
C
106 })
107 })
108
109 describe('When adding a video thread', function () {
110
111 it('Should fail with a non authenticated user', async function () {
112 const fields = {
113 text: 'text'
114 }
2d53be02
RK
115 await makePostBodyRequest({
116 url: server.url,
117 path: pathThread,
118 token: 'none',
119 fields,
c0e8b12e 120 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
2d53be02 121 })
e2e22e40
C
122 })
123
124 it('Should fail with nothing', async function () {
125 const fields = {}
126 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
127 })
128
129 it('Should fail with a short comment', async function () {
130 const fields = {
53eb90c0 131 text: ''
e2e22e40
C
132 }
133 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
134 })
135
136 it('Should fail with a long comment', async function () {
137 const fields = {
298b3fd3 138 text: 'h'.repeat(10001)
e2e22e40
C
139 }
140 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
141 })
142
143 it('Should fail with an incorrect video', async function () {
144 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads'
145 const fields = {
146 text: 'super comment'
147 }
2d53be02
RK
148 await makePostBodyRequest({
149 url: server.url,
150 path,
151 token: server.accessToken,
152 fields,
c0e8b12e 153 expectedStatus: HttpStatusCode.NOT_FOUND_404
2d53be02 154 })
e2e22e40
C
155 })
156
157 it('Should succeed with the correct parameters', async function () {
158 const fields = {
159 text: 'super comment'
160 }
2d53be02
RK
161 await makePostBodyRequest({
162 url: server.url,
163 path: pathThread,
164 token: server.accessToken,
165 fields,
c0e8b12e 166 expectedStatus: HttpStatusCode.OK_200
2d53be02 167 })
e2e22e40
C
168 })
169 })
170
171 describe('When adding a comment to a thread', function () {
172 it('Should fail with a non authenticated user', async function () {
173 const fields = {
174 text: 'text'
175 }
2d53be02
RK
176 await makePostBodyRequest({
177 url: server.url,
178 path: pathComment,
179 token: 'none',
180 fields,
c0e8b12e 181 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
2d53be02 182 })
e2e22e40
C
183 })
184
185 it('Should fail with nothing', async function () {
186 const fields = {}
187 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
188 })
189
190 it('Should fail with a short comment', async function () {
191 const fields = {
53eb90c0 192 text: ''
e2e22e40
C
193 }
194 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
195 })
196
197 it('Should fail with a long comment', async function () {
198 const fields = {
298b3fd3 199 text: 'h'.repeat(10001)
e2e22e40
C
200 }
201 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
202 })
203
204 it('Should fail with an incorrect video', async function () {
205 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
206 const fields = {
207 text: 'super comment'
208 }
2d53be02
RK
209 await makePostBodyRequest({
210 url: server.url,
211 path,
212 token: server.accessToken,
213 fields,
c0e8b12e 214 expectedStatus: HttpStatusCode.NOT_FOUND_404
2d53be02 215 })
e2e22e40
C
216 })
217
218 it('Should fail with an incorrect comment', async function () {
d4a8e7a6 219 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
e2e22e40
C
220 const fields = {
221 text: 'super comment'
222 }
2d53be02
RK
223 await makePostBodyRequest({
224 url: server.url,
225 path,
226 token: server.accessToken,
227 fields,
c0e8b12e 228 expectedStatus: HttpStatusCode.NOT_FOUND_404
2d53be02 229 })
e2e22e40
C
230 })
231
232 it('Should succeed with the correct parameters', async function () {
233 const fields = {
234 text: 'super comment'
235 }
2d53be02
RK
236 await makePostBodyRequest({
237 url: server.url,
238 path: pathComment,
239 token: server.accessToken,
240 fields,
c0e8b12e 241 expectedStatus: HttpStatusCode.OK_200
2d53be02 242 })
e2e22e40
C
243 })
244 })
245
4cb6d457
C
246 describe('When removing video comments', function () {
247 it('Should fail with a non authenticated user', async function () {
c0e8b12e 248 await makeDeleteRequest({ url: server.url, path: pathComment, token: 'none', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
4cb6d457
C
249 })
250
251 it('Should fail with another user', async function () {
2d53be02
RK
252 await makeDeleteRequest({
253 url: server.url,
254 path: pathComment,
255 token: userAccessToken,
c0e8b12e 256 expectedStatus: HttpStatusCode.FORBIDDEN_403
2d53be02 257 })
4cb6d457
C
258 })
259
260 it('Should fail with an incorrect video', async function () {
261 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
c0e8b12e 262 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
4cb6d457
C
263 })
264
265 it('Should fail with an incorrect comment', async function () {
d4a8e7a6 266 const path = '/api/v1/videos/' + video.uuid + '/comments/124'
c0e8b12e 267 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
4cb6d457
C
268 })
269
fde37dc9
C
270 it('Should succeed with the same user', async function () {
271 let commentToDelete: number
272
273 {
89d241a7 274 const created = await server.comments.createThread({ videoId: video.uuid, token: userAccessToken, text: 'hello' })
12edc149 275 commentToDelete = created.id
fde37dc9
C
276 }
277
d4a8e7a6 278 const path = '/api/v1/videos/' + video.uuid + '/comments/' + commentToDelete
fde37dc9 279
c0e8b12e
C
280 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
281 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, expectedStatus: HttpStatusCode.NO_CONTENT_204 })
fde37dc9
C
282 })
283
284 it('Should succeed with the owner of the video', async function () {
285 let commentToDelete: number
286 let anotherVideoUUID: string
287
288 {
89d241a7 289 const { uuid } = await server.videos.upload({ token: userAccessToken, attributes: { name: 'video' } })
d23dd9fb 290 anotherVideoUUID = uuid
fde37dc9
C
291 }
292
293 {
89d241a7 294 const created = await server.comments.createThread({ videoId: anotherVideoUUID, text: 'hello' })
12edc149 295 commentToDelete = created.id
fde37dc9
C
296 }
297
298 const path = '/api/v1/videos/' + anotherVideoUUID + '/comments/' + commentToDelete
299
c0e8b12e
C
300 await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
301 await makeDeleteRequest({ url: server.url, path, token: userAccessToken, expectedStatus: HttpStatusCode.NO_CONTENT_204 })
fde37dc9
C
302 })
303
4cb6d457 304 it('Should succeed with the correct parameters', async function () {
2d53be02
RK
305 await makeDeleteRequest({
306 url: server.url,
307 path: pathComment,
308 token: server.accessToken,
c0e8b12e 309 expectedStatus: HttpStatusCode.NO_CONTENT_204
2d53be02 310 })
4cb6d457
C
311 })
312 })
313
47564bbe
C
314 describe('When a video has comments disabled', function () {
315 before(async function () {
89d241a7 316 video = await server.videos.upload({ attributes: { commentsEnabled: false } })
d4a8e7a6 317 pathThread = '/api/v1/videos/' + video.uuid + '/comment-threads'
47564bbe
C
318 })
319
320 it('Should return an empty thread list', async function () {
321 const res = await makeGetRequest({
322 url: server.url,
323 path: pathThread,
c0e8b12e 324 expectedStatus: HttpStatusCode.OK_200
47564bbe
C
325 })
326 expect(res.body.total).to.equal(0)
327 expect(res.body.data).to.have.lengthOf(0)
328 })
329
330 it('Should return an thread comments list')
331
332 it('Should return conflict on thread add', async function () {
333 const fields = {
334 text: 'super comment'
335 }
2d53be02
RK
336 await makePostBodyRequest({
337 url: server.url,
338 path: pathThread,
339 token: server.accessToken,
340 fields,
c0e8b12e 341 expectedStatus: HttpStatusCode.CONFLICT_409
2d53be02 342 })
47564bbe
C
343 })
344
345 it('Should return conflict on comment thread add')
346 })
347
f1273314
C
348 describe('When listing admin comments threads', function () {
349 const path = '/api/v1/videos/comments'
350
351 it('Should fail with a bad start pagination', async function () {
352 await checkBadStartPagination(server.url, path, server.accessToken)
353 })
354
355 it('Should fail with a bad count pagination', async function () {
356 await checkBadCountPagination(server.url, path, server.accessToken)
357 })
358
359 it('Should fail with an incorrect sort', async function () {
360 await checkBadSortPagination(server.url, path, server.accessToken)
361 })
362
363 it('Should fail with a non authenticated user', async function () {
364 await makeGetRequest({
365 url: server.url,
366 path,
c0e8b12e 367 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
f1273314
C
368 })
369 })
370
371 it('Should fail with a non admin user', async function () {
372 await makeGetRequest({
373 url: server.url,
374 path,
375 token: userAccessToken,
c0e8b12e 376 expectedStatus: HttpStatusCode.FORBIDDEN_403
f1273314
C
377 })
378 })
379
380 it('Should succeed with the correct params', async function () {
381 await makeGetRequest({
382 url: server.url,
383 path,
384 token: server.accessToken,
385 query: {
386 isLocal: false,
387 search: 'toto',
388 searchAccount: 'toto',
389 searchVideo: 'toto'
390 },
c0e8b12e 391 expectedStatus: HttpStatusCode.OK_200
f1273314
C
392 })
393 })
394 })
395
7c3b7976
C
396 after(async function () {
397 await cleanupTests([ server ])
e2e22e40
C
398 })
399})