1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { AccountBlock, ServerBlock, Video } from '../../../../shared/index'
9 flushAndRunMultipleServers,
15 } from '../../utils/index'
16 import { setAccessTokensToServers } from '../../utils/users/login'
17 import { getVideosListWithToken } from '../../utils/videos/videos'
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'
27 addAccountToAccountBlocklist,
28 addServerToAccountBlocklist,
29 getAccountBlocklistByAccount, getServerBlocklistByAccount,
30 removeAccountFromAccountBlocklist,
31 removeServerFromAccountBlocklist
32 } from '../../utils/users/blocklist'
34 const expect = chai.expect
36 async function checkAllVideos (url: string, token: string) {
37 const res = await getVideosListWithToken(url, token)
39 expect(res.body.data).to.have.lengthOf(4)
42 async function checkAllComments (url: string, token: string, videoUUID: string) {
43 const resThreads = await getVideoCommentThreads(url, videoUUID, 0, 5, '-createdAt', token)
45 const threads: VideoComment[] = resThreads.body.data
46 expect(threads).to.have.lengthOf(2)
48 for (const thread of threads) {
49 const res = await getVideoThreadComments(url, videoUUID, thread.id, token)
51 const tree: VideoCommentThreadTree = res.body
52 expect(tree.children).to.have.lengthOf(1)
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
63 before(async function () {
68 servers = await flushAndRunMultipleServers(2)
69 await setAccessTokensToServers(servers)
72 const user = { username: 'user1', password: 'password' }
73 await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)
75 userToken1 = await userLogin(servers[0], user)
76 await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
80 const user = { username: 'user2', password: 'password' }
81 await createUser(servers[1].url, servers[1].accessToken, user.username, user.password)
83 userToken2 = await userLogin(servers[1], user)
84 await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
88 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
89 videoUUID1 = res.body.video.uuid
93 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
94 videoUUID2 = res.body.video.uuid
97 await doubleFollow(servers[0], servers[1])
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')
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')
110 await waitJobs(servers)
113 describe('When managing account blocklist', function () {
114 it('Should list all videos', function () {
115 return checkAllVideos(servers[0].url, servers[0].accessToken)
118 it('Should list the comments', function () {
119 return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
122 it('Should block a remote account', async function () {
123 await addAccountToAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:9002')
126 it('Should hide its videos', async function () {
127 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
129 const videos: Video[] = res.body.data
130 expect(videos).to.have.lengthOf(3)
132 const v = videos.find(v => v.name === 'video user 2')
133 expect(v).to.be.undefined
136 it('Should block a local account', async function () {
137 await addAccountToAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1')
140 it('Should hide its videos', async function () {
141 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
143 const videos: Video[] = res.body.data
144 expect(videos).to.have.lengthOf(2)
146 const v = videos.find(v => v.name === 'video user 1')
147 expect(v).to.be.undefined
150 it('Should hide its comments', async function () {
151 const resThreads = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 5, '-createdAt', servers[0].accessToken)
153 const threads: VideoComment[] = resThreads.body.data
154 expect(threads).to.have.lengthOf(1)
155 expect(threads[0].totalReplies).to.equal(0)
157 const t = threads.find(t => t.text === 'comment user 1')
158 expect(t).to.be.undefined
160 for (const thread of threads) {
161 const res = await getVideoThreadComments(servers[0].url, videoUUID1, thread.id, servers[0].accessToken)
163 const tree: VideoCommentThreadTree = res.body
164 expect(tree.children).to.have.lengthOf(0)
168 it('Should list all the videos with another user', async function () {
169 return checkAllVideos(servers[0].url, userToken1)
172 it('Should list all the comments with another user', async function () {
173 return checkAllComments(servers[0].url, userToken1, videoUUID1)
176 it('Should list blocked accounts', async function () {
178 const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
179 const blocks: AccountBlock[] = res.body.data
181 expect(res.body.total).to.equal(2)
183 const block = blocks[0]
184 expect(block.byAccount.displayName).to.equal('root')
185 expect(block.byAccount.name).to.equal('root')
186 expect(block.blockedAccount.displayName).to.equal('user2')
187 expect(block.blockedAccount.name).to.equal('user2')
188 expect(block.blockedAccount.host).to.equal('localhost:9002')
192 const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
193 const blocks: AccountBlock[] = res.body.data
195 expect(res.body.total).to.equal(2)
197 const block = blocks[0]
198 expect(block.byAccount.displayName).to.equal('root')
199 expect(block.byAccount.name).to.equal('root')
200 expect(block.blockedAccount.displayName).to.equal('user1')
201 expect(block.blockedAccount.name).to.equal('user1')
202 expect(block.blockedAccount.host).to.equal('localhost:9001')
206 it('Should unblock the remote account', async function () {
207 await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:9002')
210 it('Should display its videos', async function () {
211 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
213 const videos: Video[] = res.body.data
214 expect(videos).to.have.lengthOf(3)
216 const v = videos.find(v => v.name === 'video user 2')
217 expect(v).not.to.be.undefined
220 it('Should unblock the local account', async function () {
221 await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1')
224 it('Should display its comments', function () {
225 return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
229 describe('When managing server blocklist', function () {
230 it('Should list all videos', function () {
231 return checkAllVideos(servers[0].url, servers[0].accessToken)
234 it('Should list the comments', function () {
235 return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
238 it('Should block a remote server', async function () {
239 await addServerToAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:9002')
242 it('Should hide its videos', async function () {
243 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
245 const videos: Video[] = res.body.data
246 expect(videos).to.have.lengthOf(2)
248 const v1 = videos.find(v => v.name === 'video user 2')
249 const v2 = videos.find(v => v.name === 'video server 2')
251 expect(v1).to.be.undefined
252 expect(v2).to.be.undefined
255 it('Should list all the videos with another user', async function () {
256 return checkAllVideos(servers[0].url, userToken1)
259 it('Should hide its comments')
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
265 expect(res.body.total).to.equal(1)
267 const block = blocks[0]
268 expect(block.byAccount.displayName).to.equal('root')
269 expect(block.byAccount.name).to.equal('root')
270 expect(block.blockedServer.host).to.equal('localhost:9002')
273 it('Should unblock the remote server', async function () {
274 await removeServerFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:9002')
277 it('Should display its videos', function () {
278 return checkAllVideos(servers[0].url, servers[0].accessToken)
281 it('Should display its comments', function () {
282 return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
286 after(async function () {
287 killallServers(servers)
289 // Keep the logs if the test failed