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