Skip to content

Commit

Permalink
Implement MobileConfig endpoint (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
rapkis authored Sep 19, 2023
1 parent 94b679f commit 630d9bd
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/ControlD.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}
}
7 changes: 6 additions & 1 deletion src/Middleware/ControlDErrorHandlerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Expand Down
31 changes: 31 additions & 0 deletions src/Resources/MobileConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Rapkis\Controld\Resources;

use Illuminate\Http\Client\PendingRequest;

class MobileConfig
{
public function __construct(private readonly PendingRequest $client)
{
}

public function generateProfile(
string $devicePk,
array $excludeWifi = [],
array $excludeDomain = [],
bool $dontSign = false,
bool $excludeCommon = false,
string $clientId = null,
): string {
return $this->client->get("mobileconfig/{$devicePk}", [
'exclude_wifi' => $excludeWifi,
'exclude_domain' => $excludeDomain,
'dont_sign' => (int) $dontSign,
'exclude_common' => (int) $excludeCommon,
'client_id' => $clientId,
])->body();
}
}
6 changes: 6 additions & 0 deletions tests/ControlDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
17 changes: 15 additions & 2 deletions tests/Middleware/ControlDErrorHandlerMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
5 changes: 5 additions & 0 deletions tests/Mocks/Endpoints/mobile-config.bin
Original file line number Diff line number Diff line change
@@ -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@ð
24 changes: 24 additions & 0 deletions tests/Resources/MobileConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Http;
use Rapkis\Controld\Resources\MobileConfig;

beforeEach(function () {
Http::preventStrayRequests();
});

it('displays mobile config', function () {
$dummyBinary = file_get_contents(__DIR__.'/../Mocks/Endpoints/mobile-config.bin');

$request = Http::fake([
'mobileconfig/device_pk?dont_sign=0&exclude_common=0' => Http::response($dummyBinary),
])->asJson();

$resource = new MobileConfig($request);

$result = $resource->generateProfile('device_pk');

expect($result)->toBe($dummyBinary);
});

0 comments on commit 630d9bd

Please sign in to comment.