Add ability for users to block an account/instance on server side
[oweals/peertube.git] / server / tests / api / users / account-blocklist.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { AccountBlock, ServerBlock, Video } from '../../../../shared/index'
6 import {
7   createUser,
8   doubleFollow,
9   flushAndRunMultipleServers,
10   flushTests,
11   killallServers,
12   ServerInfo,
13   uploadVideo,
14   userLogin
15 } from '../../utils/index'
16 import { setAccessTokensToServers } from '../../utils/users/login'
17 import { getVideosListWithToken } from '../../utils/videos/videos'
18 import {
19   addVideoCommentReply,
20   addVideoCommentThread,
21   getVideoCommentThreads,
22   getVideoThreadComments
23 } from '../../utils/videos/video-comments'
24 import { waitJobs } from '../../utils/server/jobs'
25 import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
26 import {
27   addAccountToAccountBlocklist,
28   addServerToAccountBlocklist,
29   getAccountBlocklistByAccount, getServerBlocklistByAccount,
30   removeAccountFromAccountBlocklist,
31   removeServerFromAccountBlocklist
32 } from '../../utils/users/blocklist'
33
34 const expect = chai.expect
35
36 async function checkAllVideos (url: string, token: string) {
37   const res = await getVideosListWithToken(url, token)
38
39   expect(res.body.data).to.have.lengthOf(4)
40 }
41
42 async function checkAllComments (url: string, token: string, videoUUID: string) {
43   const resThreads = await getVideoCommentThreads(url, videoUUID, 0, 5, '-createdAt', token)
44
45   const threads: VideoComment[] = resThreads.body.data
46   expect(threads).to.have.lengthOf(2)
47
48   for (const thread of threads) {
49     const res = await getVideoThreadComments(url, videoUUID, thread.id, token)
50
51     const tree: VideoCommentThreadTree = res.body
52     expect(tree.children).to.have.lengthOf(1)
53   }
54 }
55
56 describe('Test accounts blocklist', function () {
57   let servers: ServerInfo[]
58   let videoUUID1: string
59   let videoUUID2: string
60   let userToken1: string
61   let userToken2: string
62
63   before(async function () {
64     this.timeout(60000)
65
66     await flushTests()
67
68     servers = await flushAndRunMultipleServers(2)
69     await setAccessTokensToServers(servers)
70
71     {
72       const user = { username: 'user1', password: 'password' }
73       await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)
74
75       userToken1 = await userLogin(servers[0], user)
76       await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
77     }
78
79     {
80       const user = { username: 'user2', password: 'password' }
81       await createUser(servers[1].url, servers[1].accessToken, user.username, user.password)
82
83       userToken2 = await userLogin(servers[1], user)
84       await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
85     }
86
87     {
88       const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
89       videoUUID1 = res.body.video.uuid
90     }
91
92     {
93       const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
94       videoUUID2 = res.body.video.uuid
95     }
96
97     await doubleFollow(servers[0], servers[1])
98
99     {
100       const resComment = await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, 'comment root 1')
101       const resReply = await addVideoCommentReply(servers[ 0 ].url, userToken1, videoUUID1, resComment.body.comment.id, 'comment user 1')
102       await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resReply.body.comment.id, 'comment root 1')
103     }
104
105     {
106       const resComment = await addVideoCommentThread(servers[ 0 ].url, userToken1, videoUUID1, 'comment user 1')
107       await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resComment.body.comment.id, 'comment root 1')
108     }
109
110     await waitJobs(servers)
111   })
112
113   describe('When managing account blocklist', function () {
114     it('Should list all videos', function () {
115       return checkAllVideos(servers[0].url, servers[0].accessToken)
116     })
117
118     it('Should list the comments', function () {
119       return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
120     })
121
122     it('Should block a remote account', async function () {
123       await addAccountToAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:9002')
124     })
125
126     it('Should hide its videos', async function () {
127       const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
128
129       const videos: Video[] = res.body.data
130       expect(videos).to.have.lengthOf(3)
131
132       const v = videos.find(v => v.name === 'video user 2')
133       expect(v).to.be.undefined
134     })
135
136     it('Should block a local account', async function () {
137       await addAccountToAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1')
138     })
139
140     it('Should hide its videos', async function () {
141       const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
142
143       const videos: Video[] = res.body.data
144       expect(videos).to.have.lengthOf(2)
145
146       const v = videos.find(v => v.name === 'video user 1')
147       expect(v).to.be.undefined
148     })
149
150     it('Should hide its comments', async function () {
151       const resThreads = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 5, '-createdAt', servers[0].accessToken)
152
153       const threads: VideoComment[] = resThreads.body.data
154       expect(threads).to.have.lengthOf(1)
155       expect(threads[0].totalReplies).to.equal(0)
156
157       const t = threads.find(t => t.text === 'comment user 1')
158       expect(t).to.be.undefined
159
160       for (const thread of threads) {
161         const res = await getVideoThreadComments(servers[0].url, videoUUID1, thread.id, servers[0].accessToken)
162
163         const tree: VideoCommentThreadTree = res.body
164         expect(tree.children).to.have.lengthOf(0)
165       }
166     })
167
168     it('Should list all the videos with another user', async function () {
169       return checkAllVideos(servers[0].url, userToken1)
170     })
171
172     it('Should list all the comments with another user', async function () {
173       return checkAllComments(servers[0].url, userToken1, videoUUID1)
174     })
175
176     it('Should list blocked accounts', async function () {
177       {
178         const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
179         const blocks: AccountBlock[] = res.body.data
180
181         expect(res.body.total).to.equal(2)
182
183         const block = blocks[0]
184         expect(block.byAccount.displayName).to.equal('root')
185         expect(block.byAccount.name).to.equal('root')
186         expect(block.accountBlocked.displayName).to.equal('user2')
187         expect(block.accountBlocked.name).to.equal('user2')
188         expect(block.accountBlocked.host).to.equal('localhost:9002')
189       }
190
191       {
192         const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
193         const blocks: AccountBlock[] = res.body.data
194
195         expect(res.body.total).to.equal(2)
196
197         const block = blocks[0]
198         expect(block.byAccount.displayName).to.equal('root')
199         expect(block.byAccount.name).to.equal('root')
200         expect(block.accountBlocked.displayName).to.equal('user1')
201         expect(block.accountBlocked.name).to.equal('user1')
202         expect(block.accountBlocked.host).to.equal('localhost:9001')
203       }
204     })
205
206     it('Should unblock the remote account', async function () {
207       await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:9002')
208     })
209
210     it('Should display its videos', async function () {
211       const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
212
213       const videos: Video[] = res.body.data
214       expect(videos).to.have.lengthOf(3)
215
216       const v = videos.find(v => v.name === 'video user 2')
217       expect(v).not.to.be.undefined
218     })
219
220     it('Should unblock the local account', async function () {
221       await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1')
222     })
223
224     it('Should display its comments', function () {
225       return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
226     })
227   })
228
229   describe('When managing server blocklist', function () {
230     it('Should list all videos', function () {
231       return checkAllVideos(servers[0].url, servers[0].accessToken)
232     })
233
234     it('Should list the comments', function () {
235       return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
236     })
237
238     it('Should block a remote server', async function () {
239       await addServerToAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:9002')
240     })
241
242     it('Should hide its videos', async function () {
243       const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
244
245       const videos: Video[] = res.body.data
246       expect(videos).to.have.lengthOf(2)
247
248       const v1 = videos.find(v => v.name === 'video user 2')
249       const v2 = videos.find(v => v.name === 'video server 2')
250
251       expect(v1).to.be.undefined
252       expect(v2).to.be.undefined
253     })
254
255     it('Should list all the videos with another user', async function () {
256       return checkAllVideos(servers[0].url, userToken1)
257     })
258
259     it('Should hide its comments')
260
261     it('Should list blocked servers', async function () {
262       const res = await getServerBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
263       const blocks: ServerBlock[] = res.body.data
264
265       expect(res.body.total).to.equal(1)
266
267       const block = blocks[0]
268       expect(block.byAccount.displayName).to.equal('root')
269       expect(block.byAccount.name).to.equal('root')
270       expect(block.serverBlocked.host).to.equal('localhost:9002')
271     })
272
273     it('Should unblock the remote server', async function () {
274       await removeServerFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:9002')
275     })
276
277     it('Should display its videos', function () {
278       return checkAllVideos(servers[0].url, servers[0].accessToken)
279     })
280
281     it('Should display its comments', function () {
282       return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
283     })
284   })
285
286   after(async function () {
287     killallServers(servers)
288
289     // Keep the logs if the test failed
290     if (this[ 'ok' ]) {
291       await flushTests()
292     }
293   })
294 })