Passed
Pull Request — master (#23)
by
unknown
04:26
created

RealMeServiceTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 6
dl 0
loc 128
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetAuthCustomAuthnContext() 0 11 1
A testGetCertificateContents() 0 42 1
A testGetAuth() 0 21 1
A testGetAuthCustomIdPEntityId() 0 9 1
A testGetAuthCustomSPEntityId() 0 9 1
A setUpOnce() 0 6 1
1
<?php
2
3
namespace SilverStripe\RealMe\Tests;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Core\Environment;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\RealMe\RealMeService;
10
11
class RealMeServiceTest extends SapphireTest
12
{
13
    private $pathForTempCertificate;
14
15
    /**
16
     * @var RealMeService
17
     */
18
    private $service;
19
20
    public function testGetCertificateContents()
21
    {
22
        $this->pathForTempCertificate = ASSETS_PATH . '/tmpcert.pem';
23
24
        /**
25
         * Test standard certificate
26
         */
27
28
        $contents = file_get_contents(BASE_PATH . '/realme/tests/certs/standard_cert.pem');
29
30
        // Strip carriage returns
31
        $contents = str_replace("\r", '', $contents);
32
33
        $path = $this->pathForTempCertificate;
34
        file_put_contents($path, $contents);
35
36
        /** @var RealMeService $service */
37
        $service = Injector::inst()->get(RealMeService::class);
38
39
        $this->assertEquals('Redacted private key goes here', $service->getCertificateContents($path, 'key'));
40
        $this->assertEquals('Redacted certificate goes here', $service->getCertificateContents($path, 'certificate'));
41
42
        unlink($path);
43
44
        /**
45
         * Test certificate with RSA private key
46
         */
47
48
        $contents = file_get_contents(BASE_PATH . '/realme/tests/certs/rsa_cert.pem');
49
50
        // Strip carriage returns
51
        $contents = str_replace("\r", '', $contents);
52
53
        $path = $this->pathForTempCertificate;
54
        file_put_contents($path, $contents);
55
56
        /** @var RealMeService $service */
57
        $service = Injector::inst()->get(RealMeService::class);
58
        $this->assertEquals('Redacted private key goes here', $service->getCertificateContents($path, 'key'));
59
        $this->assertEquals('Redacted certificate goes here', $service->getCertificateContents($path, 'certificate'));
60
61
        unlink($path);
62
    }
63
64
    public function testGetAuth()
65
    {
66
        $auth = $this->service->getAuth();
67
        $this->assertTrue(get_class($auth) === 'OneLogin_Saml2_Auth');
68
69
        // Service Provider settings
70
        $spData = $auth->getSettings()->getSPData();
71
        $this->assertSame('https://example.com/realm/service', $spData['entityId']);
72
        $this->assertSame('https://example.com/Security/realme/acs', $spData['assertionConsumerService']['url']);
73
        $this->assertSame('urn:oasis:names:tc:SAML:2.0:nameid-format:persistent', $spData['NameIDFormat']);
74
75
        // Identity Provider settings
76
        $idpData = $auth->getSettings()->getIdPData();
77
        $this->assertSame('https://mts.realme.govt.nz/saml2', $idpData['entityId']);
78
        $this->assertSame('https://mts.realme.govt.nz/logon-mts/mtsEntryPoint', $idpData['singleSignOnService']['url']);
79
80
        // Security settings
81
        $securityData = $auth->getSettings()->getSecurityData();
82
        $this->assertSame(
83
            'urn:nzl:govt:ict:stds:authn:deployment:GLS:SAML:2.0:ac:classes:LowStrength',
84
            $securityData['requestedAuthnContext'][0]
85
        );
86
    }
87
88
    public function testGetAuthCustomSPEntityId()
89
    {
90
        Config::inst()->update(
0 ignored issues
show
Bug introduced by
The method update() does not exist on SilverStripe\Config\Coll...nfigCollectionInterface. It seems like you code against a sub-type of SilverStripe\Config\Coll...nfigCollectionInterface such as SilverStripe\Config\Coll...\MemoryConfigCollection. ( Ignorable by Annotation )

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

90
        Config::inst()->/** @scrutinizer ignore-call */ update(
Loading history...
91
            RealMeService::class,
92
            'sp_entity_ids',
93
            ['mts' => 'https://example.com/custom-realm/custom-service']
94
        );
95
        $spData = $this->service->getAuth()->getSettings()->getSPData();
96
        $this->assertSame('https://example.com/custom-realm/custom-service', $spData['entityId']);
97
    }
98
99
    public function testGetAuthCustomIdPEntityId()
100
    {
101
        Config::inst()->update(
102
            RealMeService::class,
103
            'idp_entity_ids',
104
            ['mts' => ['login' => 'https://example.com/idp-entry']]
105
        );
106
        $idpData = $this->service->getAuth()->getSettings()->getIdPData();
107
        $this->assertSame('https://example.com/idp-entry', $idpData['entityId']);
108
    }
109
110
    public function testGetAuthCustomAuthnContext()
111
    {
112
        Config::inst()->update(
113
            RealMeService::class,
114
            'authn_contexts',
115
            ['mts' => 'urn:nzl:govt:ict:stds:authn:deployment:GLS:SAML:2.0:ac:classes:ModStrength::OTP:Mobile:SMS']
116
        );
117
        $securityData = $this->service->getAuth()->getSettings()->getSecurityData();
118
        $this->assertSame(
119
            'urn:nzl:govt:ict:stds:authn:deployment:GLS:SAML:2.0:ac:classes:ModStrength::OTP:Mobile:SMS',
120
            $securityData['requestedAuthnContext'][0]
121
        );
122
    }
123
124
    public function setUpOnce()
125
    {
126
        parent::setUpOnce();
0 ignored issues
show
Bug introduced by
The method setUpOnce() does not exist on SilverStripe\Dev\SapphireTest. Did you maybe mean setUp()? ( Ignorable by Annotation )

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

126
        parent::/** @scrutinizer ignore-call */ 
127
                setUpOnce();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
128
        Environment::putEnv('REALME_CERT_DIR', BASE_PATH . '/realme/tests/certs');
0 ignored issues
show
Unused Code introduced by
The call to SilverStripe\Core\Environment::putEnv() has too many arguments starting with SilverStripe\RealMe\Test.... '/realme/tests/certs'. ( Ignorable by Annotation )

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

128
        Environment::/** @scrutinizer ignore-call */ 
129
                     putEnv('REALME_CERT_DIR', BASE_PATH . '/realme/tests/certs');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
129
        Environment::putEnv('REALME_SIGNING_CERT_FILENAME', 'standard_cert.pem');
130
    }
131
132
    public function setUp()
133
    {
134
        parent::setUp();
135
        $this->service = Injector::inst()->get(RealMeService::class);
136
137
        // Configure for login integration and mts by default
138
        Config::inst()->update(RealMeService::class, 'sp_entity_ids', ['mts' => 'https://example.com/realm/service']);
139
        Config::inst()->update(
140
            RealMeService::class,
141
            'metadata_assertion_service_domains',
142
            ['mts' => 'https://example.com']
143
        );
144
        Config::inst()->update(
145
            RealMeService::class,
146
            'authn_contexts',
147
            ['mts' => 'urn:nzl:govt:ict:stds:authn:deployment:GLS:SAML:2.0:ac:classes:LowStrength']
148
        );
149
    }
150
151
    public function tearDownOnce()
152
    {
153
        parent::tearDownOnce();
0 ignored issues
show
Bug introduced by
The method tearDownOnce() does not exist on SilverStripe\Dev\SapphireTest. Did you maybe mean tearDown()? ( Ignorable by Annotation )

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

153
        parent::/** @scrutinizer ignore-call */ 
154
                tearDownOnce();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
154
155
        // Ensure $this->pathForTempCertificate is unlink'd (otherwise it won't get unlinked if the test fails)
156
        if (file_exists($this->pathForTempCertificate)) {
157
            unlink($this->pathForTempCertificate);
158
        }
159
    }
160
}
161