1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { MockSmtpServer } from '@server/tests/shared'
5 import { HttpStatusCode } from '@shared/models'
11 setAccessTokensToServers,
13 } from '@shared/server-commands'
15 describe('Test emails', function () {
16 let server: PeerTubeServer
19 let userAccessToken: string
21 let videoShortUUID: string
24 let videoUserUUID: string
26 let verificationString: string
27 let verificationString2: string
29 const emails: object[] = []
32 password: 'super_password'
35 before(async function () {
38 const emailPort = await MockSmtpServer.Instance.collectEmails(emails)
39 server = await createSingleServer(1, ConfigCommand.getEmailOverrideConfig(emailPort))
41 await setAccessTokensToServers([ server ])
42 await server.config.enableSignup(true)
45 const created = await server.users.create({ username: user.username, password: user.password })
48 userAccessToken = await server.login.getAccessToken(user)
52 const attributes = { name: 'my super user video' }
53 const { uuid } = await server.videos.upload({ token: userAccessToken, attributes })
61 const { shortUUID, id } = await server.videos.upload({ attributes })
62 videoShortUUID = shortUUID
67 describe('When resetting user password', function () {
69 it('Should ask to reset the password', async function () {
70 await server.users.askResetPassword({ email: 'user_1@example.com' })
72 await waitJobs(server)
73 expect(emails).to.have.lengthOf(1)
75 const email = emails[0]
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')
82 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
83 expect(verificationStringMatches).not.to.be.null
85 verificationString = verificationStringMatches[1]
86 expect(verificationString).to.have.length.above(2)
88 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
89 expect(userIdMatches).not.to.be.null
91 userId = parseInt(userIdMatches[1], 10)
92 expect(verificationString).to.not.be.undefined
95 it('Should not reset the password with an invalid verification string', async function () {
96 await server.users.resetPassword({
98 verificationString: verificationString + 'b',
99 password: 'super_password2',
100 expectedStatus: HttpStatusCode.FORBIDDEN_403
104 it('Should reset the password', async function () {
105 await server.users.resetPassword({ userId, verificationString, password: 'super_password2' })
108 it('Should not reset the password with the same verification string', async function () {
109 await server.users.resetPassword({
112 password: 'super_password3',
113 expectedStatus: HttpStatusCode.FORBIDDEN_403
117 it('Should login with this new password', async function () {
118 user.password = 'super_password2'
120 await server.login.getAccessToken(user)
124 describe('When creating a user without password', function () {
126 it('Should send a create password email', async function () {
129 await server.users.create({ username: 'create_password', password: '' })
131 await waitJobs(server)
132 expect(emails).to.have.lengthOf(2)
134 const email = emails[1]
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')
142 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
143 expect(verificationStringMatches).not.to.be.null
145 verificationString2 = verificationStringMatches[1]
146 expect(verificationString2).to.have.length.above(2)
148 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
149 expect(userIdMatches).not.to.be.null
151 userId2 = parseInt(userIdMatches[1], 10)
154 it('Should not reset the password with an invalid verification string', async function () {
155 await server.users.resetPassword({
157 verificationString: verificationString2 + 'c',
158 password: 'newly_created_password',
159 expectedStatus: HttpStatusCode.FORBIDDEN_403
163 it('Should reset the password', async function () {
164 await server.users.resetPassword({
166 verificationString: verificationString2,
167 password: 'newly_created_password'
171 it('Should login with this new password', async function () {
172 await server.login.getAccessToken({
173 username: 'create_password',
174 password: 'newly_created_password'
179 describe('When creating an abuse', function () {
181 it('Should send the notification email', async function () {
184 const reason = 'my super bad reason'
185 await server.abuses.report({ token: userAccessToken, videoId, reason })
187 await waitJobs(server)
188 expect(emails).to.have.lengthOf(3)
190 const email = emails[2]
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)
200 describe('When blocking/unblocking user', function () {
202 it('Should send the notification email when blocking a user', async function () {
205 const reason = 'my super bad reason'
206 await server.users.banUser({ userId, reason })
208 await waitJobs(server)
209 expect(emails).to.have.lengthOf(4)
211 const email = emails[3]
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')
221 it('Should send the notification email when unblocking a user', async function () {
224 await server.users.unbanUser({ userId })
226 await waitJobs(server)
227 expect(emails).to.have.lengthOf(5)
229 const email = emails[4]
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')
239 describe('When blacklisting a video', function () {
240 it('Should send the notification email', async function () {
243 const reason = 'my super reason'
244 await server.blacklist.add({ videoId: videoUserUUID, reason })
246 await waitJobs(server)
247 expect(emails).to.have.lengthOf(6)
249 const email = emails[5]
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')
259 it('Should send the notification email', async function () {
262 await server.blacklist.remove({ videoId: videoUserUUID })
264 await waitJobs(server)
265 expect(emails).to.have.lengthOf(7)
267 const email = emails[6]
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')
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')
282 describe('When verifying a user email', function () {
284 it('Should ask to send the verification email', async function () {
287 await server.users.askSendVerifyEmail({ email: 'user_1@example.com' })
289 await waitJobs(server)
290 expect(emails).to.have.lengthOf(8)
292 const email = emails[7]
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')
299 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
300 expect(verificationStringMatches).not.to.be.null
302 verificationString = verificationStringMatches[1]
303 expect(verificationString).to.not.be.undefined
304 expect(verificationString).to.have.length.above(2)
306 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
307 expect(userIdMatches).not.to.be.null
309 userId = parseInt(userIdMatches[1], 10)
312 it('Should not verify the email with an invalid verification string', async function () {
313 await server.users.verifyEmail({
315 verificationString: verificationString + 'b',
316 isPendingEmail: false,
317 expectedStatus: HttpStatusCode.FORBIDDEN_403
321 it('Should verify the email', async function () {
322 await server.users.verifyEmail({ userId, verificationString })
326 describe('When verifying a registration email', function () {
327 let registrationId: number
328 let registrationIdEmail: number
330 before(async function () {
331 const { id } = await server.registrations.requestRegistration({
332 username: 'request_1',
333 email: 'request_1@example.com',
334 registrationReason: 'tt'
339 it('Should ask to send the verification email', async function () {
342 await server.registrations.askSendVerifyEmail({ email: 'request_1@example.com' })
344 await waitJobs(server)
345 expect(emails).to.have.lengthOf(9)
347 const email = emails[8]
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')
354 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
355 expect(verificationStringMatches).not.to.be.null
357 verificationString = verificationStringMatches[1]
358 expect(verificationString).to.not.be.undefined
359 expect(verificationString).to.have.length.above(2)
361 const registrationIdMatches = /registrationId=([0-9]+)/.exec(email['text'])
362 expect(registrationIdMatches).not.to.be.null
364 registrationIdEmail = parseInt(registrationIdMatches[1], 10)
366 expect(registrationId).to.equal(registrationIdEmail)
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
377 it('Should verify the email', async function () {
378 await server.registrations.verifyEmail({ registrationId: registrationIdEmail, verificationString })
382 after(async function () {
383 MockSmtpServer.Instance.kill()
385 await cleanupTests([ server ])