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