]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/email.ts
Fix CI using 127.0.0.1 for tests
[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
86347717 3import { expect } from 'chai'
c55e3d72 4import { MockSmtpServer } from '@server/tests/shared'
4c7e60bc 5import { HttpStatusCode } from '@shared/models'
c55e3d72 6import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands'
f076daa7 7
f076daa7 8describe('Test emails', function () {
254d3579 9 let server: PeerTubeServer
f076daa7 10 let userId: number
45f1bd72 11 let userId2: number
26b7305a 12 let userAccessToken: string
4f32032f 13
29837f88 14 let videoShortUUID: string
4f32032f
C
15 let videoId: number
16
26b7305a 17 let videoUserUUID: string
4f32032f 18
f076daa7 19 let verificationString: string
45f1bd72 20 let verificationString2: string
4f32032f 21
f076daa7
C
22 const emails: object[] = []
23 const user = {
24 username: 'user_1',
25 password: 'super_password'
26 }
48f07b4a 27 let emailPort: number
f076daa7
C
28
29 before(async function () {
2284f202 30 this.timeout(50000)
f076daa7 31
48f07b4a 32 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
f076daa7 33
f076daa7
C
34 const overrideConfig = {
35 smtp: {
2732eeff 36 hostname: '127.0.0.1',
48f07b4a 37 port: emailPort
f076daa7
C
38 }
39 }
254d3579 40 server = await createSingleServer(1, overrideConfig)
f076daa7
C
41 await setAccessTokensToServers([ server ])
42
ba75d268 43 {
89d241a7 44 const created = await server.users.create({ username: user.username, password: user.password })
7926c5f9 45 userId = created.id
26b7305a 46
89d241a7 47 userAccessToken = await server.login.getAccessToken(user)
26b7305a
C
48 }
49
50 {
d23dd9fb 51 const attributes = { name: 'my super user video' }
89d241a7 52 const { uuid } = await server.videos.upload({ token: userAccessToken, attributes })
d23dd9fb 53 videoUserUUID = uuid
ba75d268
C
54 }
55
56 {
57 const attributes = {
58 name: 'my super name'
59 }
29837f88
C
60 const { shortUUID, id } = await server.videos.upload({ attributes })
61 videoShortUUID = shortUUID
d23dd9fb 62 videoId = id
ba75d268 63 }
f076daa7
C
64 })
65
66 describe('When resetting user password', function () {
67
68 it('Should ask to reset the password', async function () {
89d241a7 69 await server.users.askResetPassword({ email: 'user_1@example.com' })
f076daa7 70
3cd0734f 71 await waitJobs(server)
f076daa7
C
72 expect(emails).to.have.lengthOf(1)
73
74 const email = emails[0]
75
113d4a3f 76 expect(email['from'][0]['name']).equal('PeerTube')
2732eeff 77 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
f076daa7
C
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 () {
89d241a7 95 await server.users.resetPassword({
7926c5f9
C
96 userId,
97 verificationString: verificationString + 'b',
98 password: 'super_password2',
99 expectedStatus: HttpStatusCode.FORBIDDEN_403
100 })
f076daa7
C
101 })
102
103 it('Should reset the password', async function () {
89d241a7 104 await server.users.resetPassword({ userId, verificationString, password: 'super_password2' })
f076daa7
C
105 })
106
e9c5f123 107 it('Should not reset the password with the same verification string', async function () {
89d241a7 108 await server.users.resetPassword({
7926c5f9
C
109 userId,
110 verificationString,
111 password: 'super_password3',
112 expectedStatus: HttpStatusCode.FORBIDDEN_403
113 })
e9c5f123
C
114 })
115
f076daa7
C
116 it('Should login with this new password', async function () {
117 user.password = 'super_password2'
118
89d241a7 119 await server.login.getAccessToken(user)
f076daa7
C
120 })
121 })
122
45f1bd72 123 describe('When creating a user without password', function () {
7926c5f9 124
45f1bd72
JL
125 it('Should send a create password email', async function () {
126 this.timeout(10000)
127
89d241a7 128 await server.users.create({ username: 'create_password', password: '' })
45f1bd72
JL
129
130 await waitJobs(server)
131 expect(emails).to.have.lengthOf(2)
132
133 const email = emails[1]
134
113d4a3f 135 expect(email['from'][0]['name']).equal('PeerTube')
2732eeff 136 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
45f1bd72
JL
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 () {
89d241a7 154 await server.users.resetPassword({
7926c5f9
C
155 userId: userId2,
156 verificationString: verificationString2 + 'c',
157 password: 'newly_created_password',
158 expectedStatus: HttpStatusCode.FORBIDDEN_403
159 })
45f1bd72
JL
160 })
161
162 it('Should reset the password', async function () {
89d241a7 163 await server.users.resetPassword({
7926c5f9
C
164 userId: userId2,
165 verificationString: verificationString2,
166 password: 'newly_created_password'
167 })
45f1bd72
JL
168 })
169
170 it('Should login with this new password', async function () {
89d241a7 171 await server.login.getAccessToken({
45f1bd72
JL
172 username: 'create_password',
173 password: 'newly_created_password'
174 })
175 })
176 })
177
310b5219 178 describe('When creating an abuse', function () {
29837f88 179
ba75d268
C
180 it('Should send the notification email', async function () {
181 this.timeout(10000)
182
183 const reason = 'my super bad reason'
0fbc0dec 184 await server.abuses.report({ token: userAccessToken, videoId, reason })
ba75d268 185
3cd0734f 186 await waitJobs(server)
45f1bd72 187 expect(emails).to.have.lengthOf(3)
ba75d268 188
45f1bd72 189 const email = emails[2]
ba75d268 190
113d4a3f 191 expect(email['from'][0]['name']).equal('PeerTube')
2732eeff 192 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
48f07b4a 193 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
ba75d268 194 expect(email['subject']).contains('abuse')
29837f88 195 expect(email['text']).contains(videoShortUUID)
ba75d268
C
196 })
197 })
198
9b39106d
C
199 describe('When blocking/unblocking user', function () {
200
eacb25c4
C
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'
89d241a7 205 await server.users.banUser({ userId, reason })
eacb25c4
C
206
207 await waitJobs(server)
45f1bd72 208 expect(emails).to.have.lengthOf(4)
eacb25c4 209
45f1bd72 210 const email = emails[3]
eacb25c4 211
113d4a3f 212 expect(email['from'][0]['name']).equal('PeerTube')
2732eeff 213 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
eacb25c4
C
214 expect(email['to'][0]['address']).equal('user_1@example.com')
215 expect(email['subject']).contains(' blocked')
216 expect(email['text']).contains(' blocked')
113d4a3f 217 expect(email['text']).contains('bad reason')
eacb25c4
C
218 })
219
220 it('Should send the notification email when unblocking a user', async function () {
221 this.timeout(10000)
222
89d241a7 223 await server.users.unbanUser({ userId })
eacb25c4
C
224
225 await waitJobs(server)
45f1bd72 226 expect(emails).to.have.lengthOf(5)
eacb25c4 227
45f1bd72 228 const email = emails[4]
eacb25c4 229
113d4a3f 230 expect(email['from'][0]['name']).equal('PeerTube')
2732eeff 231 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
eacb25c4
C
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
26b7305a
C
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'
89d241a7 243 await server.blacklist.add({ videoId: videoUserUUID, reason })
26b7305a
C
244
245 await waitJobs(server)
45f1bd72 246 expect(emails).to.have.lengthOf(6)
26b7305a 247
45f1bd72 248 const email = emails[5]
26b7305a 249
113d4a3f 250 expect(email['from'][0]['name']).equal('PeerTube')
2732eeff 251 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
26b7305a
C
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
89d241a7 261 await server.blacklist.remove({ videoId: videoUserUUID })
26b7305a
C
262
263 await waitJobs(server)
45f1bd72 264 expect(emails).to.have.lengthOf(7)
26b7305a 265
45f1bd72 266 const email = emails[6]
26b7305a 267
113d4a3f 268 expect(email['from'][0]['name']).equal('PeerTube')
2732eeff 269 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
26b7305a
C
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 })
b9cf3fb6
C
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 })
26b7305a
C
279 })
280
d9eaee39
JM
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
89d241a7 286 await server.users.askSendVerifyEmail({ email: 'user_1@example.com' })
d9eaee39
JM
287
288 await waitJobs(server)
45f1bd72 289 expect(emails).to.have.lengthOf(8)
d9eaee39 290
45f1bd72 291 const email = emails[7]
d9eaee39 292
113d4a3f 293 expect(email['from'][0]['name']).equal('PeerTube')
2732eeff 294 expect(email['from'][0]['address']).equal('test-admin@127.0.0.1')
d9eaee39
JM
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 () {
89d241a7 312 await server.users.verifyEmail({
7926c5f9
C
313 userId,
314 verificationString: verificationString + 'b',
315 isPendingEmail: false,
316 expectedStatus: HttpStatusCode.FORBIDDEN_403
317 })
d9eaee39
JM
318 })
319
320 it('Should verify the email', async function () {
89d241a7 321 await server.users.verifyEmail({ userId, verificationString })
d9eaee39
JM
322 })
323 })
324
7c3b7976 325 after(async function () {
89ada4e2 326 MockSmtpServer.Instance.kill()
7c3b7976
C
327
328 await cleanupTests([ server ])
f076daa7
C
329 })
330})