]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import 'mocha' | |
4 | import * as chai from 'chai' | |
5 | import { MockSmtpServer } from '@server/tests/shared' | |
6 | import { HttpStatusCode } from '@shared/models' | |
7 | import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands' | |
8 | ||
9 | const expect = chai.expect | |
10 | ||
11 | describe('Test users account verification', function () { | |
12 | let server: PeerTubeServer | |
13 | let userId: number | |
14 | let userAccessToken: string | |
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 | ||
30 | const port = await MockSmtpServer.Instance.collectEmails(emails) | |
31 | ||
32 | const overrideConfig = { | |
33 | smtp: { | |
34 | hostname: 'localhost', | |
35 | port | |
36 | } | |
37 | } | |
38 | server = await createSingleServer(1, overrideConfig) | |
39 | ||
40 | await setAccessTokensToServers([ server ]) | |
41 | }) | |
42 | ||
43 | it('Should register user and send verification email if verification required', async function () { | |
44 | this.timeout(30000) | |
45 | ||
46 | await server.config.updateCustomSubConfig({ | |
47 | newConfig: { | |
48 | signup: { | |
49 | enabled: true, | |
50 | requiresEmailVerification: true, | |
51 | limit: 10 | |
52 | } | |
53 | } | |
54 | }) | |
55 | ||
56 | await server.users.register(user1) | |
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 | ||
75 | const body = await server.users.get({ userId }) | |
76 | expect(body.emailVerified).to.be.false | |
77 | }) | |
78 | ||
79 | it('Should not allow login for user with unverified email', async function () { | |
80 | const { detail } = await server.login.login({ user: user1, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
81 | expect(detail).to.contain('User email is not verified.') | |
82 | }) | |
83 | ||
84 | it('Should verify the user via email and allow login', async function () { | |
85 | await server.users.verifyEmail({ userId, verificationString }) | |
86 | ||
87 | const body = await server.login.login({ user: user1 }) | |
88 | userAccessToken = body.access_token | |
89 | ||
90 | const user = await server.users.get({ userId }) | |
91 | expect(user.emailVerified).to.be.true | |
92 | }) | |
93 | ||
94 | it('Should be able to change the user email', async function () { | |
95 | this.timeout(10000) | |
96 | ||
97 | let updateVerificationString: string | |
98 | ||
99 | { | |
100 | await server.users.updateMe({ | |
101 | token: userAccessToken, | |
102 | email: 'updated@example.com', | |
103 | currentPassword: user1.password | |
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 | { | |
117 | const me = await server.users.getMyInfo({ token: userAccessToken }) | |
118 | expect(me.email).to.equal('user_1@example.com') | |
119 | expect(me.pendingEmail).to.equal('updated@example.com') | |
120 | } | |
121 | ||
122 | { | |
123 | await server.users.verifyEmail({ userId, verificationString: updateVerificationString, isPendingEmail: true }) | |
124 | ||
125 | const me = await server.users.getMyInfo({ token: userAccessToken }) | |
126 | expect(me.email).to.equal('updated@example.com') | |
127 | expect(me.pendingEmail).to.be.null | |
128 | } | |
129 | }) | |
130 | ||
131 | it('Should register user not requiring email verification if setting not enabled', async function () { | |
132 | this.timeout(5000) | |
133 | await server.config.updateCustomSubConfig({ | |
134 | newConfig: { | |
135 | signup: { | |
136 | enabled: true, | |
137 | requiresEmailVerification: false, | |
138 | limit: 10 | |
139 | } | |
140 | } | |
141 | }) | |
142 | ||
143 | await server.users.register(user2) | |
144 | ||
145 | await waitJobs(server) | |
146 | expect(emails).to.have.lengthOf(expectedEmailsLength) | |
147 | ||
148 | const accessToken = await server.login.getAccessToken(user2) | |
149 | ||
150 | const user = await server.users.getMyInfo({ token: accessToken }) | |
151 | expect(user.emailVerified).to.be.null | |
152 | }) | |
153 | ||
154 | it('Should allow login for user with unverified email when setting later enabled', async function () { | |
155 | await server.config.updateCustomSubConfig({ | |
156 | newConfig: { | |
157 | signup: { | |
158 | enabled: true, | |
159 | requiresEmailVerification: true, | |
160 | limit: 10 | |
161 | } | |
162 | } | |
163 | }) | |
164 | ||
165 | await server.login.getAccessToken(user2) | |
166 | }) | |
167 | ||
168 | after(async function () { | |
169 | MockSmtpServer.Instance.kill() | |
170 | ||
171 | await cleanupTests([ server ]) | |
172 | }) | |
173 | }) |