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