]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/users-verification.ts
Introduce config command
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users-verification.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
6 import {
7 cleanupTests,
8 flushAndRunServer,
9 getMyUserInformation,
10 getUserInformation,
11 login,
12 registerUser,
13 ServerInfo,
14 updateMyUser,
15 userLogin,
16 verifyEmail
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'
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(30000)
59
60 await server.configCommand.updateCustomSubConfig({
61 newConfig: {
62 signup: {
63 enabled: true,
64 requiresEmailVerification: true,
65 limit: 10
66 }
67 }
68 })
69
70 await registerUser(server.url, user1.username, user1.password)
71
72 await waitJobs(server)
73 expectedEmailsLength++
74 expect(emails).to.have.lengthOf(expectedEmailsLength)
75
76 const email = emails[expectedEmailsLength - 1]
77
78 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
79 expect(verificationStringMatches).not.to.be.null
80
81 verificationString = verificationStringMatches[1]
82 expect(verificationString).to.have.length.above(2)
83
84 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
85 expect(userIdMatches).not.to.be.null
86
87 userId = parseInt(userIdMatches[1], 10)
88
89 const resUserInfo = await getUserInformation(server.url, server.accessToken, userId)
90 expect(resUserInfo.body.emailVerified).to.be.false
91 })
92
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.')
96 })
97
98 it('Should verify the user via email and allow login', async function () {
99 await verifyEmail(server.url, userId, verificationString)
100
101 const res = await login(server.url, server.client, user1)
102 userAccessToken = res.body.access_token
103
104 const resUserVerified = await getUserInformation(server.url, server.accessToken, userId)
105 expect(resUserVerified.body.emailVerified).to.be.true
106 })
107
108 it('Should be able to change the user email', async function () {
109 this.timeout(10000)
110
111 let updateVerificationString: string
112
113 {
114 await updateMyUser({
115 url: server.url,
116 accessToken: userAccessToken,
117 email: 'updated@example.com',
118 currentPassword: user1.password
119 })
120
121 await waitJobs(server)
122 expectedEmailsLength++
123 expect(emails).to.have.lengthOf(expectedEmailsLength)
124
125 const email = emails[expectedEmailsLength - 1]
126
127 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
128 updateVerificationString = verificationStringMatches[1]
129 }
130
131 {
132 const res = await getMyUserInformation(server.url, userAccessToken)
133 const me: User = res.body
134
135 expect(me.email).to.equal('user_1@example.com')
136 expect(me.pendingEmail).to.equal('updated@example.com')
137 }
138
139 {
140 await verifyEmail(server.url, userId, updateVerificationString, true)
141
142 const res = await getMyUserInformation(server.url, userAccessToken)
143 const me: User = res.body
144
145 expect(me.email).to.equal('updated@example.com')
146 expect(me.pendingEmail).to.be.null
147 }
148 })
149
150 it('Should register user not requiring email verification if setting not enabled', async function () {
151 this.timeout(5000)
152 await server.configCommand.updateCustomSubConfig({
153 newConfig: {
154 signup: {
155 enabled: true,
156 requiresEmailVerification: false,
157 limit: 10
158 }
159 }
160 })
161
162 await registerUser(server.url, user2.username, user2.password)
163
164 await waitJobs(server)
165 expect(emails).to.have.lengthOf(expectedEmailsLength)
166
167 const accessToken = await userLogin(server, user2)
168
169 const resMyUserInfo = await getMyUserInformation(server.url, accessToken)
170 expect(resMyUserInfo.body.emailVerified).to.be.null
171 })
172
173 it('Should allow login for user with unverified email when setting later enabled', async function () {
174 await server.configCommand.updateCustomSubConfig({
175 newConfig: {
176 signup: {
177 enabled: true,
178 requiresEmailVerification: true,
179 limit: 10
180 }
181 }
182 })
183
184 await userLogin(server, user2)
185 })
186
187 after(async function () {
188 MockSmtpServer.Instance.kill()
189
190 await cleanupTests([ server ])
191 })
192 })