1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
6 registerUser, flushTests, getUserInformation, getMyUserInformation, killallServers,
7 userLogin, login, runServer, ServerInfo, verifyEmail, updateCustomSubConfig
9 import { setAccessTokensToServers } from '../../utils/users/login'
10 import { mockSmtpServer } from '../../utils/miscs/email'
11 import { waitJobs } from '../../utils/server/jobs'
13 const expect = chai.expect
15 describe('Test users account verification', function () {
16 let server: ServerInfo
18 let verificationString: string
19 let expectedEmailsLength = 0
22 password: 'super password'
26 password: 'super password'
28 const emails: object[] = []
30 before(async function () {
33 await mockSmtpServer(emails)
37 const overrideConfig = {
42 server = await runServer(1, overrideConfig)
44 await setAccessTokensToServers([ server ])
47 it('Should register user and send verification email if verification required', async function () {
49 await updateCustomSubConfig(server.url, server.accessToken, {
52 requiresEmailVerification: true,
57 await registerUser(server.url, user1.username, user1.password)
59 await waitJobs(server)
60 expectedEmailsLength++
61 expect(emails).to.have.lengthOf(expectedEmailsLength)
63 const email = emails[expectedEmailsLength - 1]
65 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
66 expect(verificationStringMatches).not.to.be.null
68 verificationString = verificationStringMatches[1]
69 expect(verificationString).to.have.length.above(2)
71 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
72 expect(userIdMatches).not.to.be.null
74 userId = parseInt(userIdMatches[1], 10)
76 const resUserInfo = await getUserInformation(server.url, server.accessToken, userId)
77 expect(resUserInfo.body.emailVerified).to.be.false
80 it('Should not allow login for user with unverified email', async function () {
81 const resLogin = await login(server.url, server.client, user1, 400)
82 expect(resLogin.body.error).to.contain('User email is not verified.')
85 it('Should verify the user via email and allow login', async function () {
86 await verifyEmail(server.url, userId, verificationString)
87 await login(server.url, server.client, user1)
88 const resUserVerified = await getUserInformation(server.url, server.accessToken, userId)
89 expect(resUserVerified.body.emailVerified).to.be.true
92 it('Should register user not requiring email verification if setting not enabled', async function () {
94 await updateCustomSubConfig(server.url, server.accessToken, {
97 requiresEmailVerification: false,
102 await registerUser(server.url, user2.username, user2.password)
104 await waitJobs(server)
105 expect(emails).to.have.lengthOf(expectedEmailsLength)
107 const accessToken = await userLogin(server, user2)
109 const resMyUserInfo = await getMyUserInformation(server.url, accessToken)
110 expect(resMyUserInfo.body.emailVerified).to.be.null
113 it('Should allow login for user with unverified email when setting later enabled', async function () {
114 await updateCustomSubConfig(server.url, server.accessToken, {
117 requiresEmailVerification: true,
122 await userLogin(server, user2)
125 after(async function () {
126 killallServers([ server ])
128 // Keep the logs if the test failed