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