]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/video-comments.ts
Merge branch 'release/2.3.0' into develop
[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 2
47564bbe 3import * as chai from 'chai'
e2e22e40 4import 'mocha'
11ba2ab3 5import {
7c3b7976 6 cleanupTests,
4cb6d457 7 createUser,
42e1ec25 8 flushAndRunServer,
42e1ec25
C
9 makeDeleteRequest,
10 makeGetRequest,
11 makePostBodyRequest,
12 ServerInfo,
13 setAccessTokensToServers,
14 uploadVideo,
15 userLogin
94565d52 16} from '../../../../shared/extra-utils'
9639bd17 17import {
18 checkBadCountPagination,
19 checkBadSortPagination,
20 checkBadStartPagination
94565d52
C
21} from '../../../../shared/extra-utils/requests/check-api-params'
22import { addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
e2e22e40 23
47564bbe
C
24const expect = chai.expect
25
e2e22e40
C
26describe('Test video comments API validator', function () {
27 let pathThread: string
28 let pathComment: string
29 let server: ServerInfo
e2e22e40 30 let videoUUID: string
4cb6d457 31 let userAccessToken: string
fde37dc9 32 let userAccessToken2: string
e2e22e40
C
33 let commentId: number
34
35 // ---------------------------------------------------------------
36
37 before(async function () {
e212f887 38 this.timeout(30000)
e2e22e40 39
210feb6c 40 server = await flushAndRunServer(1)
e2e22e40
C
41
42 await setAccessTokensToServers([ server ])
43
e2e22e40
C
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 }
4cb6d457
C
55
56 {
fde37dc9 57 const user = { username: 'user1', password: 'my super password' }
1eddc9a7 58 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
4cb6d457
C
59 userAccessToken = await userLogin(server, user)
60 }
fde37dc9
C
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 }
e2e22e40
C
67 })
68
69 describe('When listing video comment threads', function () {
70 it('Should fail with a bad start pagination', async function () {
11ba2ab3 71 await checkBadStartPagination(server.url, pathThread, server.accessToken)
e2e22e40
C
72 })
73
74 it('Should fail with a bad count pagination', async function () {
11ba2ab3 75 await checkBadCountPagination(server.url, pathThread, server.accessToken)
e2e22e40
C
76 })
77
78 it('Should fail with an incorrect sort', async function () {
11ba2ab3 79 await checkBadSortPagination(server.url, pathThread, server.accessToken)
e2e22e40
C
80 })
81
82 it('Should fail with an incorrect video', async function () {
11ba2ab3
C
83 await makeGetRequest({
84 url: server.url,
85 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads',
86 statusCodeExpected: 404
87 })
e2e22e40
C
88 })
89 })
90
91 describe('When listing comments of a thread', function () {
92 it('Should fail with an incorrect video', async function () {
11ba2ab3
C
93 await makeGetRequest({
94 url: server.url,
95 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads/' + commentId,
96 statusCodeExpected: 404
97 })
e2e22e40
C
98 })
99
100 it('Should fail with an incorrect thread id', async function () {
11ba2ab3
C
101 await makeGetRequest({
102 url: server.url,
103 path: '/api/v1/videos/' + videoUUID + '/comment-threads/156',
104 statusCodeExpected: 404
105 })
e2e22e40
C
106 })
107
108 it('Should success with the correct params', async function () {
11ba2ab3
C
109 await makeGetRequest({
110 url: server.url,
111 path: '/api/v1/videos/' + videoUUID + '/comment-threads/' + commentId,
112 statusCodeExpected: 200
113 })
e2e22e40
C
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 = {
53eb90c0 133 text: ''
e2e22e40
C
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 = {
298b3fd3 140 text: 'h'.repeat(10001)
e2e22e40
C
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 = {
53eb90c0 176 text: ''
e2e22e40
C
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 = {
298b3fd3 183 text: 'h'.repeat(10001)
e2e22e40
C
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
4cb6d457
C
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
fde37dc9
C
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
4cb6d457
C
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
47564bbe
C
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
7c3b7976
C
299 after(async function () {
300 await cleanupTests([ server ])
e2e22e40
C
301 })
302})