]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import * as chai from 'chai' | |
4 | import 'mocha' | |
5 | import { | |
6 | cleanupTests, | |
7 | flushAndRunServer, | |
8 | getMyUserInformation, | |
9 | getUserInformation, | |
10 | login, | |
11 | registerUser, | |
12 | ServerInfo, | |
13 | updateCustomSubConfig, | |
14 | updateMyUser, | |
15 | userLogin, | |
16 | verifyEmail | |
17 | } from '../../../../shared/extra-utils' | |
18 | import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login' | |
19 | import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email' | |
20 | import { waitJobs } from '../../../../shared/extra-utils/server/jobs' | |
21 | import { User } from '../../../../shared/models/users' | |
22 | ||
23 | const expect = chai.expect | |
24 | ||
25 | describe('Test users account verification', function () { | |
26 | let server: ServerInfo | |
27 | let userId: number | |
28 | let userAccessToken: string | |
29 | let verificationString: string | |
30 | let expectedEmailsLength = 0 | |
31 | const user1 = { | |
32 | username: 'user_1', | |
33 | password: 'super password' | |
34 | } | |
35 | const user2 = { | |
36 | username: 'user_2', | |
37 | password: 'super password' | |
38 | } | |
39 | const emails: object[] = [] | |
40 | ||
41 | before(async function () { | |
42 | this.timeout(30000) | |
43 | ||
44 | const port = await MockSmtpServer.Instance.collectEmails(emails) | |
45 | ||
46 | const overrideConfig = { | |
47 | smtp: { | |
48 | hostname: 'localhost', | |
49 | port | |
50 | } | |
51 | } | |
52 | server = await flushAndRunServer(1, overrideConfig) | |
53 | ||
54 | await setAccessTokensToServers([ server ]) | |
55 | }) | |
56 | ||
57 | it('Should register user and send verification email if verification required', async function () { | |
58 | this.timeout(5000) | |
59 | await updateCustomSubConfig(server.url, server.accessToken, { | |
60 | signup: { | |
61 | enabled: true, | |
62 | requiresEmailVerification: true, | |
63 | limit: 10 | |
64 | } | |
65 | }) | |
66 | ||
67 | await registerUser(server.url, user1.username, user1.password) | |
68 | ||
69 | await waitJobs(server) | |
70 | expectedEmailsLength++ | |
71 | expect(emails).to.have.lengthOf(expectedEmailsLength) | |
72 | ||
73 | const email = emails[expectedEmailsLength - 1] | |
74 | ||
75 | const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text']) | |
76 | expect(verificationStringMatches).not.to.be.null | |
77 | ||
78 | verificationString = verificationStringMatches[1] | |
79 | expect(verificationString).to.have.length.above(2) | |
80 | ||
81 | const userIdMatches = /userId=([0-9]+)/.exec(email['text']) | |
82 | expect(userIdMatches).not.to.be.null | |
83 | ||
84 | userId = parseInt(userIdMatches[1], 10) | |
85 | ||
86 | const resUserInfo = await getUserInformation(server.url, server.accessToken, userId) | |
87 | expect(resUserInfo.body.emailVerified).to.be.false | |
88 | }) | |
89 | ||
90 | it('Should not allow login for user with unverified email', async function () { | |
91 | const resLogin = await login(server.url, server.client, user1, 400) | |
92 | expect(resLogin.body.error).to.contain('User email is not verified.') | |
93 | }) | |
94 | ||
95 | it('Should verify the user via email and allow login', async function () { | |
96 | await verifyEmail(server.url, userId, verificationString) | |
97 | ||
98 | const res = await login(server.url, server.client, user1) | |
99 | userAccessToken = res.body.access_token | |
100 | ||
101 | const resUserVerified = await getUserInformation(server.url, server.accessToken, userId) | |
102 | expect(resUserVerified.body.emailVerified).to.be.true | |
103 | }) | |
104 | ||
105 | it('Should be able to change the user email', async function () { | |
106 | let updateVerificationString: string | |
107 | ||
108 | { | |
109 | await updateMyUser({ | |
110 | url: server.url, | |
111 | accessToken: userAccessToken, | |
112 | email: 'updated@example.com', | |
113 | currentPassword: user1.password | |
114 | }) | |
115 | ||
116 | await waitJobs(server) | |
117 | expectedEmailsLength++ | |
118 | expect(emails).to.have.lengthOf(expectedEmailsLength) | |
119 | ||
120 | const email = emails[expectedEmailsLength - 1] | |
121 | ||
122 | const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text']) | |
123 | updateVerificationString = verificationStringMatches[1] | |
124 | } | |
125 | ||
126 | { | |
127 | const res = await getMyUserInformation(server.url, userAccessToken) | |
128 | const me: User = res.body | |
129 | ||
130 | expect(me.email).to.equal('user_1@example.com') | |
131 | expect(me.pendingEmail).to.equal('updated@example.com') | |
132 | } | |
133 | ||
134 | { | |
135 | await verifyEmail(server.url, userId, updateVerificationString, true) | |
136 | ||
137 | const res = await getMyUserInformation(server.url, userAccessToken) | |
138 | const me: User = res.body | |
139 | ||
140 | expect(me.email).to.equal('updated@example.com') | |
141 | expect(me.pendingEmail).to.be.null | |
142 | } | |
143 | }) | |
144 | ||
145 | it('Should register user not requiring email verification if setting not enabled', async function () { | |
146 | this.timeout(5000) | |
147 | await updateCustomSubConfig(server.url, server.accessToken, { | |
148 | signup: { | |
149 | enabled: true, | |
150 | requiresEmailVerification: false, | |
151 | limit: 10 | |
152 | } | |
153 | }) | |
154 | ||
155 | await registerUser(server.url, user2.username, user2.password) | |
156 | ||
157 | await waitJobs(server) | |
158 | expect(emails).to.have.lengthOf(expectedEmailsLength) | |
159 | ||
160 | const accessToken = await userLogin(server, user2) | |
161 | ||
162 | const resMyUserInfo = await getMyUserInformation(server.url, accessToken) | |
163 | expect(resMyUserInfo.body.emailVerified).to.be.null | |
164 | }) | |
165 | ||
166 | it('Should allow login for user with unverified email when setting later enabled', async function () { | |
167 | await updateCustomSubConfig(server.url, server.accessToken, { | |
168 | signup: { | |
169 | enabled: true, | |
170 | requiresEmailVerification: true, | |
171 | limit: 10 | |
172 | } | |
173 | }) | |
174 | ||
175 | await userLogin(server, user2) | |
176 | }) | |
177 | ||
178 | after(async function () { | |
179 | MockSmtpServer.Instance.kill() | |
180 | ||
181 | await cleanupTests([ server ]) | |
182 | }) | |
183 | }) |