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