aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/users/account-blocklist.ts
blob: 026971331a11f18b325e0747138176e0d268d77f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* tslint:disable:no-unused-expression */

import * as chai from 'chai'
import 'mocha'
import { AccountBlock, ServerBlock, Video } from '../../../../shared/index'
import {
  createUser,
  doubleFollow,
  flushAndRunMultipleServers,
  flushTests,
  killallServers,
  ServerInfo,
  uploadVideo,
  userLogin
} from '../../utils/index'
import { setAccessTokensToServers } from '../../utils/users/login'
import { getVideosListWithToken } from '../../utils/videos/videos'
import {
  addVideoCommentReply,
  addVideoCommentThread,
  getVideoCommentThreads,
  getVideoThreadComments
} from '../../utils/videos/video-comments'
import { waitJobs } from '../../utils/server/jobs'
import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
import {
  addAccountToAccountBlocklist,
  addServerToAccountBlocklist,
  getAccountBlocklistByAccount, getServerBlocklistByAccount,
  removeAccountFromAccountBlocklist,
  removeServerFromAccountBlocklist
} from '../../utils/users/blocklist'

const expect = chai.expect

async function checkAllVideos (url: string, token: string) {
  const res = await getVideosListWithToken(url, token)

  expect(res.body.data).to.have.lengthOf(4)
}

async function checkAllComments (url: string, token: string, videoUUID: string) {
  const resThreads = await getVideoCommentThreads(url, videoUUID, 0, 5, '-createdAt', token)

  const threads: VideoComment[] = resThreads.body.data
  expect(threads).to.have.lengthOf(2)

  for (const thread of threads) {
    const res = await getVideoThreadComments(url, videoUUID, thread.id, token)

    const tree: VideoCommentThreadTree = res.body
    expect(tree.children).to.have.lengthOf(1)
  }
}

describe('Test accounts blocklist', function () {
  let servers: ServerInfo[]
  let videoUUID1: string
  let videoUUID2: string
  let userToken1: string
  let userToken2: string

  before(async function () {
    this.timeout(60000)

    await flushTests()

    servers = await flushAndRunMultipleServers(2)
    await setAccessTokensToServers(servers)

    {
      const user = { username: 'user1', password: 'password' }
      await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)

      userToken1 = await userLogin(servers[0], user)
      await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
    }

    {
      const user = { username: 'user2', password: 'password' }
      await createUser(servers[1].url, servers[1].accessToken, user.username, user.password)

      userToken2 = await userLogin(servers[1], user)
      await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
    }

    {
      const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
      videoUUID1 = res.body.video.uuid
    }

    {
      const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
      videoUUID2 = res.body.video.uuid
    }

    await doubleFollow(servers[0], servers[1])

    {
      const resComment = await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, 'comment root 1')
      const resReply = await addVideoCommentReply(servers[ 0 ].url, userToken1, videoUUID1, resComment.body.comment.id, 'comment user 1')
      await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resReply.body.comment.id, 'comment root 1')
    }

    {
      const resComment = await addVideoCommentThread(servers[ 0 ].url, userToken1, videoUUID1, 'comment user 1')
      await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resComment.body.comment.id, 'comment root 1')
    }

    await waitJobs(servers)
  })

  describe('When managing account blocklist', function () {
    it('Should list all videos', function () {
      return checkAllVideos(servers[0].url, servers[0].accessToken)
    })

    it('Should list the comments', function () {
      return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
    })

    it('Should block a remote account', async function () {
      await addAccountToAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:9002')
    })

    it('Should hide its videos', async function () {
      const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)

      const videos: Video[] = res.body.data
      expect(videos).to.have.lengthOf(3)

      const v = videos.find(v => v.name === 'video user 2')
      expect(v).to.be.undefined
    })

    it('Should block a local account', async function () {
      await addAccountToAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1')
    })

    it('Should hide its videos', async function () {
      const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)

      const videos: Video[] = res.body.data
      expect(videos).to.have.lengthOf(2)

      const v = videos.find(v => v.name === 'video user 1')
      expect(v).to.be.undefined
    })

    it('Should hide its comments', async function () {
      const resThreads = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 5, '-createdAt', servers[0].accessToken)

      const threads: VideoComment[] = resThreads.body.data
      expect(threads).to.have.lengthOf(1)
      expect(threads[0].totalReplies).to.equal(0)

      const t = threads.find(t => t.text === 'comment user 1')
      expect(t).to.be.undefined

      for (const thread of threads) {
        const res = await getVideoThreadComments(servers[0].url, videoUUID1, thread.id, servers[0].accessToken)

        const tree: VideoCommentThreadTree = res.body
        expect(tree.children).to.have.lengthOf(0)
      }
    })

    it('Should list all the videos with another user', async function () {
      return checkAllVideos(servers[0].url, userToken1)
    })

    it('Should list all the comments with another user', async function () {
      return checkAllComments(servers[0].url, userToken1, videoUUID1)
    })

    it('Should list blocked accounts', async function () {
      {
        const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
        const blocks: AccountBlock[] = res.body.data

        expect(res.body.total).to.equal(2)

        const block = blocks[0]
        expect(block.byAccount.displayName).to.equal('root')
        expect(block.byAccount.name).to.equal('root')
        expect(block.blockedAccount.displayName).to.equal('user2')
        expect(block.blockedAccount.name).to.equal('user2')
        expect(block.blockedAccount.host).to.equal('localhost:9002')
      }

      {
        const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
        const blocks: AccountBlock[] = res.body.data

        expect(res.body.total).to.equal(2)

        const block = blocks[0]
        expect(block.byAccount.displayName).to.equal('root')
        expect(block.byAccount.name).to.equal('root')
        expect(block.blockedAccount.displayName).to.equal('user1')
        expect(block.blockedAccount.name).to.equal('user1')
        expect(block.blockedAccount.host).to.equal('localhost:9001')
      }
    })

    it('Should unblock the remote account', async function () {
      await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:9002')
    })

    it('Should display its videos', async function () {
      const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)

      const videos: Video[] = res.body.data
      expect(videos).to.have.lengthOf(3)

      const v = videos.find(v => v.name === 'video user 2')
      expect(v).not.to.be.undefined
    })

    it('Should unblock the local account', async function () {
      await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1')
    })

    it('Should display its comments', function () {
      return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
    })
  })

  describe('When managing server blocklist', function () {
    it('Should list all videos', function () {
      return checkAllVideos(servers[0].url, servers[0].accessToken)
    })

    it('Should list the comments', function () {
      return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
    })

    it('Should block a remote server', async function () {
      await addServerToAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:9002')
    })

    it('Should hide its videos', async function () {
      const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)

      const videos: Video[] = res.body.data
      expect(videos).to.have.lengthOf(2)

      const v1 = videos.find(v => v.name === 'video user 2')
      const v2 = videos.find(v => v.name === 'video server 2')

      expect(v1).to.be.undefined
      expect(v2).to.be.undefined
    })

    it('Should list all the videos with another user', async function () {
      return checkAllVideos(servers[0].url, userToken1)
    })

    it('Should hide its comments')

    it('Should list blocked servers', async function () {
      const res = await getServerBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
      const blocks: ServerBlock[] = res.body.data

      expect(res.body.total).to.equal(1)

      const block = blocks[0]
      expect(block.byAccount.displayName).to.equal('root')
      expect(block.byAccount.name).to.equal('root')
      expect(block.blockedServer.host).to.equal('localhost:9002')
    })

    it('Should unblock the remote server', async function () {
      await removeServerFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:9002')
    })

    it('Should display its videos', function () {
      return checkAllVideos(servers[0].url, servers[0].accessToken)
    })

    it('Should display its comments', function () {
      return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
    })
  })

  after(async function () {
    killallServers(servers)

    // Keep the logs if the test failed
    if (this[ 'ok' ]) {
      await flushTests()
    }
  })
})