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