]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/moderation/blocklist-notification.ts
Add mute status in account and channel pages
[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 { cleanupTests, createMultipleServers, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
6 import { UserNotificationType } from '@shared/models'
7
8 const expect = chai.expect
9
10 async function checkNotifications (server: PeerTubeServer, token: string, expected: UserNotificationType[]) {
11 const { data } = await server.notifications.list({ token, start: 0, count: 10, unread: true })
12 expect(data).to.have.lengthOf(expected.length)
13
14 for (const type of expected) {
15 expect(data.find(n => n.type === type)).to.exist
16 }
17 }
18
19 describe('Test blocklist', function () {
20 let servers: PeerTubeServer[]
21 let videoUUID: string
22
23 let userToken1: string
24 let userToken2: string
25 let remoteUserToken: string
26
27 async function resetState () {
28 try {
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 })
31 } catch {}
32
33 await waitJobs(servers)
34
35 await servers[0].notifications.markAsReadAll({ token: userToken1 })
36 await servers[0].notifications.markAsReadAll({ token: userToken2 })
37
38 {
39 const { uuid } = await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video' } })
40 videoUUID = uuid
41
42 await waitJobs(servers)
43 }
44
45 {
46 await servers[1].comments.createThread({
47 token: remoteUserToken,
48 videoId: videoUUID,
49 text: '@user2@' + servers[0].host + ' hello'
50 })
51 }
52
53 {
54
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 })
57 }
58
59 await waitJobs(servers)
60 }
61
62 before(async function () {
63 this.timeout(60000)
64
65 servers = await createMultipleServers(2)
66 await setAccessTokensToServers(servers)
67
68 {
69 const user = { username: 'user1', password: 'password' }
70 await servers[0].users.create({
71 username: user.username,
72 password: user.password,
73 videoQuota: -1,
74 videoQuotaDaily: -1
75 })
76
77 userToken1 = await servers[0].login.getAccessToken(user)
78 await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video user 1' } })
79 }
80
81 {
82 const user = { username: 'user2', password: 'password' }
83 await servers[0].users.create({ username: user.username, password: user.password })
84
85 userToken2 = await servers[0].login.getAccessToken(user)
86 }
87
88 {
89 const user = { username: 'user3', password: 'password' }
90 await servers[1].users.create({ username: user.username, password: user.password })
91
92 remoteUserToken = await servers[1].login.getAccessToken(user)
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 ]
108 await checkNotifications(servers[0], userToken1, notifs)
109 })
110
111 it('Should block an account', async function () {
112 this.timeout(10000)
113
114 await servers[0].blocklist.addToMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
115 await waitJobs(servers)
116 })
117
118 it('Should not have notifications from this account', async function () {
119 await checkNotifications(servers[0], userToken1, [])
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
125 await checkNotifications(servers[0], userToken2, notifs)
126
127 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken1, account: 'user3@' + servers[1].host })
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 ]
141 await checkNotifications(servers[0], userToken1, notifs)
142 })
143
144 it('Should block an account', async function () {
145 this.timeout(10000)
146
147 await servers[0].blocklist.addToMyBlocklist({ token: userToken1, server: servers[1].host })
148 await waitJobs(servers)
149 })
150
151 it('Should not have notifications from this account', async function () {
152 await checkNotifications(servers[0], userToken1, [])
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
158 await checkNotifications(servers[0], userToken2, notifs)
159
160 await servers[0].blocklist.removeFromMyBlocklist({ token: userToken1, server: servers[1].host })
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 ]
175 await checkNotifications(servers[0], userToken1, notifs)
176 }
177
178 {
179 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
180 await checkNotifications(servers[0], userToken2, notifs)
181 }
182 })
183
184 it('Should block an account', async function () {
185 this.timeout(10000)
186
187 await servers[0].blocklist.addToServerBlocklist({ account: 'user3@' + servers[1].host })
188 await waitJobs(servers)
189 })
190
191 it('Should not have notifications from this account', async function () {
192 await checkNotifications(servers[0], userToken1, [])
193 await checkNotifications(servers[0], userToken2, [])
194
195 await servers[0].blocklist.removeFromServerBlocklist({ account: 'user3@' + servers[1].host })
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 ]
210 await checkNotifications(servers[0], userToken1, notifs)
211 }
212
213 {
214 const notifs = [ UserNotificationType.COMMENT_MENTION, UserNotificationType.NEW_FOLLOW ]
215 await checkNotifications(servers[0], userToken2, notifs)
216 }
217 })
218
219 it('Should block an account', async function () {
220 this.timeout(10000)
221
222 await servers[0].blocklist.addToServerBlocklist({ server: servers[1].host })
223 await waitJobs(servers)
224 })
225
226 it('Should not have notifications from this account', async function () {
227 await checkNotifications(servers[0], userToken1, [])
228 await checkNotifications(servers[0], userToken2, [])
229 })
230 })
231
232 after(async function () {
233 await cleanupTests(servers)
234 })
235 })