]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/email.ts
Introduce abuse command
[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'
8ef9457f 5import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
eacb25c4 6import {
26b7305a 7 addVideoToBlacklist,
eacb25c4 8 askResetPassword,
d9eaee39 9 askSendVerifyEmail,
eacb25c4 10 blockUser,
7243f84d
C
11 cleanupTests,
12 createUser,
13 flushAndRunServer,
14 removeVideoFromBlacklist,
eacb25c4 15 resetPassword,
7243f84d
C
16 ServerInfo,
17 setAccessTokensToServers,
eacb25c4
C
18 unblockUser,
19 uploadVideo,
d9eaee39 20 userLogin,
7243f84d 21 verifyEmail
94565d52 22} from '../../../../shared/extra-utils'
8ef9457f 23import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
94565d52 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
113d4a3f 100 expect(email['from'][0]['name']).equal('PeerTube')
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 () {
f2eb23cd 119 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', HttpStatusCode.FORBIDDEN_403)
f076daa7
C
120 })
121
122 it('Should reset the password', async function () {
123 await resetPassword(server.url, userId, verificationString, 'super_password2')
124 })
125
e9c5f123 126 it('Should not reset the password with the same verification string', async function () {
f2eb23cd 127 await resetPassword(server.url, userId, verificationString, 'super_password3', HttpStatusCode.FORBIDDEN_403)
e9c5f123
C
128 })
129
f076daa7
C
130 it('Should login with this new password', async function () {
131 user.password = 'super_password2'
132
133 await userLogin(server, user)
134 })
135 })
136
45f1bd72
JL
137 describe('When creating a user without password', function () {
138 it('Should send a create password email', async function () {
139 this.timeout(10000)
140
141 await createUser({
142 url: server.url,
143 accessToken: server.accessToken,
144 username: 'create_password',
145 password: ''
146 })
147
148 await waitJobs(server)
149 expect(emails).to.have.lengthOf(2)
150
151 const email = emails[1]
152
113d4a3f 153 expect(email['from'][0]['name']).equal('PeerTube')
45f1bd72
JL
154 expect(email['from'][0]['address']).equal('test-admin@localhost')
155 expect(email['to'][0]['address']).equal('create_password@example.com')
156 expect(email['subject']).contains('account')
157 expect(email['subject']).contains('password')
158
159 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
160 expect(verificationStringMatches).not.to.be.null
161
162 verificationString2 = verificationStringMatches[1]
163 expect(verificationString2).to.have.length.above(2)
164
165 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
166 expect(userIdMatches).not.to.be.null
167
168 userId2 = parseInt(userIdMatches[1], 10)
169 })
170
171 it('Should not reset the password with an invalid verification string', async function () {
f2eb23cd 172 await resetPassword(server.url, userId2, verificationString2 + 'c', 'newly_created_password', HttpStatusCode.FORBIDDEN_403)
45f1bd72
JL
173 })
174
175 it('Should reset the password', async function () {
176 await resetPassword(server.url, userId2, verificationString2, 'newly_created_password')
177 })
178
179 it('Should login with this new password', async function () {
180 await userLogin(server, {
181 username: 'create_password',
182 password: 'newly_created_password'
183 })
184 })
185 })
186
310b5219 187 describe('When creating an abuse', function () {
ba75d268
C
188 it('Should send the notification email', async function () {
189 this.timeout(10000)
190
191 const reason = 'my super bad reason'
0c1a77e9 192 await server.abusesCommand.report({ videoId, reason })
ba75d268 193
3cd0734f 194 await waitJobs(server)
45f1bd72 195 expect(emails).to.have.lengthOf(3)
ba75d268 196
45f1bd72 197 const email = emails[2]
ba75d268 198
113d4a3f 199 expect(email['from'][0]['name']).equal('PeerTube')
ba75d268 200 expect(email['from'][0]['address']).equal('test-admin@localhost')
48f07b4a 201 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
ba75d268
C
202 expect(email['subject']).contains('abuse')
203 expect(email['text']).contains(videoUUID)
204 })
205 })
206
9b39106d
C
207 describe('When blocking/unblocking user', function () {
208
eacb25c4
C
209 it('Should send the notification email when blocking a user', async function () {
210 this.timeout(10000)
211
212 const reason = 'my super bad reason'
f2eb23cd 213 await blockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204, reason)
eacb25c4
C
214
215 await waitJobs(server)
45f1bd72 216 expect(emails).to.have.lengthOf(4)
eacb25c4 217
45f1bd72 218 const email = emails[3]
eacb25c4 219
113d4a3f 220 expect(email['from'][0]['name']).equal('PeerTube')
eacb25c4
C
221 expect(email['from'][0]['address']).equal('test-admin@localhost')
222 expect(email['to'][0]['address']).equal('user_1@example.com')
223 expect(email['subject']).contains(' blocked')
224 expect(email['text']).contains(' blocked')
113d4a3f 225 expect(email['text']).contains('bad reason')
eacb25c4
C
226 })
227
228 it('Should send the notification email when unblocking a user', async function () {
229 this.timeout(10000)
230
f2eb23cd 231 await unblockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204)
eacb25c4
C
232
233 await waitJobs(server)
45f1bd72 234 expect(emails).to.have.lengthOf(5)
eacb25c4 235
45f1bd72 236 const email = emails[4]
eacb25c4 237
113d4a3f 238 expect(email['from'][0]['name']).equal('PeerTube')
eacb25c4
C
239 expect(email['from'][0]['address']).equal('test-admin@localhost')
240 expect(email['to'][0]['address']).equal('user_1@example.com')
241 expect(email['subject']).contains(' unblocked')
242 expect(email['text']).contains(' unblocked')
243 })
244 })
245
26b7305a
C
246 describe('When blacklisting a video', function () {
247 it('Should send the notification email', async function () {
248 this.timeout(10000)
249
250 const reason = 'my super reason'
251 await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
252
253 await waitJobs(server)
45f1bd72 254 expect(emails).to.have.lengthOf(6)
26b7305a 255
45f1bd72 256 const email = emails[5]
26b7305a 257
113d4a3f 258 expect(email['from'][0]['name']).equal('PeerTube')
26b7305a
C
259 expect(email['from'][0]['address']).equal('test-admin@localhost')
260 expect(email['to'][0]['address']).equal('user_1@example.com')
261 expect(email['subject']).contains(' blacklisted')
262 expect(email['text']).contains('my super user video')
263 expect(email['text']).contains('my super reason')
264 })
265
266 it('Should send the notification email', async function () {
267 this.timeout(10000)
268
269 await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
270
271 await waitJobs(server)
45f1bd72 272 expect(emails).to.have.lengthOf(7)
26b7305a 273
45f1bd72 274 const email = emails[6]
26b7305a 275
113d4a3f 276 expect(email['from'][0]['name']).equal('PeerTube')
26b7305a
C
277 expect(email['from'][0]['address']).equal('test-admin@localhost')
278 expect(email['to'][0]['address']).equal('user_1@example.com')
279 expect(email['subject']).contains(' unblacklisted')
280 expect(email['text']).contains('my super user video')
281 })
b9cf3fb6
C
282
283 it('Should have the manage preferences link in the email', async function () {
284 const email = emails[6]
285 expect(email['text']).to.contain('Manage your notification preferences')
286 })
26b7305a
C
287 })
288
d9eaee39
JM
289 describe('When verifying a user email', function () {
290
291 it('Should ask to send the verification email', async function () {
292 this.timeout(10000)
293
294 await askSendVerifyEmail(server.url, 'user_1@example.com')
295
296 await waitJobs(server)
45f1bd72 297 expect(emails).to.have.lengthOf(8)
d9eaee39 298
45f1bd72 299 const email = emails[7]
d9eaee39 300
113d4a3f 301 expect(email['from'][0]['name']).equal('PeerTube')
d9eaee39
JM
302 expect(email['from'][0]['address']).equal('test-admin@localhost')
303 expect(email['to'][0]['address']).equal('user_1@example.com')
304 expect(email['subject']).contains('Verify')
305
306 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
307 expect(verificationStringMatches).not.to.be.null
308
309 verificationString = verificationStringMatches[1]
310 expect(verificationString).to.not.be.undefined
311 expect(verificationString).to.have.length.above(2)
312
313 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
314 expect(userIdMatches).not.to.be.null
315
316 userId = parseInt(userIdMatches[1], 10)
317 })
318
319 it('Should not verify the email with an invalid verification string', async function () {
f2eb23cd 320 await verifyEmail(server.url, userId, verificationString + 'b', false, HttpStatusCode.FORBIDDEN_403)
d9eaee39
JM
321 })
322
323 it('Should verify the email', async function () {
324 await verifyEmail(server.url, userId, verificationString)
325 })
326 })
327
7c3b7976 328 after(async function () {
89ada4e2 329 MockSmtpServer.Instance.kill()
7c3b7976
C
330
331 await cleanupTests([ server ])
f076daa7
C
332 })
333})