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