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