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