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,
20 } from '../../../../shared/extra-utils'
21 import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
22 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
24 const expect = chai.expect
26 describe('Test emails', function () {
27 let server: ServerInfo
30 let userAccessToken: string
35 let videoUserUUID: string
37 let verificationString: string
38 let verificationString2: string
40 const emails: object[] = []
43 password: 'super_password'
47 before(async function () {
50 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
52 const overrideConfig = {
54 hostname: 'localhost',
58 server = await flushAndRunServer(1, overrideConfig)
59 await setAccessTokensToServers([ server ])
62 const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
63 userId = res.body.user.id
65 userAccessToken = await userLogin(server, user)
70 name: 'my super user video'
72 const res = await uploadVideo(server.url, userAccessToken, attributes)
73 videoUserUUID = res.body.video.uuid
80 const res = await uploadVideo(server.url, server.accessToken, attributes)
81 videoUUID = res.body.video.uuid
82 videoId = res.body.video.id
86 describe('When resetting user password', function () {
88 it('Should ask to reset the password', async function () {
91 await askResetPassword(server.url, 'user_1@example.com')
93 await waitJobs(server)
94 expect(emails).to.have.lengthOf(1)
96 const email = emails[0]
98 expect(email['from'][0]['name']).equal('PeerTube')
99 expect(email['from'][0]['address']).equal('test-admin@localhost')
100 expect(email['to'][0]['address']).equal('user_1@example.com')
101 expect(email['subject']).contains('password')
103 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
104 expect(verificationStringMatches).not.to.be.null
106 verificationString = verificationStringMatches[1]
107 expect(verificationString).to.have.length.above(2)
109 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
110 expect(userIdMatches).not.to.be.null
112 userId = parseInt(userIdMatches[1], 10)
113 expect(verificationString).to.not.be.undefined
116 it('Should not reset the password with an invalid verification string', async function () {
117 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', HttpStatusCode.FORBIDDEN_403)
120 it('Should reset the password', async function () {
121 await resetPassword(server.url, userId, verificationString, 'super_password2')
124 it('Should not reset the password with the same verification string', async function () {
125 await resetPassword(server.url, userId, verificationString, 'super_password3', HttpStatusCode.FORBIDDEN_403)
128 it('Should login with this new password', async function () {
129 user.password = 'super_password2'
131 await userLogin(server, user)
135 describe('When creating a user without password', function () {
136 it('Should send a create password email', async function () {
141 accessToken: server.accessToken,
142 username: 'create_password',
146 await waitJobs(server)
147 expect(emails).to.have.lengthOf(2)
149 const email = emails[1]
151 expect(email['from'][0]['name']).equal('PeerTube')
152 expect(email['from'][0]['address']).equal('test-admin@localhost')
153 expect(email['to'][0]['address']).equal('create_password@example.com')
154 expect(email['subject']).contains('account')
155 expect(email['subject']).contains('password')
157 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
158 expect(verificationStringMatches).not.to.be.null
160 verificationString2 = verificationStringMatches[1]
161 expect(verificationString2).to.have.length.above(2)
163 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
164 expect(userIdMatches).not.to.be.null
166 userId2 = parseInt(userIdMatches[1], 10)
169 it('Should not reset the password with an invalid verification string', async function () {
170 await resetPassword(server.url, userId2, verificationString2 + 'c', 'newly_created_password', HttpStatusCode.FORBIDDEN_403)
173 it('Should reset the password', async function () {
174 await resetPassword(server.url, userId2, verificationString2, 'newly_created_password')
177 it('Should login with this new password', async function () {
178 await userLogin(server, {
179 username: 'create_password',
180 password: 'newly_created_password'
185 describe('When creating an abuse', function () {
186 it('Should send the notification email', async function () {
189 const reason = 'my super bad reason'
190 await server.abusesCommand.report({ videoId, reason })
192 await waitJobs(server)
193 expect(emails).to.have.lengthOf(3)
195 const email = emails[2]
197 expect(email['from'][0]['name']).equal('PeerTube')
198 expect(email['from'][0]['address']).equal('test-admin@localhost')
199 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
200 expect(email['subject']).contains('abuse')
201 expect(email['text']).contains(videoUUID)
205 describe('When blocking/unblocking user', function () {
207 it('Should send the notification email when blocking a user', async function () {
210 const reason = 'my super bad reason'
211 await blockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204, reason)
213 await waitJobs(server)
214 expect(emails).to.have.lengthOf(4)
216 const email = emails[3]
218 expect(email['from'][0]['name']).equal('PeerTube')
219 expect(email['from'][0]['address']).equal('test-admin@localhost')
220 expect(email['to'][0]['address']).equal('user_1@example.com')
221 expect(email['subject']).contains(' blocked')
222 expect(email['text']).contains(' blocked')
223 expect(email['text']).contains('bad reason')
226 it('Should send the notification email when unblocking a user', async function () {
229 await unblockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204)
231 await waitJobs(server)
232 expect(emails).to.have.lengthOf(5)
234 const email = emails[4]
236 expect(email['from'][0]['name']).equal('PeerTube')
237 expect(email['from'][0]['address']).equal('test-admin@localhost')
238 expect(email['to'][0]['address']).equal('user_1@example.com')
239 expect(email['subject']).contains(' unblocked')
240 expect(email['text']).contains(' unblocked')
244 describe('When blacklisting a video', function () {
245 it('Should send the notification email', async function () {
248 const reason = 'my super reason'
249 await server.blacklistCommand.add({ videoId: videoUserUUID, reason })
251 await waitJobs(server)
252 expect(emails).to.have.lengthOf(6)
254 const email = emails[5]
256 expect(email['from'][0]['name']).equal('PeerTube')
257 expect(email['from'][0]['address']).equal('test-admin@localhost')
258 expect(email['to'][0]['address']).equal('user_1@example.com')
259 expect(email['subject']).contains(' blacklisted')
260 expect(email['text']).contains('my super user video')
261 expect(email['text']).contains('my super reason')
264 it('Should send the notification email', async function () {
267 await server.blacklistCommand.remove({ videoId: videoUserUUID })
269 await waitJobs(server)
270 expect(emails).to.have.lengthOf(7)
272 const email = emails[6]
274 expect(email['from'][0]['name']).equal('PeerTube')
275 expect(email['from'][0]['address']).equal('test-admin@localhost')
276 expect(email['to'][0]['address']).equal('user_1@example.com')
277 expect(email['subject']).contains(' unblacklisted')
278 expect(email['text']).contains('my super user video')
281 it('Should have the manage preferences link in the email', async function () {
282 const email = emails[6]
283 expect(email['text']).to.contain('Manage your notification preferences')
287 describe('When verifying a user email', function () {
289 it('Should ask to send the verification email', async function () {
292 await askSendVerifyEmail(server.url, 'user_1@example.com')
294 await waitJobs(server)
295 expect(emails).to.have.lengthOf(8)
297 const email = emails[7]
299 expect(email['from'][0]['name']).equal('PeerTube')
300 expect(email['from'][0]['address']).equal('test-admin@localhost')
301 expect(email['to'][0]['address']).equal('user_1@example.com')
302 expect(email['subject']).contains('Verify')
304 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
305 expect(verificationStringMatches).not.to.be.null
307 verificationString = verificationStringMatches[1]
308 expect(verificationString).to.not.be.undefined
309 expect(verificationString).to.have.length.above(2)
311 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
312 expect(userIdMatches).not.to.be.null
314 userId = parseInt(userIdMatches[1], 10)
317 it('Should not verify the email with an invalid verification string', async function () {
318 await verifyEmail(server.url, userId, verificationString + 'b', false, HttpStatusCode.FORBIDDEN_403)
321 it('Should verify the email', async function () {
322 await verifyEmail(server.url, userId, verificationString)
326 after(async function () {
327 MockSmtpServer.Instance.kill()
329 await cleanupTests([ server ])