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 () {
127 await server.users.create({ username: 'create_password', password: '' })
129 await waitJobs(server)
130 expect(emails).to.have.lengthOf(2)
132 const email = emails[1]
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')
140 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
141 expect(verificationStringMatches).not.to.be.null
143 verificationString2 = verificationStringMatches[1]
144 expect(verificationString2).to.have.length.above(2)
146 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
147 expect(userIdMatches).not.to.be.null
149 userId2 = parseInt(userIdMatches[1], 10)
152 it('Should not reset the password with an invalid verification string', async function () {
153 await server.users.resetPassword({
155 verificationString: verificationString2 + 'c',
156 password: 'newly_created_password',
157 expectedStatus: HttpStatusCode.FORBIDDEN_403
161 it('Should reset the password', async function () {
162 await server.users.resetPassword({
164 verificationString: verificationString2,
165 password: 'newly_created_password'
169 it('Should login with this new password', async function () {
170 await server.login.getAccessToken({
171 username: 'create_password',
172 password: 'newly_created_password'
177 describe('When creating an abuse', function () {
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 })
183 await waitJobs(server)
184 expect(emails).to.have.lengthOf(3)
186 const email = emails[2]
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)
196 describe('When blocking/unblocking user', function () {
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 })
202 await waitJobs(server)
203 expect(emails).to.have.lengthOf(4)
205 const email = emails[3]
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')
215 it('Should send the notification email when unblocking a user', async function () {
216 await server.users.unbanUser({ userId })
218 await waitJobs(server)
219 expect(emails).to.have.lengthOf(5)
221 const email = emails[4]
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')
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 })
236 await waitJobs(server)
237 expect(emails).to.have.lengthOf(6)
239 const email = emails[5]
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')
249 it('Should send the notification email', async function () {
250 await server.blacklist.remove({ videoId: videoUserUUID })
252 await waitJobs(server)
253 expect(emails).to.have.lengthOf(7)
255 const email = emails[6]
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')
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')
270 describe('When verifying a user email', function () {
272 it('Should ask to send the verification email', async function () {
273 await server.users.askSendVerifyEmail({ email: 'user_1@example.com' })
275 await waitJobs(server)
276 expect(emails).to.have.lengthOf(8)
278 const email = emails[7]
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')
285 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
286 expect(verificationStringMatches).not.to.be.null
288 verificationString = verificationStringMatches[1]
289 expect(verificationString).to.not.be.undefined
290 expect(verificationString).to.have.length.above(2)
292 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
293 expect(userIdMatches).not.to.be.null
295 userId = parseInt(userIdMatches[1], 10)
298 it('Should not verify the email with an invalid verification string', async function () {
299 await server.users.verifyEmail({
301 verificationString: verificationString + 'b',
302 isPendingEmail: false,
303 expectedStatus: HttpStatusCode.FORBIDDEN_403
307 it('Should verify the email', async function () {
308 await server.users.verifyEmail({ userId, verificationString })
312 describe('When verifying a registration email', function () {
313 let registrationId: number
314 let registrationIdEmail: number
316 before(async function () {
317 const { id } = await server.registrations.requestRegistration({
318 username: 'request_1',
319 email: 'request_1@example.com',
320 registrationReason: 'tt'
325 it('Should ask to send the verification email', async function () {
326 await server.registrations.askSendVerifyEmail({ email: 'request_1@example.com' })
328 await waitJobs(server)
329 expect(emails).to.have.lengthOf(9)
331 const email = emails[8]
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')
338 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
339 expect(verificationStringMatches).not.to.be.null
341 verificationString = verificationStringMatches[1]
342 expect(verificationString).to.not.be.undefined
343 expect(verificationString).to.have.length.above(2)
345 const registrationIdMatches = /registrationId=([0-9]+)/.exec(email['text'])
346 expect(registrationIdMatches).not.to.be.null
348 registrationIdEmail = parseInt(registrationIdMatches[1], 10)
350 expect(registrationId).to.equal(registrationIdEmail)
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
361 it('Should verify the email', async function () {
362 await server.registrations.verifyEmail({ registrationId: registrationIdEmail, verificationString })
366 after(async function () {
367 MockSmtpServer.Instance.kill()
369 await cleanupTests([ server ])