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/miscs/http-error-codes'
15 setAccessTokensToServers,
19 } from '../../../../shared/extra-utils'
20 import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
21 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
23 const expect = chai.expect
25 describe('Test emails', function () {
26 let server: ServerInfo
29 let userAccessToken: string
34 let videoUserUUID: string
36 let verificationString: string
37 let verificationString2: string
39 const emails: object[] = []
42 password: 'super_password'
46 before(async function () {
49 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
51 const overrideConfig = {
53 hostname: 'localhost',
57 server = await flushAndRunServer(1, overrideConfig)
58 await setAccessTokensToServers([ server ])
61 const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
62 userId = res.body.user.id
64 userAccessToken = await server.loginCommand.getAccessToken(user)
69 name: 'my super user video'
71 const res = await uploadVideo(server.url, userAccessToken, attributes)
72 videoUserUUID = res.body.video.uuid
79 const res = await uploadVideo(server.url, server.accessToken, attributes)
80 videoUUID = res.body.video.uuid
81 videoId = res.body.video.id
85 describe('When resetting user password', function () {
87 it('Should ask to reset the password', async function () {
90 await askResetPassword(server.url, 'user_1@example.com')
92 await waitJobs(server)
93 expect(emails).to.have.lengthOf(1)
95 const email = emails[0]
97 expect(email['from'][0]['name']).equal('PeerTube')
98 expect(email['from'][0]['address']).equal('test-admin@localhost')
99 expect(email['to'][0]['address']).equal('user_1@example.com')
100 expect(email['subject']).contains('password')
102 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
103 expect(verificationStringMatches).not.to.be.null
105 verificationString = verificationStringMatches[1]
106 expect(verificationString).to.have.length.above(2)
108 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
109 expect(userIdMatches).not.to.be.null
111 userId = parseInt(userIdMatches[1], 10)
112 expect(verificationString).to.not.be.undefined
115 it('Should not reset the password with an invalid verification string', async function () {
116 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', HttpStatusCode.FORBIDDEN_403)
119 it('Should reset the password', async function () {
120 await resetPassword(server.url, userId, verificationString, 'super_password2')
123 it('Should not reset the password with the same verification string', async function () {
124 await resetPassword(server.url, userId, verificationString, 'super_password3', HttpStatusCode.FORBIDDEN_403)
127 it('Should login with this new password', async function () {
128 user.password = 'super_password2'
130 await server.loginCommand.getAccessToken(user)
134 describe('When creating a user without password', function () {
135 it('Should send a create password email', async function () {
140 accessToken: server.accessToken,
141 username: 'create_password',
145 await waitJobs(server)
146 expect(emails).to.have.lengthOf(2)
148 const email = emails[1]
150 expect(email['from'][0]['name']).equal('PeerTube')
151 expect(email['from'][0]['address']).equal('test-admin@localhost')
152 expect(email['to'][0]['address']).equal('create_password@example.com')
153 expect(email['subject']).contains('account')
154 expect(email['subject']).contains('password')
156 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
157 expect(verificationStringMatches).not.to.be.null
159 verificationString2 = verificationStringMatches[1]
160 expect(verificationString2).to.have.length.above(2)
162 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
163 expect(userIdMatches).not.to.be.null
165 userId2 = parseInt(userIdMatches[1], 10)
168 it('Should not reset the password with an invalid verification string', async function () {
169 await resetPassword(server.url, userId2, verificationString2 + 'c', 'newly_created_password', HttpStatusCode.FORBIDDEN_403)
172 it('Should reset the password', async function () {
173 await resetPassword(server.url, userId2, verificationString2, 'newly_created_password')
176 it('Should login with this new password', async function () {
177 await server.loginCommand.getAccessToken({
178 username: 'create_password',
179 password: 'newly_created_password'
184 describe('When creating an abuse', function () {
185 it('Should send the notification email', async function () {
188 const reason = 'my super bad reason'
189 await server.abusesCommand.report({ videoId, reason })
191 await waitJobs(server)
192 expect(emails).to.have.lengthOf(3)
194 const email = emails[2]
196 expect(email['from'][0]['name']).equal('PeerTube')
197 expect(email['from'][0]['address']).equal('test-admin@localhost')
198 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
199 expect(email['subject']).contains('abuse')
200 expect(email['text']).contains(videoUUID)
204 describe('When blocking/unblocking user', function () {
206 it('Should send the notification email when blocking a user', async function () {
209 const reason = 'my super bad reason'
210 await blockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204, reason)
212 await waitJobs(server)
213 expect(emails).to.have.lengthOf(4)
215 const email = emails[3]
217 expect(email['from'][0]['name']).equal('PeerTube')
218 expect(email['from'][0]['address']).equal('test-admin@localhost')
219 expect(email['to'][0]['address']).equal('user_1@example.com')
220 expect(email['subject']).contains(' blocked')
221 expect(email['text']).contains(' blocked')
222 expect(email['text']).contains('bad reason')
225 it('Should send the notification email when unblocking a user', async function () {
228 await unblockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204)
230 await waitJobs(server)
231 expect(emails).to.have.lengthOf(5)
233 const email = emails[4]
235 expect(email['from'][0]['name']).equal('PeerTube')
236 expect(email['from'][0]['address']).equal('test-admin@localhost')
237 expect(email['to'][0]['address']).equal('user_1@example.com')
238 expect(email['subject']).contains(' unblocked')
239 expect(email['text']).contains(' unblocked')
243 describe('When blacklisting a video', function () {
244 it('Should send the notification email', async function () {
247 const reason = 'my super reason'
248 await server.blacklistCommand.add({ videoId: videoUserUUID, reason })
250 await waitJobs(server)
251 expect(emails).to.have.lengthOf(6)
253 const email = emails[5]
255 expect(email['from'][0]['name']).equal('PeerTube')
256 expect(email['from'][0]['address']).equal('test-admin@localhost')
257 expect(email['to'][0]['address']).equal('user_1@example.com')
258 expect(email['subject']).contains(' blacklisted')
259 expect(email['text']).contains('my super user video')
260 expect(email['text']).contains('my super reason')
263 it('Should send the notification email', async function () {
266 await server.blacklistCommand.remove({ videoId: videoUserUUID })
268 await waitJobs(server)
269 expect(emails).to.have.lengthOf(7)
271 const email = emails[6]
273 expect(email['from'][0]['name']).equal('PeerTube')
274 expect(email['from'][0]['address']).equal('test-admin@localhost')
275 expect(email['to'][0]['address']).equal('user_1@example.com')
276 expect(email['subject']).contains(' unblacklisted')
277 expect(email['text']).contains('my super user video')
280 it('Should have the manage preferences link in the email', async function () {
281 const email = emails[6]
282 expect(email['text']).to.contain('Manage your notification preferences')
286 describe('When verifying a user email', function () {
288 it('Should ask to send the verification email', async function () {
291 await askSendVerifyEmail(server.url, 'user_1@example.com')
293 await waitJobs(server)
294 expect(emails).to.have.lengthOf(8)
296 const email = emails[7]
298 expect(email['from'][0]['name']).equal('PeerTube')
299 expect(email['from'][0]['address']).equal('test-admin@localhost')
300 expect(email['to'][0]['address']).equal('user_1@example.com')
301 expect(email['subject']).contains('Verify')
303 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
304 expect(verificationStringMatches).not.to.be.null
306 verificationString = verificationStringMatches[1]
307 expect(verificationString).to.not.be.undefined
308 expect(verificationString).to.have.length.above(2)
310 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
311 expect(userIdMatches).not.to.be.null
313 userId = parseInt(userIdMatches[1], 10)
316 it('Should not verify the email with an invalid verification string', async function () {
317 await verifyEmail(server.url, userId, verificationString + 'b', false, HttpStatusCode.FORBIDDEN_403)
320 it('Should verify the email', async function () {
321 await verifyEmail(server.url, userId, verificationString)
325 after(async function () {
326 MockSmtpServer.Instance.kill()
328 await cleanupTests([ server ])