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