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