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
|
/*!
* Piwik - free/libre analytics platform
*
* Screenshot integration tests.
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
describe("TwoFactorAuth", function () {
this.timeout(0);
this.fixture = "Piwik\\Plugins\\TwoFactorAuth\\tests\\Fixtures\\TwoFactorFixture";
var generalParams = 'idSite=1&period=day&date=2010-01-03',
userSettings = '?module=UsersManager&action=userSettings&' + generalParams,
logoutUrl = '?module=Login&action=logout&period=day&date=yesterday';
async function selectModalButton(button)
{
await (await page.jQuery('.modal.open .modal-footer a:contains('+button+')')).click();
await page.waitForNetworkIdle();
}
async function loginUser(username, doAuth)
{
// make sure to log out previous session
await page.goto(logoutUrl);
var cookies = await page.cookies();
cookies.forEach(cookie => {
page.deleteCookie(cookie);
});
if (typeof doAuth === 'undefined') {
doAuth = true;
}
var logMeUrl = '?module=Login&action=logme&login=' + username + '&password=240161a241087c28d92d8d7ff3b6186b';
if (doAuth) {
logMeUrl += '&authCode=123456'; // we make sure in test config this code always works
}
await page.waitFor(1000);
await page.goto(logMeUrl);
}
function requireTwoFa() {
testEnvironment.requireTwoFa = 1;
testEnvironment.save();
}
function fakeCorrectAuthCode() {
testEnvironment.fakeCorrectAuthCode = 1;
testEnvironment.save();
}
before(function () {
testEnvironment.pluginsToLoad = ['TwoFactorAuth'];
testEnvironment.queryParamOverride = { date: '2018-03-04' };
testEnvironment.save();
});
beforeEach(function () {
testEnvironment.testUseMockAuth = 0;
testEnvironment.restoreRecoveryCodes = 1;
testEnvironment.save();
});
afterEach(function () {
delete testEnvironment.requireTwoFa;
delete testEnvironment.restoreRecoveryCodes;
delete testEnvironment.fakeCorrectAuthCode;
testEnvironment.testUseMockAuth = 1;
testEnvironment.save();
});
async function confirmPassword()
{
await page.waitFor('.confirmPasswordForm');
await page.evaluate(function(){
$('.confirmPasswordForm #login_form_password').val('123abcDk3_l3');
$('.confirmPasswordForm #login_form_submit').click();
});
await page.waitForNetworkIdle();
await page.waitFor(100);
}
it('a user with 2fa can open the widgetized view by token without needing to verify', async function () {
await page.goto('?module=Widgetize&action=iframe&moduleToWidgetize=Actions&actionToWidgetize=getPageUrls&date=2018-03-04&token_auth=c4ca4238a0b923820dcc509a6f75849b&' + generalParams);
expect(await page.screenshotSelector('.widget')).to.matchImage('widgetized_no_verify');
});
it('when logging in through logme and not providing auth code it should show auth code screen', async function () {
await loginUser('with2FA', false);
expect(await page.screenshotSelector('.loginSection')).to.matchImage('logme_not_verified');
});
it('when logging in and providing wrong code an error is shown', async function () {
await page.type('.loginTwoFaForm #login_form_authcode', '555555');
await page.evaluate(function(){
$('.loginTwoFaForm #login_form_submit').click();
});
await page.waitForNetworkIdle();
const element = await page.$('.loginSection');
expect(await element.screenshot()).to.matchImage('logme_not_verified_wrong_code');
});
it('when logging in through logme and verifying screen it works to access ui', async function () {
await page.type('.loginTwoFaForm #login_form_authcode', '123456');
await page.evaluate(function(){
$('.loginTwoFaForm #login_form_submit').click();
});
await page.waitForNetworkIdle();
await page.waitFor('.widget');
await page.waitForNetworkIdle();
expect(await page.screenshotSelector('.pageWrap')).to.matchImage('logme_verified');
});
it('should show user settings when two-fa enabled', async function () {
await loginUser('with2FA');
await page.goto(userSettings);
await page.waitFor('.userSettings2FA', { visible: true });
await page.waitFor(500); // animation
const elem = await page.$('.userSettings2FA');
expect(await elem.screenshot()).to.matchImage('usersettings_twofa_enabled');
});
it('should be possible to show recovery codes step1 authentication', async function () {
await page.click('.showRecoveryCodesLink');
await page.waitForNetworkIdle();
expect(await page.screenshotSelector('.loginSection')).to.matchImage('show_recovery_codes_step1');
});
it('should be possible to show recovery codes step2 done', async function () {
await confirmPassword();
await page.waitForNetworkIdle();
expect(await page.screenshotSelector('#content')).to.matchImage('show_recovery_codes_step2');
});
it('should show user settings when two-fa enabled', async function () {
requireTwoFa();
await page.goto(userSettings);
expect(await page.screenshotSelector('.userSettings2FA')).to.matchImage('usersettings_twofa_enabled_required');
});
it('should be possible to disable two factor', async function () {
await loginUser('with2FADisable');
await page.goto(userSettings);
await page.click('.disable2FaLink');
const modal = await page.$('.modal.open');
expect(await modal.screenshot()).to.matchImage('usersettings_twofa_disable_step1');
});
it('should be possible to disable two factor step 2 confirmed', async function () {
await selectModalButton('Yes');
await page.waitFor(150);
expect(await page.screenshotSelector('.loginSection')).to.matchImage('usersettings_twofa_disable_step2');
});
it('should be possible to disable two factor step 3 verified', async function () {
await confirmPassword();
await page.waitFor('.userSettings2FA');
const elem = await page.$('.userSettings2FA');
expect(await elem.screenshot()).to.matchImage('usersettings_twofa_disable_step3');
});
it('should show setup screen - step 1', async function () {
await loginUser('without2FA');
await page.goto(userSettings);
await page.click('.enable2FaLink');
await confirmPassword();
expect(await page.screenshotSelector('#content')).to.matchImage('twofa_setup_step1');
});
it('should move to second step in setup - step 2', async function () {
await page.evaluate(function(){
$('.setupTwoFactorAuthentication .backupRecoveryCode:first').click();
});
await page.waitForNetworkIdle();
await page.click('.setupTwoFactorAuthentication .goToStep2');
await page.waitForNetworkIdle();
await page.evaluate(function () {
$('#qrcode').hide();
});
expect(await page.screenshotSelector('#content')).to.matchImage('twofa_setup_step2');
});
it('should move to third step in setup - step 3', async function () {
await page.click('.setupTwoFactorAuthentication .goToStep3');
await page.waitForNetworkIdle();
expect(await page.screenshotSelector('#content')).to.matchImage('twofa_setup_step3');
});
it('should move to third step in setup - step 4 confirm', async function () {
fakeCorrectAuthCode();
await page.type('.setupConfirmAuthCodeForm input[type=text]', '123458');
await page.evaluate(function () {
$('.setupConfirmAuthCodeForm input[type=text]').change();
});
await page.evaluate(function () {
$('.setupConfirmAuthCodeForm .confirmAuthCode').click();
});
await page.waitForNetworkIdle();
await page.waitFor('#content', { visible: true });
await page.waitForNetworkIdle();
expect(await page.screenshotSelector('#content')).to.matchImage('twofa_setup_step4');
});
it('should force user to setup 2fa when not set up yet but enforced', async function () {
requireTwoFa();
await loginUser('no2FA', false);
expect(await page.screenshotSelector('.loginSection,#content,#notificationContainer')).to.matchImage('twofa_forced_step1');
});
it('should force user to setup 2fa when not set up yet but enforced step 2', async function () {
await (await page.jQuery('.setupTwoFactorAuthentication .backupRecoveryCode:first')).click();
await page.click('.setupTwoFactorAuthentication .goToStep2');
await page.mouse.move(-10, -10);
await page.waitFor(100);
expect(await page.screenshotSelector('.loginSection,#content,#notificationContainer')).to.matchImage('twofa_forced_step2');
});
it('should force user to setup 2fa when not set up yet but enforced step 3', async function () {
await page.click('.setupTwoFactorAuthentication .goToStep3');
await page.mouse.move(-10, -10);
await page.waitFor(100);
expect(await page.screenshotSelector('.loginSection,#content,#notificationContainer')).to.matchImage('twofa_forced_step3');
});
it('should force user to setup 2fa when not set up yet but enforced confirm code', async function () {
requireTwoFa();
fakeCorrectAuthCode();
await page.type('.setupConfirmAuthCodeForm input[type=text]', '123458');
await page.evaluate(function () {
$('.setupConfirmAuthCodeForm input[type=text]').change();
});
await page.evaluate(function () {
$('.setupConfirmAuthCodeForm .confirmAuthCode').click();
});
await page.waitForNetworkIdle();
expect(await page.screenshotSelector('.loginSection,#content,#notificationContainer')).to.matchImage('twofa_forced_step4');
});
});
|