]>
Commit | Line | Data |
---|---|---|
f076daa7 C |
1 | /* tslint:disable:no-unused-expression */ |
2 | ||
3 | import * as chai from 'chai' | |
4 | import 'mocha' | |
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' | |
8 | ||
9 | const expect = chai.expect | |
10 | ||
11 | describe('Test emails', function () { | |
12 | let server: ServerInfo | |
13 | let userId: number | |
14 | let verificationString: string | |
15 | const emails: object[] = [] | |
16 | const user = { | |
17 | username: 'user_1', | |
18 | password: 'super_password' | |
19 | } | |
20 | ||
21 | before(async function () { | |
22 | this.timeout(30000) | |
23 | ||
24 | await mockSmtpServer(emails) | |
25 | ||
26 | await flushTests() | |
27 | ||
28 | const overrideConfig = { | |
29 | smtp: { | |
30 | hostname: 'localhost' | |
31 | } | |
32 | } | |
33 | server = await runServer(1, overrideConfig) | |
34 | ||
35 | await wait(5000) | |
36 | await setAccessTokensToServers([ server ]) | |
37 | ||
38 | const res = await createUser(server.url, server.accessToken, user.username, user.password) | |
39 | userId = res.body.user.id | |
40 | }) | |
41 | ||
42 | describe('When resetting user password', function () { | |
43 | ||
44 | it('Should ask to reset the password', async function () { | |
45 | this.timeout(10000) | |
46 | ||
47 | await askResetPassword(server.url, 'user_1@example.com') | |
48 | ||
49 | await wait(3000) | |
50 | expect(emails).to.have.lengthOf(1) | |
51 | ||
52 | const email = emails[0] | |
53 | ||
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') | |
57 | ||
58 | const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text']) | |
59 | expect(verificationStringMatches).not.to.be.null | |
60 | ||
61 | verificationString = verificationStringMatches[1] | |
62 | expect(verificationString).to.have.length.above(2) | |
63 | ||
64 | const userIdMatches = /userId=([0-9]+)/.exec(email['text']) | |
65 | expect(userIdMatches).not.to.be.null | |
66 | ||
67 | userId = parseInt(userIdMatches[1], 10) | |
68 | expect(verificationString).to.not.be.undefined | |
69 | }) | |
70 | ||
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) | |
73 | }) | |
74 | ||
75 | it('Should reset the password', async function () { | |
76 | await resetPassword(server.url, userId, verificationString, 'super_password2') | |
77 | }) | |
78 | ||
79 | it('Should login with this new password', async function () { | |
80 | user.password = 'super_password2' | |
81 | ||
82 | await userLogin(server, user) | |
83 | }) | |
84 | }) | |
85 | ||
86 | after(async function () { | |
87 | killallServers([ server ]) | |
88 | ||
89 | // Keep the logs if the test failed | |
90 | if (this['ok']) { | |
91 | await flushTests() | |
92 | } | |
93 | }) | |
94 | }) |