11<?php
22
3- namespace OCA \Solid \Controller \ ServerController ;
3+ namespace OCA \Solid \Controller ;
44
5+ use OC \AppFramework \Http ;
56use OCA \Solid \AppInfo \Application ;
6- use OCA \Solid \Controller \ServerController ;
77use OCA \Solid \Service \UserService ;
88use OCP \AppFramework \Http \JSONResponse ;
99use OCP \IConfig ;
1515use PHPUnit \Framework \MockObject \MockObject ;
1616use PHPUnit \Framework \TestCase ;
1717
18+
19+ function file_get_contents ($ filename )
20+ {
21+ if ($ filename === 'php://input ' ) {
22+ return ServerControllerTest::$ clientData ;
23+ }
24+
25+ return \file_get_contents ($ filename );
26+ }
27+
1828/**
1929 * @coversDefaultClass \OCA\Solid\Controller\ServerController
2030 * @covers ::__construct
@@ -31,12 +41,13 @@ class ServerControllerTest extends TestCase
3141 private const MOCK_CLIENT_ID = 'mock-client-id ' ;
3242 private const MOCK_USER_ID = 'mock user id ' ;
3343
44+ public static string $ clientData = '' ;
3445 private static string $ privateKey ;
3546
3647 private IConfig |MockObject $ mockConfig ;
48+ private IURLGenerator |MockObject $ mockURLGenerator ;
3749 private IUserManager |MockObject $ mockUserManager ;
3850
39-
4051 public static function setUpBeforeClass (): void
4152 {
4253 $ keyPath = __DIR__ . '/../../fixtures/keys ' ;
@@ -231,6 +242,75 @@ public function testAuthorizeWithApprovedClient()
231242 ], $ url );
232243 }
233244
245+ /**
246+ * @testdox ServerController should return a 400 when asked to register without valid client data
247+ *
248+ * @covers ::register
249+ */
250+ public function testRegisterWithoutRedirectUris ()
251+ {
252+ $ parameters = $ this ->createMockConstructorParameters ();
253+
254+ $ controller = new ServerController (...$ parameters );
255+
256+ $ actual = $ controller ->register ();
257+
258+ $ this ->assertEquals (
259+ new JSONResponse ('Missing redirect URIs ' , Http::STATUS_BAD_REQUEST ),
260+ $ actual
261+ );
262+ }
263+
264+ /**
265+ * @testdox ServerController should return a 200 with client data when asked to register with valid redirect URIs
266+ *
267+ * @covers ::register
268+ */
269+ public function testRegisterWithRedirectUris ()
270+ {
271+ $ parameters = $ this ->createMockConstructorParameters ();
272+
273+ $ this ->mockURLGenerator ->method ('getBaseUrl ' )
274+ ->willReturn ('https://mock.server ' );
275+
276+ $ controller = new ServerController (...$ parameters );
277+
278+ self ::$ clientData = json_encode (['redirect_uris ' => ['https://mock.client/redirect ' ]]);
279+
280+ $ response = $ controller ->register ();
281+
282+ $ actual = [
283+ 'data ' => $ response ->getData (),
284+ 'headers ' => $ response ->getHeaders (),
285+ 'status ' => $ response ->getStatus (),
286+ ];
287+
288+ // Not comparing time-sensitive data
289+ unset($ actual ['data ' ]['client_id_issued_at ' ], $ actual ['headers ' ]['X-Request-Id ' ]);
290+
291+ $ this ->assertEquals ([
292+ 'data ' => [
293+ 'application_type ' => 'web ' ,
294+ 'client_id ' => 'f4a2d00f7602948a97ff409d7a581ec2 ' ,
295+ 'grant_types ' => ['implicit ' ],
296+ 'id_token_signed_response_alg ' => 'RS256 ' ,
297+ 'redirect_uris ' => ['https://mock.client/redirect ' ],
298+ 'registration_access_token ' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczovL21vY2suc2VydmVyIiwiYXVkIjoiZjRhMmQwMGY3NjAyOTQ4YTk3ZmY0MDlkN2E1ODFlYzIiLCJzdWIiOiJmNGEyZDAwZjc2MDI5NDhhOTdmZjQwOWQ3YTU4MWVjMiJ9.AfOi9YW70rL0EKn4_dvhkyu02iI4yGYV-Xh8hQ9RbHBUnvcXROFfQzn-OL-R3kV3nn8tknmpG-r_8Ouoo7O_Sjo8Hx1QSFfeqjJGOgB8HbXV7WN2spOMicSB-68EyftqfTGH0ksyPyJaNSTbkdIqtawsDaSKUVqTmziEo4IrE5anwDLZrtSUcS0A4KVrOAkJmgYGiC4MC0NMYXeBRxgkr1_h7GN4hekAXs9-5XwRH1mwswUVRL-6prx0IYpPNURFNqkS2NU83xNf-vONThOdLVkADVy-l3PCHT3E1sRdkklCHLjhWiZo7NcMlB0WdS-APnZYCi5hLEr5-jwNI2sxoA ' ,
299+ 'registration_client_uri ' => '' ,
300+ 'response_types ' => ['id_token token ' ],
301+ 'token_endpoint_auth_method ' => 'client_secret_basic ' ,
302+ ],
303+ 'headers ' => [
304+ 'Cache-Control ' => 'no-cache, no-store, must-revalidate ' ,
305+ 'Content-Security-Policy ' => "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none' " ,
306+ 'Feature-Policy ' => "autoplay 'none';camera 'none';fullscreen 'none';geolocation 'none';microphone 'none';payment 'none' " ,
307+ 'X-Robots-Tag ' => 'noindex, nofollow ' ,
308+ 'Content-Type ' => 'application/json; charset=utf-8 ' ,
309+ ],
310+ 'status ' => Http::STATUS_OK ,
311+ ], $ actual );
312+ }
313+
234314 ////////////////////////////// MOCKS AND STUBS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
235315
236316 public function createMockConfig ($ clientData ): IConfig |MockObject
@@ -244,18 +324,21 @@ public function createMockConfig($clientData): IConfig|MockObject
244324 [Application::APP_ID , 'profileData ' , '' , 'return ' => '' ],
245325 [Application::APP_ID , 'encryptionKey ' , '' , 'return ' => 'mock encryption key ' ],
246326 [Application::APP_ID , 'privateKey ' , '' , 'return ' => self ::$ privateKey ],
327+ // Client ID from register() with https://mock.client
328+ [Application::APP_ID , 'client-f4a2d00f7602948a97ff409d7a581ec2 ' , '{} ' , 'return ' => $ clientData ],
247329 ]);
248330
249331 return $ this ->mockConfig ;
250332 }
333+
251334 public function createMockConstructorParameters ($ clientData = '{} ' ): array
252335 {
253336 $ parameters = [
254337 'mock appname ' ,
255338 $ this ->createMock (IRequest::class),
256339 $ this ->createMock (ISession::class),
257340 $ this ->createMockUserManager (),
258- $ this ->createMock (IURLGenerator::class ),
341+ $ this ->createMockUrlGenerator ( ),
259342 self ::MOCK_USER_ID ,
260343 $ this ->createMockConfig ($ clientData ),
261344 $ this ->createMock (UserService::class),
@@ -265,6 +348,13 @@ public function createMockConstructorParameters($clientData = '{}'): array
265348 return $ parameters ;
266349 }
267350
351+ public function createMockUrlGenerator (): IURLGenerator |MockObject
352+ {
353+ $ this ->mockURLGenerator = $ this ->createMock (IURLGenerator::class);
354+
355+ return $ this ->mockURLGenerator ;
356+ }
357+
268358 public function createMockUserManager (): IUserManager |MockObject
269359 {
270360 $ this ->mockUserManager = $ this ->createMock (IUserManager::class);
0 commit comments