]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/email.ts
Correctly export misc files
[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 addVideoToBlacklist,
8 askResetPassword,
9 askSendVerifyEmail,
10 blockUser,
11 cleanupTests,
12 createUser,
13 flushAndRunServer,
14 removeVideoFromBlacklist,
15 reportAbuse,
16 resetPassword,
17 ServerInfo,
18 setAccessTokensToServers,
19 unblockUser,
20 uploadVideo,
21 userLogin,
22 verifyEmail
23 } from '../../../../shared/extra-utils'
24 import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
25 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
26
27 const expect = chai.expect
28
29 describe('Test emails', function () {
30 let server: ServerInfo
31 let userId: number
32 let userId2: number
33 let userAccessToken: string
34
35 let videoUUID: string
36 let videoId: number
37
38 let videoUserUUID: string
39
40 let verificationString: string
41 let verificationString2: string
42
43 const emails: object[] = []
44 const user = {
45 username: 'user_1',
46 password: 'super_password'
47 }
48 let emailPort: number
49
50 before(async function () {
51 this.timeout(50000)
52
53 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
54
55 const overrideConfig = {
56 smtp: {
57 hostname: 'localhost',
58 port: emailPort
59 }
60 }
61 server = await flushAndRunServer(1, overrideConfig)
62 await setAccessTokensToServers([ server ])
63
64 {
65 const res = await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
66 userId = res.body.user.id
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
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
85 videoId = res.body.video.id
86 }
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
96 await waitJobs(server)
97 expect(emails).to.have.lengthOf(1)
98
99 const email = emails[0]
100
101 expect(email['from'][0]['name']).equal('PeerTube')
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 () {
120 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', HttpStatusCode.FORBIDDEN_403)
121 })
122
123 it('Should reset the password', async function () {
124 await resetPassword(server.url, userId, verificationString, 'super_password2')
125 })
126
127 it('Should not reset the password with the same verification string', async function () {
128 await resetPassword(server.url, userId, verificationString, 'super_password3', HttpStatusCode.FORBIDDEN_403)
129 })
130
131 it('Should login with this new password', async function () {
132 user.password = 'super_password2'
133
134 await userLogin(server, user)
135 })
136 })
137
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('PeerTube')
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 () {
173 await resetPassword(server.url, userId2, verificationString2 + 'c', 'newly_created_password', HttpStatusCode.FORBIDDEN_403)
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
188 describe('When creating an abuse', function () {
189 it('Should send the notification email', async function () {
190 this.timeout(10000)
191
192 const reason = 'my super bad reason'
193 await reportAbuse({ url: server.url, token: server.accessToken, videoId, reason })
194
195 await waitJobs(server)
196 expect(emails).to.have.lengthOf(3)
197
198 const email = emails[2]
199
200 expect(email['from'][0]['name']).equal('PeerTube')
201 expect(email['from'][0]['address']).equal('test-admin@localhost')
202 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
203 expect(email['subject']).contains('abuse')
204 expect(email['text']).contains(videoUUID)
205 })
206 })
207
208 describe('When blocking/unblocking user', function () {
209
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'
214 await blockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204, reason)
215
216 await waitJobs(server)
217 expect(emails).to.have.lengthOf(4)
218
219 const email = emails[3]
220
221 expect(email['from'][0]['name']).equal('PeerTube')
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('bad reason')
227 })
228
229 it('Should send the notification email when unblocking a user', async function () {
230 this.timeout(10000)
231
232 await unblockUser(server.url, userId, server.accessToken, HttpStatusCode.NO_CONTENT_204)
233
234 await waitJobs(server)
235 expect(emails).to.have.lengthOf(5)
236
237 const email = emails[4]
238
239 expect(email['from'][0]['name']).equal('PeerTube')
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
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)
255 expect(emails).to.have.lengthOf(6)
256
257 const email = emails[5]
258
259 expect(email['from'][0]['name']).equal('PeerTube')
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)
273 expect(emails).to.have.lengthOf(7)
274
275 const email = emails[6]
276
277 expect(email['from'][0]['name']).equal('PeerTube')
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 })
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 })
288 })
289
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)
298 expect(emails).to.have.lengthOf(8)
299
300 const email = emails[7]
301
302 expect(email['from'][0]['name']).equal('PeerTube')
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 () {
321 await verifyEmail(server.url, userId, verificationString + 'b', false, HttpStatusCode.FORBIDDEN_403)
322 })
323
324 it('Should verify the email', async function () {
325 await verifyEmail(server.url, userId, verificationString)
326 })
327 })
328
329 after(async function () {
330 MockSmtpServer.Instance.kill()
331
332 await cleanupTests([ server ])
333 })
334 })