1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { cleanupTests, createMultipleServers, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
6 import { UserNotificationType } from '@shared/models'
8 const expect = chai.expect
10 async function checkNotifications (server: PeerTubeServer, token: string, expected: UserNotificationType[]) {
11 const { data } = await server.notifications.list({ token, start: 0, count: 10, unread: true })
12 expect(data).to.have.lengthOf(expected.length)
14 for (const type of expected) {
15 expect(data.find(n => n.type === type)).to.exist
19 describe('Test blocklist', function () {
20 let servers: PeerTubeServer[]
23 let userToken1: string
24 let userToken2: string
25 let remoteUserToken: string
27 async function resetState () {
29 await servers[1].subscriptions.remove({ token: remoteUserToken, uri: 'user1_channel@' + servers[0].host })
30 await servers[1].subscriptions.remove({ token: remoteUserToken, uri: 'user2_channel@' + servers[0].host })
33 await waitJobs(servers)
35 await servers[0].notifications.markAsReadAll({ token: userToken1 })
36 await servers[0].notifications.markAsReadAll({ token: userToken2 })
39 const { uuid } = await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video' } })
42 await waitJobs(servers)
46 await servers[1].comments.createThread({
47 token: remoteUserToken,
49 text: '@user2@' + servers[0].host + ' hello'
55 await servers[1].subscriptions.add({ token: remoteUserToken, targetUri: 'user1_channel@' + servers[0].host })
56 await servers[1].subscriptions.add({ token: remoteUserToken, targetUri: 'user2_channel@' + servers[0].host })
59 await waitJobs(servers)
62 before(async function () {
65 servers = await createMultipleServers(2)
66 await setAccessTokensToServers(servers)
69 const user = { username: 'user1', password: 'password' }
70 await servers[0].users.create({
71 username: user.username,
72 password: user.password,
77 userToken1 = await servers[0].login.getAccessToken(user)
78 await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video user 1' } })
82 const user = { username: 'user2', password: 'password' }
83 await servers[0].users.create({ username: user.username, password: user.password })
85 userToken2 = await servers[0].login.getAccessToken(user)
89 const user = { username: 'user3', password: 'password' }
90 await servers[1].users.create({ username: user.username, password: user.password })
92 remoteUserToken = await servers[1].login.getAccessToken(user)
95 await doubleFollow(servers[0], servers[1])
98 describe('User blocks another user', function () {
100 before(async function () {
106 it('Should have appropriate notifications', async function () {
107 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
108 await checkNotifications(servers[0], userToken1, notifs)
111 it('Should block an account', async function () {
114 await servers[0].blocklist.addToMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
115 await waitJobs(servers)
118 it('Should not have notifications from this account', async function () {
119 await checkNotifications(servers[0], userToken1, [])
122 it('Should have notifications of this account on user 2', async function () {
123 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
125 await checkNotifications(servers[0], userToken2, notifs)
127 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
131 describe('User blocks another server', function () {
133 before(async function () {
139 it('Should have appropriate notifications', async function () {
140 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
141 await checkNotifications(servers[0], userToken1, notifs)
144 it('Should block an account', async function () {
147 await servers[0].blocklist.addToMyBlocklist({ token: userToken1, server: servers[1].host })
148 await waitJobs(servers)
151 it('Should not have notifications from this account', async function () {
152 await checkNotifications(servers[0], userToken1, [])
155 it('Should have notifications of this account on user 2', async function () {
156 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
158 await checkNotifications(servers[0], userToken2, notifs)
160 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken1, server: servers[1].host })
164 describe('Server blocks a user', function () {
166 before(async function () {
172 it('Should have appropriate notifications', async function () {
174 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
175 await checkNotifications(servers[0], userToken1, notifs)
179 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
180 await checkNotifications(servers[0], userToken2, notifs)
184 it('Should block an account', async function () {
187 await servers[0].blocklist.addToServerBlocklist({ account: 'user3@' + servers[1].host })
188 await waitJobs(servers)
191 it('Should not have notifications from this account', async function () {
192 await checkNotifications(servers[0], userToken1, [])
193 await checkNotifications(servers[0], userToken2, [])
195 await servers[0].blocklist.removeFromServerBlocklist({ account: 'user3@' + servers[1].host })
199 describe('Server blocks a server', function () {
201 before(async function () {
207 it('Should have appropriate notifications', async function () {
209 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
210 await checkNotifications(servers[0], userToken1, notifs)
214 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
215 await checkNotifications(servers[0], userToken2, notifs)
219 it('Should block an account', async function () {
222 await servers[0].blocklist.addToServerBlocklist({ server: servers[1].host })
223 await waitJobs(servers)
226 it('Should not have notifications from this account', async function () {
227 await checkNotifications(servers[0], userToken1, [])
228 await checkNotifications(servers[0], userToken2, [])
232 after(async function () {
233 await cleanupTests(servers)