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