]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/email.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / email.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { expect } from 'chai'
4 import { MockSmtpServer } from '@server/tests/shared'
5 import { HttpStatusCode } from '@shared/models'
6 import {
7 cleanupTests,
8 ConfigCommand,
9 createSingleServer,
10 PeerTubeServer,
11 setAccessTokensToServers,
12 waitJobs
13 } from '@shared/server-commands'
14
15 describe('Test emails', function () {
16 let server: PeerTubeServer
17 let userId: number
18 let userId2: number
19 let userAccessToken: string
20
21 let videoShortUUID: string
22 let videoId: number
23
24 let videoUserUUID: string
25
26 let verificationString: string
27 let verificationString2: string
28
29 const emails: object[] = []
30 const user = {
31 username: 'user_1',
32 password: 'super_password'
33 }
34
35 before(async function () {
36 this.timeout(50000)
37
38 const emailPort = await MockSmtpServer.Instance.collectEmails(emails)
39 server = await createSingleServer(1, ConfigCommand.getEmailOverrideConfig(emailPort))
40
41 await setAccessTokensToServers([ server ])
42 await server.config.enableSignup(true)
43
44 {
45 const created = await server.users.create({ username: user.username, password: user.password })
46 userId = created.id
47
48 userAccessToken = await server.login.getAccessToken(user)
49 }
50
51 {
52 const attributes = { name: 'my super user video' }
53 const { uuid } = await server.videos.upload({ token: userAccessToken, attributes })
54 videoUserUUID = uuid
55 }
56
57 {
58 const attributes = {
59 name: 'my super name'
60 }
61 const { shortUUID, id } = await server.videos.upload({ attributes })
62 videoShortUUID = shortUUID
63 videoId = id
64 }
65 })
66
67 describe('When resetting user password', function () {
68
69 it('Should ask to reset the password', async function () {
70 await server.users.askResetPassword({ email: 'user_1@example.com' })
71
72 await waitJobs(server)
73 expect(emails).to.have.lengthOf(1)
74
75 const email = emails[0]
76
77 expect(email['from'][0]['name']).equal('PeerTube')
78 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
79 expect(email['to'][0]['address']).equal('user_1@example.com')
80 expect(email['subject']).contains('password')
81
82 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
83 expect(verificationStringMatches).not.to.be.null
84
85 verificationString = verificationStringMatches[1]
86 expect(verificationString).to.have.length.above(2)
87
88 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
89 expect(userIdMatches).not.to.be.null
90
91 userId = parseInt(userIdMatches[1], 10)
92 expect(verificationString).to.not.be.undefined
93 })
94
95 it('Should not reset the password with an invalid verification string', async function () {
96 await server.users.resetPassword({
97 userId,
98 verificationString: verificationString + 'b',
99 password: 'super_password2',
100 expectedStatus: HttpStatusCode.FORBIDDEN_403
101 })
102 })
103
104 it('Should reset the password', async function () {
105 await server.users.resetPassword({ userId, verificationString, password: 'super_password2' })
106 })
107
108 it('Should not reset the password with the same verification string', async function () {
109 await server.users.resetPassword({
110 userId,
111 verificationString,
112 password: 'super_password3',
113 expectedStatus: HttpStatusCode.FORBIDDEN_403
114 })
115 })
116
117 it('Should login with this new password', async function () {
118 user.password = 'super_password2'
119
120 await server.login.getAccessToken(user)
121 })
122 })
123
124 describe('When creating a user without password', function () {
125
126 it('Should send a create password email', async function () {
127 this.timeout(10000)
128
129 await server.users.create({ username: 'create_password', password: '' })
130
131 await waitJobs(server)
132 expect(emails).to.have.lengthOf(2)
133
134 const email = emails[1]
135
136 expect(email['from'][0]['name']).equal('PeerTube')
137 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
138 expect(email['to'][0]['address']).equal('create_password@example.com')
139 expect(email['subject']).contains('account')
140 expect(email['subject']).contains('password')
141
142 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
143 expect(verificationStringMatches).not.to.be.null
144
145 verificationString2 = verificationStringMatches[1]
146 expect(verificationString2).to.have.length.above(2)
147
148 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
149 expect(userIdMatches).not.to.be.null
150
151 userId2 = parseInt(userIdMatches[1], 10)
152 })
153
154 it('Should not reset the password with an invalid verification string', async function () {
155 await server.users.resetPassword({
156 userId: userId2,
157 verificationString: verificationString2 + 'c',
158 password: 'newly_created_password',
159 expectedStatus: HttpStatusCode.FORBIDDEN_403
160 })
161 })
162
163 it('Should reset the password', async function () {
164 await server.users.resetPassword({
165 userId: userId2,
166 verificationString: verificationString2,
167 password: 'newly_created_password'
168 })
169 })
170
171 it('Should login with this new password', async function () {
172 await server.login.getAccessToken({
173 username: 'create_password',
174 password: 'newly_created_password'
175 })
176 })
177 })
178
179 describe('When creating an abuse', function () {
180
181 it('Should send the notification email', async function () {
182 this.timeout(10000)
183
184 const reason = 'my super bad reason'
185 await server.abuses.report({ token: userAccessToken, videoId, reason })
186
187 await waitJobs(server)
188 expect(emails).to.have.lengthOf(3)
189
190 const email = emails[2]
191
192 expect(email['from'][0]['name']).equal('PeerTube')
193 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
194 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
195 expect(email['subject']).contains('abuse')
196 expect(email['text']).contains(videoShortUUID)
197 })
198 })
199
200 describe('When blocking/unblocking user', function () {
201
202 it('Should send the notification email when blocking a user', async function () {
203 this.timeout(10000)
204
205 const reason = 'my super bad reason'
206 await server.users.banUser({ userId, reason })
207
208 await waitJobs(server)
209 expect(emails).to.have.lengthOf(4)
210
211 const email = emails[3]
212
213 expect(email['from'][0]['name']).equal('PeerTube')
214 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
215 expect(email['to'][0]['address']).equal('user_1@example.com')
216 expect(email['subject']).contains(' blocked')
217 expect(email['text']).contains(' blocked')
218 expect(email['text']).contains('bad reason')
219 })
220
221 it('Should send the notification email when unblocking a user', async function () {
222 this.timeout(10000)
223
224 await server.users.unbanUser({ userId })
225
226 await waitJobs(server)
227 expect(emails).to.have.lengthOf(5)
228
229 const email = emails[4]
230
231 expect(email['from'][0]['name']).equal('PeerTube')
232 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
233 expect(email['to'][0]['address']).equal('user_1@example.com')
234 expect(email['subject']).contains(' unblocked')
235 expect(email['text']).contains(' unblocked')
236 })
237 })
238
239 describe('When blacklisting a video', function () {
240 it('Should send the notification email', async function () {
241 this.timeout(10000)
242
243 const reason = 'my super reason'
244 await server.blacklist.add({ videoId: videoUserUUID, reason })
245
246 await waitJobs(server)
247 expect(emails).to.have.lengthOf(6)
248
249 const email = emails[5]
250
251 expect(email['from'][0]['name']).equal('PeerTube')
252 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
253 expect(email['to'][0]['address']).equal('user_1@example.com')
254 expect(email['subject']).contains(' blacklisted')
255 expect(email['text']).contains('my super user video')
256 expect(email['text']).contains('my super reason')
257 })
258
259 it('Should send the notification email', async function () {
260 this.timeout(10000)
261
262 await server.blacklist.remove({ videoId: videoUserUUID })
263
264 await waitJobs(server)
265 expect(emails).to.have.lengthOf(7)
266
267 const email = emails[6]
268
269 expect(email['from'][0]['name']).equal('PeerTube')
270 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
271 expect(email['to'][0]['address']).equal('user_1@example.com')
272 expect(email['subject']).contains(' unblacklisted')
273 expect(email['text']).contains('my super user video')
274 })
275
276 it('Should have the manage preferences link in the email', async function () {
277 const email = emails[6]
278 expect(email['text']).to.contain('Manage your notification preferences')
279 })
280 })
281
282 describe('When verifying a user email', function () {
283
284 it('Should ask to send the verification email', async function () {
285 this.timeout(10000)
286
287 await server.users.askSendVerifyEmail({ email: 'user_1@example.com' })
288
289 await waitJobs(server)
290 expect(emails).to.have.lengthOf(8)
291
292 const email = emails[7]
293
294 expect(email['from'][0]['name']).equal('PeerTube')
295 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
296 expect(email['to'][0]['address']).equal('user_1@example.com')
297 expect(email['subject']).contains('Verify')
298
299 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
300 expect(verificationStringMatches).not.to.be.null
301
302 verificationString = verificationStringMatches[1]
303 expect(verificationString).to.not.be.undefined
304 expect(verificationString).to.have.length.above(2)
305
306 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
307 expect(userIdMatches).not.to.be.null
308
309 userId = parseInt(userIdMatches[1], 10)
310 })
311
312 it('Should not verify the email with an invalid verification string', async function () {
313 await server.users.verifyEmail({
314 userId,
315 verificationString: verificationString + 'b',
316 isPendingEmail: false,
317 expectedStatus: HttpStatusCode.FORBIDDEN_403
318 })
319 })
320
321 it('Should verify the email', async function () {
322 await server.users.verifyEmail({ userId, verificationString })
323 })
324 })
325
326 describe('When verifying a registration email', function () {
327 let registrationId: number
328 let registrationIdEmail: number
329
330 before(async function () {
331 const { id } = await server.registrations.requestRegistration({
332 username: 'request_1',
333 email: 'request_1@example.com',
334 registrationReason: 'tt'
335 })
336 registrationId = id
337 })
338
339 it('Should ask to send the verification email', async function () {
340 this.timeout(10000)
341
342 await server.registrations.askSendVerifyEmail({ email: 'request_1@example.com' })
343
344 await waitJobs(server)
345 expect(emails).to.have.lengthOf(9)
346
347 const email = emails[8]
348
349 expect(email['from'][0]['name']).equal('PeerTube')
350 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
351 expect(email['to'][0]['address']).equal('request_1@example.com')
352 expect(email['subject']).contains('Verify')
353
354 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
355 expect(verificationStringMatches).not.to.be.null
356
357 verificationString = verificationStringMatches[1]
358 expect(verificationString).to.not.be.undefined
359 expect(verificationString).to.have.length.above(2)
360
361 const registrationIdMatches = /registrationId=([0-9]+)/.exec(email['text'])
362 expect(registrationIdMatches).not.to.be.null
363
364 registrationIdEmail = parseInt(registrationIdMatches[1], 10)
365
366 expect(registrationId).to.equal(registrationIdEmail)
367 })
368
369 it('Should not verify the email with an invalid verification string', async function () {
370 await server.registrations.verifyEmail({
371 registrationId: registrationIdEmail,
372 verificationString: verificationString + 'b',
373 expectedStatus: HttpStatusCode.FORBIDDEN_403
374 })
375 })
376
377 it('Should verify the email', async function () {
378 await server.registrations.verifyEmail({ registrationId: registrationIdEmail, verificationString })
379 })
380 })
381
382 after(async function () {
383 MockSmtpServer.Instance.kill()
384
385 await cleanupTests([ server ])
386 })
387 })