1010use SimpleSAML \Auth \Simple ;
1111use SimpleSAML \Configuration ;
1212use SimpleSAML \Error \Exception ;
13+ use SimpleSAML \Module \oidc \Bridges \SspBridge ;
1314use SimpleSAML \Module \oidc \Factories \AuthSimpleFactory ;
1415use SimpleSAML \Module \oidc \ModuleConfig ;
1516use SimpleSAML \Module \oidc \Services \AuthContextService ;
17+ use SimpleSAML \Utils \Attributes ;
1618
1719/**
1820 * @covers \SimpleSAML\Module\oidc\Services\AuthContextService
@@ -28,6 +30,9 @@ class AuthContextServiceTest extends TestCase
2830 protected MockObject $ moduleConfigMock ;
2931 protected MockObject $ authSimpleService ;
3032 protected MockObject $ authSimpleFactory ;
33+ protected MockObject $ sspBridgeMock ;
34+ protected MockObject $ sspBridgeUtilsMock ;
35+ protected MockObject $ sspBridgeUtilsAttributesMock ;
3136
3237 /**
3338 * @throws \PHPUnit\Framework\MockObject\Exception
@@ -52,21 +57,35 @@ protected function setUp(): void
5257
5358 $ this ->authSimpleFactory = $ this ->createMock (AuthSimpleFactory::class);
5459 $ this ->authSimpleFactory ->method ('getDefaultAuthSource ' )->willReturn ($ this ->authSimpleService );
60+
61+ $ this ->sspBridgeMock = $ this ->createMock (SspBridge::class);
62+ $ this ->sspBridgeUtilsMock = $ this ->createMock (SspBridge \Utils::class);
63+ $ this ->sspBridgeMock ->method ('utils ' )->willReturn ($ this ->sspBridgeUtilsMock );
64+ $ this ->sspBridgeUtilsAttributesMock = $ this ->createMock (Attributes::class);
65+ $ this ->sspBridgeUtilsMock ->method ('attributes ' )->willReturn ($ this ->sspBridgeUtilsAttributesMock );
5566 }
5667
57- protected function prepareMockedInstance (): AuthContextService
58- {
68+ protected function sut (
69+ ?ModuleConfig $ moduleConfig = null ,
70+ ?AuthSimpleFactory $ authSimpleFactory = null ,
71+ ?SspBridge $ sspBridge = null ,
72+ ): AuthContextService {
73+ $ moduleConfig ??= $ this ->moduleConfigMock ;
74+ $ authSimpleFactory ??= $ this ->authSimpleFactory ;
75+ $ sspBridge ??= $ this ->sspBridgeMock ;
76+
5977 return new AuthContextService (
60- $ this ->moduleConfigMock ,
61- $ this ->authSimpleFactory ,
78+ $ moduleConfig ,
79+ $ authSimpleFactory ,
80+ $ sspBridge ,
6281 );
6382 }
6483
6584 public function testItIsInitializable (): void
6685 {
6786 $ this ->assertInstanceOf (
6887 AuthContextService::class,
69- $ this ->prepareMockedInstance (),
88+ $ this ->sut (),
7089 );
7190 }
7291
@@ -77,9 +96,15 @@ public function testItReturnsUsername(): void
7796 {
7897 $ this ->moduleConfigMock ->method ('getUserIdentifierAttribute ' )->willReturn ('idAttribute ' );
7998 $ this ->authSimpleService ->method ('getAttributes ' )->willReturn (self ::AUTHORIZED_USER );
99+ $ this ->sspBridgeUtilsAttributesMock ->expects ($ this ->once ())->method ('getExpectedAttribute ' )
100+ ->with (
101+ self ::AUTHORIZED_USER ,
102+ 'idAttribute ' ,
103+ )
104+ ->willReturn (self ::AUTHORIZED_USER ['idAttribute ' ][0 ]);
80105
81106 $ this ->assertSame (
82- $ this ->prepareMockedInstance ()->getAuthUserId (),
107+ $ this ->sut ()->getAuthUserId (),
83108 'myUsername ' ,
84109 );
85110 }
@@ -94,8 +119,12 @@ public function testItThrowsWhenNoUsername(): void
94119 ->willReturn ('attributeNotSet ' );
95120 $ this ->authSimpleService ->method ('getAttributes ' )->willReturn (self ::AUTHORIZED_USER );
96121
122+ $ this ->sspBridgeUtilsAttributesMock ->expects ($ this ->once ())->method ('getExpectedAttribute ' )
123+ ->with (self ::AUTHORIZED_USER )
124+ ->willThrowException (new Exception ('error ' ));
125+
97126 $ this ->expectException (Exception::class);
98- $ this ->prepareMockedInstance ()->getAuthUserId ();
127+ $ this ->sut ()->getAuthUserId ();
99128 }
100129
101130 /**
@@ -108,7 +137,7 @@ public function testPermissionsOk(): void
108137 ->willReturn ($ this ->permissions );
109138 $ this ->authSimpleService ->method ('getAttributes ' )->willReturn (self ::AUTHORIZED_USER );
110139
111- $ this ->prepareMockedInstance ()->requirePermission ('client ' );
140+ $ this ->sut ()->requirePermission ('client ' );
112141 $ this ->expectNotToPerformAssertions ();
113142 }
114143
@@ -121,7 +150,7 @@ public function testItThrowsIfNotAuthorizedForPermission(): void
121150 ->with (ModuleConfig::OPTION_ADMIN_UI_PERMISSIONS , null )
122151 ->willReturn ($ this ->permissions );
123152 $ this ->expectException (RuntimeException::class);
124- $ this ->prepareMockedInstance ()->requirePermission ('no-match ' );
153+ $ this ->sut ()->requirePermission ('no-match ' );
125154 }
126155
127156 /**
@@ -141,7 +170,7 @@ public function testItThrowsForWrongEntitlements(): void
141170 );
142171
143172 $ this ->expectException (RuntimeException::class);
144- $ this ->prepareMockedInstance ()->requirePermission ('client ' );
173+ $ this ->sut ()->requirePermission ('client ' );
145174 }
146175
147176 /**
@@ -160,7 +189,7 @@ public function testItThrowsForNotHavingEntitlementAttribute(): void
160189 );
161190
162191 $ this ->expectException (RuntimeException::class);
163- $ this ->prepareMockedInstance ()->requirePermission ('client ' );
192+ $ this ->sut ()->requirePermission ('client ' );
164193 }
165194
166195 /**
@@ -173,6 +202,6 @@ public function testThrowsForNotHavingEnabledPermissions(): void
173202 ->willReturn (Configuration::loadFromArray ([]));
174203
175204 $ this ->expectException (RuntimeException::class);
176- $ this ->prepareMockedInstance ()->requirePermission ('client ' );
205+ $ this ->sut ()->requirePermission ('client ' );
177206 }
178207}
0 commit comments