]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/email.ts
Refractor audit user identifier
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / email.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6 addVideoToBlacklist,
7 askResetPassword,
8 askSendVerifyEmail,
9 blockUser,
10 createUser, removeVideoFromBlacklist,
11 reportVideoAbuse,
12 resetPassword,
13 runServer,
14 unblockUser,
15 uploadVideo,
16 userLogin,
17 verifyEmail
18 } from '../../utils'
19 import { flushTests, killallServers, ServerInfo, setAccessTokensToServers } from '../../utils/index'
20 import { mockSmtpServer } from '../../utils/miscs/email'
21 import { waitJobs } from '../../utils/server/jobs'
22
23 const expect = chai.expect
24
25 describe('Test emails', function () {
26 let server: ServerInfo
27 let userId: number
28 let userAccessToken: string
29 let videoUUID: string
30 let videoUserUUID: string
31 let verificationString: string
32 const emails: object[] = []
33 const user = {
34 username: 'user_1',
35 password: 'super_password'
36 }
37
38 before(async function () {
39 this.timeout(30000)
40
41 await mockSmtpServer(emails)
42
43 await flushTests()
44
45 const overrideConfig = {
46 smtp: {
47 hostname: 'localhost'
48 }
49 }
50 server = await runServer(1, overrideConfig)
51 await setAccessTokensToServers([ server ])
52
53 {
54 const res = await createUser(server.url, server.accessToken, user.username, user.password)
55 userId = res.body.user.id
56
57 userAccessToken = await userLogin(server, user)
58 }
59
60 {
61 const attributes = {
62 name: 'my super user video'
63 }
64 const res = await uploadVideo(server.url, userAccessToken, attributes)
65 videoUserUUID = res.body.video.uuid
66 }
67
68 {
69 const attributes = {
70 name: 'my super name'
71 }
72 const res = await uploadVideo(server.url, server.accessToken, attributes)
73 videoUUID = res.body.video.uuid
74 }
75 })
76
77 describe('When resetting user password', function () {
78
79 it('Should ask to reset the password', async function () {
80 this.timeout(10000)
81
82 await askResetPassword(server.url, 'user_1@example.com')
83
84 await waitJobs(server)
85 expect(emails).to.have.lengthOf(1)
86
87 const email = emails[0]
88
89 expect(email['from'][0]['address']).equal('test-admin@localhost')
90 expect(email['to'][0]['address']).equal('user_1@example.com')
91 expect(email['subject']).contains('password')
92
93 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
94 expect(verificationStringMatches).not.to.be.null
95
96 verificationString = verificationStringMatches[1]
97 expect(verificationString).to.have.length.above(2)
98
99 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
100 expect(userIdMatches).not.to.be.null
101
102 userId = parseInt(userIdMatches[1], 10)
103 expect(verificationString).to.not.be.undefined
104 })
105
106 it('Should not reset the password with an invalid verification string', async function () {
107 await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403)
108 })
109
110 it('Should reset the password', async function () {
111 await resetPassword(server.url, userId, verificationString, 'super_password2')
112 })
113
114 it('Should login with this new password', async function () {
115 user.password = 'super_password2'
116
117 await userLogin(server, user)
118 })
119 })
120
121 describe('When creating a video abuse', function () {
122 it('Should send the notification email', async function () {
123 this.timeout(10000)
124
125 const reason = 'my super bad reason'
126 await reportVideoAbuse(server.url, server.accessToken, videoUUID, reason)
127
128 await waitJobs(server)
129 expect(emails).to.have.lengthOf(2)
130
131 const email = emails[1]
132
133 expect(email['from'][0]['address']).equal('test-admin@localhost')
134 expect(email['to'][0]['address']).equal('admin1@example.com')
135 expect(email['subject']).contains('abuse')
136 expect(email['text']).contains(videoUUID)
137 })
138 })
139
140 describe('When blocking/unblocking user', async function () {
141 it('Should send the notification email when blocking a user', async function () {
142 this.timeout(10000)
143
144 const reason = 'my super bad reason'
145 await blockUser(server.url, userId, server.accessToken, 204, reason)
146
147 await waitJobs(server)
148 expect(emails).to.have.lengthOf(3)
149
150 const email = emails[2]
151
152 expect(email['from'][0]['address']).equal('test-admin@localhost')
153 expect(email['to'][0]['address']).equal('user_1@example.com')
154 expect(email['subject']).contains(' blocked')
155 expect(email['text']).contains(' blocked')
156 expect(email['text']).contains(reason)
157 })
158
159 it('Should send the notification email when unblocking a user', async function () {
160 this.timeout(10000)
161
162 await unblockUser(server.url, userId, server.accessToken, 204)
163
164 await waitJobs(server)
165 expect(emails).to.have.lengthOf(4)
166
167 const email = emails[3]
168
169 expect(email['from'][0]['address']).equal('test-admin@localhost')
170 expect(email['to'][0]['address']).equal('user_1@example.com')
171 expect(email['subject']).contains(' unblocked')
172 expect(email['text']).contains(' unblocked')
173 })
174 })
175
176 describe('When blacklisting a video', function () {
177 it('Should send the notification email', async function () {
178 this.timeout(10000)
179
180 const reason = 'my super reason'
181 await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason)
182
183 await waitJobs(server)
184 expect(emails).to.have.lengthOf(5)
185
186 const email = emails[4]
187
188 expect(email['from'][0]['address']).equal('test-admin@localhost')
189 expect(email['to'][0]['address']).equal('user_1@example.com')
190 expect(email['subject']).contains(' blacklisted')
191 expect(email['text']).contains('my super user video')
192 expect(email['text']).contains('my super reason')
193 })
194
195 it('Should send the notification email', async function () {
196 this.timeout(10000)
197
198 await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID)
199
200 await waitJobs(server)
201 expect(emails).to.have.lengthOf(6)
202
203 const email = emails[5]
204
205 expect(email['from'][0]['address']).equal('test-admin@localhost')
206 expect(email['to'][0]['address']).equal('user_1@example.com')
207 expect(email['subject']).contains(' unblacklisted')
208 expect(email['text']).contains('my super user video')
209 })
210 })
211
212 describe('When verifying a user email', function () {
213
214 it('Should ask to send the verification email', async function () {
215 this.timeout(10000)
216
217 await askSendVerifyEmail(server.url, 'user_1@example.com')
218
219 await waitJobs(server)
220 expect(emails).to.have.lengthOf(7)
221
222 const email = emails[6]
223
224 expect(email['from'][0]['address']).equal('test-admin@localhost')
225 expect(email['to'][0]['address']).equal('user_1@example.com')
226 expect(email['subject']).contains('Verify')
227
228 const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text'])
229 expect(verificationStringMatches).not.to.be.null
230
231 verificationString = verificationStringMatches[1]
232 expect(verificationString).to.not.be.undefined
233 expect(verificationString).to.have.length.above(2)
234
235 const userIdMatches = /userId=([0-9]+)/.exec(email['text'])
236 expect(userIdMatches).not.to.be.null
237
238 userId = parseInt(userIdMatches[1], 10)
239 })
240
241 it('Should not verify the email with an invalid verification string', async function () {
242 await verifyEmail(server.url, userId, verificationString + 'b', 403)
243 })
244
245 it('Should verify the email', async function () {
246 await verifyEmail(server.url, userId, verificationString)
247 })
248 })
249
250 after(async function () {
251 killallServers([ server ])
252 })
253 })