Passed
Push — master ( d12a36...d0848f )
by Ross
22:44
created

testQrCodeOnSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
/**
3
 * A two factor authentication module that protects both the admin and customer logins
4
 * Copyright (C) 2017  Ross Mitchell
5
 *
6
 * This file is part of Rossmitchell/Twofactor.
7
 *
8
 * Rossmitchell/Twofactor is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace Rossmitchell\Twofactor\Tests\Integration\Admin\EnterCodePage;
23
24
use Magento\User\Model\User;
25
use Rossmitchell\Twofactor\Tests\Integration\Abstracts\AbstractTestClass;
26
use Rossmitchell\Twofactor\Tests\Integration\FixtureLoader\Traits\AdminUserLoader;
27
use Rossmitchell\Twofactor\Tests\Integration\FixtureLoader\Traits\ConfigurationLoader;
28
29
class QRCodeShouldNotChangeIfAlreadyExistsTest extends AbstractTestClass
30
{
31
    use AdminUserLoader;
32
    use ConfigurationLoader;
33
34
    public static function getAdminUserDataPath()
35
    {
36
        return __DIR__ . '/../_files/adminUser.php';
37
    }
38
39
    public static function getConfigurationDataPath()
40
    {
41
        return __DIR__ . '/../_files/two_factor_enabled.php';
42
    }
43
44
    /**
45
     * @magentoDbIsolation   enabled
46
     * @magentoDataFixture   loadAdminUsers
47
     * @magentoDataFixture   loadConfiguration
48
     */
49
    public function testQrCodeOnSave()
50
    {
51
        $this->disableSecretKeys();
52
        $this->loginAdmin('two_factor_enabled', 'password123');
53
        /** @var User $currentUser */
54
        $currentUser = $this->createObject(User::class);
55
        $currentUser->loadByUsername('two_factor_enabled');
56
        $currentSecret = $currentUser->getData('two_factor_secret');
57
        $this->assertNotEmpty($currentSecret);
58
        $this->setPostParams(1, 'password123');
59
        $this->dispatch('/backend/admin/system_account/save');
60
        /** @var User $user */
61
        $user = $this->createObject(User::class);
62
        $user->loadByUsername('two_factor_enabled');
63
        $this->assertEquals($currentSecret, $user->getData('two_factor_secret'));
64
    }
65
66
    private function setPostParams($useTwoFactor, $password)
67
    {
68
        $adminUser = $this->getAdminSession()->getUser();
69
70
        $this->getRequest()
71
            ->setMethod('POST')
72
            ->setParam('form_key', $this->getFormKey())
73
            ->setParam('username', $adminUser->getUserName())
74
            ->setParam('firstname', $adminUser->getFirstName())
75
            ->setParam('lastname', $adminUser->getLastName())
76
            ->setParam('user_id', $adminUser->getId())
77
            ->setParam('email', $adminUser->getEmail())
78
            ->setParam('password', '')
79
            ->setParam('password_confirmation', '')
80
            ->setParam('interface_locale', $adminUser->getInterfaceLocale())
81
            ->setParam('use_two_factor', "$useTwoFactor")
82
            ->setParam('current_password', $password);
83
    }
84
}
85