]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/email.ts
Introduce blacklist command
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / email.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
f076daa7 2
f076daa7 3import 'mocha'
4f32032f 4import * as chai from 'chai'
8ef9457f 5import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
eacb25c4
C
6import {
7 askResetPassword,
d9eaee39 8 askSendVerifyEmail,
eacb25c4 9 blockUser,
7243f84d
C
10 cleanupTests,
11 createUser,
12 flushAndRunServer,
eacb25c4 13 resetPassword,
7243f84d
C
14 ServerInfo,
15 setAccessTokensToServers,
eacb25c4
C
16 unblockUser,
17 uploadVideo,
d9eaee39 18 userLogin,
7243f84d 19 verifyEmail
94565d52 20} from '../../../../shared/extra-utils'
8ef9457f 21import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
94565d52 22import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
f076daa7
C
23
24const expect = chai.expect
25
26describe('Test emails', function () {
27 let server: ServerInfo
28 let userId: number
45f1bd72 29 let userId2: number
26b7305a 30 let userAccessToken: string
4f32032f 31
ba75d268 32 let videoUUID: string
4f32032f
C
33 let videoId: number
34
26b7305a 35 let videoUserUUID: string
4f32032f 36
f076daa7 37 let verificationString: string
45f1bd72 38 let verificationString2: string
4f32032f 39
f076daa7
C
40 const emails: object[] = []
41 const user = {
42 username: 'user_1',
43 password: 'super_password'
44 }
48f07b4a 45 let emailPort: number
f076daa7
C
46
47 before(async function () {
2284f202 48 this.timeout(50000)
f076daa7 49
48f07b4a 50 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
f076daa7 51
f076daa7
C
52 const overrideConfig = {
53 smtp: {
48f07b4a
C
54 hostname: 'localhost',
55 port: emailPort
f076daa7
C
56 }
57 }
210feb6c 58 server = await flushAndRunServer(1, overrideConfig)
f076daa7
C
59 await setAccessTokensToServers([ server ])
60
ba75d268 61 {
1eddc9a7 62 const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
ba75d268 63 userId = res.body.user.id
26b7305a
C
64
65 userAccessToken = await userLogin(server, user)
66 }
67
68 {
69 const attributes = {
70 name: 'my super user video'
71 }
72 const res = await uploadVideo(server.url, userAccessToken, attributes)
73 videoUserUUID = res.body.video.uuid
ba75d268
C
74 }
75
76 {
77 const attributes = {
78 name: 'my super name'
79 }
80 const res = await uploadVideo(server.url, server.accessToken, attributes)
81 videoUUID = res.body.video.uuid
4f32032f 82 videoId = res.body.video.id
ba75d268 83 }
f076daa7
C
84 })
85
86 describe('When resetting user password', function () {
87
88 it('Should ask to reset the password', async function () {
89 this.timeout(10000)
90
91 await askResetPassword(server.url, 'user_1@example.com')
92
3cd0734f 93 await waitJobs(server)
f076daa7
C
94 expect(emails).to.have.lengthOf(1)
95
96 const email = emails[0]
97
113d4a3f 98 expect(email['from'][0]['name']).equal('PeerTube')
f076daa7
C
99 expect(email['from'][0]['address']).equal('test-admin@localhost')
100 expect(email['to'][0]['address']).equal('user_1@example.com')
101 expect(email['subject']).contains('password')
102
103 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
104 expect(verificationStringMatches).not.to.be.null
105
106 verificationString = verificationStringMatches[1]
107 expect(verificationString).to.have.length.above(2)
108
109 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
110 expect(userIdMatches).not.to.be.null
111
112 userId = parseInt(userIdMatches[1], 10)
113 expect(verificationString).to.not.be.undefined
114 })
115
116 it('Should not reset the password with an invalid verification string', async function () {
f2eb23cd 117 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', HttpStatusCode.FORBIDDEN_403)
f076daa7
C
118 })
119
120 it('Should reset the password', async function () {
121 await resetPassword(server.url, userId, verificationString, 'super_password2')
122 })
123
e9c5f123 124 it('Should not reset the password with the same verification string', async function () {
f2eb23cd 125 await resetPassword(server.url, userId, verificationString, 'super_password3', HttpStatusCode.FORBIDDEN_403)
e9c5f123
C
126 })
127
f076daa7
C
128 it('Should login with this new password', async function () {
129 user.password = 'super_password2'
130
131 await userLogin(server, user)
132 })
133 })
134
45f1bd72
JL
135 describe('When creating a user without password', function () {
136 it('Should send a create password email', async function () {
137 this.timeout(10000)
138
139 await createUser({
140 url: server.url,
141 accessToken: server.accessToken,
142 username: 'create_password',
143 password: ''
144 })
145
146 await waitJobs(server)
147 expect(emails).to.have.lengthOf(2)
148
149 const email = emails[1]
150
113d4a3f 151 expect(email['from'][0]['name']).equal('PeerTube')
45f1bd72
JL
152 expect(email['from'][0]['address']).equal('test-admin@localhost')
153 expect(email['to'][0]['address']).equal('create_password@example.com')
154 expect(email['subject']).contains('account')
155 expect(email['subject']).contains('password')
156
157 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
158 expect(verificationStringMatches).not.to.be.null
159
160 verificationString2 = verificationStringMatches[1]
161 expect(verificationString2).to.have.length.above(2)
162
163 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
164 expect(userIdMatches).not.to.be.null
165
166 userId2 = parseInt(userIdMatches[1], 10)
167 })
168
169 it('Should not reset the password with an invalid verification string', async function () {
f2eb23cd 170 await resetPassword(server.url, userId2, verificationString2 + 'c', 'newly_created_password', HttpStatusCode.FORBIDDEN_403)
45f1bd72
JL
171 })
172
173 it('Should reset the password', async function () {
174 await resetPassword(server.url, userId2, verificationString2, 'newly_created_password')
175 })
176
177 it('Should login with this new password', async function () {
178 await userLogin(server, {
179 username: 'create_password',
180 password: 'newly_created_password'
181 })
182 })
183 })
184
310b5219 185 describe('When creating an abuse', function () {
ba75d268
C
186 it('Should send the notification email', async function () {
187 this.timeout(10000)
188
189 const reason = 'my super bad reason'
0c1a77e9 190 await server.abusesCommand.report({ videoId, reason })
ba75d268 191
3cd0734f 192 await waitJobs(server)
45f1bd72 193 expect(emails).to.have.lengthOf(3)
ba75d268 194
45f1bd72 195 const email = emails[2]
ba75d268 196
113d4a3f 197 expect(email['from'][0]['name']).equal('PeerTube')
ba75d268 198 expect(email['from'][0]['address']).equal('test-admin@localhost')
48f07b4a 199 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
ba75d268
C
200 expect(email['subject']).contains('abuse')
201 expect(email['text']).contains(videoUUID)
202 })
203 })
204
9b39106d
C
205 describe('When blocking/unblocking user', function () {
206
eacb25c4
C
207 it('Should send the notification email when blocking a user', async function () {
208 this.timeout(10000)
209
210 const reason = 'my super bad reason'
f2eb23cd 211 await blockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204, reason)
eacb25c4
C
212
213 await waitJobs(server)
45f1bd72 214 expect(emails).to.have.lengthOf(4)
eacb25c4 215
45f1bd72 216 const email = emails[3]
eacb25c4 217
113d4a3f 218 expect(email['from'][0]['name']).equal('PeerTube')
eacb25c4
C
219 expect(email['from'][0]['address']).equal('test-admin@localhost')
220 expect(email['to'][0]['address']).equal('user_1@example.com')
221 expect(email['subject']).contains(' blocked')
222 expect(email['text']).contains(' blocked')
113d4a3f 223 expect(email['text']).contains('bad reason')
eacb25c4
C
224 })
225
226 it('Should send the notification email when unblocking a user', async function () {
227 this.timeout(10000)
228
f2eb23cd 229 await unblockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204)
eacb25c4
C
230
231 await waitJobs(server)
45f1bd72 232 expect(emails).to.have.lengthOf(5)
eacb25c4 233
45f1bd72 234 const email = emails[4]
eacb25c4 235
113d4a3f 236 expect(email['from'][0]['name']).equal('PeerTube')
eacb25c4
C
237 expect(email['from'][0]['address']).equal('test-admin@localhost')
238 expect(email['to'][0]['address']).equal('user_1@example.com')
239 expect(email['subject']).contains(' unblocked')
240 expect(email['text']).contains(' unblocked')
241 })
242 })
243
26b7305a
C
244 describe('When blacklisting a video', function () {
245 it('Should send the notification email', async function () {
246 this.timeout(10000)
247
248 const reason = 'my super reason'
e3d15a6a 249 await server.blacklistCommand.add({ videoId: videoUserUUID, reason })
26b7305a
C
250
251 await waitJobs(server)
45f1bd72 252 expect(emails).to.have.lengthOf(6)
26b7305a 253
45f1bd72 254 const email = emails[5]
26b7305a 255
113d4a3f 256 expect(email['from'][0]['name']).equal('PeerTube')
26b7305a
C
257 expect(email['from'][0]['address']).equal('test-admin@localhost')
258 expect(email['to'][0]['address']).equal('user_1@example.com')
259 expect(email['subject']).contains(' blacklisted')
260 expect(email['text']).contains('my super user video')
261 expect(email['text']).contains('my super reason')
262 })
263
264 it('Should send the notification email', async function () {
265 this.timeout(10000)
266
e3d15a6a 267 await server.blacklistCommand.remove({ videoId: videoUserUUID })
26b7305a
C
268
269 await waitJobs(server)
45f1bd72 270 expect(emails).to.have.lengthOf(7)
26b7305a 271
45f1bd72 272 const email = emails[6]
26b7305a 273
113d4a3f 274 expect(email['from'][0]['name']).equal('PeerTube')
26b7305a
C
275 expect(email['from'][0]['address']).equal('test-admin@localhost')
276 expect(email['to'][0]['address']).equal('user_1@example.com')
277 expect(email['subject']).contains(' unblacklisted')
278 expect(email['text']).contains('my super user video')
279 })
b9cf3fb6
C
280
281 it('Should have the manage preferences link in the email', async function () {
282 const email = emails[6]
283 expect(email['text']).to.contain('Manage your notification preferences')
284 })
26b7305a
C
285 })
286
d9eaee39
JM
287 describe('When verifying a user email', function () {
288
289 it('Should ask to send the verification email', async function () {
290 this.timeout(10000)
291
292 await askSendVerifyEmail(server.url, 'user_1@example.com')
293
294 await waitJobs(server)
45f1bd72 295 expect(emails).to.have.lengthOf(8)
d9eaee39 296
45f1bd72 297 const email = emails[7]
d9eaee39 298
113d4a3f 299 expect(email['from'][0]['name']).equal('PeerTube')
d9eaee39
JM
300 expect(email['from'][0]['address']).equal('test-admin@localhost')
301 expect(email['to'][0]['address']).equal('user_1@example.com')
302 expect(email['subject']).contains('Verify')
303
304 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
305 expect(verificationStringMatches).not.to.be.null
306
307 verificationString = verificationStringMatches[1]
308 expect(verificationString).to.not.be.undefined
309 expect(verificationString).to.have.length.above(2)
310
311 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
312 expect(userIdMatches).not.to.be.null
313
314 userId = parseInt(userIdMatches[1], 10)
315 })
316
317 it('Should not verify the email with an invalid verification string', async function () {
f2eb23cd 318 await verifyEmail(server.url, userId, verificationString + 'b', false, HttpStatusCode.FORBIDDEN_403)
d9eaee39
JM
319 })
320
321 it('Should verify the email', async function () {
322 await verifyEmail(server.url, userId, verificationString)
323 })
324 })
325
7c3b7976 326 after(async function () {
89ada4e2 327 MockSmtpServer.Instance.kill()
7c3b7976
C
328
329 await cleanupTests([ server ])
f076daa7
C
330 })
331})