1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { UserNotificationType } from '@shared/models'
11 setAccessTokensToServers,
13 } from '@shared/server-commands'
15 const expect = chai.expect
17 async function checkNotifications (server: PeerTubeServer, token: string, expected: UserNotificationType[]) {
18 const { data } = await server.notifications.list({ token, start: 0, count: 10, unread: true })
19 expect(data).to.have.lengthOf(expected.length)
21 for (const type of expected) {
22 expect(data.find(n => n.type === type)).to.exist
26 describe('Test blocklist', function () {
27 let servers: PeerTubeServer[]
30 let userToken1: string
31 let userToken2: string
32 let remoteUserToken: string
34 async function resetState () {
36 await servers[1].subscriptions.remove({ token: remoteUserToken, uri: 'user1_channel@' + servers[0].host })
37 await servers[1].subscriptions.remove({ token: remoteUserToken, uri: 'user2_channel@' + servers[0].host })
40 await waitJobs(servers)
42 await servers[0].notifications.markAsReadAll({ token: userToken1 })
43 await servers[0].notifications.markAsReadAll({ token: userToken2 })
46 const { uuid } = await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video' } })
49 await waitJobs(servers)
53 await servers[1].comments.createThread({
54 token: remoteUserToken,
56 text: '@user2@' + servers[0].host + ' hello'
62 await servers[1].subscriptions.add({ token: remoteUserToken, targetUri: 'user1_channel@' + servers[0].host })
63 await servers[1].subscriptions.add({ token: remoteUserToken, targetUri: 'user2_channel@' + servers[0].host })
66 await waitJobs(servers)
69 before(async function () {
72 servers = await createMultipleServers(2)
73 await setAccessTokensToServers(servers)
76 const user = { username: 'user1', password: 'password' }
77 await servers[0].users.create({
78 username: user.username,
79 password: user.password,
84 userToken1 = await servers[0].login.getAccessToken(user)
85 await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video user 1' } })
89 const user = { username: 'user2', password: 'password' }
90 await servers[0].users.create({ username: user.username, password: user.password })
92 userToken2 = await servers[0].login.getAccessToken(user)
96 const user = { username: 'user3', password: 'password' }
97 await servers[1].users.create({ username: user.username, password: user.password })
99 remoteUserToken = await servers[1].login.getAccessToken(user)
102 await doubleFollow(servers[0], servers[1])
105 describe('User blocks another user', function () {
107 before(async function () {
113 it('Should have appropriate notifications', async function () {
114 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
115 await checkNotifications(servers[0], userToken1, notifs)
118 it('Should block an account', async function () {
121 await servers[0].blocklist.addToMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
122 await waitJobs(servers)
125 it('Should not have notifications from this account', async function () {
126 await checkNotifications(servers[0], userToken1, [])
129 it('Should have notifications of this account on user 2', async function () {
130 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
132 await checkNotifications(servers[0], userToken2, notifs)
134 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
138 describe('User blocks another server', function () {
140 before(async function () {
146 it('Should have appropriate notifications', async function () {
147 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
148 await checkNotifications(servers[0], userToken1, notifs)
151 it('Should block an account', async function () {
154 await servers[0].blocklist.addToMyBlocklist({ token: userToken1, server: servers[1].host })
155 await waitJobs(servers)
158 it('Should not have notifications from this account', async function () {
159 await checkNotifications(servers[0], userToken1, [])
162 it('Should have notifications of this account on user 2', async function () {
163 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
165 await checkNotifications(servers[0], userToken2, notifs)
167 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken1, server: servers[1].host })
171 describe('Server blocks a user', function () {
173 before(async function () {
179 it('Should have appropriate notifications', async function () {
181 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
182 await checkNotifications(servers[0], userToken1, notifs)
186 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
187 await checkNotifications(servers[0], userToken2, notifs)
191 it('Should block an account', async function () {
194 await servers[0].blocklist.addToServerBlocklist({ account: 'user3@' + servers[1].host })
195 await waitJobs(servers)
198 it('Should not have notifications from this account', async function () {
199 await checkNotifications(servers[0], userToken1, [])
200 await checkNotifications(servers[0], userToken2, [])
202 await servers[0].blocklist.removeFromServerBlocklist({ account: 'user3@' + servers[1].host })
206 describe('Server blocks a server', function () {
208 before(async function () {
214 it('Should have appropriate notifications', async function () {
216 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
217 await checkNotifications(servers[0], userToken1, notifs)
221 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
222 await checkNotifications(servers[0], userToken2, notifs)
226 it('Should block an account', async function () {
229 await servers[0].blocklist.addToServerBlocklist({ server: servers[1].host })
230 await waitJobs(servers)
233 it('Should not have notifications from this account', async function () {
234 await checkNotifications(servers[0], userToken1, [])
235 await checkNotifications(servers[0], userToken2, [])
239 after(async function () {
240 await cleanupTests(servers)