TOTPAuthenticatorTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidateTOTPWithValidData() 0 9 1
A algorithmProvider() 0 6 1
A testValidateTOTPWithoutToken() 0 6 1
A testValidateTOTPReturnsValidationResultOnFailure() 0 6 1
A setUp() 0 16 1
A testValidateTOTPWithMismatchingKeyProvided() 0 6 1
A testGetAlgorithm() 0 5 1
1
<?php
2
3
namespace ElliotSawyer\TOTPAuthenticator\Tests\Authenticators;
4
5
use ElliotSawyer\TOTPAuthenticator\TOTPAuthenticator;
6
use Firesphere\BootstrapMFA\Authenticators\BootstrapMFAAuthenticator;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Control\Session;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\ORM\ValidationResult;
12
use SilverStripe\Security\Member;
13
14
class TOTPAuthenticatorTest extends SapphireTest
15
{
16
    protected static $fixture_file = 'TOTPAuthenticatorTest.yml';
17
18
    /**
19
     * @var HTTPRequest
20
     */
21
    protected $request;
22
23
    /**
24
     * @var ValidationResult
25
     */
26
    protected $result;
27
28
    /**
29
     * @var TOTPAuthenticator
30
     */
31
    protected $authenticator;
32
33
    protected function setUp()
34
    {
35
        parent::setUp();
36
37
        $this->request = new HTTPRequest('GET', '/');
38
        $this->request->setSession(new Session([]));
39
40
        $this->result = new ValidationResult();
41
42
        $this->authenticator = $this->getMockBuilder(TOTPAuthenticator::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(El...nFromTOTP'))->getMock() of type PHPUnit_Framework_MockObject_MockObject is incompatible with the declared type ElliotSawyer\TOTPAuthenticator\TOTPAuthenticator of property $authenticator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
            ->setMethods(['getTokenFromTOTP'])
44
            ->getMock();
45
46
        // Assign a member ID from the fixtures to the session
47
        $memberId = $this->idFromFixture(Member::class, 'admin_user');
48
        $this->request->getSession()->set(BootstrapMFAAuthenticator::SESSION_KEY . '.MemberID', $memberId);
49
    }
50
51
    /**
52
     * @todo is this actually desired behaviour?
53
     */
54
    public function testValidateTOTPReturnsValidationResultOnFailure()
55
    {
56
        $this->request->getSession()->clearAll();
57
        $result = $this->authenticator->validateTOTP([], $this->request, $this->result);
58
59
        $this->assertInstanceOf(ValidationResult::class, $result);
60
    }
61
62
    public function testValidateTOTPWithoutToken()
63
    {
64
        $this->authenticator->validateTOTP([], $this->request, $this->result);
65
66
        $this->assertFalse($this->result->isValid(), 'Missing input data should cause an error');
67
        $this->assertContains('No token sent', $this->result->serialize());
68
    }
69
70
    public function testValidateTOTPWithMismatchingKeyProvided()
71
    {
72
        $this->authenticator->validateTOTP(['token' => 'willnotmatch'], $this->request, $this->result);
73
74
        $this->assertFalse($this->result->isValid(), 'Mismatching token should cause an error');
75
        $this->assertContains('TOTP Failed', $this->result->serialize());
76
    }
77
78
    public function testValidateTOTPWithValidData()
79
    {
80
        $this->authenticator->expects($this->once())->method('getTokenFromTOTP')->willReturn('123456');
0 ignored issues
show
Bug introduced by
The method expects() does not exist on ElliotSawyer\TOTPAuthenticator\TOTPAuthenticator. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        $this->authenticator->/** @scrutinizer ignore-call */ 
81
                              expects($this->once())->method('getTokenFromTOTP')->willReturn('123456');
Loading history...
81
        $memberToken = '123456';
82
83
        $result = $this->authenticator->validateTOTP(['token' => $memberToken], $this->request, $this->result);
84
85
        $this->assertTrue($this->result->isValid(), 'Valid TOTP token should validate successfully');
86
        $this->assertInstanceOf(Member::class, $result, 'The member object should be returned on success');
87
    }
88
89
    /**
90
     * @param string $configuredAlgorithm
91
     * @param string $expected
92
     * @dataProvider algorithmProvider
93
     */
94
    public function testGetAlgorithm($configuredAlgorithm, $expected)
95
    {
96
        Config::modify()->set(TOTPAuthenticator::class, 'algorithm', $configuredAlgorithm);
97
98
        $this->assertSame($expected, TOTPAuthenticator::get_algorithm());
99
    }
100
101
    /**
102
     * @return array[]
103
     */
104
    public function algorithmProvider()
105
    {
106
        return [
107
            'valid algorithm' => ['sha256', 'sha256'],
108
            'another valid algorithm' => ['sha512', 'sha512'],
109
            'invalid algorithm' => ['foo123', 'sha1'],
110
        ];
111
    }
112
}
113