1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/*!
* Matomo - free/libre analytics platform
*
* login & password reset screenshot tests.
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
describe("Login", function () {
this.timeout(0);
var md5Pass = "0adcc0d741277f74c64c8abab7330d1c", // md5("smarty-pants")
formlessLoginUrl = "?module=Login&action=logme&login=oliverqueen&password=" + md5Pass,
bruteForceLogUrl = "?module=Login&action=bruteForceLog",
apiAuthUrl = "?module=API&method=UsersManager.getTokenAuth&format=json&userLogin=ovliverqueen&md5Password=" + md5Pass;
before(async function () {
testEnvironment.testUseMockAuth = 0;
testEnvironment.queryParamOverride = {date: "2012-01-01", period: "year"};
testEnvironment.save();
await page.clearCookies();
});
beforeEach(function () {
testEnvironment.testUseMockAuth = 0;
testEnvironment.queryParamOverride = {date: "2012-01-01", period: "year"};
testEnvironment.save();
});
after(async function () {
testEnvironment.overrideConfig('General', 'login_allow_logme', '0')
testEnvironment.testUseMockAuth = 1;
delete testEnvironment.bruteForceBlockIps;
delete testEnvironment.bruteForceBlockThisIp;
delete testEnvironment.queryParamOverride;
delete testEnvironment.configOverride.General;
testEnvironment.save();
await page.clearCookies();
});
afterEach(function () {
testEnvironment.testUseMockAuth = 1;
delete testEnvironment.bruteForceBlockIps;
delete testEnvironment.bruteForceBlockThisIp;
delete testEnvironment.queryParamOverride;
delete testEnvironment.configOverride.General;
testEnvironment.save();
});
it("should show error when trying to log in through login form", async function () {
testEnvironment.testUseMockAuth = 0;
testEnvironment.bruteForceBlockThisIp = 1;
delete testEnvironment.bruteForceBlockIps;
delete testEnvironment.queryParamOverride;
testEnvironment.save();
await page.goto("");
expect(await page.screenshot({ fullPage: true })).to.matchImage('bruteforcelog_blockedlogin');
});
it("should load correctly", async function() {
await page.goto("");
await page.waitForNetworkIdle();
await page.waitFor('input');
await page.mouse.click(0, 0);
await page.waitFor(250);
expect(await page.screenshot({ fullPage: true })).to.matchImage('login_form');
});
it("should fail when incorrect credentials are supplied", async function() {
await page.type('#login_form_login', 'superUserLogin');
await page.type('#login_form_password', 'wrongpassword');
await page.evaluate(function(){
$('#login_form_submit').click();
});
await page.waitForNetworkIdle();
await page.waitFor('.notification');
await page.mouse.click(0, 0);
await page.waitFor(250);
expect(await page.screenshot({ fullPage: true })).to.matchImage('login_fail');
});
it("should redirect to Matomo when correct credentials are supplied", async function() {
await page.type("#login_form_login", "superUserLogin");
await page.type("#login_form_password", "superUserPass");
await page.evaluate(function(){
$('#login_form_submit').click();
});
await page.waitForNetworkIdle();
// check dashboard is shown
await page.waitForSelector('#dashboard');
await page.waitForNetworkIdle();
});
it("should redirect to login when logout link clicked", async function() {
await page.click("nav .right .icon-sign-out");
await page.waitForNetworkIdle();
await page.waitFor('input');
await page.mouse.click(0, 0);
await page.waitFor(250);
expect(await page.screenshot({ fullPage: true })).to.matchImage('login_form_logout');
});
it("login with email and password should work", async function() {
await page.type("#login_form_login", "hello@example.org");
await page.type("#login_form_password", "superUserPass");
await page.evaluate(function(){
$('#login_form_submit').click();
});
// check dashboard is shown
await page.waitForNetworkIdle();
await page.waitForSelector('#dashboard');
});
it("should display password reset form when forgot password link clicked", async function() {
await page.click("nav .right .icon-sign-out");
await page.waitForNetworkIdle();
await page.waitFor("a#login_form_nav");
await page.click("a#login_form_nav");
await page.waitForNetworkIdle();
expect(await page.screenshot({ fullPage: true })).to.matchImage('forgot_password');
});
it("should show reset password form and error message on error", async function() {
await page.type("#reset_form_login", "superUserLogin");
await page.type("#reset_form_password", "superUserPass2");
await page.click("#reset_form_submit");
await page.waitForNetworkIdle();
await page.waitFor('.notification');
expect(await page.screenshot({ fullPage: true })).to.matchImage('password_reset_error');
});
it("should send email when password reset form submitted", async function() {
await page.reload();
await page.click("a#login_form_nav");
await page.type("#reset_form_login", "superUserLogin");
await page.type("#reset_form_password", "superUserPass2");
await page.type("#reset_form_password_bis", "superUserPass2");
await page.click("#reset_form_submit");
await page.waitForNetworkIdle();
expect(await page.screenshot({ fullPage: true })).to.matchImage('password_reset');
});
it("should show reset password confirmation page when password reset link is clicked", async function() {
var expectedMailOutputFile = PIWIK_INCLUDE_PATH + '/tmp/Login.resetPassword.mail.json',
fileContents = require("fs").readFileSync(expectedMailOutputFile),
mailSent = JSON.parse(fileContents),
resetUrl = mailSent.contents.match(/http:\/\/[^\s]+resetToken[^\s]+<\/p>/);
if (!resetUrl || !resetUrl[0]) {
throw new Error(`Could not find reset URL in email, captured mail info: ${fileContents}`)
}
resetUrl = resetUrl[0].replace(/<\/p>$/, '');
await page.goto(resetUrl);
await page.waitForNetworkIdle();
expect(await page.screenshot({ fullPage: true })).to.matchImage('password_reset_confirm');
});
it("should reset password when password reset link is clicked", async function() {
await page.type("#mtmpasswordconfirm", "superUserPass2");
await page.click("#login_reset_confirm");
await page.waitForNetworkIdle();
expect(await page.screenshot({ fullPage: true })).to.matchImage('password_reset_complete');
});
it("should login successfully when new credentials used", async function() {
await page.type("#login_form_login", "superUserLogin");
await page.type("#login_form_password", "superUserPass2");
await page.evaluate(function(){
$('#login_form_submit').click();
});
// check dashboard is shown
await page.waitForNetworkIdle();
await page.waitForSelector('#dashboard');
});
it("should show error when formless login used, but disabled", async function () {
testEnvironment.overrideConfig('General', 'login_allow_logme', '0')
testEnvironment.save();
await page.click("nav .right .icon-sign-out");
await page.waitForNetworkIdle();
await page.goto(formlessLoginUrl);
expect(await page.screenshot({ fullPage: true })).to.matchImage('logme_disabled');
});
it("should login successfully when formless login used", async function() {
testEnvironment.overrideConfig('General', 'login_allow_logme', '1')
testEnvironment.save();
await page.goto("about:blank");
await page.goto(formlessLoginUrl);
// check dashboard is shown
await page.waitForNetworkIdle();
await page.waitForSelector('#dashboard');
});
it('should not show login page when ips whitelisted and ip is not matching', async function() {
testEnvironment.overrideConfig('General', 'login_allowlist_ip', ['199.199.199.199']);
testEnvironment.save();
await page.goto('');
await page.waitForNetworkIdle();
const element = await page.$('.box');
expect(await element.screenshot()).to.matchImage('ip_not_whitelisted');
});
it("should show brute force log url when there are no entries", async function () {
testEnvironment.testUseMockAuth = 1;
delete testEnvironment.queryParamOverride;
delete testEnvironment.bruteForceBlockThisIp;
delete testEnvironment.bruteForceBlockIps;
testEnvironment.overrideConfig('General', 'login_allowlist_ip', []);
testEnvironment.save();
await page.goto(bruteForceLogUrl);
expect(await page.screenshot({ fullPage: true })).to.matchImage('bruteforcelog_noentries');
});
it("should show brute force log url when there are entries", async function () {
testEnvironment.testUseMockAuth = 1;
testEnvironment.bruteForceBlockIps = 1;
delete testEnvironment.bruteForceBlockThisIp;
delete testEnvironment.queryParamOverride;
testEnvironment.save();
await page.goto(bruteForceLogUrl);
expect(await page.screenshot({ fullPage: true })).to.matchImage('bruteforcelog_withentries');
});
it("should show error when trying to attempt a log in through API", async function () {
testEnvironment.testUseMockAuth = 1;
testEnvironment.bruteForceBlockThisIp = 1;
delete testEnvironment.bruteForceBlockIps;
delete testEnvironment.queryParamOverride;
testEnvironment.save();
await page.goto(apiAuthUrl);
expect(await page.screenshot({ fullPage: true })).to.matchImage('bruteforcelog_blockedapi');
});
it("should show error when trying to log in through logme", async function () {
testEnvironment.testUseMockAuth = 0;
testEnvironment.bruteForceBlockThisIp = 1;
delete testEnvironment.bruteForceBlockIps;
delete testEnvironment.queryParamOverride;
testEnvironment.save();
await page.goto(formlessLoginUrl);
expect(await page.screenshot({ fullPage: true })).to.matchImage('bruteforcelog_blockedlogme');
});
it("should show invalid host warning if redirect url is not trusted in logme", async function () {
testEnvironment.overrideConfig('General', 'login_allow_logme', '1')
testEnvironment.testUseMockAuth = 0;
testEnvironment.save();
await page.goto(formlessLoginUrl + "&url="+encodeURIComponent("https://www.matomo.org/security"));
expect(await page.screenshot({ fullPage: true })).to.matchImage('logme_redirect_invalid');
});
it("should redirect if host is trusted in logme", async function () {
testEnvironment.overrideConfig('General', 'login_allow_logme', '1');
testEnvironment.overrideConfig('General', 'trusted_hosts', ["matomo.org"]);
testEnvironment.testUseMockAuth = 0;
testEnvironment.save();
await page.goto(formlessLoginUrl + "&url="+encodeURIComponent("https://matomo.org/security/"));
expect(await page.getWholeCurrentUrl()).to.equal("https://matomo.org/security/");
});
});
|