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