Completed
Push — master ( 707825...e17347 )
by Garion
14s queued 10s
created

RegisterHandlerTest::hostProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 12
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\WebAuthn\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\MFA\Store\SessionStore;
7
use SilverStripe\Security\Member;
8
use SilverStripe\WebAuthn\RegisterHandler;
9
use Webauthn\PublicKeyCredentialCreationOptions;
10
11
class RegisterHandlerTest extends SapphireTest
12
{
13
    protected $usesDatabase = true;
14
15
    /**
16
     * @var RegisterHandler
17
     */
18
    protected $handler;
19
20
    /**
21
     * @var Member
22
     */
23
    protected $member;
24
25
    /**
26
     * @var array
27
     */
28
    protected $originalServer;
29
30
    protected function setUp()
31
    {
32
        parent::setUp();
33
34
        $this->handler = new RegisterHandler();
35
36
        $memberID = $this->logInWithPermission();
37
        /** @var Member $member */
38
        $this->member = Member::get()->byID($memberID);
39
40
        $this->originalServer = $_SERVER;
41
    }
42
43
    protected function tearDown()
44
    {
45
        $_SERVER = $this->originalServer;
46
47
        parent::tearDown();
48
    }
49
50
    /**
51
     * @param string $baseUrl
52
     * @param string $expected
53
     * @dataProvider hostProvider
54
     */
55
    public function testRelyingPartyEntityDomainIncludesSilverStripeDomain(string $baseUrl, string $expected)
56
    {
57
        $_SERVER['HTTP_HOST'] = $baseUrl;
58
59
        $store = new SessionStore($this->member);
60
        $result = $this->handler->start($store);
61
62
        $this->assertArrayHasKey('keyData', $result);
63
64
        /** @var PublicKeyCredentialCreationOptions $options */
65
        $options = $result['keyData'];
66
        $this->assertInstanceOf(PublicKeyCredentialCreationOptions::class, $options);
67
68
        $relyingPartyEntity = $options->getRp();
69
        $this->assertSame(
70
            $expected,
71
            $relyingPartyEntity->getId(),
72
            'Relying party entity should identify the current SilverStripe domain'
73
        );
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function hostProvider(): array
80
    {
81
        return [
82
            'domain only' => ['http://example.com', 'example.com'],
83
            'domain with port' => ['https://example.com:8080', 'example.com'],
84
            'subdomain' => ['https://www.example.com', 'www.example.com'],
85
            'subdomain with port' => ['http://my.example.com:8887', 'my.example.com'],
86
            'subfolder' => ['https://example.com/mysite', 'example.com'],
87
            'subfolder with port' => ['http://example.com:8080/mysite', 'example.com'],
88
            'subdomain with subfolder' => ['http://my.example.com/mysite', 'my.example.com'],
89
            'subdomain with port and subfolder' => ['https://my.example.com:8080/mysite', 'my.example.com'],
90
            'credentials with domain and trailing slash' => ['http://foo:[email protected]/', 'example.com'],
91
        ];
92
    }
93
}
94