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