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