]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/email.ts
Fix tests
[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(120000)
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 await server.users.create({ username: 'create_password', password: '' })
128
129 await waitJobs(server)
130 expect(emails).to.have.lengthOf(2)
131
132 const email = emails[1]
133
134 expect(email['from'][0]['name']).equal('PeerTube')
135 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
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 () {
153 await server.users.resetPassword({
154 userId: userId2,
155 verificationString: verificationString2 + 'c',
156 password: 'newly_created_password',
157 expectedStatus: HttpStatusCode.FORBIDDEN_403
158 })
159 })
160
161 it('Should reset the password', async function () {
162 await server.users.resetPassword({
163 userId: userId2,
164 verificationString: verificationString2,
165 password: 'newly_created_password'
166 })
167 })
168
169 it('Should login with this new password', async function () {
170 await server.login.getAccessToken({
171 username: 'create_password',
172 password: 'newly_created_password'
173 })
174 })
175 })
176
177 describe('When creating an abuse', function () {
178
179 it('Should send the notification email', async function () {
180 const reason = 'my super bad reason'
181 await server.abuses.report({ token: userAccessToken, videoId, reason })
182
183 await waitJobs(server)
184 expect(emails).to.have.lengthOf(3)
185
186 const email = emails[2]
187
188 expect(email['from'][0]['name']).equal('PeerTube')
189 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
190 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
191 expect(email['subject']).contains('abuse')
192 expect(email['text']).contains(videoShortUUID)
193 })
194 })
195
196 describe('When blocking/unblocking user', function () {
197
198 it('Should send the notification email when blocking a user', async function () {
199 const reason = 'my super bad reason'
200 await server.users.banUser({ userId, reason })
201
202 await waitJobs(server)
203 expect(emails).to.have.lengthOf(4)
204
205 const email = emails[3]
206
207 expect(email['from'][0]['name']).equal('PeerTube')
208 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
209 expect(email['to'][0]['address']).equal('user_1@example.com')
210 expect(email['subject']).contains(' blocked')
211 expect(email['text']).contains(' blocked')
212 expect(email['text']).contains('bad reason')
213 })
214
215 it('Should send the notification email when unblocking a user', async function () {
216 await server.users.unbanUser({ userId })
217
218 await waitJobs(server)
219 expect(emails).to.have.lengthOf(5)
220
221 const email = emails[4]
222
223 expect(email['from'][0]['name']).equal('PeerTube')
224 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
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
231 describe('When blacklisting a video', function () {
232 it('Should send the notification email', async function () {
233 const reason = 'my super reason'
234 await server.blacklist.add({ videoId: videoUserUUID, reason })
235
236 await waitJobs(server)
237 expect(emails).to.have.lengthOf(6)
238
239 const email = emails[5]
240
241 expect(email['from'][0]['name']).equal('PeerTube')
242 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
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 () {
250 await server.blacklist.remove({ videoId: videoUserUUID })
251
252 await waitJobs(server)
253 expect(emails).to.have.lengthOf(7)
254
255 const email = emails[6]
256
257 expect(email['from'][0]['name']).equal('PeerTube')
258 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
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 })
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 })
268 })
269
270 describe('When verifying a user email', function () {
271
272 it('Should ask to send the verification email', async function () {
273 await server.users.askSendVerifyEmail({ email: 'user_1@example.com' })
274
275 await waitJobs(server)
276 expect(emails).to.have.lengthOf(8)
277
278 const email = emails[7]
279
280 expect(email['from'][0]['name']).equal('PeerTube')
281 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
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 () {
299 await server.users.verifyEmail({
300 userId,
301 verificationString: verificationString + 'b',
302 isPendingEmail: false,
303 expectedStatus: HttpStatusCode.FORBIDDEN_403
304 })
305 })
306
307 it('Should verify the email', async function () {
308 await server.users.verifyEmail({ userId, verificationString })
309 })
310 })
311
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 () {
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
366 after(async function () {
367 MockSmtpServer.Instance.kill()
368
369 await cleanupTests([ server ])
370 })
371 })