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