/
AffiliationDescriptorTest.php
179 lines (149 loc) · 5.7 KB
/
AffiliationDescriptorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\SAML2\XML\md;
use DateTimeImmutable;
use Exception;
use PHPUnit\Framework\TestCase;
use SimpleSAML\SAML2\Utils;
use SimpleSAML\SAML2\XML\md\AffiliateMember;
use SimpleSAML\SAML2\XML\md\AffiliationDescriptor;
use SimpleSAML\SAML2\XML\md\KeyDescriptor;
use SimpleSAML\Test\SAML2\Constants as C;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\Exception\MissingAttributeException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
use SimpleSAML\XML\Utils as XMLUtils;
use SimpleSAML\XMLSecurity\TestUtils\SignedElementTestTrait;
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
use SimpleSAML\XMLSecurity\XML\ds\KeyName;
use function dirname;
use function strval;
/**
* Tests for the AffiliationDescriptor class.
*
* @covers \SimpleSAML\SAML2\XML\md\AbstractMdElement
* @covers \SimpleSAML\SAML2\XML\md\AbstractSignedMdElement
* @covers \SimpleSAML\SAML2\XML\md\AbstractMetadataDocument
* @covers \SimpleSAML\SAML2\XML\md\AffiliationDescriptor
* @package simplesamlphp/saml2
*/
final class AffiliationDescriptorTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;
use SignedElementTestTrait;
/**
*/
public static function setUpBeforeClass(): void
{
self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/saml-schema-metadata-2.0.xsd';
self::$testedClass = AffiliationDescriptor::class;
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/md_AffiliationDescriptor.xml',
);
}
// test marshalling
/**
* Test creating an AffiliationDescriptor object from scratch.
*/
public function testMarshalling(): void
{
$affiliationDescriptor = new AffiliationDescriptor(
affiliationOwnerId: C::ENTITY_IDP,
affiliateMember: [new AffiliateMember(C::ENTITY_SP), new AffiliateMember(C::ENTITY_OTHER)],
ID: 'TheID',
validUntil: new DateTimeImmutable('2009-02-13T23:31:30Z'),
cacheDuration: 'PT5000S',
keyDescriptor: [
new KeyDescriptor(
new KeyInfo(
[
new KeyName('IdentityProvider.com SSO Key'),
],
),
'signing',
),
],
namespacedAttribute: [new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'value1')],
);
$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($affiliationDescriptor),
);
}
/**
* Test that creating an AffiliationDescriptor with an empty owner ID fails.
*/
public function testMarhsallingWithEmptyOwnerID(): void
{
$this->expectException(SchemaViolationException::class);
new AffiliationDescriptor(
affiliationOwnerId: '',
affiliateMember: [new AffiliateMember(C::ENTITY_SP), new AffiliateMember(C::ENTITY_OTHER)],
);
}
/**
* Test that creating an AffiliationDescriptor with an empty list of members fails.
*/
public function testMarshallingWithEmptyMemberList(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('List of affiliated members must not be empty.');
new AffiliationDescriptor(
affiliationOwnerId: C::ENTITY_IDP,
affiliateMember: [],
);
}
// test unmarshalling
/**
* Test creating an AffiliationDescriptor from XML.
*/
public function testUnmarshalling(): void
{
$affiliationDescriptor = AffiliationDescriptor::fromXML(self::$xmlRepresentation->documentElement);
$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($affiliationDescriptor),
);
}
/**
* Test failure to create an AffiliationDescriptor from XML when there's no affiliation members.
*/
public function testUnmarshallingWithoutMembers(): void
{
$mdNamespace = AffiliationDescriptor::NS;
$entity_idp = C::ENTITY_IDP;
$document = DOMDocumentFactory::fromString(<<<XML
<md:AffiliationDescriptor xmlns:md="{$mdNamespace}" affiliationOwnerID="{$entity_idp}" ID="TheID"
validUntil="2009-02-13T23:31:30Z" cacheDuration="PT5000S">
</md:AffiliationDescriptor>
XML
);
$this->expectException(Exception::class);
$this->expectExceptionMessage('List of affiliated members must not be empty.');
AffiliationDescriptor::fromXML($document->documentElement);
}
/**
* Test failure to create an AffiliationDescriptor from XML when there's no owner specified.
*/
public function testUnmarshallingWithoutOwner(): void
{
$mdNamespace = AffiliationDescriptor::NS;
$entity_other = C::ENTITY_OTHER;
$entity_sp = C::ENTITY_SP;
$document = DOMDocumentFactory::fromString(<<<XML
<md:AffiliationDescriptor xmlns:md="{$mdNamespace}" ID="TheID"
validUntil="2009-02-13T23:31:30Z" cacheDuration="PT5000S">
<md:AffiliateMember>{$entity_sp}</md:AffiliateMember>
<md:AffiliateMember>{$entity_other}</md:AffiliateMember>
</md:AffiliationDescriptor>
XML
);
$this->expectException(MissingAttributeException::class);
$this->expectExceptionMessage("Missing 'affiliationOwnerID' attribute on md:AffiliationDescriptor.");
AffiliationDescriptor::fromXML($document->documentElement);
}
}