1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
17 } from '../../../../shared/extra-utils'
18 import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
19 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
20 import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
21 import { User } from '../../../../shared/models/users'
23 const expect = chai.expect
25 describe('Test users account verification', function () {
26 let server: ServerInfo
28 let userAccessToken: string
29 let verificationString: string
30 let expectedEmailsLength = 0
33 password: 'super password'
37 password: 'super password'
39 const emails: object[] = []
41 before(async function () {
44 const port = await MockSmtpServer.Instance.collectEmails(emails)
46 const overrideConfig = {
48 hostname: 'localhost',
52 server = await flushAndRunServer(1, overrideConfig)
54 await setAccessTokensToServers([ server ])
57 it('Should register user and send verification email if verification required', async function () {
60 await server.configCommand.updateCustomSubConfig({
64 requiresEmailVerification: true,
70 await registerUser(server.url, user1.username, user1.password)
72 await waitJobs(server)
73 expectedEmailsLength++
74 expect(emails).to.have.lengthOf(expectedEmailsLength)
76 const email = emails[expectedEmailsLength - 1]
78 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
79 expect(verificationStringMatches).not.to.be.null
81 verificationString = verificationStringMatches[1]
82 expect(verificationString).to.have.length.above(2)
84 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
85 expect(userIdMatches).not.to.be.null
87 userId = parseInt(userIdMatches[1], 10)
89 const resUserInfo = await getUserInformation(server.url, server.accessToken, userId)
90 expect(resUserInfo.body.emailVerified).to.be.false
93 it('Should not allow login for user with unverified email', async function () {
94 const resLogin = await login(server.url, server.client, user1, HttpStatusCode.BAD_REQUEST_400)
95 expect(resLogin.body.detail).to.contain('User email is not verified.')
98 it('Should verify the user via email and allow login', async function () {
99 await verifyEmail(server.url, userId, verificationString)
101 const res = await login(server.url, server.client, user1)
102 userAccessToken = res.body.access_token
104 const resUserVerified = await getUserInformation(server.url, server.accessToken, userId)
105 expect(resUserVerified.body.emailVerified).to.be.true
108 it('Should be able to change the user email', async function () {
111 let updateVerificationString: string
116 accessToken: userAccessToken,
117 email: 'updated@example.com',
118 currentPassword: user1.password
121 await waitJobs(server)
122 expectedEmailsLength++
123 expect(emails).to.have.lengthOf(expectedEmailsLength)
125 const email = emails[expectedEmailsLength - 1]
127 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
128 updateVerificationString = verificationStringMatches[1]
132 const res = await getMyUserInformation(server.url, userAccessToken)
133 const me: User = res.body
135 expect(me.email).to.equal('user_1@example.com')
136 expect(me.pendingEmail).to.equal('updated@example.com')
140 await verifyEmail(server.url, userId, updateVerificationString, true)
142 const res = await getMyUserInformation(server.url, userAccessToken)
143 const me: User = res.body
145 expect(me.email).to.equal('updated@example.com')
146 expect(me.pendingEmail).to.be.null
150 it('Should register user not requiring email verification if setting not enabled', async function () {
152 await server.configCommand.updateCustomSubConfig({
156 requiresEmailVerification: false,
162 await registerUser(server.url, user2.username, user2.password)
164 await waitJobs(server)
165 expect(emails).to.have.lengthOf(expectedEmailsLength)
167 const accessToken = await userLogin(server, user2)
169 const resMyUserInfo = await getMyUserInformation(server.url, accessToken)
170 expect(resMyUserInfo.body.emailVerified).to.be.null
173 it('Should allow login for user with unverified email when setting later enabled', async function () {
174 await server.configCommand.updateCustomSubConfig({
178 requiresEmailVerification: true,
184 await userLogin(server, user2)
187 after(async function () {
188 MockSmtpServer.Instance.kill()
190 await cleanupTests([ server ])