]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/moderation/blocklist-notification.ts
We don't need to import mocha
[github/Chocobozzz/PeerTube.git] / server / tests / api / moderation / blocklist-notification.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import { UserNotificationType } from '@shared/models'
5 import {
6 cleanupTests,
7 createMultipleServers,
8 doubleFollow,
9 PeerTubeServer,
10 setAccessTokensToServers,
11 waitJobs
12 } from '@shared/server-commands'
13
14 const expect = chai.expect
15
16 async function checkNotifications (server: PeerTubeServer, token: string, expected: UserNotificationType[]) {
17 const { data } = await server.notifications.list({ token, start: 0, count: 10, unread: true })
18 expect(data).to.have.lengthOf(expected.length)
19
20 for (const type of expected) {
21 expect(data.find(n => n.type === type)).to.exist
22 }
23 }
24
25 describe('Test blocklist notifications', function () {
26 let servers: PeerTubeServer[]
27 let videoUUID: string
28
29 let userToken1: string
30 let userToken2: string
31 let remoteUserToken: string
32
33 async function resetState () {
34 try {
35 await servers[1].subscriptions.remove({ token: remoteUserToken, uri: 'user1_channel@' + servers[0].host })
36 await servers[1].subscriptions.remove({ token: remoteUserToken, uri: 'user2_channel@' + servers[0].host })
37 } catch {}
38
39 await waitJobs(servers)
40
41 await servers[0].notifications.markAsReadAll({ token: userToken1 })
42 await servers[0].notifications.markAsReadAll({ token: userToken2 })
43
44 {
45 const { uuid } = await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video' } })
46 videoUUID = uuid
47
48 await waitJobs(servers)
49 }
50
51 {
52 await servers[1].comments.createThread({
53 token: remoteUserToken,
54 videoId: videoUUID,
55 text: '@user2@' + servers[0].host + ' hello'
56 })
57 }
58
59 {
60
61 await servers[1].subscriptions.add({ token: remoteUserToken, targetUri: 'user1_channel@' + servers[0].host })
62 await servers[1].subscriptions.add({ token: remoteUserToken, targetUri: 'user2_channel@' + servers[0].host })
63 }
64
65 await waitJobs(servers)
66 }
67
68 before(async function () {
69 this.timeout(60000)
70
71 servers = await createMultipleServers(2)
72 await setAccessTokensToServers(servers)
73
74 {
75 const user = { username: 'user1', password: 'password' }
76 await servers[0].users.create({
77 username: user.username,
78 password: user.password,
79 videoQuota: -1,
80 videoQuotaDaily: -1
81 })
82
83 userToken1 = await servers[0].login.getAccessToken(user)
84 await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video user 1' } })
85 }
86
87 {
88 const user = { username: 'user2', password: 'password' }
89 await servers[0].users.create({ username: user.username, password: user.password })
90
91 userToken2 = await servers[0].login.getAccessToken(user)
92 }
93
94 {
95 const user = { username: 'user3', password: 'password' }
96 await servers[1].users.create({ username: user.username, password: user.password })
97
98 remoteUserToken = await servers[1].login.getAccessToken(user)
99 }
100
101 await doubleFollow(servers[0], servers[1])
102 })
103
104 describe('User blocks another user', function () {
105
106 before(async function () {
107 this.timeout(30000)
108
109 await resetState()
110 })
111
112 it('Should have appropriate notifications', async function () {
113 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
114 await checkNotifications(servers[0], userToken1, notifs)
115 })
116
117 it('Should block an account', async function () {
118 this.timeout(10000)
119
120 await servers[0].blocklist.addToMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
121 await waitJobs(servers)
122 })
123
124 it('Should not have notifications from this account', async function () {
125 await checkNotifications(servers[0], userToken1, [])
126 })
127
128 it('Should have notifications of this account on user 2', async function () {
129 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
130
131 await checkNotifications(servers[0], userToken2, notifs)
132
133 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
134 })
135 })
136
137 describe('User blocks another server', function () {
138
139 before(async function () {
140 this.timeout(30000)
141
142 await resetState()
143 })
144
145 it('Should have appropriate notifications', async function () {
146 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
147 await checkNotifications(servers[0], userToken1, notifs)
148 })
149
150 it('Should block an account', async function () {
151 this.timeout(10000)
152
153 await servers[0].blocklist.addToMyBlocklist({ token: userToken1, server: servers[1].host })
154 await waitJobs(servers)
155 })
156
157 it('Should not have notifications from this account', async function () {
158 await checkNotifications(servers[0], userToken1, [])
159 })
160
161 it('Should have notifications of this account on user 2', async function () {
162 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
163
164 await checkNotifications(servers[0], userToken2, notifs)
165
166 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken1, server: servers[1].host })
167 })
168 })
169
170 describe('Server blocks a user', function () {
171
172 before(async function () {
173 this.timeout(30000)
174
175 await resetState()
176 })
177
178 it('Should have appropriate notifications', async function () {
179 {
180 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
181 await checkNotifications(servers[0], userToken1, notifs)
182 }
183
184 {
185 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
186 await checkNotifications(servers[0], userToken2, notifs)
187 }
188 })
189
190 it('Should block an account', async function () {
191 this.timeout(10000)
192
193 await servers[0].blocklist.addToServerBlocklist({ account: 'user3@' + servers[1].host })
194 await waitJobs(servers)
195 })
196
197 it('Should not have notifications from this account', async function () {
198 await checkNotifications(servers[0], userToken1, [])
199 await checkNotifications(servers[0], userToken2, [])
200
201 await servers[0].blocklist.removeFromServerBlocklist({ account: 'user3@' + servers[1].host })
202 })
203 })
204
205 describe('Server blocks a server', function () {
206
207 before(async function () {
208 this.timeout(30000)
209
210 await resetState()
211 })
212
213 it('Should have appropriate notifications', async function () {
214 {
215 const notifs = [ UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, UserNotificationType.NEW_FOLLOW ]
216 await checkNotifications(servers[0], userToken1, notifs)
217 }
218
219 {
220 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
221 await checkNotifications(servers[0], userToken2, notifs)
222 }
223 })
224
225 it('Should block an account', async function () {
226 this.timeout(10000)
227
228 await servers[0].blocklist.addToServerBlocklist({ server: servers[1].host })
229 await waitJobs(servers)
230 })
231
232 it('Should not have notifications from this account', async function () {
233 await checkNotifications(servers[0], userToken1, [])
234 await checkNotifications(servers[0], userToken2, [])
235 })
236 })
237
238 after(async function () {
239 await cleanupTests(servers)
240 })
241 })