1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
11 setAccessTokensToServers,
14 } from '@shared/extra-utils'
16 const expect = chai.expect
18 describe('Test emails', function () {
19 let server: ServerInfo
22 let userAccessToken: string
27 let videoUserUUID: string
29 let verificationString: string
30 let verificationString2: string
32 const emails: object[] = []
35 password: 'super_password'
39 before(async function () {
42 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
44 const overrideConfig = {
46 hostname: 'localhost',
50 server = await flushAndRunServer(1, overrideConfig)
51 await setAccessTokensToServers([ server ])
54 const created = await server.usersCommand.create({ username: user.username, password: user.password })
57 userAccessToken = await server.loginCommand.getAccessToken(user)
62 name: 'my super user video'
64 const res = await uploadVideo(server.url, userAccessToken, attributes)
65 videoUserUUID = res.body.video.uuid
72 const res = await uploadVideo(server.url, server.accessToken, attributes)
73 videoUUID = res.body.video.uuid
74 videoId = res.body.video.id
78 describe('When resetting user password', function () {
80 it('Should ask to reset the password', async function () {
83 await server.usersCommand.askResetPassword({ email: 'user_1@example.com' })
85 await waitJobs(server)
86 expect(emails).to.have.lengthOf(1)
88 const email = emails[0]
90 expect(email['from'][0]['name']).equal('PeerTube')
91 expect(email['from'][0]['address']).equal('test-admin@localhost')
92 expect(email['to'][0]['address']).equal('user_1@example.com')
93 expect(email['subject']).contains('password')
95 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
96 expect(verificationStringMatches).not.to.be.null
98 verificationString = verificationStringMatches[1]
99 expect(verificationString).to.have.length.above(2)
101 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
102 expect(userIdMatches).not.to.be.null
104 userId = parseInt(userIdMatches[1], 10)
105 expect(verificationString).to.not.be.undefined
108 it('Should not reset the password with an invalid verification string', async function () {
109 await server.usersCommand.resetPassword({
111 verificationString: verificationString + 'b',
112 password: 'super_password2',
113 expectedStatus: HttpStatusCode.FORBIDDEN_403
117 it('Should reset the password', async function () {
118 await server.usersCommand.resetPassword({ userId, verificationString, password: 'super_password2' })
121 it('Should not reset the password with the same verification string', async function () {
122 await server.usersCommand.resetPassword({
125 password: 'super_password3',
126 expectedStatus: HttpStatusCode.FORBIDDEN_403
130 it('Should login with this new password', async function () {
131 user.password = 'super_password2'
133 await server.loginCommand.getAccessToken(user)
137 describe('When creating a user without password', function () {
139 it('Should send a create password email', async function () {
142 await server.usersCommand.create({ username: 'create_password', password: '' })
144 await waitJobs(server)
145 expect(emails).to.have.lengthOf(2)
147 const email = emails[1]
149 expect(email['from'][0]['name']).equal('PeerTube')
150 expect(email['from'][0]['address']).equal('test-admin@localhost')
151 expect(email['to'][0]['address']).equal('create_password@example.com')
152 expect(email['subject']).contains('account')
153 expect(email['subject']).contains('password')
155 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
156 expect(verificationStringMatches).not.to.be.null
158 verificationString2 = verificationStringMatches[1]
159 expect(verificationString2).to.have.length.above(2)
161 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
162 expect(userIdMatches).not.to.be.null
164 userId2 = parseInt(userIdMatches[1], 10)
167 it('Should not reset the password with an invalid verification string', async function () {
168 await server.usersCommand.resetPassword({
170 verificationString: verificationString2 + 'c',
171 password: 'newly_created_password',
172 expectedStatus: HttpStatusCode.FORBIDDEN_403
176 it('Should reset the password', async function () {
177 await server.usersCommand.resetPassword({
179 verificationString: verificationString2,
180 password: 'newly_created_password'
184 it('Should login with this new password', async function () {
185 await server.loginCommand.getAccessToken({
186 username: 'create_password',
187 password: 'newly_created_password'
192 describe('When creating an abuse', function () {
193 it('Should send the notification email', async function () {
196 const reason = 'my super bad reason'
197 await server.abusesCommand.report({ videoId, reason })
199 await waitJobs(server)
200 expect(emails).to.have.lengthOf(3)
202 const email = emails[2]
204 expect(email['from'][0]['name']).equal('PeerTube')
205 expect(email['from'][0]['address']).equal('test-admin@localhost')
206 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
207 expect(email['subject']).contains('abuse')
208 expect(email['text']).contains(videoUUID)
212 describe('When blocking/unblocking user', function () {
214 it('Should send the notification email when blocking a user', async function () {
217 const reason = 'my super bad reason'
218 await server.usersCommand.banUser({ userId, reason })
220 await waitJobs(server)
221 expect(emails).to.have.lengthOf(4)
223 const email = emails[3]
225 expect(email['from'][0]['name']).equal('PeerTube')
226 expect(email['from'][0]['address']).equal('test-admin@localhost')
227 expect(email['to'][0]['address']).equal('user_1@example.com')
228 expect(email['subject']).contains(' blocked')
229 expect(email['text']).contains(' blocked')
230 expect(email['text']).contains('bad reason')
233 it('Should send the notification email when unblocking a user', async function () {
236 await server.usersCommand.unbanUser({ userId })
238 await waitJobs(server)
239 expect(emails).to.have.lengthOf(5)
241 const email = emails[4]
243 expect(email['from'][0]['name']).equal('PeerTube')
244 expect(email['from'][0]['address']).equal('test-admin@localhost')
245 expect(email['to'][0]['address']).equal('user_1@example.com')
246 expect(email['subject']).contains(' unblocked')
247 expect(email['text']).contains(' unblocked')
251 describe('When blacklisting a video', function () {
252 it('Should send the notification email', async function () {
255 const reason = 'my super reason'
256 await server.blacklistCommand.add({ videoId: videoUserUUID, reason })
258 await waitJobs(server)
259 expect(emails).to.have.lengthOf(6)
261 const email = emails[5]
263 expect(email['from'][0]['name']).equal('PeerTube')
264 expect(email['from'][0]['address']).equal('test-admin@localhost')
265 expect(email['to'][0]['address']).equal('user_1@example.com')
266 expect(email['subject']).contains(' blacklisted')
267 expect(email['text']).contains('my super user video')
268 expect(email['text']).contains('my super reason')
271 it('Should send the notification email', async function () {
274 await server.blacklistCommand.remove({ videoId: videoUserUUID })
276 await waitJobs(server)
277 expect(emails).to.have.lengthOf(7)
279 const email = emails[6]
281 expect(email['from'][0]['name']).equal('PeerTube')
282 expect(email['from'][0]['address']).equal('test-admin@localhost')
283 expect(email['to'][0]['address']).equal('user_1@example.com')
284 expect(email['subject']).contains(' unblacklisted')
285 expect(email['text']).contains('my super user video')
288 it('Should have the manage preferences link in the email', async function () {
289 const email = emails[6]
290 expect(email['text']).to.contain('Manage your notification preferences')
294 describe('When verifying a user email', function () {
296 it('Should ask to send the verification email', async function () {
299 await server.usersCommand.askSendVerifyEmail({ email: 'user_1@example.com' })
301 await waitJobs(server)
302 expect(emails).to.have.lengthOf(8)
304 const email = emails[7]
306 expect(email['from'][0]['name']).equal('PeerTube')
307 expect(email['from'][0]['address']).equal('test-admin@localhost')
308 expect(email['to'][0]['address']).equal('user_1@example.com')
309 expect(email['subject']).contains('Verify')
311 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
312 expect(verificationStringMatches).not.to.be.null
314 verificationString = verificationStringMatches[1]
315 expect(verificationString).to.not.be.undefined
316 expect(verificationString).to.have.length.above(2)
318 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
319 expect(userIdMatches).not.to.be.null
321 userId = parseInt(userIdMatches[1], 10)
324 it('Should not verify the email with an invalid verification string', async function () {
325 await server.usersCommand.verifyEmail({
327 verificationString: verificationString + 'b',
328 isPendingEmail: false,
329 expectedStatus: HttpStatusCode.FORBIDDEN_403
333 it('Should verify the email', async function () {
334 await server.usersCommand.verifyEmail({ userId, verificationString })
338 after(async function () {
339 MockSmtpServer.Instance.kill()
341 await cleanupTests([ server ])