@@ -189,6 +189,67 @@ public function testAuthorizeWithoutApprovedClient()
189189 $ this ->assertEquals ($ expected , $ actual );
190190 }
191191
192+ /**
193+ * @testdox
194+ *
195+ * @covers ::authorize
196+ */
197+ public function testAuthorizeWithTrustedApp ()
198+ {
199+ $ _GET ['client_id ' ] = self ::MOCK_CLIENT_ID ;
200+ $ _GET ['redirect_uri ' ] = 'https://mock.client/redirect ' ;
201+
202+ $ origin = 'https://mock.client/ ' ;
203+ $ clientData = json_encode ([
204+ 'client_name ' => 'Mock Client ' ,
205+ 'origin ' => $ origin ,
206+ 'redirect_uris ' => ['https://mock.client/redirect ' ],
207+ ], JSON_THROW_ON_ERROR );
208+ $ trustedApps = json_encode ([$ origin ], JSON_THROW_ON_ERROR );
209+
210+ $ parameters = $ this ->createMockConstructorParameters ($ clientData , $ trustedApps );
211+
212+ $ this ->mockConfig ->method ('getUserValue ' )->willReturnArgument (3 );
213+
214+ $ this ->mockUserManager ->method ('userExists ' )->willReturn (true );
215+
216+ $ controller = new ServerController (...$ parameters );
217+
218+ $ response = $ controller ->authorize ();
219+
220+ $ expected = $ this ->createExpectedResponse ();
221+
222+ $ actual = [
223+ 'data ' => $ response ->getData (),
224+ 'headers ' => $ response ->getHeaders (),
225+ 'status ' => $ response ->getStatus (),
226+ ];
227+
228+ $ location = $ actual ['headers ' ]['Location ' ] ?? '' ;
229+
230+ // Not comparing time-sensitive data
231+ unset($ actual ['headers ' ]['X-Request-Id ' ], $ actual ['headers ' ]['Location ' ]);
232+
233+ $ this ->assertEquals ($ expected , $ actual );
234+
235+ // @TODO: Move $location assert to a separate test
236+ $ url = parse_url ($ location );
237+
238+ parse_str ($ url ['fragment ' ], $ url ['fragment ' ]);
239+
240+ unset($ url ['fragment ' ]['access_token ' ], $ url ['fragment ' ]['id_token ' ]);
241+
242+ $ this ->assertEquals ([
243+ 'scheme ' => 'https ' ,
244+ 'host ' => 'mock.client ' ,
245+ 'path ' => '/redirect ' ,
246+ 'fragment ' => [
247+ 'token_type ' => 'Bearer ' ,
248+ 'expires_in ' => '3600 ' ,
249+ ],
250+ ], $ url );
251+ }
252+
192253 /**
193254 * @testdox ServerController should return a 400 when asked to authorize a client that sends an incorrect redirect URI
194255 *
@@ -460,7 +521,7 @@ public function testToken()
460521
461522 ////////////////////////////// MOCKS AND STUBS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
462523
463- public function createMockConfig ($ clientData ): IConfig |MockObject
524+ public function createMockConfig ($ clientData, $ trustedApps ): IConfig |MockObject
464525 {
465526 $ this ->mockConfig = $ this ->createMock (IConfig::class);
466527
@@ -470,12 +531,13 @@ public function createMockConfig($clientData): IConfig|MockObject
470531 [Application::APP_ID , 'client- ' , '{} ' , 'return ' => $ clientData ],
471532 [Application::APP_ID , 'encryptionKey ' , '' , 'return ' => 'mock encryption key ' ],
472533 [Application::APP_ID , 'privateKey ' , '' , 'return ' => self ::$ privateKey ],
534+ [Application::APP_ID , 'trustedApps ' , '[] ' , 'return ' => $ trustedApps ],
473535 ]);
474536
475537 return $ this ->mockConfig ;
476538 }
477539
478- public function createMockConstructorParameters ($ clientData = '{} ' ): array
540+ public function createMockConstructorParameters ($ clientData = '{} ' , $ trustedApps = ' [] ' ): array
479541 {
480542 $ parameters = [
481543 'mock appname ' ,
@@ -484,7 +546,7 @@ public function createMockConstructorParameters($clientData = '{}'): array
484546 $ this ->createMockUserManager (),
485547 $ this ->createMockUrlGenerator (),
486548 self ::MOCK_USER_ID ,
487- $ this ->createMockConfig ($ clientData ),
549+ $ this ->createMockConfig ($ clientData, $ trustedApps ),
488550 $ this ->createMock (UserService::class),
489551 $ this ->createMock (IDBConnection::class),
490552 ];
0 commit comments