1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { cleanupTests, createSingleServer, MockSmtpServer, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
6 import { HttpStatusCode } from '@shared/models'
8 const expect = chai.expect
10 describe('Test emails', function () {
11 let server: PeerTubeServer
14 let userAccessToken: string
16 let videoShortUUID: string
19 let videoUserUUID: string
21 let verificationString: string
22 let verificationString2: string
24 const emails: object[] = []
27 password: 'super_password'
31 before(async function () {
34 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
36 const overrideConfig = {
38 hostname: 'localhost',
42 server = await createSingleServer(1, overrideConfig)
43 await setAccessTokensToServers([ server ])
46 const created = await server.users.create({ username: user.username, password: user.password })
49 userAccessToken = await server.login.getAccessToken(user)
53 const attributes = { name: 'my super user video' }
54 const { uuid } = await server.videos.upload({ token: userAccessToken, attributes })
62 const { shortUUID, id } = await server.videos.upload({ attributes })
63 videoShortUUID = shortUUID
68 describe('When resetting user password', function () {
70 it('Should ask to reset the password', async function () {
73 await server.users.askResetPassword({ email: 'user_1@example.com' })
75 await waitJobs(server)
76 expect(emails).to.have.lengthOf(1)
78 const email = emails[0]
80 expect(email['from'][0]['name']).equal('PeerTube')
81 expect(email['from'][0]['address']).equal('test-admin@localhost')
82 expect(email['to'][0]['address']).equal('user_1@example.com')
83 expect(email['subject']).contains('password')
85 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
86 expect(verificationStringMatches).not.to.be.null
88 verificationString = verificationStringMatches[1]
89 expect(verificationString).to.have.length.above(2)
91 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
92 expect(userIdMatches).not.to.be.null
94 userId = parseInt(userIdMatches[1], 10)
95 expect(verificationString).to.not.be.undefined
98 it('Should not reset the password with an invalid verification string', async function () {
99 await server.users.resetPassword({
101 verificationString: verificationString + 'b',
102 password: 'super_password2',
103 expectedStatus: HttpStatusCode.FORBIDDEN_403
107 it('Should reset the password', async function () {
108 await server.users.resetPassword({ userId, verificationString, password: 'super_password2' })
111 it('Should not reset the password with the same verification string', async function () {
112 await server.users.resetPassword({
115 password: 'super_password3',
116 expectedStatus: HttpStatusCode.FORBIDDEN_403
120 it('Should login with this new password', async function () {
121 user.password = 'super_password2'
123 await server.login.getAccessToken(user)
127 describe('When creating a user without password', function () {
129 it('Should send a create password email', async function () {
132 await server.users.create({ username: 'create_password', password: '' })
134 await waitJobs(server)
135 expect(emails).to.have.lengthOf(2)
137 const email = emails[1]
139 expect(email['from'][0]['name']).equal('PeerTube')
140 expect(email['from'][0]['address']).equal('test-admin@localhost')
141 expect(email['to'][0]['address']).equal('create_password@example.com')
142 expect(email['subject']).contains('account')
143 expect(email['subject']).contains('password')
145 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
146 expect(verificationStringMatches).not.to.be.null
148 verificationString2 = verificationStringMatches[1]
149 expect(verificationString2).to.have.length.above(2)
151 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
152 expect(userIdMatches).not.to.be.null
154 userId2 = parseInt(userIdMatches[1], 10)
157 it('Should not reset the password with an invalid verification string', async function () {
158 await server.users.resetPassword({
160 verificationString: verificationString2 + 'c',
161 password: 'newly_created_password',
162 expectedStatus: HttpStatusCode.FORBIDDEN_403
166 it('Should reset the password', async function () {
167 await server.users.resetPassword({
169 verificationString: verificationString2,
170 password: 'newly_created_password'
174 it('Should login with this new password', async function () {
175 await server.login.getAccessToken({
176 username: 'create_password',
177 password: 'newly_created_password'
182 describe('When creating an abuse', function () {
184 it('Should send the notification email', async function () {
187 const reason = 'my super bad reason'
188 await server.abuses.report({ videoId, reason })
190 await waitJobs(server)
191 expect(emails).to.have.lengthOf(3)
193 const email = emails[2]
195 expect(email['from'][0]['name']).equal('PeerTube')
196 expect(email['from'][0]['address']).equal('test-admin@localhost')
197 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
198 expect(email['subject']).contains('abuse')
199 expect(email['text']).contains(videoShortUUID)
203 describe('When blocking/unblocking user', function () {
205 it('Should send the notification email when blocking a user', async function () {
208 const reason = 'my super bad reason'
209 await server.users.banUser({ userId, reason })
211 await waitJobs(server)
212 expect(emails).to.have.lengthOf(4)
214 const email = emails[3]
216 expect(email['from'][0]['name']).equal('PeerTube')
217 expect(email['from'][0]['address']).equal('test-admin@localhost')
218 expect(email['to'][0]['address']).equal('user_1@example.com')
219 expect(email['subject']).contains(' blocked')
220 expect(email['text']).contains(' blocked')
221 expect(email['text']).contains('bad reason')
224 it('Should send the notification email when unblocking a user', async function () {
227 await server.users.unbanUser({ userId })
229 await waitJobs(server)
230 expect(emails).to.have.lengthOf(5)
232 const email = emails[4]
234 expect(email['from'][0]['name']).equal('PeerTube')
235 expect(email['from'][0]['address']).equal('test-admin@localhost')
236 expect(email['to'][0]['address']).equal('user_1@example.com')
237 expect(email['subject']).contains(' unblocked')
238 expect(email['text']).contains(' unblocked')
242 describe('When blacklisting a video', function () {
243 it('Should send the notification email', async function () {
246 const reason = 'my super reason'
247 await server.blacklist.add({ videoId: videoUserUUID, reason })
249 await waitJobs(server)
250 expect(emails).to.have.lengthOf(6)
252 const email = emails[5]
254 expect(email['from'][0]['name']).equal('PeerTube')
255 expect(email['from'][0]['address']).equal('test-admin@localhost')
256 expect(email['to'][0]['address']).equal('user_1@example.com')
257 expect(email['subject']).contains(' blacklisted')
258 expect(email['text']).contains('my super user video')
259 expect(email['text']).contains('my super reason')
262 it('Should send the notification email', async function () {
265 await server.blacklist.remove({ videoId: videoUserUUID })
267 await waitJobs(server)
268 expect(emails).to.have.lengthOf(7)
270 const email = emails[6]
272 expect(email['from'][0]['name']).equal('PeerTube')
273 expect(email['from'][0]['address']).equal('test-admin@localhost')
274 expect(email['to'][0]['address']).equal('user_1@example.com')
275 expect(email['subject']).contains(' unblacklisted')
276 expect(email['text']).contains('my super user video')
279 it('Should have the manage preferences link in the email', async function () {
280 const email = emails[6]
281 expect(email['text']).to.contain('Manage your notification preferences')
285 describe('When verifying a user email', function () {
287 it('Should ask to send the verification email', async function () {
290 await server.users.askSendVerifyEmail({ email: 'user_1@example.com' })
292 await waitJobs(server)
293 expect(emails).to.have.lengthOf(8)
295 const email = emails[7]
297 expect(email['from'][0]['name']).equal('PeerTube')
298 expect(email['from'][0]['address']).equal('test-admin@localhost')
299 expect(email['to'][0]['address']).equal('user_1@example.com')
300 expect(email['subject']).contains('Verify')
302 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
303 expect(verificationStringMatches).not.to.be.null
305 verificationString = verificationStringMatches[1]
306 expect(verificationString).to.not.be.undefined
307 expect(verificationString).to.have.length.above(2)
309 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
310 expect(userIdMatches).not.to.be.null
312 userId = parseInt(userIdMatches[1], 10)
315 it('Should not verify the email with an invalid verification string', async function () {
316 await server.users.verifyEmail({
318 verificationString: verificationString + 'b',
319 isPendingEmail: false,
320 expectedStatus: HttpStatusCode.FORBIDDEN_403
324 it('Should verify the email', async function () {
325 await server.users.verifyEmail({ userId, verificationString })
329 after(async function () {
330 MockSmtpServer.Instance.kill()
332 await cleanupTests([ server ])