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