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