1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
10 createUser, removeVideoFromBlacklist,
21 setAccessTokensToServers
22 } from '../../../../shared/utils'
23 import { mockSmtpServer } from '../../../../shared/utils/miscs/email'
24 import { waitJobs } from '../../../../shared/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(emails)
48 const overrideConfig = {
53 server = await runServer(1, overrideConfig)
54 await setAccessTokensToServers([ server ])
57 const res = await createUser(server.url, server.accessToken, user.username, 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]['address']).equal('test-admin@localhost')
93 expect(email['to'][0]['address']).equal('user_1@example.com')
94 expect(email['subject']).contains('password')
96 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
97 expect(verificationStringMatches).not.to.be.null
99 verificationString = verificationStringMatches[1]
100 expect(verificationString).to.have.length.above(2)
102 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
103 expect(userIdMatches).not.to.be.null
105 userId = parseInt(userIdMatches[1], 10)
106 expect(verificationString).to.not.be.undefined
109 it('Should not reset the password with an invalid verification string', async function () {
110 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
113 it('Should reset the password', async function () {
114 await resetPassword(server.url, userId, verificationString, 'super_password2')
117 it('Should login with this new password', async function () {
118 user.password = 'super_password2'
120 await userLogin(server, user)
124 describe('When creating a video abuse', function () {
125 it('Should send the notification email', async function () {
128 const reason = 'my super bad reason'
129 await reportVideoAbuse(server.url, server.accessToken, videoUUID, reason)
131 await waitJobs(server)
132 expect(emails).to.have.lengthOf(2)
134 const email = emails[1]
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', async function () {
144 it('Should send the notification email when blocking a user', async function () {
147 const reason = 'my super bad reason'
148 await blockUser(server.url, userId, server.accessToken, 204, reason)
150 await waitJobs(server)
151 expect(emails).to.have.lengthOf(3)
153 const email = emails[2]
155 expect(email['from'][0]['address']).equal('test-admin@localhost')
156 expect(email['to'][0]['address']).equal('user_1@example.com')
157 expect(email['subject']).contains(' blocked')
158 expect(email['text']).contains(' blocked')
159 expect(email['text']).contains(reason)
162 it('Should send the notification email when unblocking a user', async function () {
165 await unblockUser(server.url, userId, server.accessToken, 204)
167 await waitJobs(server)
168 expect(emails).to.have.lengthOf(4)
170 const email = emails[3]
172 expect(email['from'][0]['address']).equal('test-admin@localhost')
173 expect(email['to'][0]['address']).equal('user_1@example.com')
174 expect(email['subject']).contains(' unblocked')
175 expect(email['text']).contains(' unblocked')
179 describe('When blacklisting a video', function () {
180 it('Should send the notification email', async function () {
183 const reason = 'my super reason'
184 await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
186 await waitJobs(server)
187 expect(emails).to.have.lengthOf(5)
189 const email = emails[4]
191 expect(email['from'][0]['address']).equal('test-admin@localhost')
192 expect(email['to'][0]['address']).equal('user_1@example.com')
193 expect(email['subject']).contains(' blacklisted')
194 expect(email['text']).contains('my super user video')
195 expect(email['text']).contains('my super reason')
198 it('Should send the notification email', async function () {
201 await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
203 await waitJobs(server)
204 expect(emails).to.have.lengthOf(6)
206 const email = emails[5]
208 expect(email['from'][0]['address']).equal('test-admin@localhost')
209 expect(email['to'][0]['address']).equal('user_1@example.com')
210 expect(email['subject']).contains(' unblacklisted')
211 expect(email['text']).contains('my super user video')
215 describe('When verifying a user email', function () {
217 it('Should ask to send the verification email', async function () {
220 await askSendVerifyEmail(server.url, 'user_1@example.com')
222 await waitJobs(server)
223 expect(emails).to.have.lengthOf(7)
225 const email = emails[6]
227 expect(email['from'][0]['address']).equal('test-admin@localhost')
228 expect(email['to'][0]['address']).equal('user_1@example.com')
229 expect(email['subject']).contains('Verify')
231 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
232 expect(verificationStringMatches).not.to.be.null
234 verificationString = verificationStringMatches[1]
235 expect(verificationString).to.not.be.undefined
236 expect(verificationString).to.have.length.above(2)
238 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
239 expect(userIdMatches).not.to.be.null
241 userId = parseInt(userIdMatches[1], 10)
244 it('Should not verify the email with an invalid verification string', async function () {
245 await verifyEmail(server.url, userId, verificationString + 'b', 403)
248 it('Should verify the email', async function () {
249 await verifyEmail(server.url, userId, verificationString)
253 after(async function () {
254 killallServers([ server ])