]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
f076daa7 | 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' | |
f076daa7 | 14 | |
f076daa7 | 15 | describe('Test emails', function () { |
254d3579 | 16 | let server: PeerTubeServer |
f076daa7 | 17 | let userId: number |
45f1bd72 | 18 | let userId2: number |
26b7305a | 19 | let userAccessToken: string |
4f32032f | 20 | |
29837f88 | 21 | let videoShortUUID: string |
4f32032f C |
22 | let videoId: number |
23 | ||
26b7305a | 24 | let videoUserUUID: string |
4f32032f | 25 | |
f076daa7 | 26 | let verificationString: string |
45f1bd72 | 27 | let verificationString2: string |
4f32032f | 28 | |
f076daa7 C |
29 | const emails: object[] = [] |
30 | const user = { | |
31 | username: 'user_1', | |
32 | password: 'super_password' | |
33 | } | |
34 | ||
35 | before(async function () { | |
81f14b91 | 36 | this.timeout(120000) |
f076daa7 | 37 | |
b379759f C |
38 | const emailPort = await MockSmtpServer.Instance.collectEmails(emails) |
39 | server = await createSingleServer(1, ConfigCommand.getEmailOverrideConfig(emailPort)) | |
f076daa7 | 40 | |
f076daa7 | 41 | await setAccessTokensToServers([ server ]) |
b379759f | 42 | await server.config.enableSignup(true) |
f076daa7 | 43 | |
ba75d268 | 44 | { |
89d241a7 | 45 | const created = await server.users.create({ username: user.username, password: user.password }) |
7926c5f9 | 46 | userId = created.id |
26b7305a | 47 | |
89d241a7 | 48 | userAccessToken = await server.login.getAccessToken(user) |
26b7305a C |
49 | } |
50 | ||
51 | { | |
d23dd9fb | 52 | const attributes = { name: 'my super user video' } |
89d241a7 | 53 | const { uuid } = await server.videos.upload({ token: userAccessToken, attributes }) |
d23dd9fb | 54 | videoUserUUID = uuid |
ba75d268 C |
55 | } |
56 | ||
57 | { | |
58 | const attributes = { | |
59 | name: 'my super name' | |
60 | } | |
29837f88 C |
61 | const { shortUUID, id } = await server.videos.upload({ attributes }) |
62 | videoShortUUID = shortUUID | |
d23dd9fb | 63 | videoId = id |
ba75d268 | 64 | } |
f076daa7 C |
65 | }) |
66 | ||
67 | describe('When resetting user password', function () { | |
68 | ||
69 | it('Should ask to reset the password', async function () { | |
89d241a7 | 70 | await server.users.askResetPassword({ email: 'user_1@example.com' }) |
f076daa7 | 71 | |
3cd0734f | 72 | await waitJobs(server) |
f076daa7 C |
73 | expect(emails).to.have.lengthOf(1) |
74 | ||
75 | const email = emails[0] | |
76 | ||
113d4a3f | 77 | expect(email['from'][0]['name']).equal('PeerTube') |
2732eeff | 78 | expect(email['from'][0]['address']).equal('test-admin@127.0.0.1') |
f076daa7 C |
79 | expect(email['to'][0]['address']).equal('user_1@example.com') |
80 | expect(email['subject']).contains('password') | |
81 | ||
82 | const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text']) | |
83 | expect(verificationStringMatches).not.to.be.null | |
84 | ||
85 | verificationString = verificationStringMatches[1] | |
86 | expect(verificationString).to.have.length.above(2) | |
87 | ||
88 | const userIdMatches = /userId=([0-9]+)/.exec(email['text']) | |
89 | expect(userIdMatches).not.to.be.null | |
90 | ||
91 | userId = parseInt(userIdMatches[1], 10) | |
92 | expect(verificationString).to.not.be.undefined | |
93 | }) | |
94 | ||
95 | it('Should not reset the password with an invalid verification string', async function () { | |
89d241a7 | 96 | await server.users.resetPassword({ |
7926c5f9 C |
97 | userId, |
98 | verificationString: verificationString + 'b', | |
99 | password: 'super_password2', | |
100 | expectedStatus: HttpStatusCode.FORBIDDEN_403 | |
101 | }) | |
f076daa7 C |
102 | }) |
103 | ||
104 | it('Should reset the password', async function () { | |
89d241a7 | 105 | await server.users.resetPassword({ userId, verificationString, password: 'super_password2' }) |
f076daa7 C |
106 | }) |
107 | ||
e9c5f123 | 108 | it('Should not reset the password with the same verification string', async function () { |
89d241a7 | 109 | await server.users.resetPassword({ |
7926c5f9 C |
110 | userId, |
111 | verificationString, | |
112 | password: 'super_password3', | |
113 | expectedStatus: HttpStatusCode.FORBIDDEN_403 | |
114 | }) | |
e9c5f123 C |
115 | }) |
116 | ||
f076daa7 C |
117 | it('Should login with this new password', async function () { |
118 | user.password = 'super_password2' | |
119 | ||
89d241a7 | 120 | await server.login.getAccessToken(user) |
f076daa7 C |
121 | }) |
122 | }) | |
123 | ||
45f1bd72 | 124 | describe('When creating a user without password', function () { |
7926c5f9 | 125 | |
45f1bd72 JL |
126 | it('Should send a create password email', async function () { |
127 | this.timeout(10000) | |
128 | ||
89d241a7 | 129 | await server.users.create({ username: 'create_password', password: '' }) |
45f1bd72 JL |
130 | |
131 | await waitJobs(server) | |
132 | expect(emails).to.have.lengthOf(2) | |
133 | ||
134 | const email = emails[1] | |
135 | ||
113d4a3f | 136 | expect(email['from'][0]['name']).equal('PeerTube') |
2732eeff | 137 | expect(email['from'][0]['address']).equal('test-admin@127.0.0.1') |
45f1bd72 JL |
138 | expect(email['to'][0]['address']).equal('create_password@example.com') |
139 | expect(email['subject']).contains('account') | |
140 | expect(email['subject']).contains('password') | |
141 | ||
142 | const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text']) | |
143 | expect(verificationStringMatches).not.to.be.null | |
144 | ||
145 | verificationString2 = verificationStringMatches[1] | |
146 | expect(verificationString2).to.have.length.above(2) | |
147 | ||
148 | const userIdMatches = /userId=([0-9]+)/.exec(email['text']) | |
149 | expect(userIdMatches).not.to.be.null | |
150 | ||
151 | userId2 = parseInt(userIdMatches[1], 10) | |
152 | }) | |
153 | ||
154 | it('Should not reset the password with an invalid verification string', async function () { | |
89d241a7 | 155 | await server.users.resetPassword({ |
7926c5f9 C |
156 | userId: userId2, |
157 | verificationString: verificationString2 + 'c', | |
158 | password: 'newly_created_password', | |
159 | expectedStatus: HttpStatusCode.FORBIDDEN_403 | |
160 | }) | |
45f1bd72 JL |
161 | }) |
162 | ||
163 | it('Should reset the password', async function () { | |
89d241a7 | 164 | await server.users.resetPassword({ |
7926c5f9 C |
165 | userId: userId2, |
166 | verificationString: verificationString2, | |
167 | password: 'newly_created_password' | |
168 | }) | |
45f1bd72 JL |
169 | }) |
170 | ||
171 | it('Should login with this new password', async function () { | |
89d241a7 | 172 | await server.login.getAccessToken({ |
45f1bd72 JL |
173 | username: 'create_password', |
174 | password: 'newly_created_password' | |
175 | }) | |
176 | }) | |
177 | }) | |
178 | ||
310b5219 | 179 | describe('When creating an abuse', function () { |
29837f88 | 180 | |
ba75d268 C |
181 | it('Should send the notification email', async function () { |
182 | this.timeout(10000) | |
183 | ||
184 | const reason = 'my super bad reason' | |
0fbc0dec | 185 | await server.abuses.report({ token: userAccessToken, videoId, reason }) |
ba75d268 | 186 | |
3cd0734f | 187 | await waitJobs(server) |
45f1bd72 | 188 | expect(emails).to.have.lengthOf(3) |
ba75d268 | 189 | |
45f1bd72 | 190 | const email = emails[2] |
ba75d268 | 191 | |
113d4a3f | 192 | expect(email['from'][0]['name']).equal('PeerTube') |
2732eeff | 193 | expect(email['from'][0]['address']).equal('test-admin@127.0.0.1') |
48f07b4a | 194 | expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com') |
ba75d268 | 195 | expect(email['subject']).contains('abuse') |
29837f88 | 196 | expect(email['text']).contains(videoShortUUID) |
ba75d268 C |
197 | }) |
198 | }) | |
199 | ||
9b39106d C |
200 | describe('When blocking/unblocking user', function () { |
201 | ||
eacb25c4 C |
202 | it('Should send the notification email when blocking a user', async function () { |
203 | this.timeout(10000) | |
204 | ||
205 | const reason = 'my super bad reason' | |
89d241a7 | 206 | await server.users.banUser({ userId, reason }) |
eacb25c4 C |
207 | |
208 | await waitJobs(server) | |
45f1bd72 | 209 | expect(emails).to.have.lengthOf(4) |
eacb25c4 | 210 | |
45f1bd72 | 211 | const email = emails[3] |
eacb25c4 | 212 | |
113d4a3f | 213 | expect(email['from'][0]['name']).equal('PeerTube') |
2732eeff | 214 | expect(email['from'][0]['address']).equal('test-admin@127.0.0.1') |
eacb25c4 C |
215 | expect(email['to'][0]['address']).equal('user_1@example.com') |
216 | expect(email['subject']).contains(' blocked') | |
217 | expect(email['text']).contains(' blocked') | |
113d4a3f | 218 | expect(email['text']).contains('bad reason') |
eacb25c4 C |
219 | }) |
220 | ||
221 | it('Should send the notification email when unblocking a user', async function () { | |
222 | this.timeout(10000) | |
223 | ||
89d241a7 | 224 | await server.users.unbanUser({ userId }) |
eacb25c4 C |
225 | |
226 | await waitJobs(server) | |
45f1bd72 | 227 | expect(emails).to.have.lengthOf(5) |
eacb25c4 | 228 | |
45f1bd72 | 229 | const email = emails[4] |
eacb25c4 | 230 | |
113d4a3f | 231 | expect(email['from'][0]['name']).equal('PeerTube') |
2732eeff | 232 | expect(email['from'][0]['address']).equal('test-admin@127.0.0.1') |
eacb25c4 C |
233 | expect(email['to'][0]['address']).equal('user_1@example.com') |
234 | expect(email['subject']).contains(' unblocked') | |
235 | expect(email['text']).contains(' unblocked') | |
236 | }) | |
237 | }) | |
238 | ||
26b7305a C |
239 | describe('When blacklisting a video', function () { |
240 | it('Should send the notification email', async function () { | |
241 | this.timeout(10000) | |
242 | ||
243 | const reason = 'my super reason' | |
89d241a7 | 244 | await server.blacklist.add({ videoId: videoUserUUID, reason }) |
26b7305a C |
245 | |
246 | await waitJobs(server) | |
45f1bd72 | 247 | expect(emails).to.have.lengthOf(6) |
26b7305a | 248 | |
45f1bd72 | 249 | const email = emails[5] |
26b7305a | 250 | |
113d4a3f | 251 | expect(email['from'][0]['name']).equal('PeerTube') |
2732eeff | 252 | expect(email['from'][0]['address']).equal('test-admin@127.0.0.1') |
26b7305a C |
253 | expect(email['to'][0]['address']).equal('user_1@example.com') |
254 | expect(email['subject']).contains(' blacklisted') | |
255 | expect(email['text']).contains('my super user video') | |
256 | expect(email['text']).contains('my super reason') | |
257 | }) | |
258 | ||
259 | it('Should send the notification email', async function () { | |
260 | this.timeout(10000) | |
261 | ||
89d241a7 | 262 | await server.blacklist.remove({ videoId: videoUserUUID }) |
26b7305a C |
263 | |
264 | await waitJobs(server) | |
45f1bd72 | 265 | expect(emails).to.have.lengthOf(7) |
26b7305a | 266 | |
45f1bd72 | 267 | const email = emails[6] |
26b7305a | 268 | |
113d4a3f | 269 | expect(email['from'][0]['name']).equal('PeerTube') |
2732eeff | 270 | expect(email['from'][0]['address']).equal('test-admin@127.0.0.1') |
26b7305a C |
271 | expect(email['to'][0]['address']).equal('user_1@example.com') |
272 | expect(email['subject']).contains(' unblacklisted') | |
273 | expect(email['text']).contains('my super user video') | |
274 | }) | |
b9cf3fb6 C |
275 | |
276 | it('Should have the manage preferences link in the email', async function () { | |
277 | const email = emails[6] | |
278 | expect(email['text']).to.contain('Manage your notification preferences') | |
279 | }) | |
26b7305a C |
280 | }) |
281 | ||
d9eaee39 JM |
282 | describe('When verifying a user email', function () { |
283 | ||
284 | it('Should ask to send the verification email', async function () { | |
285 | this.timeout(10000) | |
286 | ||
89d241a7 | 287 | await server.users.askSendVerifyEmail({ email: 'user_1@example.com' }) |
d9eaee39 JM |
288 | |
289 | await waitJobs(server) | |
45f1bd72 | 290 | expect(emails).to.have.lengthOf(8) |
d9eaee39 | 291 | |
45f1bd72 | 292 | const email = emails[7] |
d9eaee39 | 293 | |
113d4a3f | 294 | expect(email['from'][0]['name']).equal('PeerTube') |
2732eeff | 295 | expect(email['from'][0]['address']).equal('test-admin@127.0.0.1') |
d9eaee39 JM |
296 | expect(email['to'][0]['address']).equal('user_1@example.com') |
297 | expect(email['subject']).contains('Verify') | |
298 | ||
299 | const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text']) | |
300 | expect(verificationStringMatches).not.to.be.null | |
301 | ||
302 | verificationString = verificationStringMatches[1] | |
303 | expect(verificationString).to.not.be.undefined | |
304 | expect(verificationString).to.have.length.above(2) | |
305 | ||
306 | const userIdMatches = /userId=([0-9]+)/.exec(email['text']) | |
307 | expect(userIdMatches).not.to.be.null | |
308 | ||
309 | userId = parseInt(userIdMatches[1], 10) | |
310 | }) | |
311 | ||
312 | it('Should not verify the email with an invalid verification string', async function () { | |
89d241a7 | 313 | await server.users.verifyEmail({ |
7926c5f9 C |
314 | userId, |
315 | verificationString: verificationString + 'b', | |
316 | isPendingEmail: false, | |
317 | expectedStatus: HttpStatusCode.FORBIDDEN_403 | |
318 | }) | |
d9eaee39 JM |
319 | }) |
320 | ||
321 | it('Should verify the email', async function () { | |
89d241a7 | 322 | await server.users.verifyEmail({ userId, verificationString }) |
d9eaee39 JM |
323 | }) |
324 | }) | |
325 | ||
b379759f C |
326 | describe('When verifying a registration email', function () { |
327 | let registrationId: number | |
328 | let registrationIdEmail: number | |
329 | ||
330 | before(async function () { | |
331 | const { id } = await server.registrations.requestRegistration({ | |
332 | username: 'request_1', | |
333 | email: 'request_1@example.com', | |
334 | registrationReason: 'tt' | |
335 | }) | |
336 | registrationId = id | |
337 | }) | |
338 | ||
339 | it('Should ask to send the verification email', async function () { | |
340 | this.timeout(10000) | |
341 | ||
342 | await server.registrations.askSendVerifyEmail({ email: 'request_1@example.com' }) | |
343 | ||
344 | await waitJobs(server) | |
345 | expect(emails).to.have.lengthOf(9) | |
346 | ||
347 | const email = emails[8] | |
348 | ||
349 | expect(email['from'][0]['name']).equal('PeerTube') | |
350 | expect(email['from'][0]['address']).equal('test-admin@127.0.0.1') | |
351 | expect(email['to'][0]['address']).equal('request_1@example.com') | |
352 | expect(email['subject']).contains('Verify') | |
353 | ||
354 | const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text']) | |
355 | expect(verificationStringMatches).not.to.be.null | |
356 | ||
357 | verificationString = verificationStringMatches[1] | |
358 | expect(verificationString).to.not.be.undefined | |
359 | expect(verificationString).to.have.length.above(2) | |
360 | ||
361 | const registrationIdMatches = /registrationId=([0-9]+)/.exec(email['text']) | |
362 | expect(registrationIdMatches).not.to.be.null | |
363 | ||
364 | registrationIdEmail = parseInt(registrationIdMatches[1], 10) | |
365 | ||
366 | expect(registrationId).to.equal(registrationIdEmail) | |
367 | }) | |
368 | ||
369 | it('Should not verify the email with an invalid verification string', async function () { | |
370 | await server.registrations.verifyEmail({ | |
371 | registrationId: registrationIdEmail, | |
372 | verificationString: verificationString + 'b', | |
373 | expectedStatus: HttpStatusCode.FORBIDDEN_403 | |
374 | }) | |
375 | }) | |
376 | ||
377 | it('Should verify the email', async function () { | |
378 | await server.registrations.verifyEmail({ registrationId: registrationIdEmail, verificationString }) | |
379 | }) | |
380 | }) | |
381 | ||
7c3b7976 | 382 | after(async function () { |
89ada4e2 | 383 | MockSmtpServer.Instance.kill() |
7c3b7976 C |
384 | |
385 | await cleanupTests([ server ]) | |
f076daa7 C |
386 | }) |
387 | }) |