From 630d9bd96176c2a3bee1072312d6ff3e99ccf28f Mon Sep 17 00:00:00 2001 From: Rapolas Gruzdys Date: Tue, 19 Sep 2023 17:34:39 +0300 Subject: [PATCH] Implement MobileConfig endpoint (#28) --- src/ControlD.php | 6 ++++ .../ControlDErrorHandlerMiddleware.php | 7 ++++- src/Resources/MobileConfig.php | 31 +++++++++++++++++++ tests/ControlDTest.php | 6 ++++ .../ControlDErrorHandlerMiddlewareTest.php | 17 ++++++++-- tests/Mocks/Endpoints/mobile-config.bin | 5 +++ tests/Resources/MobileConfigTest.php | 24 ++++++++++++++ 7 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 src/Resources/MobileConfig.php create mode 100644 tests/Mocks/Endpoints/mobile-config.bin create mode 100644 tests/Resources/MobileConfigTest.php diff --git a/src/ControlD.php b/src/ControlD.php index 665a1ae..11baaf3 100644 --- a/src/ControlD.php +++ b/src/ControlD.php @@ -9,6 +9,7 @@ use Rapkis\Controld\Resources\Account; use Rapkis\Controld\Resources\Analytics; use Rapkis\Controld\Resources\Devices; +use Rapkis\Controld\Resources\MobileConfig; use Rapkis\Controld\Resources\Organizations; use Rapkis\Controld\Resources\Profiles; use Rapkis\Controld\Resources\Services; @@ -53,4 +54,9 @@ public function organizations(): Organizations { return app(Organizations::class, ['client' => $this->request]); } + + public function mobileConfig(): MobileConfig + { + return app(MobileConfig::class, ['client' => $this->request]); + } } diff --git a/src/Middleware/ControlDErrorHandlerMiddleware.php b/src/Middleware/ControlDErrorHandlerMiddleware.php index afc63ae..a3f3f45 100644 --- a/src/Middleware/ControlDErrorHandlerMiddleware.php +++ b/src/Middleware/ControlDErrorHandlerMiddleware.php @@ -21,12 +21,17 @@ protected function handleResponse(ResponseInterface $response): void { $data = json_decode($response->getBody()->getContents(), true); + $shouldBeJson = in_array('application/json', $response->getHeader('Content-Type')); + if (! $shouldBeJson && $data === null) { + return; + } + if (($data['success'] ?? false)) { return; } throw new ControlDErrorException( - $data['error']['message'] ?? 'An ControlD unknown error has occurred', + $data['error']['message'] ?? 'An unknown ControlD error has occurred', $data['error']['code'] ?? 500, ); } diff --git a/src/Resources/MobileConfig.php b/src/Resources/MobileConfig.php new file mode 100644 index 0000000..e192163 --- /dev/null +++ b/src/Resources/MobileConfig.php @@ -0,0 +1,31 @@ +client->get("mobileconfig/{$devicePk}", [ + 'exclude_wifi' => $excludeWifi, + 'exclude_domain' => $excludeDomain, + 'dont_sign' => (int) $dontSign, + 'exclude_common' => (int) $excludeCommon, + 'client_id' => $clientId, + ])->body(); + } +} diff --git a/tests/ControlDTest.php b/tests/ControlDTest.php index ba7d190..eff4d8a 100644 --- a/tests/ControlDTest.php +++ b/tests/ControlDTest.php @@ -8,6 +8,7 @@ use Rapkis\Controld\Resources\Account; use Rapkis\Controld\Resources\Analytics; use Rapkis\Controld\Resources\Devices; +use Rapkis\Controld\Resources\MobileConfig; use Rapkis\Controld\Resources\Organizations; use Rapkis\Controld\Resources\Profiles; use Rapkis\Controld\Resources\Services; @@ -46,3 +47,8 @@ $client = new ControlD($this->createStub(PendingRequest::class)); expect($client->organizations())->toBeInstanceOf(Organizations::class); }); + +it('accesses mobile config resource', function () { + $client = new ControlD($this->createStub(PendingRequest::class)); + expect($client->mobileConfig())->toBeInstanceOf(MobileConfig::class); +}); diff --git a/tests/Middleware/ControlDErrorHandlerMiddlewareTest.php b/tests/Middleware/ControlDErrorHandlerMiddlewareTest.php index 6b937a9..88d49ba 100644 --- a/tests/Middleware/ControlDErrorHandlerMiddlewareTest.php +++ b/tests/Middleware/ControlDErrorHandlerMiddlewareTest.php @@ -12,7 +12,20 @@ (new ControlDErrorHandlerMiddleware())($response); })->throws(ControlDErrorException::class, 'This profile does not exist', 40401); -it('handles success', function () { - $response = new Response(404, [], mockJsonEndpoint('profiles-delete')); +it('handles success', function (array $headers) { + $response = new Response(404, $headers, mockJsonEndpoint('profiles-delete')); + expect((new ControlDErrorHandlerMiddleware())($response))->toBeInstanceOf(ResponseInterface::class); +})->with([ + [['Content-Type' => 'application/json']], + [[]], +]); + +it('skips non-json', function () { + $response = new Response(404, [], null); expect((new ControlDErrorHandlerMiddleware())($response))->toBeInstanceOf(ResponseInterface::class); }); + +it('handles missing json response data', function () { + $response = new Response(404, ['Content-Type' => 'application/json'], 'malformed json'); + (new ControlDErrorHandlerMiddleware())($response); +})->throws(ControlDErrorException::class, 'An unknown ControlD error has occurred', 500); diff --git a/tests/Mocks/Endpoints/mobile-config.bin b/tests/Mocks/Endpoints/mobile-config.bin new file mode 100644 index 0000000..9a18d94 --- /dev/null +++ b/tests/Mocks/Endpoints/mobile-config.bin @@ -0,0 +1,5 @@ +²§Ë¿¶>¾ópºµ™™xÞxõ`¿Ã¹dcšRo"iÏ i”fÀ]ºDrҗ±”BÙx/é“X_Og`Äɧێ™’¾ãBǼÚüÁ̪U”Þågl9ÿÅ&1˜†HA”ëО5 ÛX{¯â£Ð¬'“Ö¨ÞU + +¹f+BéÔ~…á  sJ ØWt +Xp-¬[jQ]ă~õr˜Ït‰¤š +Uo±„çڔ_¬¢vÛÕ:'c¹kÓx…‚~Âӗnc»°™ ©ç?©Ë]”>ŒÁ^ݙ}TH¿ûØõãÔ5†Ð¯0+–çtäȝÕé°Òõ>D!:kLû˜Ê‹6@ð diff --git a/tests/Resources/MobileConfigTest.php b/tests/Resources/MobileConfigTest.php new file mode 100644 index 0000000..12d064c --- /dev/null +++ b/tests/Resources/MobileConfigTest.php @@ -0,0 +1,24 @@ + Http::response($dummyBinary), + ])->asJson(); + + $resource = new MobileConfig($request); + + $result = $resource->generateProfile('device_pk'); + + expect($result)->toBe($dummyBinary); +});