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