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