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'
26 const expect = chai.expect
28 describe('Test emails', function () {
29 let server: ServerInfo
32 let userAccessToken: string
37 let videoUserUUID: string
39 let verificationString: string
40 let verificationString2: string
42 const emails: object[] = []
45 password: 'super_password'
49 before(async function () {
52 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
54 const overrideConfig = {
56 hostname: 'localhost',
60 server = await flushAndRunServer(1, overrideConfig)
61 await setAccessTokensToServers([ server ])
64 const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
65 userId = res.body.user.id
67 userAccessToken = await userLogin(server, user)
72 name: 'my super user video'
74 const res = await uploadVideo(server.url, userAccessToken, attributes)
75 videoUserUUID = res.body.video.uuid
82 const res = await uploadVideo(server.url, server.accessToken, attributes)
83 videoUUID = res.body.video.uuid
84 videoId = res.body.video.id
88 describe('When resetting user password', function () {
90 it('Should ask to reset the password', async function () {
93 await askResetPassword(server.url, 'user_1@example.com')
95 await waitJobs(server)
96 expect(emails).to.have.lengthOf(1)
98 const email = emails[0]
100 expect(email['from'][0]['name']).equal('localhost:' + server.port)
101 expect(email['from'][0]['address']).equal('test-admin@localhost')
102 expect(email['to'][0]['address']).equal('user_1@example.com')
103 expect(email['subject']).contains('password')
105 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
106 expect(verificationStringMatches).not.to.be.null
108 verificationString = verificationStringMatches[1]
109 expect(verificationString).to.have.length.above(2)
111 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
112 expect(userIdMatches).not.to.be.null
114 userId = parseInt(userIdMatches[1], 10)
115 expect(verificationString).to.not.be.undefined
118 it('Should not reset the password with an invalid verification string', async function () {
119 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
122 it('Should reset the password', async function () {
123 await resetPassword(server.url, userId, verificationString, 'super_password2')
126 it('Should not reset the password with the same verification string', async function () {
127 await resetPassword(server.url, userId, verificationString, 'super_password3', 403)
130 it('Should login with this new password', async function () {
131 user.password = 'super_password2'
133 await userLogin(server, user)
137 describe('When creating a user without password', function () {
138 it('Should send a create password email', async function () {
143 accessToken: server.accessToken,
144 username: 'create_password',
148 await waitJobs(server)
149 expect(emails).to.have.lengthOf(2)
151 const email = emails[1]
153 expect(email['from'][0]['name']).equal('localhost:' + server.port)
154 expect(email['from'][0]['address']).equal('test-admin@localhost')
155 expect(email['to'][0]['address']).equal('create_password@example.com')
156 expect(email['subject']).contains('account')
157 expect(email['subject']).contains('password')
159 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
160 expect(verificationStringMatches).not.to.be.null
162 verificationString2 = verificationStringMatches[1]
163 expect(verificationString2).to.have.length.above(2)
165 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
166 expect(userIdMatches).not.to.be.null
168 userId2 = parseInt(userIdMatches[1], 10)
171 it('Should not reset the password with an invalid verification string', async function () {
172 await resetPassword(server.url, userId2, verificationString2 + 'c', 'newly_created_password', 403)
175 it('Should reset the password', async function () {
176 await resetPassword(server.url, userId2, verificationString2, 'newly_created_password')
179 it('Should login with this new password', async function () {
180 await userLogin(server, {
181 username: 'create_password',
182 password: 'newly_created_password'
187 describe('When creating an abuse', function () {
188 it('Should send the notification email', async function () {
191 const reason = 'my super bad reason'
192 await reportAbuse({ url: server.url, token: server.accessToken, videoId, reason })
194 await waitJobs(server)
195 expect(emails).to.have.lengthOf(3)
197 const email = emails[2]
199 expect(email['from'][0]['name']).equal('localhost:' + server.port)
200 expect(email['from'][0]['address']).equal('test-admin@localhost')
201 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
202 expect(email['subject']).contains('abuse')
203 expect(email['text']).contains(videoUUID)
207 describe('When blocking/unblocking user', function () {
209 it('Should send the notification email when blocking a user', async function () {
212 const reason = 'my super bad reason'
213 await blockUser(server.url, userId, server.accessToken, 204, reason)
215 await waitJobs(server)
216 expect(emails).to.have.lengthOf(4)
218 const email = emails[3]
220 expect(email['from'][0]['name']).equal('localhost:' + server.port)
221 expect(email['from'][0]['address']).equal('test-admin@localhost')
222 expect(email['to'][0]['address']).equal('user_1@example.com')
223 expect(email['subject']).contains(' blocked')
224 expect(email['text']).contains(' blocked')
225 expect(email['text']).contains(reason)
228 it('Should send the notification email when unblocking a user', async function () {
231 await unblockUser(server.url, userId, server.accessToken, 204)
233 await waitJobs(server)
234 expect(emails).to.have.lengthOf(5)
236 const email = emails[4]
238 expect(email['from'][0]['name']).equal('localhost:' + server.port)
239 expect(email['from'][0]['address']).equal('test-admin@localhost')
240 expect(email['to'][0]['address']).equal('user_1@example.com')
241 expect(email['subject']).contains(' unblocked')
242 expect(email['text']).contains(' unblocked')
246 describe('When blacklisting a video', function () {
247 it('Should send the notification email', async function () {
250 const reason = 'my super reason'
251 await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
253 await waitJobs(server)
254 expect(emails).to.have.lengthOf(6)
256 const email = emails[5]
258 expect(email['from'][0]['name']).equal('localhost:' + server.port)
259 expect(email['from'][0]['address']).equal('test-admin@localhost')
260 expect(email['to'][0]['address']).equal('user_1@example.com')
261 expect(email['subject']).contains(' blacklisted')
262 expect(email['text']).contains('my super user video')
263 expect(email['text']).contains('my super reason')
266 it('Should send the notification email', async function () {
269 await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
271 await waitJobs(server)
272 expect(emails).to.have.lengthOf(7)
274 const email = emails[6]
276 expect(email['from'][0]['name']).equal('localhost:' + server.port)
277 expect(email['from'][0]['address']).equal('test-admin@localhost')
278 expect(email['to'][0]['address']).equal('user_1@example.com')
279 expect(email['subject']).contains(' unblacklisted')
280 expect(email['text']).contains('my super user video')
284 describe('When verifying a user email', function () {
286 it('Should ask to send the verification email', async function () {
289 await askSendVerifyEmail(server.url, 'user_1@example.com')
291 await waitJobs(server)
292 expect(emails).to.have.lengthOf(8)
294 const email = emails[7]
296 expect(email['from'][0]['name']).equal('localhost:' + server.port)
297 expect(email['from'][0]['address']).equal('test-admin@localhost')
298 expect(email['to'][0]['address']).equal('user_1@example.com')
299 expect(email['subject']).contains('Verify')
301 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
302 expect(verificationStringMatches).not.to.be.null
304 verificationString = verificationStringMatches[1]
305 expect(verificationString).to.not.be.undefined
306 expect(verificationString).to.have.length.above(2)
308 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
309 expect(userIdMatches).not.to.be.null
311 userId = parseInt(userIdMatches[1], 10)
314 it('Should not verify the email with an invalid verification string', async function () {
315 await verifyEmail(server.url, userId, verificationString + 'b', false, 403)
318 it('Should verify the email', async function () {
319 await verifyEmail(server.url, userId, verificationString)
323 after(async function () {
324 MockSmtpServer.Instance.kill()
326 await cleanupTests([ server ])