1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { askResetPassword, createUser, resetPassword, runServer, userLogin, wait } from '../../utils'
6 import { flushTests, killallServers, ServerInfo, setAccessTokensToServers } from '../../utils/index'
7 import { mockSmtpServer } from '../../utils/miscs/email'
9 const expect = chai.expect
11 describe('Test emails', function () {
12 let server: ServerInfo
14 let verificationString: string
15 const emails: object[] = []
18 password: 'super_password'
21 before(async function () {
24 await mockSmtpServer(emails)
28 const overrideConfig = {
33 server = await runServer(1, overrideConfig)
36 await setAccessTokensToServers([ server ])
38 const res = await createUser(server.url, server.accessToken, user.username, user.password)
39 userId = res.body.user.id
42 describe('When resetting user password', function () {
44 it('Should ask to reset the password', async function () {
47 await askResetPassword(server.url, 'user_1@example.com')
50 expect(emails).to.have.lengthOf(1)
52 const email = emails[0]
54 expect(email['from'][0]['address']).equal('test-admin@localhost')
55 expect(email['to'][0]['address']).equal('user_1@example.com')
56 expect(email['subject']).contains('password')
58 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
59 expect(verificationStringMatches).not.to.be.null
61 verificationString = verificationStringMatches[1]
62 expect(verificationString).to.have.length.above(2)
64 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
65 expect(userIdMatches).not.to.be.null
67 userId = parseInt(userIdMatches[1], 10)
68 expect(verificationString).to.not.be.undefined
71 it('Should not reset the password with an invalid verification string', async function () {
72 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
75 it('Should reset the password', async function () {
76 await resetPassword(server.url, userId, verificationString, 'super_password2')
79 it('Should login with this new password', async function () {
80 user.password = 'super_password2'
82 await userLogin(server, user)
86 after(async function () {
87 killallServers([ server ])
89 // Keep the logs if the test failed