1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
10 createUser, removeVideoFromBlacklist,
19 import { flushTests, killallServers, ServerInfo, setAccessTokensToServers } from '../../utils/index'
20 import { mockSmtpServer } from '../../utils/miscs/email'
21 import { waitJobs } from '../../utils/server/jobs'
23 const expect = chai.expect
25 describe('Test emails', function () {
26 let server: ServerInfo
28 let userAccessToken: string
30 let videoUserUUID: string
31 let verificationString: string
32 const emails: object[] = []
35 password: 'super_password'
38 before(async function () {
41 await mockSmtpServer(emails)
45 const overrideConfig = {
50 server = await runServer(1, overrideConfig)
51 await setAccessTokensToServers([ server ])
54 const res = await createUser(server.url, server.accessToken, user.username, user.password)
55 userId = res.body.user.id
57 userAccessToken = await userLogin(server, user)
62 name: 'my super user video'
64 const res = await uploadVideo(server.url, userAccessToken, attributes)
65 videoUserUUID = res.body.video.uuid
72 const res = await uploadVideo(server.url, server.accessToken, attributes)
73 videoUUID = res.body.video.uuid
77 describe('When resetting user password', function () {
79 it('Should ask to reset the password', async function () {
82 await askResetPassword(server.url, 'user_1@example.com')
84 await waitJobs(server)
85 expect(emails).to.have.lengthOf(1)
87 const email = emails[0]
89 expect(email['from'][0]['address']).equal('test-admin@localhost')
90 expect(email['to'][0]['address']).equal('user_1@example.com')
91 expect(email['subject']).contains('password')
93 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
94 expect(verificationStringMatches).not.to.be.null
96 verificationString = verificationStringMatches[1]
97 expect(verificationString).to.have.length.above(2)
99 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
100 expect(userIdMatches).not.to.be.null
102 userId = parseInt(userIdMatches[1], 10)
103 expect(verificationString).to.not.be.undefined
106 it('Should not reset the password with an invalid verification string', async function () {
107 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
110 it('Should reset the password', async function () {
111 await resetPassword(server.url, userId, verificationString, 'super_password2')
114 it('Should login with this new password', async function () {
115 user.password = 'super_password2'
117 await userLogin(server, user)
121 describe('When creating a video abuse', function () {
122 it('Should send the notification email', async function () {
125 const reason = 'my super bad reason'
126 await reportVideoAbuse(server.url, server.accessToken, videoUUID, reason)
128 await waitJobs(server)
129 expect(emails).to.have.lengthOf(2)
131 const email = emails[1]
133 expect(email['from'][0]['address']).equal('test-admin@localhost')
134 expect(email['to'][0]['address']).equal('admin1@example.com')
135 expect(email['subject']).contains('abuse')
136 expect(email['text']).contains(videoUUID)
140 describe('When blocking/unblocking user', async function () {
141 it('Should send the notification email when blocking a user', async function () {
144 const reason = 'my super bad reason'
145 await blockUser(server.url, userId, server.accessToken, 204, reason)
147 await waitJobs(server)
148 expect(emails).to.have.lengthOf(3)
150 const email = emails[2]
152 expect(email['from'][0]['address']).equal('test-admin@localhost')
153 expect(email['to'][0]['address']).equal('user_1@example.com')
154 expect(email['subject']).contains(' blocked')
155 expect(email['text']).contains(' blocked')
156 expect(email['text']).contains(reason)
159 it('Should send the notification email when unblocking a user', async function () {
162 await unblockUser(server.url, userId, server.accessToken, 204)
164 await waitJobs(server)
165 expect(emails).to.have.lengthOf(4)
167 const email = emails[3]
169 expect(email['from'][0]['address']).equal('test-admin@localhost')
170 expect(email['to'][0]['address']).equal('user_1@example.com')
171 expect(email['subject']).contains(' unblocked')
172 expect(email['text']).contains(' unblocked')
176 describe('When blacklisting a video', function () {
177 it('Should send the notification email', async function () {
180 const reason = 'my super reason'
181 await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
183 await waitJobs(server)
184 expect(emails).to.have.lengthOf(5)
186 const email = emails[4]
188 expect(email['from'][0]['address']).equal('test-admin@localhost')
189 expect(email['to'][0]['address']).equal('user_1@example.com')
190 expect(email['subject']).contains(' blacklisted')
191 expect(email['text']).contains('my super user video')
192 expect(email['text']).contains('my super reason')
195 it('Should send the notification email', async function () {
198 await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
200 await waitJobs(server)
201 expect(emails).to.have.lengthOf(6)
203 const email = emails[5]
205 expect(email['from'][0]['address']).equal('test-admin@localhost')
206 expect(email['to'][0]['address']).equal('user_1@example.com')
207 expect(email['subject']).contains(' unblacklisted')
208 expect(email['text']).contains('my super user video')
212 describe('When verifying a user email', function () {
214 it('Should ask to send the verification email', async function () {
217 await askSendVerifyEmail(server.url, 'user_1@example.com')
219 await waitJobs(server)
220 expect(emails).to.have.lengthOf(7)
222 const email = emails[6]
224 expect(email['from'][0]['address']).equal('test-admin@localhost')
225 expect(email['to'][0]['address']).equal('user_1@example.com')
226 expect(email['subject']).contains('Verify')
228 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
229 expect(verificationStringMatches).not.to.be.null
231 verificationString = verificationStringMatches[1]
232 expect(verificationString).to.not.be.undefined
233 expect(verificationString).to.have.length.above(2)
235 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
236 expect(userIdMatches).not.to.be.null
238 userId = parseInt(userIdMatches[1], 10)
241 it('Should not verify the email with an invalid verification string', async function () {
242 await verifyEmail(server.url, userId, verificationString + 'b', 403)
245 it('Should verify the email', async function () {
246 await verifyEmail(server.url, userId, verificationString)
250 after(async function () {
251 killallServers([ server ])