1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
10 createUser, removeVideoFromBlacklist,
21 setAccessTokensToServers, cleanupTests
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
31 let userAccessToken: string
33 let videoUserUUID: string
34 let verificationString: string
35 const emails: object[] = []
38 password: 'super_password'
41 before(async function () {
44 await MockSmtpServer.Instance.collectEmails(emails)
46 const overrideConfig = {
51 server = await flushAndRunServer(1, overrideConfig)
52 await setAccessTokensToServers([ server ])
55 const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
56 userId = res.body.user.id
58 userAccessToken = await userLogin(server, user)
63 name: 'my super user video'
65 const res = await uploadVideo(server.url, userAccessToken, attributes)
66 videoUserUUID = res.body.video.uuid
73 const res = await uploadVideo(server.url, server.accessToken, attributes)
74 videoUUID = res.body.video.uuid
78 describe('When resetting user password', function () {
80 it('Should ask to reset the password', async function () {
83 await askResetPassword(server.url, 'user_1@example.com')
85 await waitJobs(server)
86 expect(emails).to.have.lengthOf(1)
88 const email = emails[0]
90 expect(email['from'][0]['name']).equal('localhost:9001')
91 expect(email['from'][0]['address']).equal('test-admin@localhost')
92 expect(email['to'][0]['address']).equal('user_1@example.com')
93 expect(email['subject']).contains('password')
95 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
96 expect(verificationStringMatches).not.to.be.null
98 verificationString = verificationStringMatches[1]
99 expect(verificationString).to.have.length.above(2)
101 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
102 expect(userIdMatches).not.to.be.null
104 userId = parseInt(userIdMatches[1], 10)
105 expect(verificationString).to.not.be.undefined
108 it('Should not reset the password with an invalid verification string', async function () {
109 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
112 it('Should reset the password', async function () {
113 await resetPassword(server.url, userId, verificationString, 'super_password2')
116 it('Should login with this new password', async function () {
117 user.password = 'super_password2'
119 await userLogin(server, user)
123 describe('When creating a video abuse', function () {
124 it('Should send the notification email', async function () {
127 const reason = 'my super bad reason'
128 await reportVideoAbuse(server.url, server.accessToken, videoUUID, reason)
130 await waitJobs(server)
131 expect(emails).to.have.lengthOf(2)
133 const email = emails[1]
135 expect(email['from'][0]['name']).equal('localhost:9001')
136 expect(email['from'][0]['address']).equal('test-admin@localhost')
137 expect(email['to'][0]['address']).equal('admin1@example.com')
138 expect(email['subject']).contains('abuse')
139 expect(email['text']).contains(videoUUID)
143 describe('When blocking/unblocking user', function () {
145 it('Should send the notification email when blocking a user', async function () {
148 const reason = 'my super bad reason'
149 await blockUser(server.url, userId, server.accessToken, 204, reason)
151 await waitJobs(server)
152 expect(emails).to.have.lengthOf(3)
154 const email = emails[2]
156 expect(email['from'][0]['name']).equal('localhost:9001')
157 expect(email['from'][0]['address']).equal('test-admin@localhost')
158 expect(email['to'][0]['address']).equal('user_1@example.com')
159 expect(email['subject']).contains(' blocked')
160 expect(email['text']).contains(' blocked')
161 expect(email['text']).contains(reason)
164 it('Should send the notification email when unblocking a user', async function () {
167 await unblockUser(server.url, userId, server.accessToken, 204)
169 await waitJobs(server)
170 expect(emails).to.have.lengthOf(4)
172 const email = emails[3]
174 expect(email['from'][0]['name']).equal('localhost:9001')
175 expect(email['from'][0]['address']).equal('test-admin@localhost')
176 expect(email['to'][0]['address']).equal('user_1@example.com')
177 expect(email['subject']).contains(' unblocked')
178 expect(email['text']).contains(' unblocked')
182 describe('When blacklisting a video', function () {
183 it('Should send the notification email', async function () {
186 const reason = 'my super reason'
187 await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
189 await waitJobs(server)
190 expect(emails).to.have.lengthOf(5)
192 const email = emails[4]
194 expect(email['from'][0]['name']).equal('localhost:9001')
195 expect(email['from'][0]['address']).equal('test-admin@localhost')
196 expect(email['to'][0]['address']).equal('user_1@example.com')
197 expect(email['subject']).contains(' blacklisted')
198 expect(email['text']).contains('my super user video')
199 expect(email['text']).contains('my super reason')
202 it('Should send the notification email', async function () {
205 await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
207 await waitJobs(server)
208 expect(emails).to.have.lengthOf(6)
210 const email = emails[5]
212 expect(email['from'][0]['name']).equal('localhost:9001')
213 expect(email['from'][0]['address']).equal('test-admin@localhost')
214 expect(email['to'][0]['address']).equal('user_1@example.com')
215 expect(email['subject']).contains(' unblacklisted')
216 expect(email['text']).contains('my super user video')
220 describe('When verifying a user email', function () {
222 it('Should ask to send the verification email', async function () {
225 await askSendVerifyEmail(server.url, 'user_1@example.com')
227 await waitJobs(server)
228 expect(emails).to.have.lengthOf(7)
230 const email = emails[6]
232 expect(email['from'][0]['name']).equal('localhost:9001')
233 expect(email['from'][0]['address']).equal('test-admin@localhost')
234 expect(email['to'][0]['address']).equal('user_1@example.com')
235 expect(email['subject']).contains('Verify')
237 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
238 expect(verificationStringMatches).not.to.be.null
240 verificationString = verificationStringMatches[1]
241 expect(verificationString).to.not.be.undefined
242 expect(verificationString).to.have.length.above(2)
244 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
245 expect(userIdMatches).not.to.be.null
247 userId = parseInt(userIdMatches[1], 10)
250 it('Should not verify the email with an invalid verification string', async function () {
251 await verifyEmail(server.url, userId, verificationString + 'b', 403)
254 it('Should verify the email', async function () {
255 await verifyEmail(server.url, userId, verificationString)
259 after(async function () {
260 MockSmtpServer.Instance.kill()
262 await cleanupTests([ server ])