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