1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { cleanupTests, createSingleServer, MockSmtpServer, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
6 import { HttpStatusCode } from '@shared/models'
8 const expect = chai.expect
10 describe('Test users account verification', function () {
11 let server: PeerTubeServer
13 let userAccessToken: string
14 let verificationString: string
15 let expectedEmailsLength = 0
18 password: 'super password'
22 password: 'super password'
24 const emails: object[] = []
26 before(async function () {
29 const port = await MockSmtpServer.Instance.collectEmails(emails)
31 const overrideConfig = {
33 hostname: 'localhost',
37 server = await createSingleServer(1, overrideConfig)
39 await setAccessTokensToServers([ server ])
42 it('Should register user and send verification email if verification required', async function () {
45 await server.config.updateCustomSubConfig({
49 requiresEmailVerification: true,
55 await server.users.register(user1)
57 await waitJobs(server)
58 expectedEmailsLength++
59 expect(emails).to.have.lengthOf(expectedEmailsLength)
61 const email = emails[expectedEmailsLength - 1]
63 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
64 expect(verificationStringMatches).not.to.be.null
66 verificationString = verificationStringMatches[1]
67 expect(verificationString).to.have.length.above(2)
69 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
70 expect(userIdMatches).not.to.be.null
72 userId = parseInt(userIdMatches[1], 10)
74 const body = await server.users.get({ userId })
75 expect(body.emailVerified).to.be.false
78 it('Should not allow login for user with unverified email', async function () {
79 const { detail } = await server.login.login({ user: user1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
80 expect(detail).to.contain('User email is not verified.')
83 it('Should verify the user via email and allow login', async function () {
84 await server.users.verifyEmail({ userId, verificationString })
86 const body = await server.login.login({ user: user1 })
87 userAccessToken = body.access_token
89 const user = await server.users.get({ userId })
90 expect(user.emailVerified).to.be.true
93 it('Should be able to change the user email', async function () {
96 let updateVerificationString: string
99 await server.users.updateMe({
100 token: userAccessToken,
101 email: 'updated@example.com',
102 currentPassword: user1.password
105 await waitJobs(server)
106 expectedEmailsLength++
107 expect(emails).to.have.lengthOf(expectedEmailsLength)
109 const email = emails[expectedEmailsLength - 1]
111 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
112 updateVerificationString = verificationStringMatches[1]
116 const me = await server.users.getMyInfo({ token: userAccessToken })
117 expect(me.email).to.equal('user_1@example.com')
118 expect(me.pendingEmail).to.equal('updated@example.com')
122 await server.users.verifyEmail({ userId, verificationString: updateVerificationString, isPendingEmail: true })
124 const me = await server.users.getMyInfo({ token: userAccessToken })
125 expect(me.email).to.equal('updated@example.com')
126 expect(me.pendingEmail).to.be.null
130 it('Should register user not requiring email verification if setting not enabled', async function () {
132 await server.config.updateCustomSubConfig({
136 requiresEmailVerification: false,
142 await server.users.register(user2)
144 await waitJobs(server)
145 expect(emails).to.have.lengthOf(expectedEmailsLength)
147 const accessToken = await server.login.getAccessToken(user2)
149 const user = await server.users.getMyInfo({ token: accessToken })
150 expect(user.emailVerified).to.be.null
153 it('Should allow login for user with unverified email when setting later enabled', async function () {
154 await server.config.updateCustomSubConfig({
158 requiresEmailVerification: true,
164 await server.login.getAccessToken(user2)
167 after(async function () {
168 MockSmtpServer.Instance.kill()
170 await cleanupTests([ server ])