]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/moderation/blocklist-notification.ts
Adapt CLI to new commands
[github/Chocobozzz/PeerTube.git] / server / tests / api / moderation / blocklist-notification.ts
CommitLineData
ea3674d0
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import * as chai from 'chai'
ea3674d0
C
5import {
6 cleanupTests,
7 createUser,
8 doubleFollow,
9 flushAndRunMultipleServers,
10 ServerInfo,
5f8bd4cb 11 setAccessTokensToServers,
ea3674d0 12 uploadVideo,
5f8bd4cb
C
13 waitJobs
14} from '@shared/extra-utils'
dd0ebb71 15import { UserNotificationType } from '@shared/models'
ea3674d0
C
16
17const expect = chai.expect
18
dd0ebb71
C
19async 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)
ea3674d0
C
22
23 for (const type of expected) {
dd0ebb71 24 expect(data.find(n => n.type === type)).to.exist
ea3674d0
C
25 }
26}
27
28describe('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 {
2c27e704
C
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 })
ea3674d0
C
40 } catch {}
41
42 await waitJobs(servers)
43
dd0ebb71
C
44 await servers[0].notificationsCommand.markAsReadAll({ token: userToken1 })
45 await servers[0].notificationsCommand.markAsReadAll({ token: userToken2 })
ea3674d0
C
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 {
12edc149
C
55 await servers[1].commentsCommand.createThread({
56 token: remoteUserToken,
57 videoId: videoUUID,
58 text: '@user2@' + servers[0].host + ' hello'
59 })
ea3674d0
C
60 }
61
62 {
63
2c27e704
C
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 })
ea3674d0
C
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
41d1d075 88 userToken1 = await servers[0].loginCommand.getAccessToken(user)
ea3674d0
C
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
41d1d075 96 userToken2 = await servers[0].loginCommand.getAccessToken(user)
ea3674d0
C
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
41d1d075 103 remoteUserToken = await servers[1].loginCommand.getAccessToken(user)
ea3674d0
C
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 ]
dd0ebb71 119 await checkNotifications(servers[0], userToken1, notifs)
ea3674d0
C
120 })
121
122 it('Should block an account', async function () {
123 this.timeout(10000)
124
5f8bd4cb 125 await servers[0].blocklistCommand.addToMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
ea3674d0
C
126 await waitJobs(servers)
127 })
128
129 it('Should not have notifications from this account', async function () {
dd0ebb71 130 await checkNotifications(servers[0], userToken1, [])
ea3674d0
C
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
dd0ebb71 136 await checkNotifications(servers[0], userToken2, notifs)
ea3674d0 137
5f8bd4cb 138 await servers[0].blocklistCommand.removeFromMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
ea3674d0
C
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 ]
dd0ebb71 152 await checkNotifications(servers[0], userToken1, notifs)
ea3674d0
C
153 })
154
155 it('Should block an account', async function () {
156 this.timeout(10000)
157
5f8bd4cb 158 await servers[0].blocklistCommand.addToMyBlocklist({ token: userToken1, server: servers[1].host })
ea3674d0
C
159 await waitJobs(servers)
160 })
161
162 it('Should not have notifications from this account', async function () {
dd0ebb71 163 await checkNotifications(servers[0], userToken1, [])
ea3674d0
C
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
dd0ebb71 169 await checkNotifications(servers[0], userToken2, notifs)
ea3674d0 170
5f8bd4cb 171 await servers[0].blocklistCommand.removeFromMyBlocklist({ token: userToken1, server: servers[1].host })
ea3674d0
C
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 ]
dd0ebb71 186 await checkNotifications(servers[0], userToken1, notifs)
ea3674d0
C
187 }
188
189 {
190 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
dd0ebb71 191 await checkNotifications(servers[0], userToken2, notifs)
ea3674d0
C
192 }
193 })
194
195 it('Should block an account', async function () {
196 this.timeout(10000)
197
5f8bd4cb 198 await servers[0].blocklistCommand.addToServerBlocklist({ account: 'user3@' + servers[1].host })
ea3674d0
C
199 await waitJobs(servers)
200 })
201
202 it('Should not have notifications from this account', async function () {
dd0ebb71
C
203 await checkNotifications(servers[0], userToken1, [])
204 await checkNotifications(servers[0], userToken2, [])
ea3674d0 205
5f8bd4cb 206 await servers[0].blocklistCommand.removeFromServerBlocklist({ account: 'user3@' + servers[1].host })
ea3674d0
C
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 ]
dd0ebb71 221 await checkNotifications(servers[0], userToken1, notifs)
ea3674d0
C
222 }
223
224 {
225 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
dd0ebb71 226 await checkNotifications(servers[0], userToken2, notifs)
ea3674d0
C
227 }
228 })
229
230 it('Should block an account', async function () {
231 this.timeout(10000)
232
5f8bd4cb 233 await servers[0].blocklistCommand.addToServerBlocklist({ server: servers[1].host })
ea3674d0
C
234 await waitJobs(servers)
235 })
236
237 it('Should not have notifications from this account', async function () {
dd0ebb71
C
238 await checkNotifications(servers[0], userToken1, [])
239 await checkNotifications(servers[0], userToken2, [])
ea3674d0
C
240 })
241 })
242
243 after(async function () {
244 await cleanupTests(servers)
245 })
246})