1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
13 removeVideoFromBlacklist,
17 setAccessTokensToServers,
22 } from '../../../../shared/extra-utils'
23 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
24 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
25 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
27 const expect = chai.expect
29 describe('Test emails', function () {
30 let server: ServerInfo
33 let userAccessToken: string
38 let videoUserUUID: string
40 let verificationString: string
41 let verificationString2: string
43 const emails: object[] = []
46 password: 'super_password'
50 before(async function () {
53 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
55 const overrideConfig = {
57 hostname: 'localhost',
61 server = await flushAndRunServer(1, overrideConfig)
62 await setAccessTokensToServers([ server ])
65 const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
66 userId = res.body.user.id
68 userAccessToken = await userLogin(server, user)
73 name: 'my super user video'
75 const res = await uploadVideo(server.url, userAccessToken, attributes)
76 videoUserUUID = res.body.video.uuid
83 const res = await uploadVideo(server.url, server.accessToken, attributes)
84 videoUUID = res.body.video.uuid
85 videoId = res.body.video.id
89 describe('When resetting user password', function () {
91 it('Should ask to reset the password', async function () {
94 await askResetPassword(server.url, 'user_1@example.com')
96 await waitJobs(server)
97 expect(emails).to.have.lengthOf(1)
99 const email = emails[0]
101 expect(email['from'][0]['name']).equal('PeerTube')
102 expect(email['from'][0]['address']).equal('test-admin@localhost')
103 expect(email['to'][0]['address']).equal('user_1@example.com')
104 expect(email['subject']).contains('password')
106 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
107 expect(verificationStringMatches).not.to.be.null
109 verificationString = verificationStringMatches[1]
110 expect(verificationString).to.have.length.above(2)
112 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
113 expect(userIdMatches).not.to.be.null
115 userId = parseInt(userIdMatches[1], 10)
116 expect(verificationString).to.not.be.undefined
119 it('Should not reset the password with an invalid verification string', async function () {
120 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', HttpStatusCode.FORBIDDEN_403)
123 it('Should reset the password', async function () {
124 await resetPassword(server.url, userId, verificationString, 'super_password2')
127 it('Should not reset the password with the same verification string', async function () {
128 await resetPassword(server.url, userId, verificationString, 'super_password3', HttpStatusCode.FORBIDDEN_403)
131 it('Should login with this new password', async function () {
132 user.password = 'super_password2'
134 await userLogin(server, user)
138 describe('When creating a user without password', function () {
139 it('Should send a create password email', async function () {
144 accessToken: server.accessToken,
145 username: 'create_password',
149 await waitJobs(server)
150 expect(emails).to.have.lengthOf(2)
152 const email = emails[1]
154 expect(email['from'][0]['name']).equal('PeerTube')
155 expect(email['from'][0]['address']).equal('test-admin@localhost')
156 expect(email['to'][0]['address']).equal('create_password@example.com')
157 expect(email['subject']).contains('account')
158 expect(email['subject']).contains('password')
160 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
161 expect(verificationStringMatches).not.to.be.null
163 verificationString2 = verificationStringMatches[1]
164 expect(verificationString2).to.have.length.above(2)
166 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
167 expect(userIdMatches).not.to.be.null
169 userId2 = parseInt(userIdMatches[1], 10)
172 it('Should not reset the password with an invalid verification string', async function () {
173 await resetPassword(server.url, userId2, verificationString2 + 'c', 'newly_created_password', HttpStatusCode.FORBIDDEN_403)
176 it('Should reset the password', async function () {
177 await resetPassword(server.url, userId2, verificationString2, 'newly_created_password')
180 it('Should login with this new password', async function () {
181 await userLogin(server, {
182 username: 'create_password',
183 password: 'newly_created_password'
188 describe('When creating an abuse', function () {
189 it('Should send the notification email', async function () {
192 const reason = 'my super bad reason'
193 await reportAbuse({ url: server.url, token: server.accessToken, videoId, reason })
195 await waitJobs(server)
196 expect(emails).to.have.lengthOf(3)
198 const email = emails[2]
200 expect(email['from'][0]['name']).equal('PeerTube')
201 expect(email['from'][0]['address']).equal('test-admin@localhost')
202 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
203 expect(email['subject']).contains('abuse')
204 expect(email['text']).contains(videoUUID)
208 describe('When blocking/unblocking user', function () {
210 it('Should send the notification email when blocking a user', async function () {
213 const reason = 'my super bad reason'
214 await blockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204, reason)
216 await waitJobs(server)
217 expect(emails).to.have.lengthOf(4)
219 const email = emails[3]
221 expect(email['from'][0]['name']).equal('PeerTube')
222 expect(email['from'][0]['address']).equal('test-admin@localhost')
223 expect(email['to'][0]['address']).equal('user_1@example.com')
224 expect(email['subject']).contains(' blocked')
225 expect(email['text']).contains(' blocked')
226 expect(email['text']).contains('bad reason')
229 it('Should send the notification email when unblocking a user', async function () {
232 await unblockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204)
234 await waitJobs(server)
235 expect(emails).to.have.lengthOf(5)
237 const email = emails[4]
239 expect(email['from'][0]['name']).equal('PeerTube')
240 expect(email['from'][0]['address']).equal('test-admin@localhost')
241 expect(email['to'][0]['address']).equal('user_1@example.com')
242 expect(email['subject']).contains(' unblocked')
243 expect(email['text']).contains(' unblocked')
247 describe('When blacklisting a video', function () {
248 it('Should send the notification email', async function () {
251 const reason = 'my super reason'
252 await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
254 await waitJobs(server)
255 expect(emails).to.have.lengthOf(6)
257 const email = emails[5]
259 expect(email['from'][0]['name']).equal('PeerTube')
260 expect(email['from'][0]['address']).equal('test-admin@localhost')
261 expect(email['to'][0]['address']).equal('user_1@example.com')
262 expect(email['subject']).contains(' blacklisted')
263 expect(email['text']).contains('my super user video')
264 expect(email['text']).contains('my super reason')
267 it('Should send the notification email', async function () {
270 await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
272 await waitJobs(server)
273 expect(emails).to.have.lengthOf(7)
275 const email = emails[6]
277 expect(email['from'][0]['name']).equal('PeerTube')
278 expect(email['from'][0]['address']).equal('test-admin@localhost')
279 expect(email['to'][0]['address']).equal('user_1@example.com')
280 expect(email['subject']).contains(' unblacklisted')
281 expect(email['text']).contains('my super user video')
284 it('Should have the manage preferences link in the email', async function () {
285 const email = emails[6]
286 expect(email['text']).to.contain('Manage your notification preferences')
290 describe('When verifying a user email', function () {
292 it('Should ask to send the verification email', async function () {
295 await askSendVerifyEmail(server.url, 'user_1@example.com')
297 await waitJobs(server)
298 expect(emails).to.have.lengthOf(8)
300 const email = emails[7]
302 expect(email['from'][0]['name']).equal('PeerTube')
303 expect(email['from'][0]['address']).equal('test-admin@localhost')
304 expect(email['to'][0]['address']).equal('user_1@example.com')
305 expect(email['subject']).contains('Verify')
307 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
308 expect(verificationStringMatches).not.to.be.null
310 verificationString = verificationStringMatches[1]
311 expect(verificationString).to.not.be.undefined
312 expect(verificationString).to.have.length.above(2)
314 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
315 expect(userIdMatches).not.to.be.null
317 userId = parseInt(userIdMatches[1], 10)
320 it('Should not verify the email with an invalid verification string', async function () {
321 await verifyEmail(server.url, userId, verificationString + 'b', false, HttpStatusCode.FORBIDDEN_403)
324 it('Should verify the email', async function () {
325 await verifyEmail(server.url, userId, verificationString)
329 after(async function () {
330 MockSmtpServer.Instance.kill()
332 await cleanupTests([ server ])