1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 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'
26 const expect = chai.expect
28 describe('Test emails', function () {
29 let server: ServerInfo
32 let userAccessToken: string
34 let videoUserUUID: string
35 let verificationString: string
36 let verificationString2: string
37 const emails: object[] = []
40 password: 'super_password'
44 before(async function () {
47 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
49 const overrideConfig = {
51 hostname: 'localhost',
55 server = await flushAndRunServer(1, overrideConfig)
56 await setAccessTokensToServers([ server ])
59 const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
60 userId = res.body.user.id
62 userAccessToken = await userLogin(server, user)
67 name: 'my super user video'
69 const res = await uploadVideo(server.url, userAccessToken, attributes)
70 videoUserUUID = res.body.video.uuid
77 const res = await uploadVideo(server.url, server.accessToken, attributes)
78 videoUUID = res.body.video.uuid
82 describe('When resetting user password', function () {
84 it('Should ask to reset the password', async function () {
87 await askResetPassword(server.url, 'user_1@example.com')
89 await waitJobs(server)
90 expect(emails).to.have.lengthOf(1)
92 const email = emails[0]
94 expect(email['from'][0]['name']).equal('localhost:' + server.port)
95 expect(email['from'][0]['address']).equal('test-admin@localhost')
96 expect(email['to'][0]['address']).equal('user_1@example.com')
97 expect(email['subject']).contains('password')
99 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
100 expect(verificationStringMatches).not.to.be.null
102 verificationString = verificationStringMatches[1]
103 expect(verificationString).to.have.length.above(2)
105 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
106 expect(userIdMatches).not.to.be.null
108 userId = parseInt(userIdMatches[1], 10)
109 expect(verificationString).to.not.be.undefined
112 it('Should not reset the password with an invalid verification string', async function () {
113 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
116 it('Should reset the password', async function () {
117 await resetPassword(server.url, userId, verificationString, 'super_password2')
120 it('Should login with this new password', async function () {
121 user.password = 'super_password2'
123 await userLogin(server, user)
127 describe('When creating a user without password', function () {
128 it('Should send a create password email', async function () {
133 accessToken: server.accessToken,
134 username: 'create_password',
138 await waitJobs(server)
139 expect(emails).to.have.lengthOf(2)
141 const email = emails[1]
143 expect(email['from'][0]['name']).equal('localhost:' + server.port)
144 expect(email['from'][0]['address']).equal('test-admin@localhost')
145 expect(email['to'][0]['address']).equal('create_password@example.com')
146 expect(email['subject']).contains('account')
147 expect(email['subject']).contains('password')
149 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
150 expect(verificationStringMatches).not.to.be.null
152 verificationString2 = verificationStringMatches[1]
153 expect(verificationString2).to.have.length.above(2)
155 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
156 expect(userIdMatches).not.to.be.null
158 userId2 = parseInt(userIdMatches[1], 10)
161 it('Should not reset the password with an invalid verification string', async function () {
162 await resetPassword(server.url, userId2, verificationString2 + 'c', 'newly_created_password', 403)
165 it('Should reset the password', async function () {
166 await resetPassword(server.url, userId2, verificationString2, 'newly_created_password')
169 it('Should login with this new password', async function () {
170 await userLogin(server, {
171 username: 'create_password',
172 password: 'newly_created_password'
177 describe('When creating a video abuse', function () {
178 it('Should send the notification email', async function () {
181 const reason = 'my super bad reason'
182 await reportVideoAbuse(server.url, server.accessToken, videoUUID, reason)
184 await waitJobs(server)
185 expect(emails).to.have.lengthOf(3)
187 const email = emails[2]
189 expect(email['from'][0]['name']).equal('localhost:' + server.port)
190 expect(email['from'][0]['address']).equal('test-admin@localhost')
191 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
192 expect(email['subject']).contains('abuse')
193 expect(email['text']).contains(videoUUID)
197 describe('When blocking/unblocking user', function () {
199 it('Should send the notification email when blocking a user', async function () {
202 const reason = 'my super bad reason'
203 await blockUser(server.url, userId, server.accessToken, 204, reason)
205 await waitJobs(server)
206 expect(emails).to.have.lengthOf(4)
208 const email = emails[3]
210 expect(email['from'][0]['name']).equal('localhost:' + server.port)
211 expect(email['from'][0]['address']).equal('test-admin@localhost')
212 expect(email['to'][0]['address']).equal('user_1@example.com')
213 expect(email['subject']).contains(' blocked')
214 expect(email['text']).contains(' blocked')
215 expect(email['text']).contains(reason)
218 it('Should send the notification email when unblocking a user', async function () {
221 await unblockUser(server.url, userId, server.accessToken, 204)
223 await waitJobs(server)
224 expect(emails).to.have.lengthOf(5)
226 const email = emails[4]
228 expect(email['from'][0]['name']).equal('localhost:' + server.port)
229 expect(email['from'][0]['address']).equal('test-admin@localhost')
230 expect(email['to'][0]['address']).equal('user_1@example.com')
231 expect(email['subject']).contains(' unblocked')
232 expect(email['text']).contains(' unblocked')
236 describe('When blacklisting a video', function () {
237 it('Should send the notification email', async function () {
240 const reason = 'my super reason'
241 await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
243 await waitJobs(server)
244 expect(emails).to.have.lengthOf(6)
246 const email = emails[5]
248 expect(email['from'][0]['name']).equal('localhost:' + server.port)
249 expect(email['from'][0]['address']).equal('test-admin@localhost')
250 expect(email['to'][0]['address']).equal('user_1@example.com')
251 expect(email['subject']).contains(' blacklisted')
252 expect(email['text']).contains('my super user video')
253 expect(email['text']).contains('my super reason')
256 it('Should send the notification email', async function () {
259 await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
261 await waitJobs(server)
262 expect(emails).to.have.lengthOf(7)
264 const email = emails[6]
266 expect(email['from'][0]['name']).equal('localhost:' + server.port)
267 expect(email['from'][0]['address']).equal('test-admin@localhost')
268 expect(email['to'][0]['address']).equal('user_1@example.com')
269 expect(email['subject']).contains(' unblacklisted')
270 expect(email['text']).contains('my super user video')
274 describe('When verifying a user email', function () {
276 it('Should ask to send the verification email', async function () {
279 await askSendVerifyEmail(server.url, 'user_1@example.com')
281 await waitJobs(server)
282 expect(emails).to.have.lengthOf(8)
284 const email = emails[7]
286 expect(email['from'][0]['name']).equal('localhost:' + server.port)
287 expect(email['from'][0]['address']).equal('test-admin@localhost')
288 expect(email['to'][0]['address']).equal('user_1@example.com')
289 expect(email['subject']).contains('Verify')
291 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
292 expect(verificationStringMatches).not.to.be.null
294 verificationString = verificationStringMatches[1]
295 expect(verificationString).to.not.be.undefined
296 expect(verificationString).to.have.length.above(2)
298 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
299 expect(userIdMatches).not.to.be.null
301 userId = parseInt(userIdMatches[1], 10)
304 it('Should not verify the email with an invalid verification string', async function () {
305 await verifyEmail(server.url, userId, verificationString + 'b', false, 403)
308 it('Should verify the email', async function () {
309 await verifyEmail(server.url, userId, verificationString)
313 after(async function () {
314 MockSmtpServer.Instance.kill()
316 await cleanupTests([ server ])