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( |
||
|
|||
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() |
||
121 | ); |
||
122 | } |
||
123 | |||
124 | public function setUpOnce() |
||
125 | { |
||
161 |