BootstrapMFAAuthenticatorTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidateBackupCodeWrong() 0 8 1
A testValidateBackupCodeRight() 0 24 2
A testGetLoginHandler() 0 5 1
A setUp() 0 6 1
1
<?php
2
3
namespace Firesphere\BootstrapMFA\Tests;
4
5
use Firesphere\BootstrapMFA\Authenticators\BootstrapMFAAuthenticator;
6
use Firesphere\BootstrapMFA\Generators\CodeGenerator;
7
use Firesphere\BootstrapMFA\Handlers\BootstrapMFALoginHandler;
8
use Firesphere\BootstrapMFA\Models\BackupCode;
9
use Firesphere\BootstrapMFA\Tests\Helpers\CodeHelper;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\ORM\ValidationResult;
14
use SilverStripe\Security\IdentityStore;
15
use SilverStripe\Security\Member;
16
use SilverStripe\Security\Security;
17
18
class BootstrapMFAAuthenticatorTest extends SapphireTest
19
{
20
    protected static $fixture_file = '../fixtures/member.yml';
21
22
    /**
23
     * @var BootstrapMFAAuthenticator
24
     */
25
    protected $authenticator;
26
27
    /**
28
     * Test if user codes are properly validated and expired
29
     *
30
     * @throws \Psr\Container\NotFoundExceptionInterface
31
     * @throws \SilverStripe\ORM\ValidationException
32
     * @throws \SilverStripe\Security\PasswordEncryptor_NotFoundException
33
     */
34
    public function testValidateBackupCodeRight()
35
    {
36
        /** @var Member $member */
37
        $member = $this->objFromFixture(Member::class, 'member1');
38
        Injector::inst()->get(IdentityStore::class)->logIn($member);
39
        BackupCode::generateTokensForMember($member);
40
41
        $codes = CodeHelper::getCodesFromSession();
42
        $length = Config::inst()->get(CodeGenerator::class, 'length');
43
44
        // Actual testing
45
        foreach ($codes as $code) {
46
            $this->assertEquals($length, strlen($code));
47
            $member = $this->authenticator->validateBackupCode($member, $code, $result);
48
            // All codes should be valid
49
            $this->assertTrue($result->isValid());
50
            $this->assertInstanceOf(Member::class, $member);
51
52
            $encryptedCode = Security::encrypt_password($code, $member->BackupCodeSalt);
53
54
            /** @var BackupCode $code */
55
            $code = BackupCode::get()->filter(['Code' => $encryptedCode['password']])->first();
56
57
            $this->assertTrue((bool)$code->Used);
58
        }
59
    }
60
61
    public function testValidateBackupCodeWrong()
62
    {
63
        $member = $this->objFromFixture(Member::class, 'member1');
64
65
        $this->authenticator->validateBackupCode($member, '12345', $result);
66
67
        $this->assertInstanceOf(ValidationResult::class, $result);
68
        $this->assertFalse($result->isValid());
69
    }
70
71
    public function testGetLoginHandler()
72
    {
73
        $handler = $this->authenticator->getLoginHandler('/Security/login');
74
75
        $this->assertInstanceOf(BootstrapMFALoginHandler::class, $handler);
76
    }
77
78
    protected function setUp()
79
    {
80
        $this->authenticator = Injector::inst()->get(BootstrapMFAAuthenticator::class);
81
        Config::modify()->set(BackupCode::class, 'token_limit', 3);
82
83
        return parent::setUp();
84
    }
85
}
86