Skip to content
Snippets Groups Projects
Commit 38d4585b authored by Thijs Kinkhorst's avatar Thijs Kinkhorst
Browse files

Add more coverage for MetadataStorageHandling.

Also do not restrict coverage calculation to this class since
the metada functions to test the various configured subclasses.
parent 650e72f2
No related branches found
No related tags found
No related merge requests found
...@@ -4,19 +4,17 @@ declare(strict_types=1); ...@@ -4,19 +4,17 @@ declare(strict_types=1);
namespace SimpleSAML\Test\Metadata; namespace SimpleSAML\Test\Metadata;
use Exception;
use SimpleSAML\Configuration; use SimpleSAML\Configuration;
use SimpleSAML\Error\MetadataNotFound;
use SimpleSAML\Metadata\MetaDataStorageHandler; use SimpleSAML\Metadata\MetaDataStorageHandler;
use SimpleSAML\TestUtils\ClearStateTestCase; use SimpleSAML\TestUtils\ClearStateTestCase;
/**
* @covers \SimpleSAML\Metadata\MetadataStorageHandler
*/
class MetaDataStorageHandlerTest extends ClearStateTestCase class MetaDataStorageHandlerTest extends ClearStateTestCase
{ {
/** protected $handler;
* Test that loading specific entities works, and that metadata source precedence is followed
*/ public function setUp(): void
public function testLoadEntities(): void
{ {
$c = [ $c = [
'metadata.sources' => [ 'metadata.sources' => [
...@@ -25,9 +23,15 @@ class MetaDataStorageHandlerTest extends ClearStateTestCase ...@@ -25,9 +23,15 @@ class MetaDataStorageHandlerTest extends ClearStateTestCase
], ],
]; ];
Configuration::loadFromArray($c, '', 'simplesaml'); Configuration::loadFromArray($c, '', 'simplesaml');
$handler = MetaDataStorageHandler::getMetadataHandler(); $this->handler = MetaDataStorageHandler::getMetadataHandler();
}
$entities = $handler->getMetaDataForEntities([ /**
* Test that loading specific entities works, and that metadata source precedence is followed
*/
public function testLoadEntities(): void
{
$entities = $this->handler->getMetaDataForEntities([
'entityA', 'entityA',
'entityB', 'entityB',
'nosuchEntity', 'nosuchEntity',
...@@ -47,5 +51,103 @@ class MetaDataStorageHandlerTest extends ClearStateTestCase ...@@ -47,5 +51,103 @@ class MetaDataStorageHandlerTest extends ClearStateTestCase
$entities['expiredInSrc1InSrc2']['name']['en'], $entities['expiredInSrc1InSrc2']['name']['en'],
"Entity is in both sources, expired in src1 and available from src2" "Entity is in both sources, expired in src1 and available from src2"
); );
// Did not ask for this one, which is in source1
$this->assertArrayNotHasKey('http://localhost/simplesaml', $entities);
}
/**
* Test that retrieving a full metadataSet from a source works and precedence works
*/
public function testLoadMetadataSet(): void
{
$entities = $this->handler->getList('saml20-sp-remote');
$this->assertCount(5, $entities);
$this->assertEquals('entityA SP from source1', $entities['entityA']['name']['en']);
$this->assertEquals('entityB SP from source2', $entities['entityB']['name']['en']);
$this->assertEquals(
'entityInBoth SP from source1',
$entities['entityInBoth']['name']['en'],
"Entity is in both sources, but should get loaded from the first"
);
$this->assertEquals(
'expiredInSrc1InSrc2 SP from source2',
$entities['expiredInSrc1InSrc2']['name']['en'],
"Entity is in both sources, expired in src1 and available from src2"
);
$this->assertEquals('entityA SP from source1', $entities['entityA']['name']['en']);
$this->assertEquals('hostname SP from source1', $entities['http://localhost/simplesaml']['name']['en']);
}
/**
* Query from a metadata set for which we have no entities should be empty
*/
public function testLoadMetadataSetEmpty(): void
{
$entities = $this->handler->getList('saml20-idp-remote');
$this->assertCount(0, $entities);
}
/**
* Test the current metadata entity selection
*/
public function testGetMetadataCurrent(): void
{
$entity = $this->handler->getMetaDataCurrent('saml20-sp-remote');
$this->assertEquals('http://localhost/simplesaml', $entity['entityid']);
}
/**
* Test the helper that returns the metadata as a Configuration object
*/
public function testGetMetadataConfig(): void
{
$entity = $this->handler->getMetaDataConfig('entityA', 'saml20-sp-remote');
$this->assertInstanceOf(Configuration::class, $entity);
$this->assertEquals('entityA', $entity->getValue('entityid'));
}
/**
* Test the helper that searches metadata by sha1 hash
*/
public function testGetMetadataConfigForSha1(): void
{
$hash = sha1('entityB');
$entity = $this->handler->getMetaDataConfigForSha1($hash, 'saml20-sp-remote');
$this->assertInstanceOf(Configuration::class, $entity);
$this->assertEquals('entityB', $entity->getValue('entityid'));
}
/**
* Test the helper that searches metadata by sha1 hash
*/
public function testGetMetadataConfigForSha1NotFoundReturnsNull(): void
{
$hash = sha1('entitynotexist');
$entity = $this->handler->getMetaDataConfigForSha1($hash, 'saml20-sp-remote');
$this->assertNull($entity);
}
/**
* Test the current metadata entity selection, empty set
*/
public function testGetMetadataCurrentEmptySet(): void
{
$this->expectException(Exception::class, 'Could not find any default metadata');
$this->handler->getMetaDataCurrent('saml20-idp-remote');
}
/**
* Test that trying to fetch a non-existent entity throws Exception
*/
public function testGetMetaDataNonExistentEntity(): void
{
$this->expectException(MetadataNotFound::class, "METADATANOTFOUND('%ENTITYID%' => 'doesnotexist')");
$this->handler->getMetaData('doesnotexist', 'saml20-sp-remote');
} }
} }
...@@ -9,11 +9,6 @@ use PHPUnit\Framework\TestCase; ...@@ -9,11 +9,6 @@ use PHPUnit\Framework\TestCase;
use SimpleSAML\Configuration; use SimpleSAML\Configuration;
use SimpleSAML\Metadata\MetaDataStorageSource; use SimpleSAML\Metadata\MetaDataStorageSource;
/**
* Class MetaDataStorageSourceTest
*
* @covers \SimpleSAML\Metadata\MetadataStorageSource
*/
class MetaDataStorageSourceTest extends TestCase class MetaDataStorageSourceTest extends TestCase
{ {
/** /**
......
...@@ -62,3 +62,23 @@ $metadata['expiredInSrc1InSrc2'] = [ ...@@ -62,3 +62,23 @@ $metadata['expiredInSrc1InSrc2'] = [
], ],
] ]
]; ];
$metadata['http://localhost/simplesaml'] = [
'entityid' => 'http://localhost/simplesaml',
'host' => 'localhost',
'name' =>
[
'en' => 'hostname SP from source1',
],
'metadata-set' => 'saml20-sp-remote',
'AssertionConsumerService' =>
[
0 =>
[
'Binding' => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
'Location' => 'https://hostname.example.org/Shibboleth.sso/SAML2/POST',
'index' => 1,
'isDefault' => true,
],
]
];
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment