]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/users/users-verification.ts
Introduce videos command
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users-verification.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
d9eaee39 2
d9eaee39 3import 'mocha'
8ef9457f 4import * as chai from 'chai'
41d1d075 5import { HttpStatusCode } from '@shared/core-utils'
7926c5f9 6import { cleanupTests, flushAndRunServer, MockSmtpServer, ServerInfo, setAccessTokensToServers, waitJobs } from '@shared/extra-utils'
d9eaee39
JM
7
8const expect = chai.expect
9
10describe('Test users account verification', function () {
11 let server: ServerInfo
12 let userId: number
d1ab89de 13 let userAccessToken: string
d9eaee39
JM
14 let verificationString: string
15 let expectedEmailsLength = 0
16 const user1 = {
17 username: 'user_1',
18 password: 'super password'
19 }
20 const user2 = {
21 username: 'user_2',
22 password: 'super password'
23 }
24 const emails: object[] = []
25
26 before(async function () {
27 this.timeout(30000)
28
7243f84d 29 const port = await MockSmtpServer.Instance.collectEmails(emails)
d9eaee39 30
d9eaee39
JM
31 const overrideConfig = {
32 smtp: {
7243f84d
C
33 hostname: 'localhost',
34 port
d9eaee39
JM
35 }
36 }
210feb6c 37 server = await flushAndRunServer(1, overrideConfig)
d9eaee39
JM
38
39 await setAccessTokensToServers([ server ])
40 })
41
42 it('Should register user and send verification email if verification required', async function () {
59fd824c
C
43 this.timeout(30000)
44
65e6e260
C
45 await server.configCommand.updateCustomSubConfig({
46 newConfig: {
47 signup: {
48 enabled: true,
49 requiresEmailVerification: true,
50 limit: 10
51 }
d9eaee39
JM
52 }
53 })
54
7926c5f9 55 await server.usersCommand.register(user1)
d9eaee39
JM
56
57 await waitJobs(server)
58 expectedEmailsLength++
59 expect(emails).to.have.lengthOf(expectedEmailsLength)
60
61 const email = emails[expectedEmailsLength - 1]
62
63 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
64 expect(verificationStringMatches).not.to.be.null
65
66 verificationString = verificationStringMatches[1]
67 expect(verificationString).to.have.length.above(2)
68
69 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
70 expect(userIdMatches).not.to.be.null
71
72 userId = parseInt(userIdMatches[1], 10)
73
7926c5f9
C
74 const body = await server.usersCommand.get({ userId })
75 expect(body.emailVerified).to.be.false
d9eaee39
JM
76 })
77
78 it('Should not allow login for user with unverified email', async function () {
41d1d075
C
79 const { detail } = await server.loginCommand.login({ user: user1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
80 expect(detail).to.contain('User email is not verified.')
d9eaee39
JM
81 })
82
83 it('Should verify the user via email and allow login', async function () {
7926c5f9 84 await server.usersCommand.verifyEmail({ userId, verificationString })
d1ab89de 85
41d1d075
C
86 const body = await server.loginCommand.login({ user: user1 })
87 userAccessToken = body.access_token
d1ab89de 88
7926c5f9
C
89 const user = await server.usersCommand.get({ userId })
90 expect(user.emailVerified).to.be.true
d9eaee39
JM
91 })
92
d1ab89de 93 it('Should be able to change the user email', async function () {
2dbc170d
C
94 this.timeout(10000)
95
d1ab89de
C
96 let updateVerificationString: string
97
98 {
7926c5f9
C
99 await server.usersCommand.updateMe({
100 token: userAccessToken,
5efab546
C
101 email: 'updated@example.com',
102 currentPassword: user1.password
d1ab89de
C
103 })
104
105 await waitJobs(server)
106 expectedEmailsLength++
107 expect(emails).to.have.lengthOf(expectedEmailsLength)
108
109 const email = emails[expectedEmailsLength - 1]
110
111 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
112 updateVerificationString = verificationStringMatches[1]
113 }
114
115 {
7926c5f9 116 const me = await server.usersCommand.getMyInfo({ token: userAccessToken })
d1ab89de
C
117 expect(me.email).to.equal('user_1@example.com')
118 expect(me.pendingEmail).to.equal('updated@example.com')
119 }
120
121 {
7926c5f9 122 await server.usersCommand.verifyEmail({ userId, verificationString: updateVerificationString, isPendingEmail: true })
d1ab89de 123
7926c5f9 124 const me = await server.usersCommand.getMyInfo({ token: userAccessToken })
d1ab89de
C
125 expect(me.email).to.equal('updated@example.com')
126 expect(me.pendingEmail).to.be.null
127 }
128 })
129
d9eaee39
JM
130 it('Should register user not requiring email verification if setting not enabled', async function () {
131 this.timeout(5000)
65e6e260
C
132 await server.configCommand.updateCustomSubConfig({
133 newConfig: {
134 signup: {
135 enabled: true,
136 requiresEmailVerification: false,
137 limit: 10
138 }
d9eaee39
JM
139 }
140 })
141
7926c5f9 142 await server.usersCommand.register(user2)
d9eaee39
JM
143
144 await waitJobs(server)
145 expect(emails).to.have.lengthOf(expectedEmailsLength)
146
41d1d075 147 const accessToken = await server.loginCommand.getAccessToken(user2)
d9eaee39 148
7926c5f9
C
149 const user = await server.usersCommand.getMyInfo({ token: accessToken })
150 expect(user.emailVerified).to.be.null
d9eaee39
JM
151 })
152
153 it('Should allow login for user with unverified email when setting later enabled', async function () {
65e6e260
C
154 await server.configCommand.updateCustomSubConfig({
155 newConfig: {
156 signup: {
157 enabled: true,
158 requiresEmailVerification: true,
159 limit: 10
160 }
d9eaee39
JM
161 }
162 })
163
41d1d075 164 await server.loginCommand.getAccessToken(user2)
d9eaee39
JM
165 })
166
7c3b7976 167 after(async function () {
89ada4e2 168 MockSmtpServer.Instance.kill()
7c3b7976
C
169
170 await cleanupTests([ server ])
d9eaee39
JM
171 })
172})