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