Skip to content

Commit

Permalink
Implement Devices create endpoint
Browse files Browse the repository at this point in the history
- Implement the new endpoint as per standard practice
- Added a profile2 property to Device.php, since it's apparently possible to have two profiles
  • Loading branch information
rapkis committed Sep 11, 2023
1 parent 02a641c commit 20c0385
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Factories/DeviceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function make(array $data): Device
learnIp: (bool) $data['learn_ip'],
resolvers: $this->makeResolvers($data['resolvers']),
profile: (new ProfileFactory())->make($data['profile']),
profile2: ! empty($data['profile2']) ? (new ProfileFactory())->make($data['profile2']) : null,
description: $data['description'] ?? '',
stats: DeviceAnalytics::tryFrom($data['stats'] ?? null),
icon: $data['icon'] ?? null,
Expand Down
37 changes: 37 additions & 0 deletions src/Resources/Devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Http\Client\PendingRequest;
use Rapkis\Controld\Factories\DeviceFactory;
use Rapkis\Controld\Responses\Device;

class Devices
{
Expand All @@ -27,4 +28,40 @@ public function list(): \Rapkis\Controld\Responses\Devices

return $result;
}

public function create(
string $name,
string $profilePk,
string $icon,
string $profilePk2 = null,
int $stats = null,
bool $legacyIpv4Status = false,
bool $learnIp = false,
bool $restricted = false,
bool $bumpTls = false,
string $description = null,
int $ddnsStatus = null,
string $ddnsSubdomain = null,
int $ddnsExternalStatus = null,
string $ddnsExternalHost = null,
): Device {
$response = $this->client->post('devices', [
'name' => $name,
'profile_id' => $profilePk,
'profile_id2' => $profilePk2,
'icon' => $icon,
'stats' => $stats,
'legacy_ipv4_status' => (int) $legacyIpv4Status,
'learn_ip' => (int) $learnIp,
'restricted' => (int) $restricted,
'bump_tls' => (int) $bumpTls,
'desc' => $description,
'ddns_status' => $ddnsStatus,
'ddns_subdomain' => $ddnsSubdomain,
'ddns_ext_status' => $ddnsExternalStatus,
'ddns_ext_host' => $ddnsExternalHost,
])->json('body');

return $this->device->make($response);
}
}
1 change: 1 addition & 0 deletions src/Responses/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(
public readonly bool $learnIp,
public readonly array $resolvers,
public readonly Profile $profile,
public readonly ?Profile $profile2,
public readonly string $description,
public readonly DeviceAnalytics $stats,
public readonly ?string $icon,
Expand Down
14 changes: 14 additions & 0 deletions tests/Factories/DeviceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
stats: null,
filters: null,
),
profile2: null,
description: '',
stats: DeviceAnalytics::OFF,
icon: null,
Expand Down Expand Up @@ -92,6 +93,11 @@
'updated' => 111111111,
'name' => 'Foo Profile',
],
'profile2' => [
'PK' => 'profile_2_pk',
'updated' => 111111111,
'name' => 'Bar Profile',
],
'description' => 'my foo device',
'stats' => 2, // full analytics
'icon' => 'windows',
Expand Down Expand Up @@ -136,6 +142,14 @@
stats: null,
filters: null,
),
profile2: new Profile(
pk: 'profile_2_pk',
updated: 111111111,
name: 'Bar Profile',
disableTtl: null,
stats: null,
filters: null,
),
description: 'my foo device',
stats: DeviceAnalytics::FULL,
icon: 'windows',
Expand Down
43 changes: 43 additions & 0 deletions tests/Mocks/Endpoints/devices-create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"body": {
"PK": "device_pk",
"ts": 1111111111,
"name": "device_name",
"device_id": "device_pk",
"status": 0,
"icon": "mobile-android",
"learn_ip": 0,
"bump_tls": 1,
"ddns": {
"status": 1,
"subdomain": "test",
"hostname": "test.controld.live",
"record": "0.0.0.0"
},
"ddns_ext": {
"status": 1,
"host": "external.example.com"
},
"resolvers": {
"uid": "resolver_uid",
"doh": "https://dns.controld.com/resolver_uid",
"dot": "resolver_uid.dns.controld.com",
"v6": [
"2606:1a40::2",
"22606:1a40:1::2"
]
},
"profile": {
"PK": "profile_pk",
"updated": 1111111111,
"name": "Foo Profile"
},
"profile2": {
"PK": "profile_2_pk",
"updated": 1111111111,
"name": "Bar Profile"
}
},
"success": true,
"message": "Device has been added"
}
16 changes: 16 additions & 0 deletions tests/Resources/DevicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Http;
use Rapkis\Controld\Factories\DeviceFactory;
use Rapkis\Controld\Resources\Devices;
use Rapkis\Controld\Responses\Device;

beforeEach(function () {
Http::preventStrayRequests();
Expand All @@ -25,3 +26,18 @@
expect($result)->toBeInstanceOf(\Rapkis\Controld\Responses\Devices::class)
->and($result)->toHaveCount(3);
});

it('creates a device', function () {
$request = Http::fake([
'devices' => Http::response(mockJsonEndpoint('devices-create')),
])->asJson();

$resource = new Devices(
$request,
app(DeviceFactory::class),
);

$result = $resource->create('device_name', 'profile_pk', 'icon');

expect($result)->toBeInstanceOf(Device::class);
});

0 comments on commit 20c0385

Please sign in to comment.