1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
10 createUser, removeVideoFromBlacklist,
21 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
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)
48 const overrideConfig = {
53 server = await runServer(1, overrideConfig)
54 await setAccessTokensToServers([ server ])
57 const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
58 userId = res.body.user.id
60 userAccessToken = await userLogin(server, user)
65 name: 'my super user video'
67 const res = await uploadVideo(server.url, userAccessToken, attributes)
68 videoUserUUID = res.body.video.uuid
75 const res = await uploadVideo(server.url, server.accessToken, attributes)
76 videoUUID = res.body.video.uuid
80 describe('When resetting user password', function () {
82 it('Should ask to reset the password', async function () {
85 await askResetPassword(server.url, 'user_1@example.com')
87 await waitJobs(server)
88 expect(emails).to.have.lengthOf(1)
90 const email = emails[0]
92 expect(email['from'][0]['name']).equal('localhost:9001')
93 expect(email['from'][0]['address']).equal('test-admin@localhost')
94 expect(email['to'][0]['address']).equal('user_1@example.com')
95 expect(email['subject']).contains('password')
97 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
98 expect(verificationStringMatches).not.to.be.null
100 verificationString = verificationStringMatches[1]
101 expect(verificationString).to.have.length.above(2)
103 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
104 expect(userIdMatches).not.to.be.null
106 userId = parseInt(userIdMatches[1], 10)
107 expect(verificationString).to.not.be.undefined
110 it('Should not reset the password with an invalid verification string', async function () {
111 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
114 it('Should reset the password', async function () {
115 await resetPassword(server.url, userId, verificationString, 'super_password2')
118 it('Should login with this new password', async function () {
119 user.password = 'super_password2'
121 await userLogin(server, user)
125 describe('When creating a video abuse', function () {
126 it('Should send the notification email', async function () {
129 const reason = 'my super bad reason'
130 await reportVideoAbuse(server.url, server.accessToken, videoUUID, reason)
132 await waitJobs(server)
133 expect(emails).to.have.lengthOf(2)
135 const email = emails[1]
137 expect(email['from'][0]['name']).equal('localhost:9001')
138 expect(email['from'][0]['address']).equal('test-admin@localhost')
139 expect(email['to'][0]['address']).equal('admin1@example.com')
140 expect(email['subject']).contains('abuse')
141 expect(email['text']).contains(videoUUID)
145 describe('When blocking/unblocking user', function () {
147 it('Should send the notification email when blocking a user', async function () {
150 const reason = 'my super bad reason'
151 await blockUser(server.url, userId, server.accessToken, 204, reason)
153 await waitJobs(server)
154 expect(emails).to.have.lengthOf(3)
156 const email = emails[2]
158 expect(email['from'][0]['name']).equal('localhost:9001')
159 expect(email['from'][0]['address']).equal('test-admin@localhost')
160 expect(email['to'][0]['address']).equal('user_1@example.com')
161 expect(email['subject']).contains(' blocked')
162 expect(email['text']).contains(' blocked')
163 expect(email['text']).contains(reason)
166 it('Should send the notification email when unblocking a user', async function () {
169 await unblockUser(server.url, userId, server.accessToken, 204)
171 await waitJobs(server)
172 expect(emails).to.have.lengthOf(4)
174 const email = emails[3]
176 expect(email['from'][0]['name']).equal('localhost:9001')
177 expect(email['from'][0]['address']).equal('test-admin@localhost')
178 expect(email['to'][0]['address']).equal('user_1@example.com')
179 expect(email['subject']).contains(' unblocked')
180 expect(email['text']).contains(' unblocked')
184 describe('When blacklisting a video', function () {
185 it('Should send the notification email', async function () {
188 const reason = 'my super reason'
189 await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
191 await waitJobs(server)
192 expect(emails).to.have.lengthOf(5)
194 const email = emails[4]
196 expect(email['from'][0]['name']).equal('localhost:9001')
197 expect(email['from'][0]['address']).equal('test-admin@localhost')
198 expect(email['to'][0]['address']).equal('user_1@example.com')
199 expect(email['subject']).contains(' blacklisted')
200 expect(email['text']).contains('my super user video')
201 expect(email['text']).contains('my super reason')
204 it('Should send the notification email', async function () {
207 await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
209 await waitJobs(server)
210 expect(emails).to.have.lengthOf(6)
212 const email = emails[5]
214 expect(email['from'][0]['name']).equal('localhost:9001')
215 expect(email['from'][0]['address']).equal('test-admin@localhost')
216 expect(email['to'][0]['address']).equal('user_1@example.com')
217 expect(email['subject']).contains(' unblacklisted')
218 expect(email['text']).contains('my super user video')
222 describe('When verifying a user email', function () {
224 it('Should ask to send the verification email', async function () {
227 await askSendVerifyEmail(server.url, 'user_1@example.com')
229 await waitJobs(server)
230 expect(emails).to.have.lengthOf(7)
232 const email = emails[6]
234 expect(email['from'][0]['name']).equal('localhost:9001')
235 expect(email['from'][0]['address']).equal('test-admin@localhost')
236 expect(email['to'][0]['address']).equal('user_1@example.com')
237 expect(email['subject']).contains('Verify')
239 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
240 expect(verificationStringMatches).not.to.be.null
242 verificationString = verificationStringMatches[1]
243 expect(verificationString).to.not.be.undefined
244 expect(verificationString).to.have.length.above(2)
246 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
247 expect(userIdMatches).not.to.be.null
249 userId = parseInt(userIdMatches[1], 10)
252 it('Should not verify the email with an invalid verification string', async function () {
253 await verifyEmail(server.url, userId, verificationString + 'b', 403)
256 it('Should verify the email', async function () {
257 await verifyEmail(server.url, userId, verificationString)
261 after(async function () {
262 MockSmtpServer.Instance.kill()
263 killallServers([ server ])