1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { getUserNotifications, markAsReadAllNotifications } from '@shared/extra-utils/users/user-notifications'
6 import { addUserSubscription, removeUserSubscription } from '@shared/extra-utils/users/user-subscriptions'
7 import { UserNotification, UserNotificationType } from '@shared/models'
12 flushAndRunMultipleServers,
16 } from '../../../../shared/extra-utils/index'
17 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
19 addAccountToAccountBlocklist,
20 addAccountToServerBlocklist,
21 addServerToAccountBlocklist,
22 addServerToServerBlocklist,
23 removeAccountFromAccountBlocklist,
24 removeAccountFromServerBlocklist,
25 removeServerFromAccountBlocklist
26 } from '../../../../shared/extra-utils/users/blocklist'
27 import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
28 import { addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
30 const expect = chai.expect
32 async function checkNotifications (url: string, token: string, expected: UserNotificationType[]) {
33 const res = await getUserNotifications(url, token, 0, 10, true)
35 const notifications: UserNotification[] = res.body.data
37 expect(notifications).to.have.lengthOf(expected.length)
39 for (const type of expected) {
40 expect(notifications.find(n => n.type === type)).to.exist
44 describe('Test blocklist', function () {
45 let servers: ServerInfo[]
48 let userToken1: string
49 let userToken2: string
50 let remoteUserToken: string
52 async function resetState () {
54 await removeUserSubscription(servers[1].url, remoteUserToken, 'user1_channel@' + servers[0].host)
55 await removeUserSubscription(servers[1].url, remoteUserToken, 'user2_channel@' + servers[0].host)
58 await waitJobs(servers)
60 await markAsReadAllNotifications(servers[0].url, userToken1)
61 await markAsReadAllNotifications(servers[0].url, userToken2)
64 const res = await uploadVideo(servers[0].url, userToken1, { name: 'video' })
65 videoUUID = res.body.video.uuid
67 await waitJobs(servers)
71 await addVideoCommentThread(servers[1].url, remoteUserToken, videoUUID, '@user2@' + servers[0].host + ' hello')
76 await addUserSubscription(servers[1].url, remoteUserToken, 'user1_channel@' + servers[0].host)
77 await addUserSubscription(servers[1].url, remoteUserToken, 'user2_channel@' + servers[0].host)
80 await waitJobs(servers)
83 before(async function () {
86 servers = await flushAndRunMultipleServers(2)
87 await setAccessTokensToServers(servers)
90 const user = { username: 'user1', password: 'password' }
93 accessToken: servers[0].accessToken,
94 username: user.username,
95 password: user.password,
100 userToken1 = await userLogin(servers[0], user)
101 await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
105 const user = { username: 'user2', password: 'password' }
106 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
108 userToken2 = await userLogin(servers[0], user)
112 const user = { username: 'user3', password: 'password' }
113 await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password })
115 remoteUserToken = await userLogin(servers[1], user)
118 await doubleFollow(servers[0], servers[1])
121 describe('User blocks another user', function () {
123 before(async function () {
129 it('Should have appropriate notifications', async function () {
130 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
131 await checkNotifications(servers[0].url, userToken1, notifs)
134 it('Should block an account', async function () {
137 await addAccountToAccountBlocklist(servers[0].url, userToken1, 'user3@' + servers[1].host)
138 await waitJobs(servers)
141 it('Should not have notifications from this account', async function () {
142 await checkNotifications(servers[0].url, userToken1, [])
145 it('Should have notifications of this account on user 2', async function () {
146 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
148 await checkNotifications(servers[0].url, userToken2, notifs)
150 await removeAccountFromAccountBlocklist(servers[0].url, userToken1, 'user3@' + servers[1].host)
154 describe('User blocks another server', function () {
156 before(async function () {
162 it('Should have appropriate notifications', async function () {
163 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
164 await checkNotifications(servers[0].url, userToken1, notifs)
167 it('Should block an account', async function () {
170 await addServerToAccountBlocklist(servers[0].url, userToken1, servers[1].host)
171 await waitJobs(servers)
174 it('Should not have notifications from this account', async function () {
175 await checkNotifications(servers[0].url, userToken1, [])
178 it('Should have notifications of this account on user 2', async function () {
179 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
181 await checkNotifications(servers[0].url, userToken2, notifs)
183 await removeServerFromAccountBlocklist(servers[0].url, userToken1, servers[1].host)
187 describe('Server blocks a user', function () {
189 before(async function () {
195 it('Should have appropriate notifications', async function () {
197 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
198 await checkNotifications(servers[0].url, userToken1, notifs)
202 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
203 await checkNotifications(servers[0].url, userToken2, notifs)
207 it('Should block an account', async function () {
210 await addAccountToServerBlocklist(servers[0].url, servers[0].accessToken, 'user3@' + servers[1].host)
211 await waitJobs(servers)
214 it('Should not have notifications from this account', async function () {
215 await checkNotifications(servers[0].url, userToken1, [])
216 await checkNotifications(servers[0].url, userToken2, [])
218 await removeAccountFromServerBlocklist(servers[0].url, servers[0].accessToken, 'user3@' + servers[1].host)
222 describe('Server blocks a server', function () {
224 before(async function () {
230 it('Should have appropriate notifications', async function () {
232 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
233 await checkNotifications(servers[0].url, userToken1, notifs)
237 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
238 await checkNotifications(servers[0].url, userToken2, notifs)
242 it('Should block an account', async function () {
245 await addServerToServerBlocklist(servers[0].url, servers[0].accessToken, servers[1].host)
246 await waitJobs(servers)
249 it('Should not have notifications from this account', async function () {
250 await checkNotifications(servers[0].url, userToken1, [])
251 await checkNotifications(servers[0].url, userToken2, [])
255 after(async function () {
256 await cleanupTests(servers)