allow limiting video-comments rss feeds to an account or video channel
[oweals/peertube.git] / server / tests / api / check-params / video-comments.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   cleanupTests,
7   createUser,
8   flushAndRunServer,
9   makeDeleteRequest,
10   makeGetRequest,
11   makePostBodyRequest,
12   ServerInfo,
13   setAccessTokensToServers,
14   uploadVideo,
15   userLogin
16 } from '../../../../shared/extra-utils'
17 import {
18   checkBadCountPagination,
19   checkBadSortPagination,
20   checkBadStartPagination
21 } from '../../../../shared/extra-utils/requests/check-api-params'
22 import { addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
23
24 const expect = chai.expect
25
26 describe('Test video comments API validator', function () {
27   let pathThread: string
28   let pathComment: string
29   let server: ServerInfo
30   let videoUUID: string
31   let userAccessToken: string
32   let userAccessToken2: string
33   let commentId: number
34
35   // ---------------------------------------------------------------
36
37   before(async function () {
38     this.timeout(30000)
39
40     server = await flushAndRunServer(1)
41
42     await setAccessTokensToServers([ server ])
43
44     {
45       const res = await uploadVideo(server.url, server.accessToken, {})
46       videoUUID = res.body.video.uuid
47       pathThread = '/api/v1/videos/' + videoUUID + '/comment-threads'
48     }
49
50     {
51       const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, 'coucou')
52       commentId = res.body.comment.id
53       pathComment = '/api/v1/videos/' + videoUUID + '/comments/' + commentId
54     }
55
56     {
57       const user = { username: 'user1', password: 'my super password' }
58       await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
59       userAccessToken = await userLogin(server, user)
60     }
61
62     {
63       const user = { username: 'user2', password: 'my super password' }
64       await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
65       userAccessToken2 = await userLogin(server, user)
66     }
67   })
68
69   describe('When listing video comment threads', function () {
70     it('Should fail with a bad start pagination', async function () {
71       await checkBadStartPagination(server.url, pathThread, server.accessToken)
72     })
73
74     it('Should fail with a bad count pagination', async function () {
75       await checkBadCountPagination(server.url, pathThread, server.accessToken)
76     })
77
78     it('Should fail with an incorrect sort', async function () {
79       await checkBadSortPagination(server.url, pathThread, server.accessToken)
80     })
81
82     it('Should fail with an incorrect video', async function () {
83       await makeGetRequest({
84         url: server.url,
85         path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads',
86         statusCodeExpected: 404
87       })
88     })
89   })
90
91   describe('When listing comments of a thread', function () {
92     it('Should fail with an incorrect video', async function () {
93       await makeGetRequest({
94         url: server.url,
95         path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads/' + commentId,
96         statusCodeExpected: 404
97       })
98     })
99
100     it('Should fail with an incorrect thread id', async function () {
101       await makeGetRequest({
102         url: server.url,
103         path: '/api/v1/videos/' + videoUUID + '/comment-threads/156',
104         statusCodeExpected: 404
105       })
106     })
107
108     it('Should success with the correct params', async function () {
109       await makeGetRequest({
110         url: server.url,
111         path: '/api/v1/videos/' + videoUUID + '/comment-threads/' + commentId,
112         statusCodeExpected: 200
113       })
114     })
115   })
116
117   describe('When adding a video thread', function () {
118
119     it('Should fail with a non authenticated user', async function () {
120       const fields = {
121         text: 'text'
122       }
123       await makePostBodyRequest({ url: server.url, path: pathThread, token: 'none', fields, statusCodeExpected: 401 })
124     })
125
126     it('Should fail with nothing', async function () {
127       const fields = {}
128       await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
129     })
130
131     it('Should fail with a short comment', async function () {
132       const fields = {
133         text: ''
134       }
135       await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
136     })
137
138     it('Should fail with a long comment', async function () {
139       const fields = {
140         text: 'h'.repeat(10001)
141       }
142       await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
143     })
144
145     it('Should fail with an incorrect video', async function () {
146       const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads'
147       const fields = {
148         text: 'super comment'
149       }
150       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 404 })
151     })
152
153     it('Should succeed with the correct parameters', async function () {
154       const fields = {
155         text: 'super comment'
156       }
157       await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields, statusCodeExpected: 200 })
158     })
159   })
160
161   describe('When adding a comment to a thread', function () {
162     it('Should fail with a non authenticated user', async function () {
163       const fields = {
164         text: 'text'
165       }
166       await makePostBodyRequest({ url: server.url, path: pathComment, token: 'none', fields, statusCodeExpected: 401 })
167     })
168
169     it('Should fail with nothing', async function () {
170       const fields = {}
171       await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
172     })
173
174     it('Should fail with a short comment', async function () {
175       const fields = {
176         text: ''
177       }
178       await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
179     })
180
181     it('Should fail with a long comment', async function () {
182       const fields = {
183         text: 'h'.repeat(10001)
184       }
185       await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
186     })
187
188     it('Should fail with an incorrect video', async function () {
189       const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
190       const fields = {
191         text: 'super comment'
192       }
193       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 404 })
194     })
195
196     it('Should fail with an incorrect comment', async function () {
197       const path = '/api/v1/videos/' + videoUUID + '/comments/124'
198       const fields = {
199         text: 'super comment'
200       }
201       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 404 })
202     })
203
204     it('Should succeed with the correct parameters', async function () {
205       const fields = {
206         text: 'super comment'
207       }
208       await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields, statusCodeExpected: 200 })
209     })
210   })
211
212   describe('When removing video comments', function () {
213     it('Should fail with a non authenticated user', async function () {
214       await makeDeleteRequest({ url: server.url, path: pathComment, token: 'none', statusCodeExpected: 401 })
215     })
216
217     it('Should fail with another user', async function () {
218       await makeDeleteRequest({ url: server.url, path: pathComment, token: userAccessToken, statusCodeExpected: 403 })
219     })
220
221     it('Should fail with an incorrect video', async function () {
222       const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
223       await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: 404 })
224     })
225
226     it('Should fail with an incorrect comment', async function () {
227       const path = '/api/v1/videos/' + videoUUID + '/comments/124'
228       await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: 404 })
229     })
230
231     it('Should succeed with the same user', async function () {
232       let commentToDelete: number
233
234       {
235         const res = await addVideoCommentThread(server.url, userAccessToken, videoUUID, 'hello')
236         commentToDelete = res.body.comment.id
237       }
238
239       const path = '/api/v1/videos/' + videoUUID + '/comments/' + commentToDelete
240
241       await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: 403 })
242       await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: 204 })
243     })
244
245     it('Should succeed with the owner of the video', async function () {
246       let commentToDelete: number
247       let anotherVideoUUID: string
248
249       {
250         const res = await uploadVideo(server.url, userAccessToken, { name: 'video' })
251         anotherVideoUUID = res.body.video.uuid
252       }
253
254       {
255         const res = await addVideoCommentThread(server.url, server.accessToken, anotherVideoUUID, 'hello')
256         commentToDelete = res.body.comment.id
257       }
258
259       const path = '/api/v1/videos/' + anotherVideoUUID + '/comments/' + commentToDelete
260
261       await makeDeleteRequest({ url: server.url, path, token: userAccessToken2, statusCodeExpected: 403 })
262       await makeDeleteRequest({ url: server.url, path, token: userAccessToken, statusCodeExpected: 204 })
263     })
264
265     it('Should succeed with the correct parameters', async function () {
266       await makeDeleteRequest({ url: server.url, path: pathComment, token: server.accessToken, statusCodeExpected: 204 })
267     })
268   })
269
270   describe('When a video has comments disabled', function () {
271     before(async function () {
272       const res = await uploadVideo(server.url, server.accessToken, { commentsEnabled: false })
273       videoUUID = res.body.video.uuid
274       pathThread = '/api/v1/videos/' + videoUUID + '/comment-threads'
275     })
276
277     it('Should return an empty thread list', async function () {
278       const res = await makeGetRequest({
279         url: server.url,
280         path: pathThread,
281         statusCodeExpected: 200
282       })
283       expect(res.body.total).to.equal(0)
284       expect(res.body.data).to.have.lengthOf(0)
285     })
286
287     it('Should return an thread comments list')
288
289     it('Should return conflict on thread add', async function () {
290       const fields = {
291         text: 'super comment'
292       }
293       await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields, statusCodeExpected: 409 })
294     })
295
296     it('Should return conflict on comment thread add')
297   })
298
299   after(async function () {
300     await cleanupTests([ server ])
301   })
302 })