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