]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/users/blocklist.ts
Try to fix travis
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / blocklist.ts
CommitLineData
b44164bb
C
1/* tslint:disable:no-unused-expression */
2
3import * as chai from 'chai'
4import 'mocha'
5import { AccountBlock, ServerBlock, Video } from '../../../../shared/index'
6import {
7 createUser,
8 doubleFollow,
9 flushAndRunMultipleServers,
10 flushTests,
11 killallServers,
12 ServerInfo,
13 uploadVideo,
14 userLogin
94565d52
C
15} from '../../../../shared/extra-utils/index'
16import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
17import { getVideosListWithToken, getVideosList } from '../../../../shared/extra-utils/videos/videos'
b44164bb
C
18import {
19 addVideoCommentReply,
20 addVideoCommentThread,
21 getVideoCommentThreads,
22 getVideoThreadComments
94565d52
C
23} from '../../../../shared/extra-utils/videos/video-comments'
24import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
b44164bb
C
25import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
26import {
27 addAccountToAccountBlocklist,
28 addAccountToServerBlocklist,
29 addServerToAccountBlocklist,
30 addServerToServerBlocklist,
31 getAccountBlocklistByAccount,
32 getAccountBlocklistByServer,
33 getServerBlocklistByAccount,
34 getServerBlocklistByServer,
35 removeAccountFromAccountBlocklist,
36 removeAccountFromServerBlocklist,
37 removeServerFromAccountBlocklist,
38 removeServerFromServerBlocklist
94565d52 39} from '../../../../shared/extra-utils/users/blocklist'
b44164bb
C
40
41const expect = chai.expect
42
43async function checkAllVideos (url: string, token: string) {
65b21c96
C
44 {
45 const res = await getVideosListWithToken(url, token)
b44164bb 46
65b21c96
C
47 expect(res.body.data).to.have.lengthOf(4)
48 }
49
50 {
51 const res = await getVideosList(url)
52
53 expect(res.body.data).to.have.lengthOf(4)
54 }
b44164bb
C
55}
56
57async function checkAllComments (url: string, token: string, videoUUID: string) {
58 const resThreads = await getVideoCommentThreads(url, videoUUID, 0, 5, '-createdAt', token)
59
60 const threads: VideoComment[] = resThreads.body.data
61 expect(threads).to.have.lengthOf(2)
62
63 for (const thread of threads) {
64 const res = await getVideoThreadComments(url, videoUUID, thread.id, token)
65
66 const tree: VideoCommentThreadTree = res.body
67 expect(tree.children).to.have.lengthOf(1)
68 }
69}
70
71describe('Test blocklist', function () {
72 let servers: ServerInfo[]
73 let videoUUID1: string
74 let videoUUID2: string
75 let userToken1: string
76 let userModeratorToken: string
77 let userToken2: string
78
79 before(async function () {
80 this.timeout(60000)
81
b44164bb
C
82 servers = await flushAndRunMultipleServers(2)
83 await setAccessTokensToServers(servers)
84
85 {
86 const user = { username: 'user1', password: 'password' }
1eddc9a7 87 await createUser({ url: servers[ 0 ].url, accessToken: servers[ 0 ].accessToken, username: user.username, password: user.password })
b44164bb
C
88
89 userToken1 = await userLogin(servers[0], user)
90 await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
91 }
92
93 {
94 const user = { username: 'moderator', password: 'password' }
1eddc9a7 95 await createUser({ url: servers[ 0 ].url, accessToken: servers[ 0 ].accessToken, username: user.username, password: user.password })
b44164bb
C
96
97 userModeratorToken = await userLogin(servers[0], user)
98 }
99
100 {
101 const user = { username: 'user2', password: 'password' }
1eddc9a7 102 await createUser({ url: servers[ 1 ].url, accessToken: servers[ 1 ].accessToken, username: user.username, password: user.password })
b44164bb
C
103
104 userToken2 = await userLogin(servers[1], user)
105 await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
106 }
107
108 {
109 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
110 videoUUID1 = res.body.video.uuid
111 }
112
113 {
114 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
115 videoUUID2 = res.body.video.uuid
116 }
117
118 await doubleFollow(servers[0], servers[1])
119
120 {
121 const resComment = await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, 'comment root 1')
122 const resReply = await addVideoCommentReply(servers[ 0 ].url, userToken1, videoUUID1, resComment.body.comment.id, 'comment user 1')
123 await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resReply.body.comment.id, 'comment root 1')
124 }
125
126 {
127 const resComment = await addVideoCommentThread(servers[ 0 ].url, userToken1, videoUUID1, 'comment user 1')
128 await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resComment.body.comment.id, 'comment root 1')
129 }
130
131 await waitJobs(servers)
132 })
133
134 describe('User blocklist', function () {
135
136 describe('When managing account blocklist', function () {
137 it('Should list all videos', function () {
138 return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
139 })
140
141 it('Should list the comments', function () {
142 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
143 })
144
145 it('Should block a remote account', async function () {
146 await addAccountToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
147 })
148
149 it('Should hide its videos', async function () {
150 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
151
152 const videos: Video[] = res.body.data
153 expect(videos).to.have.lengthOf(3)
154
155 const v = videos.find(v => v.name === 'video user 2')
156 expect(v).to.be.undefined
157 })
158
159 it('Should block a local account', async function () {
160 await addAccountToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
161 })
162
163 it('Should hide its videos', async function () {
164 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
165
166 const videos: Video[] = res.body.data
167 expect(videos).to.have.lengthOf(2)
168
169 const v = videos.find(v => v.name === 'video user 1')
170 expect(v).to.be.undefined
171 })
172
173 it('Should hide its comments', async function () {
174 const resThreads = await getVideoCommentThreads(servers[ 0 ].url, videoUUID1, 0, 5, '-createdAt', servers[ 0 ].accessToken)
175
176 const threads: VideoComment[] = resThreads.body.data
177 expect(threads).to.have.lengthOf(1)
178 expect(threads[ 0 ].totalReplies).to.equal(0)
179
180 const t = threads.find(t => t.text === 'comment user 1')
181 expect(t).to.be.undefined
182
183 for (const thread of threads) {
184 const res = await getVideoThreadComments(servers[ 0 ].url, videoUUID1, thread.id, servers[ 0 ].accessToken)
185
186 const tree: VideoCommentThreadTree = res.body
187 expect(tree.children).to.have.lengthOf(0)
188 }
189 })
190
191 it('Should list all the videos with another user', async function () {
192 return checkAllVideos(servers[ 0 ].url, userToken1)
193 })
194
195 it('Should list all the comments with another user', async function () {
196 return checkAllComments(servers[ 0 ].url, userToken1, videoUUID1)
197 })
198
199 it('Should list blocked accounts', async function () {
200 {
201 const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
202 const blocks: AccountBlock[] = res.body.data
203
204 expect(res.body.total).to.equal(2)
205
206 const block = blocks[ 0 ]
207 expect(block.byAccount.displayName).to.equal('root')
208 expect(block.byAccount.name).to.equal('root')
209 expect(block.blockedAccount.displayName).to.equal('user2')
210 expect(block.blockedAccount.name).to.equal('user2')
211 expect(block.blockedAccount.host).to.equal('localhost:9002')
212 }
213
214 {
215 const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
216 const blocks: AccountBlock[] = res.body.data
217
218 expect(res.body.total).to.equal(2)
219
220 const block = blocks[ 0 ]
221 expect(block.byAccount.displayName).to.equal('root')
222 expect(block.byAccount.name).to.equal('root')
223 expect(block.blockedAccount.displayName).to.equal('user1')
224 expect(block.blockedAccount.name).to.equal('user1')
225 expect(block.blockedAccount.host).to.equal('localhost:9001')
226 }
227 })
228
229 it('Should unblock the remote account', async function () {
230 await removeAccountFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
231 })
232
233 it('Should display its videos', async function () {
234 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
235
236 const videos: Video[] = res.body.data
237 expect(videos).to.have.lengthOf(3)
238
239 const v = videos.find(v => v.name === 'video user 2')
240 expect(v).not.to.be.undefined
241 })
242
243 it('Should unblock the local account', async function () {
244 await removeAccountFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
245 })
246
247 it('Should display its comments', function () {
248 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
249 })
250 })
251
252 describe('When managing server blocklist', function () {
253 it('Should list all videos', function () {
254 return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
255 })
256
257 it('Should list the comments', function () {
258 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
259 })
260
261 it('Should block a remote server', async function () {
262 await addServerToAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
263 })
264
265 it('Should hide its videos', async function () {
266 const res = await getVideosListWithToken(servers[ 0 ].url, servers[ 0 ].accessToken)
267
268 const videos: Video[] = res.body.data
269 expect(videos).to.have.lengthOf(2)
270
271 const v1 = videos.find(v => v.name === 'video user 2')
272 const v2 = videos.find(v => v.name === 'video server 2')
273
274 expect(v1).to.be.undefined
275 expect(v2).to.be.undefined
276 })
277
278 it('Should list all the videos with another user', async function () {
279 return checkAllVideos(servers[ 0 ].url, userToken1)
280 })
281
282 it('Should hide its comments')
283
284 it('Should list blocked servers', async function () {
285 const res = await getServerBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
286 const blocks: ServerBlock[] = res.body.data
287
288 expect(res.body.total).to.equal(1)
289
290 const block = blocks[ 0 ]
291 expect(block.byAccount.displayName).to.equal('root')
292 expect(block.byAccount.name).to.equal('root')
293 expect(block.blockedServer.host).to.equal('localhost:9002')
294 })
295
296 it('Should unblock the remote server', async function () {
297 await removeServerFromAccountBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
298 })
299
300 it('Should display its videos', function () {
301 return checkAllVideos(servers[ 0 ].url, servers[ 0 ].accessToken)
302 })
303
304 it('Should display its comments', function () {
305 return checkAllComments(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1)
306 })
307 })
308 })
309
310 describe('Server blocklist', function () {
311
312 describe('When managing account blocklist', function () {
313 it('Should list all videos', async function () {
314 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
315 await checkAllVideos(servers[ 0 ].url, token)
316 }
317 })
318
319 it('Should list the comments', async function () {
320 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
321 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
322 }
323 })
324
325 it('Should block a remote account', async function () {
326 await addAccountToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
327 })
328
329 it('Should hide its videos', async function () {
330 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
331 const res = await getVideosListWithToken(servers[ 0 ].url, token)
332
333 const videos: Video[] = res.body.data
334 expect(videos).to.have.lengthOf(3)
335
336 const v = videos.find(v => v.name === 'video user 2')
337 expect(v).to.be.undefined
338 }
339 })
340
341 it('Should block a local account', async function () {
342 await addAccountToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
343 })
344
345 it('Should hide its videos', async function () {
346 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
347 const res = await getVideosListWithToken(servers[ 0 ].url, token)
348
349 const videos: Video[] = res.body.data
350 expect(videos).to.have.lengthOf(2)
351
352 const v = videos.find(v => v.name === 'video user 1')
353 expect(v).to.be.undefined
354 }
355 })
356
357 it('Should hide its comments', async function () {
358 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
359 const resThreads = await getVideoCommentThreads(servers[ 0 ].url, videoUUID1, 0, 5, '-createdAt', token)
360
361 const threads: VideoComment[] = resThreads.body.data
362 expect(threads).to.have.lengthOf(1)
363 expect(threads[ 0 ].totalReplies).to.equal(0)
364
365 const t = threads.find(t => t.text === 'comment user 1')
366 expect(t).to.be.undefined
367
368 for (const thread of threads) {
369 const res = await getVideoThreadComments(servers[ 0 ].url, videoUUID1, thread.id, token)
370
371 const tree: VideoCommentThreadTree = res.body
372 expect(tree.children).to.have.lengthOf(0)
373 }
374 }
375 })
376
377 it('Should list blocked accounts', async function () {
378 {
379 const res = await getAccountBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
380 const blocks: AccountBlock[] = res.body.data
381
382 expect(res.body.total).to.equal(2)
383
384 const block = blocks[ 0 ]
385 expect(block.byAccount.displayName).to.equal('peertube')
386 expect(block.byAccount.name).to.equal('peertube')
387 expect(block.blockedAccount.displayName).to.equal('user2')
388 expect(block.blockedAccount.name).to.equal('user2')
389 expect(block.blockedAccount.host).to.equal('localhost:9002')
390 }
391
392 {
393 const res = await getAccountBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
394 const blocks: AccountBlock[] = res.body.data
395
396 expect(res.body.total).to.equal(2)
397
398 const block = blocks[ 0 ]
399 expect(block.byAccount.displayName).to.equal('peertube')
400 expect(block.byAccount.name).to.equal('peertube')
401 expect(block.blockedAccount.displayName).to.equal('user1')
402 expect(block.blockedAccount.name).to.equal('user1')
403 expect(block.blockedAccount.host).to.equal('localhost:9001')
404 }
405 })
406
407 it('Should unblock the remote account', async function () {
408 await removeAccountFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user2@localhost:9002')
409 })
410
411 it('Should display its videos', async function () {
412 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
413 const res = await getVideosListWithToken(servers[ 0 ].url, token)
414
415 const videos: Video[] = res.body.data
416 expect(videos).to.have.lengthOf(3)
417
418 const v = videos.find(v => v.name === 'video user 2')
419 expect(v).not.to.be.undefined
420 }
421 })
422
423 it('Should unblock the local account', async function () {
424 await removeAccountFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'user1')
425 })
426
427 it('Should display its comments', async function () {
428 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
429 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
430 }
431 })
432 })
433
434 describe('When managing server blocklist', function () {
435 it('Should list all videos', async function () {
436 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
437 await checkAllVideos(servers[ 0 ].url, token)
438 }
439 })
440
441 it('Should list the comments', async function () {
442 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
443 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
444 }
445 })
446
447 it('Should block a remote server', async function () {
448 await addServerToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
449 })
450
451 it('Should hide its videos', async function () {
452 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
65b21c96
C
453 const res1 = await getVideosList(servers[ 0 ].url)
454 const res2 = await getVideosListWithToken(servers[ 0 ].url, token)
b44164bb 455
65b21c96
C
456 for (const res of [ res1, res2 ]) {
457 const videos: Video[] = res.body.data
458 expect(videos).to.have.lengthOf(2)
b44164bb 459
65b21c96
C
460 const v1 = videos.find(v => v.name === 'video user 2')
461 const v2 = videos.find(v => v.name === 'video server 2')
b44164bb 462
65b21c96
C
463 expect(v1).to.be.undefined
464 expect(v2).to.be.undefined
465 }
b44164bb
C
466 }
467 })
468
469 it('Should hide its comments')
470
471 it('Should list blocked servers', async function () {
472 const res = await getServerBlocklistByServer(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
473 const blocks: ServerBlock[] = res.body.data
474
475 expect(res.body.total).to.equal(1)
476
477 const block = blocks[ 0 ]
478 expect(block.byAccount.displayName).to.equal('peertube')
479 expect(block.byAccount.name).to.equal('peertube')
480 expect(block.blockedServer.host).to.equal('localhost:9002')
481 })
482
483 it('Should unblock the remote server', async function () {
484 await removeServerFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, 'localhost:9002')
485 })
486
487 it('Should list all videos', async function () {
488 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
489 await checkAllVideos(servers[ 0 ].url, token)
490 }
491 })
492
493 it('Should list the comments', async function () {
494 for (const token of [ userModeratorToken, servers[ 0 ].accessToken ]) {
495 await checkAllComments(servers[ 0 ].url, token, videoUUID1)
496 }
497 })
498 })
499 })
500
210feb6c 501 after(function () {
b44164bb 502 killallServers(servers)
b44164bb
C
503 })
504})