Skip to content

Commit 6300cd1

Browse files
committed
WIP unit tests
1 parent 52a649c commit 6300cd1

6 files changed

Lines changed: 377 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\OpenID\ValueAbstracts;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\OpenID\Jwk\JwkDecorator;
10+
use SimpleSAML\OpenID\ValueAbstracts\KeyPair;
11+
12+
#[CoversClass(KeyPair::class)]
13+
final class KeyPairTest extends TestCase
14+
{
15+
protected \PHPUnit\Framework\MockObject\Stub $privateKeyMock;
16+
17+
protected \PHPUnit\Framework\MockObject\Stub $publicKeyMock;
18+
19+
20+
protected function setUp(): void
21+
{
22+
$this->privateKeyMock = $this->createStub(JwkDecorator::class);
23+
$this->publicKeyMock = $this->createStub(JwkDecorator::class);
24+
}
25+
26+
27+
protected function sut(
28+
?JwkDecorator $privateKey = null,
29+
?JwkDecorator $publicKey = null,
30+
?string $keyId = null,
31+
?string $privateKeyPassword = null,
32+
): KeyPair {
33+
return new KeyPair(
34+
$privateKey ?? $this->privateKeyMock,
35+
$publicKey ?? $this->publicKeyMock,
36+
$keyId ?? 'test-key-id',
37+
$privateKeyPassword,
38+
);
39+
}
40+
41+
42+
public function testCanCreateInstance(): void
43+
{
44+
$this->assertInstanceOf(KeyPair::class, $this->sut());
45+
}
46+
47+
48+
public function testGetPrivateKeyReturnsInjectedJwkDecorator(): void
49+
{
50+
$privateKey = $this->createStub(JwkDecorator::class);
51+
$sut = $this->sut(privateKey: $privateKey);
52+
53+
$this->assertSame($privateKey, $sut->getPrivateKey());
54+
}
55+
56+
57+
public function testGetPublicKeyReturnsInjectedJwkDecorator(): void
58+
{
59+
$publicKey = $this->createStub(JwkDecorator::class);
60+
$sut = $this->sut(publicKey: $publicKey);
61+
62+
$this->assertSame($publicKey, $sut->getPublicKey());
63+
}
64+
65+
66+
public function testGetKeyIdReturnsInjectedString(): void
67+
{
68+
$keyId = 'specific-key-id-123';
69+
$sut = $this->sut(keyId: $keyId);
70+
71+
$this->assertSame($keyId, $sut->getKeyId());
72+
}
73+
74+
75+
public function testGetPrivateKeyPasswordReturnsInjectedString(): void
76+
{
77+
$password = 'secret-password';
78+
$sut = $this->sut(privateKeyPassword: $password);
79+
80+
$this->assertSame($password, $sut->getPrivateKeyPassword());
81+
}
82+
83+
84+
public function testGetPrivateKeyPasswordReturnsNullWhenNullInjected(): void
85+
{
86+
$sut = $this->sut(privateKeyPassword: null);
87+
88+
$this->assertNull($sut->getPrivateKeyPassword());
89+
}
90+
}

tests/src/Algorithms/SignatureAlgorithmBagTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,20 @@ public function testCanGetAllInstances(): void
5353
{
5454
$this->assertNotEmpty($this->sut()->getAllInstances());
5555
}
56+
57+
58+
public function testCanGetAllNamesUnique(): void
59+
{
60+
$signatureAlgorithmBag = $this->sut(
61+
SignatureAlgorithmEnum::RS256,
62+
SignatureAlgorithmEnum::RS384,
63+
SignatureAlgorithmEnum::RS256,
64+
);
65+
$signatureAlgorithmBag->add(SignatureAlgorithmEnum::RS512);
66+
$signatureAlgorithmBag->add(SignatureAlgorithmEnum::RS384);
67+
68+
$names = $signatureAlgorithmBag->getAllNamesUnique();
69+
$this->assertCount(3, $names);
70+
$this->assertSame(['RS256', 'RS384', 'RS512'], $names);
71+
}
5672
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\OpenID\Codebooks;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\OpenID\Codebooks\ClientAuthenticationMethodsEnum;
10+
11+
#[CoversClass(ClientAuthenticationMethodsEnum::class)]
12+
final class ClientAuthenticationMethodsEnumTest extends TestCase
13+
{
14+
public function testIsNone(): void
15+
{
16+
$this->assertTrue(ClientAuthenticationMethodsEnum::None->isNone());
17+
$this->assertFalse(ClientAuthenticationMethodsEnum::ClientSecretBasic->isNone());
18+
$this->assertFalse(ClientAuthenticationMethodsEnum::ClientSecretPost->isNone());
19+
$this->assertFalse(ClientAuthenticationMethodsEnum::ClientSecretJwt->isNone());
20+
$this->assertFalse(ClientAuthenticationMethodsEnum::PrivateKeyJwt->isNone());
21+
}
22+
23+
24+
public function testIsNotNone(): void
25+
{
26+
$this->assertFalse(ClientAuthenticationMethodsEnum::None->isNotNone());
27+
$this->assertTrue(ClientAuthenticationMethodsEnum::ClientSecretBasic->isNotNone());
28+
$this->assertTrue(ClientAuthenticationMethodsEnum::ClientSecretPost->isNotNone());
29+
$this->assertTrue(ClientAuthenticationMethodsEnum::ClientSecretJwt->isNotNone());
30+
$this->assertTrue(ClientAuthenticationMethodsEnum::PrivateKeyJwt->isNotNone());
31+
}
32+
}

tests/src/Factories/ClaimFactoryTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use SimpleSAML\OpenID\ValueAbstracts\GenericClaim;
1515
use SimpleSAML\OpenID\ValueAbstracts\JwksClaim;
1616
use SimpleSAML\OpenID\VerifiableCredentials\VcDataModel\Factories\VcDataModelClaimFactory;
17+
use SimpleSAML\OpenID\VerifiableCredentials\VcDataModel2\Factories\VcDataModel2ClaimFactory;
1718

1819
#[CoversClass(ClaimFactory::class)]
1920
#[UsesClass(Helpers::class)]
@@ -76,6 +77,12 @@ public function testCanGetForVcDataModel(): void
7677
}
7778

7879

80+
public function testCanGetForVcDataModel2(): void
81+
{
82+
$this->assertInstanceOf(VcDataModel2ClaimFactory::class, $this->sut()->forVcDataModel2());
83+
}
84+
85+
7986
public function testCanBuildGeneric(): void
8087
{
8188
$this->assertInstanceOf(
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\OpenID\ValueAbstracts\Factories;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\UsesClass;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\OpenID\ValueAbstracts\Factories\SignatureKeyPairBagFactory;
11+
use SimpleSAML\OpenID\ValueAbstracts\Factories\SignatureKeyPairFactory;
12+
use SimpleSAML\OpenID\ValueAbstracts\KeyPair;
13+
use SimpleSAML\OpenID\ValueAbstracts\SignatureKeyPair;
14+
use SimpleSAML\OpenID\ValueAbstracts\SignatureKeyPairBag;
15+
use SimpleSAML\OpenID\ValueAbstracts\SignatureKeyPairConfig;
16+
use SimpleSAML\OpenID\ValueAbstracts\SignatureKeyPairConfigBag;
17+
18+
#[CoversClass(SignatureKeyPairBagFactory::class)]
19+
#[UsesClass(SignatureKeyPairBag::class)]
20+
final class SignatureKeyPairBagFactoryTest extends TestCase
21+
{
22+
public function testFromConfig(): void
23+
{
24+
$signatureKeyPairFactoryMock = $this->createMock(SignatureKeyPairFactory::class);
25+
$signatureKeyPairConfigBagMock = $this->createMock(SignatureKeyPairConfigBag::class);
26+
27+
$signatureKeyPairConfig1 = $this->createStub(SignatureKeyPairConfig::class);
28+
$signatureKeyPairConfig2 = $this->createStub(SignatureKeyPairConfig::class);
29+
30+
$signatureKeyPairConfigBagMock->expects($this->once())
31+
->method('getAll')
32+
->willReturn([$signatureKeyPairConfig1, $signatureKeyPairConfig2]);
33+
34+
$signatureKeyPair1 = $this->createMock(SignatureKeyPair::class);
35+
$signatureKeyPair2 = $this->createMock(SignatureKeyPair::class);
36+
37+
$keyPair1 = $this->createMock(KeyPair::class);
38+
$keyPair1->method('getKeyId')->willReturn('key1');
39+
$signatureKeyPair1->method('getKeyPair')->willReturn($keyPair1);
40+
41+
$keyPair2 = $this->createMock(KeyPair::class);
42+
$keyPair2->method('getKeyId')->willReturn('key2');
43+
$signatureKeyPair2->method('getKeyPair')->willReturn($keyPair2);
44+
45+
$signatureKeyPairFactoryMock->method('fromConfig')
46+
->willReturnCallback(function (
47+
$config,
48+
) use (
49+
$signatureKeyPairConfig1,
50+
$signatureKeyPairConfig2,
51+
$signatureKeyPair1,
52+
$signatureKeyPair2,
53+
): ?\PHPUnit\Framework\MockObject\MockObject {
54+
if ($config === $signatureKeyPairConfig1) {
55+
return $signatureKeyPair1;
56+
}
57+
58+
if ($config === $signatureKeyPairConfig2) {
59+
return $signatureKeyPair2;
60+
}
61+
62+
return null;
63+
});
64+
65+
$factory = new SignatureKeyPairBagFactory($signatureKeyPairFactoryMock);
66+
$result = $factory->fromConfig($signatureKeyPairConfigBagMock);
67+
68+
$this->assertInstanceOf(SignatureKeyPairBag::class, $result);
69+
$this->assertCount(2, $result->getAll());
70+
$this->assertSame($signatureKeyPair1, $result->getByKeyId('key1'));
71+
$this->assertSame($signatureKeyPair2, $result->getByKeyId('key2'));
72+
}
73+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\OpenID\ValueAbstracts\Factories;
6+
7+
use Jose\Component\Core\JWK as StandardJwk;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\Attributes\UsesClass;
10+
use PHPUnit\Framework\TestCase;
11+
use SimpleSAML\OpenID\Algorithms\SignatureAlgorithmEnum;
12+
use SimpleSAML\OpenID\Codebooks\ClaimsEnum;
13+
use SimpleSAML\OpenID\Codebooks\HashAlgorithmsEnum;
14+
use SimpleSAML\OpenID\Jwk;
15+
use SimpleSAML\OpenID\Jwk\Factories\JwkDecoratorFactory;
16+
use SimpleSAML\OpenID\Jwk\JwkDecorator;
17+
use SimpleSAML\OpenID\ValueAbstracts\Factories\SignatureKeyPairFactory;
18+
use SimpleSAML\OpenID\ValueAbstracts\KeyPair;
19+
use SimpleSAML\OpenID\ValueAbstracts\KeyPairConfigInterface;
20+
use SimpleSAML\OpenID\ValueAbstracts\SignatureKeyPair;
21+
use SimpleSAML\OpenID\ValueAbstracts\SignatureKeyPairConfig;
22+
23+
#[CoversClass(SignatureKeyPairFactory::class)]
24+
#[UsesClass(SignatureKeyPairConfig::class)]
25+
#[UsesClass(SignatureKeyPair::class)]
26+
#[UsesClass(KeyPair::class)]
27+
#[UsesClass(HashAlgorithmsEnum::class)]
28+
final class SignatureKeyPairFactoryTest extends TestCase
29+
{
30+
/** @var \SimpleSAML\OpenID\Jwk&\PHPUnit\Framework\MockObject\MockObject */
31+
protected \PHPUnit\Framework\MockObject\MockObject $jwkMock;
32+
33+
/** @var \SimpleSAML\OpenID\Jwk\Factories\JwkDecoratorFactory&\PHPUnit\Framework\MockObject\MockObject */
34+
protected \PHPUnit\Framework\MockObject\MockObject $jwkDecoratorFactoryMock;
35+
36+
/** @var \SimpleSAML\OpenID\Jwk\JwkDecorator&\PHPUnit\Framework\MockObject\MockObject */
37+
protected \PHPUnit\Framework\MockObject\MockObject $publicKeyJwkDecoratorMock;
38+
39+
/** @var \SimpleSAML\OpenID\Jwk\JwkDecorator&\PHPUnit\Framework\MockObject\Stub */
40+
protected \PHPUnit\Framework\MockObject\Stub $privateKeyJwkDecoratorMock;
41+
42+
43+
protected function setUp(): void
44+
{
45+
$this->jwkMock = $this->createMock(Jwk::class);
46+
$this->jwkDecoratorFactoryMock = $this->createMock(JwkDecoratorFactory::class);
47+
$this->publicKeyJwkDecoratorMock = $this->createMock(JwkDecorator::class);
48+
$this->privateKeyJwkDecoratorMock = $this->createStub(JwkDecorator::class);
49+
50+
$this->jwkMock->method('jwkDecoratorFactory')->willReturn($this->jwkDecoratorFactoryMock);
51+
}
52+
53+
54+
protected function sut(): SignatureKeyPairFactory
55+
{
56+
return new SignatureKeyPairFactory($this->jwkMock);
57+
}
58+
59+
60+
public function testFromConfigWithProvidedKeyId(): void
61+
{
62+
$keyPairConfigMock = $this->createMock(KeyPairConfigInterface::class);
63+
$keyPairConfigMock->method('getPublicKeyString')->willReturn('pub_key_string');
64+
$keyPairConfigMock->method('getPrivateKeyString')->willReturn('priv_key_string');
65+
$keyPairConfigMock->method('getKeyId')->willReturn('provided_key_id');
66+
$keyPairConfigMock->method('getPrivateKeyPassword')->willReturn('priv_password');
67+
68+
$signatureKeyPairConfig = new SignatureKeyPairConfig(
69+
SignatureAlgorithmEnum::RS256,
70+
$keyPairConfigMock,
71+
);
72+
73+
$this->jwkDecoratorFactoryMock->expects($this->exactly(2))
74+
->method('fromPkcs1Or8Key')
75+
->willReturnCallback(function (
76+
string $key,
77+
?string $password,
78+
array $additionalData,
79+
): \PHPUnit\Framework\MockObject\MockObject|\PHPUnit\Framework\MockObject\Stub|null {
80+
if ($key === 'pub_key_string') {
81+
return $this->publicKeyJwkDecoratorMock;
82+
}
83+
84+
if ($key === 'priv_key_string') {
85+
return $this->privateKeyJwkDecoratorMock;
86+
}
87+
88+
return null;
89+
});
90+
91+
$this->publicKeyJwkDecoratorMock->expects($this->once())
92+
->method('addAdditionalData')
93+
->with(ClaimsEnum::Kid->value, 'provided_key_id');
94+
95+
$factory = $this->sut();
96+
$result = $factory->fromConfig($signatureKeyPairConfig);
97+
98+
$this->assertInstanceOf(SignatureKeyPair::class, $result);
99+
$this->assertSame(SignatureAlgorithmEnum::RS256, $result->getSignatureAlgorithm());
100+
$this->assertSame('provided_key_id', $result->getKeyPair()->getKeyId());
101+
$this->assertSame('priv_password', $result->getKeyPair()->getPrivateKeyPassword());
102+
$this->assertSame($this->publicKeyJwkDecoratorMock, $result->getKeyPair()->getPublicKey());
103+
$this->assertSame($this->privateKeyJwkDecoratorMock, $result->getKeyPair()->getPrivateKey());
104+
}
105+
106+
107+
public function testFromConfigWithGeneratedKeyIdThumbprint(): void
108+
{
109+
$keyPairConfigMock = $this->createMock(KeyPairConfigInterface::class);
110+
$keyPairConfigMock->method('getPublicKeyString')->willReturn('pub_key_string');
111+
$keyPairConfigMock->method('getPrivateKeyString')->willReturn('priv_key_string');
112+
$keyPairConfigMock->method('getKeyId')->willReturn(null);
113+
$keyPairConfigMock->method('getPrivateKeyPassword')->willReturn(null);
114+
115+
$signatureKeyPairConfig = new SignatureKeyPairConfig(
116+
SignatureAlgorithmEnum::ES256,
117+
$keyPairConfigMock,
118+
);
119+
120+
$this->jwkDecoratorFactoryMock->expects($this->exactly(2))
121+
->method('fromPkcs1Or8Key')
122+
->willReturnCallback(function (
123+
string $key,
124+
?string $password,
125+
array $additionalData,
126+
): \PHPUnit\Framework\MockObject\MockObject|\PHPUnit\Framework\MockObject\Stub|null {
127+
if ($key === 'pub_key_string') {
128+
return $this->publicKeyJwkDecoratorMock;
129+
}
130+
131+
if ($key === 'priv_key_string') {
132+
return $this->privateKeyJwkDecoratorMock;
133+
}
134+
135+
return null;
136+
});
137+
138+
$standardJwkMock = $this->createMock(StandardJwk::class);
139+
$standardJwkMock->method('thumbprint')
140+
->with(HashAlgorithmsEnum::SHA_256->phpName())
141+
->willReturn('generated_thumbprint_key_id');
142+
143+
$this->publicKeyJwkDecoratorMock->method('jwk')->willReturn($standardJwkMock);
144+
145+
$this->publicKeyJwkDecoratorMock->expects($this->once())
146+
->method('addAdditionalData')
147+
->with(ClaimsEnum::Kid->value, 'generated_thumbprint_key_id');
148+
149+
$factory = $this->sut();
150+
$result = $factory->fromConfig($signatureKeyPairConfig, HashAlgorithmsEnum::SHA_256);
151+
152+
$this->assertInstanceOf(SignatureKeyPair::class, $result);
153+
$this->assertSame(SignatureAlgorithmEnum::ES256, $result->getSignatureAlgorithm());
154+
$this->assertSame('generated_thumbprint_key_id', $result->getKeyPair()->getKeyId());
155+
$this->assertNull($result->getKeyPair()->getPrivateKeyPassword());
156+
$this->assertSame($this->publicKeyJwkDecoratorMock, $result->getKeyPair()->getPublicKey());
157+
$this->assertSame($this->privateKeyJwkDecoratorMock, $result->getKeyPair()->getPrivateKey());
158+
}
159+
}

0 commit comments

Comments
 (0)