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