YubikeyLoginHandlerTest::testDoLoginBackURL()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: simon
5
 * Date: 05-May-18
6
 * Time: 15:47
7
 */
8
9
namespace Firesphere\YubiAuth\Tests;
10
11
use Firesphere\BootstrapMFA\Authenticators\BootstrapMFAAuthenticator;
12
use Firesphere\YubiAuth\Authenticators\YubikeyMemberAuthenticator;
13
use Firesphere\YubiAuth\Forms\YubikeyForm;
14
use Firesphere\YubiAuth\Forms\YubikeyLoginForm;
15
use Firesphere\YubiAuth\Handlers\YubikeyLoginHandler;
16
use SilverStripe\Control\HTTPRequest;
17
use SilverStripe\Control\Session;
18
use SilverStripe\Core\Injector\Injector;
19
use SilverStripe\Dev\SapphireTest;
20
use SilverStripe\Security\Security;
21
22
class YubikeyLoginHandlerTest extends SapphireTest
23
{
24
    protected static $fixture_file = '../fixtures/Member.yml';
25
26
    /**
27
     * @var YubikeyLoginHandler
28
     */
29
    protected $handler;
30
31
    public function testLoginForm()
32
    {
33
        $form = $this->handler->LoginForm();
34
35
        $this->assertInstanceOf(YubikeyLoginForm::class, $form);
36
    }
37
38
    public function testSecondFactor()
39
    {
40
        $result = $this->handler->secondFactor();
41
42
        $this->assertTrue(is_array($result));
43
        $this->assertInstanceOf(YubikeyForm::class, $result['Form']);
44
    }
45
46
    public function testYubikeyForm()
47
    {
48
        $this->assertInstanceOf(YubikeyForm::class, $this->handler->yubikeyForm());
49
    }
50
51
    public function testMFAForm()
52
    {
53
        $mfaForm = $this->handler->MFAForm();
54
        $this->assertInstanceOf(YubikeyForm::class, $mfaForm);
55
    }
56
57
    public function testDoLoginBackURL()
58
    {
59
        $backURL = '/blablabla';
60
        $request = new HTTPRequest('POST', '/');
61
        $request->setSession(new Session(['hi' => 'bye']));
62
        $this->handler->setRequest($request);
63
        $form = Injector::inst()->get(
64
            YubikeyLoginForm::class,
65
            true,
66
            [$this->handler, YubikeyMemberAuthenticator::class, '']
67
        );
68
        $response = $this->handler->doLogin(
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
69
            [
70
71
                'Email'    => '[email protected]',
72
                'Password' => 'password',
73
                'BackURL'  => $backURL
74
            ],
75
            $form,
76
            $request
77
        );
78
79
        $data = $request->getSession()->get(BootstrapMFAAuthenticator::SESSION_KEY);
80
        $this->assertEquals($backURL, $data['BackURL']);
81
        $this->assertEquals($backURL, $data['Data']['BackURL']);
82
    }
83
84
    public function testDoLoginUnsuccessful()
85
    {
86
        $request = new HTTPRequest('POST', Security::login_url());
87
        $request->setSession(new Session(['hi' => 'bye']));
88
        $this->handler->setRequest($request);
89
        $form = Injector::inst()->get(
90
            YubikeyLoginForm::class,
91
            true,
92
            [$this->handler, YubikeyMemberAuthenticator::class, '']
93
        );
94
        $response = $this->handler->doLogin(
95
            [
96
97
                'Email'    => '[email protected]',
98
                'Password' => 'incorrect',
99
            ],
100
            $form,
101
            $request
102
        );
103
        $this->assertContains('login', $response->getHeader('location'));
104
    }
105
106
    protected function setUp()
107
    {
108
        $this->handler = Injector::inst()->createWithArgs(
109
            YubikeyLoginHandler::class,
110
            [Security::login_url(), new YubikeyMemberAuthenticator()]
111
        );
112
113
        return parent::setUp(); // TODO: Change the autogenerated stub
114
    }
115
}
116