]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/email.ts
Merge branch 'release/2.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
f076daa7 3import 'mocha'
4f32032f 4import * as chai from 'chai'
eacb25c4 5import {
26b7305a 6 addVideoToBlacklist,
eacb25c4 7 askResetPassword,
d9eaee39 8 askSendVerifyEmail,
eacb25c4 9 blockUser,
7243f84d
C
10 cleanupTests,
11 createUser,
12 flushAndRunServer,
13 removeVideoFromBlacklist,
4f32032f 14 reportAbuse,
eacb25c4 15 resetPassword,
7243f84d
C
16 ServerInfo,
17 setAccessTokensToServers,
eacb25c4
C
18 unblockUser,
19 uploadVideo,
d9eaee39 20 userLogin,
7243f84d 21 verifyEmail
94565d52
C
22} from '../../../../shared/extra-utils'
23import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
24import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
f076daa7
C
25
26const expect = chai.expect
27
28describe('Test emails', function () {
29 let server: ServerInfo
30 let userId: number
45f1bd72 31 let userId2: number
26b7305a 32 let userAccessToken: string
4f32032f 33
ba75d268 34 let videoUUID: string
4f32032f
C
35 let videoId: number
36
26b7305a 37 let videoUserUUID: string
4f32032f 38
f076daa7 39 let verificationString: string
45f1bd72 40 let verificationString2: string
4f32032f 41
f076daa7
C
42 const emails: object[] = []
43 const user = {
44 username: 'user_1',
45 password: 'super_password'
46 }
48f07b4a 47 let emailPort: number
f076daa7
C
48
49 before(async function () {
2284f202 50 this.timeout(50000)
f076daa7 51
48f07b4a 52 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
f076daa7 53
f076daa7
C
54 const overrideConfig = {
55 smtp: {
48f07b4a
C
56 hostname: 'localhost',
57 port: emailPort
f076daa7
C
58 }
59 }
210feb6c 60 server = await flushAndRunServer(1, overrideConfig)
f076daa7
C
61 await setAccessTokensToServers([ server ])
62
ba75d268 63 {
1eddc9a7 64 const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
ba75d268 65 userId = res.body.user.id
26b7305a
C
66
67 userAccessToken = await userLogin(server, user)
68 }
69
70 {
71 const attributes = {
72 name: 'my super user video'
73 }
74 const res = await uploadVideo(server.url, userAccessToken, attributes)
75 videoUserUUID = res.body.video.uuid
ba75d268
C
76 }
77
78 {
79 const attributes = {
80 name: 'my super name'
81 }
82 const res = await uploadVideo(server.url, server.accessToken, attributes)
83 videoUUID = res.body.video.uuid
4f32032f 84 videoId = res.body.video.id
ba75d268 85 }
f076daa7
C
86 })
87
88 describe('When resetting user password', function () {
89
90 it('Should ask to reset the password', async function () {
91 this.timeout(10000)
92
93 await askResetPassword(server.url, 'user_1@example.com')
94
3cd0734f 95 await waitJobs(server)
f076daa7
C
96 expect(emails).to.have.lengthOf(1)
97
98 const email = emails[0]
99
7243f84d 100 expect(email['from'][0]['name']).equal('localhost:' + server.port)
f076daa7
C
101 expect(email['from'][0]['address']).equal('test-admin@localhost')
102 expect(email['to'][0]['address']).equal('user_1@example.com')
103 expect(email['subject']).contains('password')
104
105 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
106 expect(verificationStringMatches).not.to.be.null
107
108 verificationString = verificationStringMatches[1]
109 expect(verificationString).to.have.length.above(2)
110
111 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
112 expect(userIdMatches).not.to.be.null
113
114 userId = parseInt(userIdMatches[1], 10)
115 expect(verificationString).to.not.be.undefined
116 })
117
118 it('Should not reset the password with an invalid verification string', async function () {
119 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
120 })
121
122 it('Should reset the password', async function () {
123 await resetPassword(server.url, userId, verificationString, 'super_password2')
124 })
125
126 it('Should login with this new password', async function () {
127 user.password = 'super_password2'
128
129 await userLogin(server, user)
130 })
131 })
132
45f1bd72
JL
133 describe('When creating a user without password', function () {
134 it('Should send a create password email', async function () {
135 this.timeout(10000)
136
137 await createUser({
138 url: server.url,
139 accessToken: server.accessToken,
140 username: 'create_password',
141 password: ''
142 })
143
144 await waitJobs(server)
145 expect(emails).to.have.lengthOf(2)
146
147 const email = emails[1]
148
149 expect(email['from'][0]['name']).equal('localhost:' + server.port)
150 expect(email['from'][0]['address']).equal('test-admin@localhost')
151 expect(email['to'][0]['address']).equal('create_password@example.com')
152 expect(email['subject']).contains('account')
153 expect(email['subject']).contains('password')
154
155 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
156 expect(verificationStringMatches).not.to.be.null
157
158 verificationString2 = verificationStringMatches[1]
159 expect(verificationString2).to.have.length.above(2)
160
161 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
162 expect(userIdMatches).not.to.be.null
163
164 userId2 = parseInt(userIdMatches[1], 10)
165 })
166
167 it('Should not reset the password with an invalid verification string', async function () {
168 await resetPassword(server.url, userId2, verificationString2 + 'c', 'newly_created_password', 403)
169 })
170
171 it('Should reset the password', async function () {
172 await resetPassword(server.url, userId2, verificationString2, 'newly_created_password')
173 })
174
175 it('Should login with this new password', async function () {
176 await userLogin(server, {
177 username: 'create_password',
178 password: 'newly_created_password'
179 })
180 })
181 })
182
310b5219 183 describe('When creating an abuse', function () {
ba75d268
C
184 it('Should send the notification email', async function () {
185 this.timeout(10000)
186
187 const reason = 'my super bad reason'
4f32032f 188 await reportAbuse({ url: server.url, token: server.accessToken, 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
7243f84d 195 expect(email['from'][0]['name']).equal('localhost:' + server.port)
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
C
198 expect(email['subject']).contains('abuse')
199 expect(email['text']).contains(videoUUID)
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'
209 await blockUser(server.url, userId, server.accessToken, 204, reason)
210
211 await waitJobs(server)
45f1bd72 212 expect(emails).to.have.lengthOf(4)
eacb25c4 213
45f1bd72 214 const email = emails[3]
eacb25c4 215
7243f84d 216 expect(email['from'][0]['name']).equal('localhost:' + server.port)
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')
221 expect(email['text']).contains(reason)
222 })
223
224 it('Should send the notification email when unblocking a user', async function () {
225 this.timeout(10000)
226
227 await unblockUser(server.url, userId, server.accessToken, 204)
228
229 await waitJobs(server)
45f1bd72 230 expect(emails).to.have.lengthOf(5)
eacb25c4 231
45f1bd72 232 const email = emails[4]
eacb25c4 233
7243f84d 234 expect(email['from'][0]['name']).equal('localhost:' + server.port)
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'
247 await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
248
249 await waitJobs(server)
45f1bd72 250 expect(emails).to.have.lengthOf(6)
26b7305a 251
45f1bd72 252 const email = emails[5]
26b7305a 253
7243f84d 254 expect(email['from'][0]['name']).equal('localhost:' + server.port)
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
265 await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
266
267 await waitJobs(server)
45f1bd72 268 expect(emails).to.have.lengthOf(7)
26b7305a 269
45f1bd72 270 const email = emails[6]
26b7305a 271
7243f84d 272 expect(email['from'][0]['name']).equal('localhost:' + server.port)
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 })
278 })
279
d9eaee39
JM
280 describe('When verifying a user email', function () {
281
282 it('Should ask to send the verification email', async function () {
283 this.timeout(10000)
284
285 await askSendVerifyEmail(server.url, 'user_1@example.com')
286
287 await waitJobs(server)
45f1bd72 288 expect(emails).to.have.lengthOf(8)
d9eaee39 289
45f1bd72 290 const email = emails[7]
d9eaee39 291
7243f84d 292 expect(email['from'][0]['name']).equal('localhost:' + server.port)
d9eaee39
JM
293 expect(email['from'][0]['address']).equal('test-admin@localhost')
294 expect(email['to'][0]['address']).equal('user_1@example.com')
295 expect(email['subject']).contains('Verify')
296
297 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
298 expect(verificationStringMatches).not.to.be.null
299
300 verificationString = verificationStringMatches[1]
301 expect(verificationString).to.not.be.undefined
302 expect(verificationString).to.have.length.above(2)
303
304 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
305 expect(userIdMatches).not.to.be.null
306
307 userId = parseInt(userIdMatches[1], 10)
308 })
309
310 it('Should not verify the email with an invalid verification string', async function () {
d1ab89de 311 await verifyEmail(server.url, userId, verificationString + 'b', false, 403)
d9eaee39
JM
312 })
313
314 it('Should verify the email', async function () {
315 await verifyEmail(server.url, userId, verificationString)
316 })
317 })
318
7c3b7976 319 after(async function () {
89ada4e2 320 MockSmtpServer.Instance.kill()
7c3b7976
C
321
322 await cleanupTests([ server ])
f076daa7
C
323 })
324})