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