Skip to content

A free open source IT asset/license management system

Added tests

snipe75fbce7634a5
diff --git a/tests/Feature/SyncAdapters/Addigy/AddigyAdapterTest.php b/tests/Feature/SyncAdapters/Addigy/AddigyAdapterTest.php
new file mode 100644
index 0000000000..91ca153786
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Addigy/AddigyAdapterTest.php
@@ -0,0 +1,145 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Addigy;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\Addigy\AddigyAdapter;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the Addigy adapter through
+ * SyncHostFromAdapter. Mocks the Addigy API, asserts assets +
+ * external ids land correctly, and that the client-id / client-secret
+ * header pair (not bearer) goes out on the request.
+ */
+class AddigyAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_devices_from_addigy_and_creates_assets()
+ {
+ $adapter = $this->configuredAddigyAdapter();
+
+ Http::fake([
+ '*/api/devices' => Http::response([
+ $this->addigyDevice(agentid: 'agent-1', name: 'design-mbp', hardware_model: 'MacBook Pro'),
+ $this->addigyDevice(agentid: 'agent-2', name: 'staff-imac', hardware_model: 'iMac'),
+ ]),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'addigy', 'external_id' => 'agent-1']);
+ $this->assertDatabaseHas('assets', ['name' => 'design-mbp']);
+ }
+
+ public function test_client_id_and_client_secret_headers_are_sent_not_bearer()
+ {
+ $adapter = $this->configuredAddigyAdapter();
+
+ Http::fake([
+ '*/api/devices' => Http::response([]),
+ ]);
+
+ iterator_to_array($adapter->pull());
+
+ // Addigy's API uses a header pair, not bearer-token auth.
+ // A regression to withToken() would silently 401 against a
+ // real tenant.
+ Http::assertSent(function ($request) {
+ return $request->hasHeader('client-id', 'fake-addigy-client-id')
+ && $request->hasHeader('client-secret', 'fake-addigy-client-secret')
+ && ! $request->hasHeader('Authorization');
+ });
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredAddigyAdapter();
+
+ Http::fake([
+ '*/api/devices' => Http::response([
+ $this->addigyDevice(
+ agentid: 'agent-7',
+ name: 'lab-mbp',
+ hardware_model: 'MacBookPro18,3',
+ serial_number: 'C02XYZ',
+ network_mac_address: 'aa:bb:cc:11:22:33',
+ network_ip_address: '10.0.0.42',
+ os_type: 'macOS',
+ os_version: '14.2.1',
+ last_online: '2026-01-15T10:00:00.000Z',
+ ),
+ ]),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('addigy', $record->sourceKey);
+ $this->assertSame('agent-7', $record->sourceId);
+ $this->assertSame('lab-mbp', $record->hostname);
+ $this->assertSame('MacBookPro18,3', $record->hardwareModel);
+ $this->assertSame('C02XYZ', $record->hardwareSerial);
+ $this->assertSame('Apple', $record->manufacturer);
+ $this->assertSame('aa:bb:cc:11:22:33', $record->primaryMac);
+ $this->assertSame('10.0.0.42', $record->primaryIp);
+ $this->assertSame('macOS', $record->os);
+ $this->assertSame('14.2.1', $record->osVersion);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ private function configuredAddigyAdapter(): AddigyAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'addigy')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://prod.addigy.com');
+ // key_id is non-secret, stored as plain text.
+ SyncAdapterConfig::put($instance->id, 'key_id', 'fake-addigy-client-id');
+ SyncAdapterConfig::put($instance->id, 'key_secret', Crypt::encrypt('fake-addigy-client-secret'));
+
+ return new AddigyAdapter($instance->fresh());
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function addigyDevice(
+ string $agentid,
+ string $name = 'device',
+ string $hardware_model = 'MacBook',
+ ?string $serial_number = null,
+ ?string $network_mac_address = null,
+ ?string $network_ip_address = null,
+ ?string $os_type = 'macOS',
+ ?string $os_version = null,
+ ?string $last_online = null,
+ ): array {
+ return [
+ 'agentid' => $agentid,
+ 'name' => $name,
+ 'hardware_model' => $hardware_model,
+ 'serial_number' => $serial_number,
+ 'network_mac_address' => $network_mac_address,
+ 'network_ip_address' => $network_ip_address,
+ 'os_type' => $os_type,
+ 'os_version' => $os_version,
+ 'last_online' => $last_online,
+ 'policy_id' => 'policy-abc',
+ 'agent_version' => '4.0.0',
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Addigy/AddigySettingsPageTest.php b/tests/Feature/SyncAdapters/Addigy/AddigySettingsPageTest.php
new file mode 100644
index 0000000000..226efc6917
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Addigy/AddigySettingsPageTest.php
@@ -0,0 +1,80 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Addigy;
+
+use App\Models\Category;
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\Models\User;
+use Illuminate\Support\Facades\Crypt;
+use Tests\TestCase;
+
+/**
+ * Smoke coverage for the Addigy adapter's registration + settings-page
+ * presence. Addigy is the first adapter to use the URL+key-pair auth
+ * shape (UrlKeyPairAdapter base + url_keypair partial), so this test
+ * proves the shell renders, both credential fields persist, and each
+ * gets encrypted at rest. pull() is still a stub so no sync coverage.
+ */
+class AddigySettingsPageTest extends TestCase
+{
+ public function test_addigy_tab_renders_on_the_adapters_page()
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index'))
+ ->assertOk()
+ ->assertSee('Addigy');
+ }
+
+ public function test_saving_addigy_persists_url_and_encrypts_both_credentials()
+ {
+ $addigy = SyncAdapterInstance::where('slug', 'addigy')->firstOrFail();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('settings.adapters.save', $addigy), [
+ 'addigy_url' => 'https://prod.addigy.com/api',
+ 'addigy_key_id' => 'plaintext-addigy-client-id',
+ 'addigy_key_secret' => 'plaintext-addigy-client-secret',
+ 'addigy_default_category_id' => Category::factory()->create()->id,
+ 'addigy_default_status_id' => Statuslabel::factory()->rtd()->create()->id,
+ ])
+ ->assertRedirect(route('settings.adapters.index', ['adapter' => $addigy->slug]));
+
+ $this->assertSame('https://prod.addigy.com/api', SyncAdapterConfig::get($addigy->id, 'url'));
+
+ // key_id is a non-secret credential (Client ID is a public
+ // identifier per OAuth 2.0 convention), stored as plain text.
+ // key_secret is encrypted at rest. Both are stored under
+ // distinct keys so a mix-up would surface here.
+ $storedKeyId = SyncAdapterConfig::get($addigy->id, 'key_id');
+ $storedKeySecret = SyncAdapterConfig::get($addigy->id, 'key_secret');
+ $this->assertSame('plaintext-addigy-client-id', $storedKeyId);
+ $this->assertNotSame('plaintext-addigy-client-secret', $storedKeySecret);
+ $this->assertSame('plaintext-addigy-client-secret', Crypt::decrypt($storedKeySecret));
+ }
+
+ public function test_blank_credentials_on_save_fail_validation()
+ {
+ $addigy = SyncAdapterInstance::where('slug', 'addigy')->firstOrFail();
+ SyncAdapterConfig::put($addigy->id, 'key_id', 'existing-key-id');
+ SyncAdapterConfig::put($addigy->id, 'key_secret', Crypt::encrypt('existing-key-secret'));
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->from(route('settings.adapters.index', ['adapter' => $addigy->slug]))
+ ->post(route('settings.adapters.save', $addigy), [
+ 'addigy_url' => 'https://prod.addigy.com/api',
+ 'addigy_key_id' => '',
+ 'addigy_key_secret' => '',
+ 'addigy_default_category_id' => Category::factory()->create()->id,
+ 'addigy_default_status_id' => Statuslabel::factory()->rtd()->create()->id,
+ ])
+ ->assertRedirect()
+ ->assertSessionHasErrors(['addigy_key_id', 'addigy_key_secret']);
+
+ // Stored values stay put because the save was rejected.
+ // key_id is plain text, key_secret is encrypted.
+ $this->assertSame('existing-key-id', SyncAdapterConfig::get($addigy->id, 'key_id'));
+ $this->assertSame('existing-key-secret', Crypt::decrypt(SyncAdapterConfig::get($addigy->id, 'key_secret')));
+ }
+}
diff --git a/tests/Feature/SyncAdapters/AssetTagPatternTest.php b/tests/Feature/SyncAdapters/AssetTagPatternTest.php
new file mode 100644
index 0000000000..060f4ed99c
--- /dev/null
+++ b/tests/Feature/SyncAdapters/AssetTagPatternTest.php
@@ -0,0 +1,215 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters;
+
+use App\Models\Asset;
+use App\Models\Category;
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\HostInventoryRecord;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Tests\TestCase;
+
+/**
+ * Coverage for the per-adapter asset-tag pattern feature. Admins
+ * configure a pattern like `KANDJI-{serial}` on the adapter's
+ * settings tab. SyncHostFromAdapter substitutes placeholders from
+ * the HostInventoryRecord when creating new assets. Existing assets
+ * keep their tags. the pattern is create-only.
+ */
+class AssetTagPatternTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pattern_substitutes_serial_placeholder_on_create()
+ {
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'asset_tag_pattern', 'FLEET-{serial}');
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: '1',
+ hostname: 'wksn-01',
+ hardwareSerial: 'SN-ABC123',
+ hardwareModel: 'MacBook Pro',
+ ));
+
+ $this->assertDatabaseHas('assets', [
+ 'name' => 'wksn-01',
+ 'asset_tag' => 'FLEET-SN-ABC123',
+ ]);
+ }
+
+ public function test_pattern_substitutes_multiple_placeholders()
+ {
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'asset_tag_pattern', '{source}-{external_id}');
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'host-99',
+ hostname: 'wksn-02',
+ hardwareSerial: 'SN-XYZ',
+ hardwareModel: 'MacBook Pro',
+ ));
+
+ $this->assertDatabaseHas('assets', [
+ 'name' => 'wksn-02',
+ 'asset_tag' => 'fleet-host-99',
+ ]);
+ }
+
+ public function test_pattern_with_missing_placeholder_still_renders_when_other_data_present()
+ {
+ // Vendor sent no serial. pattern substitutes empty. Result is
+ // still a usable tag because the literal prefix survives.
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'asset_tag_pattern', 'FLEET-{external_id}-{serial}');
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'h1',
+ hostname: 'wksn-03',
+ hardwareSerial: null,
+ hardwareModel: 'MacBook Pro',
+ ));
+
+ // {serial} rendered empty, so the trailing dash stays. Ugly
+ // but predictable. admin can adjust the pattern.
+ $this->assertDatabaseHas('assets', [
+ 'name' => 'wksn-03',
+ 'asset_tag' => 'FLEET-h1-',
+ ]);
+ }
+
+ public function test_no_pattern_falls_back_to_synthetic_source_id_tag()
+ {
+ $fleet = $this->configuredFleet();
+ // No asset_tag_pattern stored.
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'h42',
+ hostname: 'wksn-04',
+ hardwareSerial: 'SN-DEF',
+ hardwareModel: 'MacBook Pro',
+ ));
+
+ // With no pattern and no Snipe-IT autoincrement setting active
+ // in this test, the fallback synthetic tag ({source}-{sourceId})
+ // wins.
+ $this->assertDatabaseHas('assets', [
+ 'name' => 'wksn-04',
+ 'asset_tag' => 'fleet-h42',
+ ]);
+ }
+
+ public function test_pattern_does_not_retag_existing_asset_on_update_sync()
+ {
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'asset_tag_pattern', 'FLEET-{serial}');
+
+ // First sync creates the asset.
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'stable-1',
+ hostname: 'wksn-05',
+ hardwareSerial: 'SN-ORIG',
+ hardwareModel: 'MacBook Pro',
+ ));
+
+ $created = Asset::where('name', 'wksn-05')->firstOrFail();
+ $this->assertSame('FLEET-SN-ORIG', $created->asset_tag);
+
+ // Admin manually renames the tag through the UI.
+ $created->update(['asset_tag' => 'CURATED-TAG-123']);
+
+ // Change the pattern between syncs (or the serial in the
+ // vendor payload). Second sync of the SAME external_id should
+ // not touch asset_tag.
+ SyncAdapterConfig::put($fleet->id, 'asset_tag_pattern', 'DIFFERENT-{serial}');
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'stable-1',
+ hostname: 'wksn-05',
+ hardwareSerial: 'SN-NEW',
+ hardwareModel: 'MacBook Pro',
+ ));
+
+ $created->refresh();
+ $this->assertSame('CURATED-TAG-123', $created->asset_tag);
+ }
+
+ public function test_default_category_id_places_auto_created_models_in_configured_category()
+ {
+ $fleet = $this->configuredFleet();
+ $category = \App\Models\Category::factory()->assetLaptopCategory()->create();
+ SyncAdapterConfig::put($fleet->id, 'default_category_id', (string) $category->id);
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'cat-1',
+ hostname: 'wksn-cat',
+ hardwareSerial: 'SN-CAT',
+ hardwareModel: 'A-New-Model-For-This-Test',
+ ));
+
+ $model = \App\Models\AssetModel::where('name', 'A-New-Model-For-This-Test')->firstOrFail();
+ $this->assertSame($category->id, (int) $model->category_id);
+ }
+
+ public function test_default_category_falls_back_to_discovered_hardware_when_unset()
+ {
+ $fleet = $this->configuredFleet();
+ // No default_category_id stored.
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'cat-2',
+ hostname: 'wksn-discovered',
+ hardwareSerial: 'SN-DIS',
+ hardwareModel: 'Another-New-Model-For-This-Test',
+ ));
+
+ $model = \App\Models\AssetModel::where('name', 'Another-New-Model-For-This-Test')->firstOrFail();
+ $fallback = \App\Models\Category::where('name', 'Discovered Hardware')->firstOrFail();
+ $this->assertSame($fallback->id, (int) $model->category_id);
+ }
+
+ public function test_pattern_field_persists_on_save()
+ {
+ $fleet = $this->configuredFleet();
+
+ $this->actingAs(\App\Models\User::factory()->superuser()->create())
+ ->post(route('settings.adapters.save', $fleet), [
+ 'fleet_url' => 'https://example.com/fleet',
+ 'fleet_token' => 'fake-token',
+ 'fleet_asset_tag_pattern' => 'FLEET-{serial}',
+ 'fleet_default_category_id' => Category::factory()->create()->id,
+ 'fleet_default_status_id' => Statuslabel::factory()->rtd()->create()->id,
+ ])
+ ->assertRedirect();
+
+ $this->assertSame(
+ 'FLEET-{serial}',
+ SyncAdapterConfig::get($fleet->id, 'asset_tag_pattern'),
+ );
+ }
+
+ private function configuredFleet(): SyncAdapterInstance
+ {
+ $instance = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://example.com/fleet');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-token'));
+
+ return $instance->fresh();
+ }
+}
diff --git a/tests/Feature/SyncAdapters/BuiltInDeleteProtectionTest.php b/tests/Feature/SyncAdapters/BuiltInDeleteProtectionTest.php
new file mode 100644
index 0000000000..07b1371e70
--- /dev/null
+++ b/tests/Feature/SyncAdapters/BuiltInDeleteProtectionTest.php
@@ -0,0 +1,142 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters;
+
+use App\Models\SyncAdapterInstance;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Built-in adapters (Fleet, Kandji, Intune, and the 8 others seeded by
+ * the migration) must never be deletable. If a user doesn't want to
+ * use one, they leave it inactive. the record has to persist because
+ * asset_external_sources.source may still reference it after past syncs.
+ *
+ * Protection lives in three places: the settings-page delete button is
+ * hidden by @if(! isBuiltIn()) in the blade, the controller refuses
+ * built_in=1 rows in deleteAdapterInstance, and the seed migration
+ * sets built_in=1 on every shipped adapter. This test exercises all
+ * three so a future refactor can't silently drop the guard.
+ */
+class BuiltInDeleteProtectionTest extends TestCase
+{
+ public function test_delete_button_is_hidden_when_a_built_in_adapter_is_selected()
+ {
+ $fleet = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index', ['adapter' => $fleet->slug]))
+ ->assertOk()
+ ->getContent();
+
+ // Trigger is always rendered so the tab-switch JS can flip its
+ // visibility as the admin moves between adapters. Server-side
+ // initial state must hide it when a built-in is the selected
+ // adapter (matches what the JS also enforces).
+ $this->assertMatchesRegularExpression(
+ '/id="adapter-delete-trigger"[^>]*style="[^"]*display:\s*none/i',
+ $html,
+ );
+ }
+
+ public function test_delete_button_is_visible_when_a_user_created_adapter_is_selected()
+ {
+ $userMade = SyncAdapterInstance::create([
+ 'adapter_type' => 'fleet',
+ 'label' => 'Second Fleet',
+ 'built_in' => false,
+ ]);
+
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index', ['adapter' => $userMade->slug]))
+ ->assertOk()
+ ->getContent();
+
+ // Visible = style="" (empty) when initially selected. The regex
+ // is generous to survive attribute-order shuffles from Blade
+ // formatting changes.
+ $this->assertMatchesRegularExpression(
+ '/id="adapter-delete-trigger"[^>]*style=""/i',
+ $html,
+ );
+
+ // data-href points at the user-created adapter, not any of the
+ // built-ins that share the tab list. The shared .delete-asset
+ // handler in snipeit.js reads data-href when the button is
+ // clicked and swaps it into #dataConfirmModal's <form action>.
+ $this->assertStringContainsString(
+ 'data-href="'.route('settings.adapters.destroy', $userMade->slug).'"',
+ $html,
+ );
+ }
+
+ public function test_tab_list_carries_data_attrs_for_the_js_toggle()
+ {
+ // Every tab link gets data-adapter-built-in + data-adapter-destroy-url
+ // so the shown.bs.tab handler can flip the delete form's
+ // visibility and action URL on client-side tab switches.
+ SyncAdapterInstance::create([
+ 'adapter_type' => 'fleet',
+ 'label' => 'Second Fleet',
+ 'built_in' => false,
+ ]);
+
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index'))
+ ->assertOk()
+ ->getContent();
+
+ // Built-in flag on the Fleet tab
+ $this->assertMatchesRegularExpression(
+ '/data-adapter-name="fleet"[^>]*data-adapter-built-in="1"/',
+ $html,
+ );
+ // User-created gets the 0 flag
+ $this->assertMatchesRegularExpression(
+ '/data-adapter-name="second-fleet"[^>]*data-adapter-built-in="0"/',
+ $html,
+ );
+ }
+
+ public function test_backend_refuses_to_delete_a_built_in_adapter()
+ {
+ $fleet = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->delete(route('settings.adapters.destroy', $fleet->slug))
+ ->assertRedirect(route('settings.adapters.index', ['adapter' => $fleet->slug]))
+ ->assertSessionHas('error');
+
+ // Row still exists. Belt-and-suspenders: even if the blade
+ // gate broke and someone crafted a raw DELETE, the built-in
+ // stays put.
+ $this->assertDatabaseHas('sync_adapter_instances', [
+ 'id' => $fleet->id,
+ 'slug' => 'fleet',
+ ]);
+ }
+
+ public function test_unknown_adapter_slug_redirects_to_index_with_error()
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index', ['adapter' => 'never-existed']))
+ ->assertRedirect(route('settings.adapters.index'))
+ ->assertSessionHas('error');
+ }
+
+ public function test_backend_deletes_a_user_created_adapter()
+ {
+ $userMade = SyncAdapterInstance::create([
+ 'adapter_type' => 'fleet',
+ 'label' => 'Second Fleet',
+ 'built_in' => false,
+ ]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->delete(route('settings.adapters.destroy', $userMade->slug))
+ ->assertRedirect(route('settings.adapters.index'))
+ ->assertSessionHas('success');
+
+ $this->assertDatabaseMissing('sync_adapter_instances', ['id' => $userMade->id]);
+ }
+}
diff --git a/tests/Feature/SyncAdapters/DemoModeLocksFormTest.php b/tests/Feature/SyncAdapters/DemoModeLocksFormTest.php
new file mode 100644
index 0000000000..45e4d1d2fd
--- /dev/null
+++ b/tests/Feature/SyncAdapters/DemoModeLocksFormTest.php
@@ -0,0 +1,86 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters;
+
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Confirms every interactive control on the shared sync-adapter form
+ * frame is disabled when the app is in demo mode. Prevents a demo user
+ * from wiring their real MDM into the public demo instance by typing
+ * credentials, flipping the active toggle, or picking mappings.
+ *
+ * Belongs at the SyncAdapters top level (not under any per-adapter
+ * directory) because the shell is shared: locking it locks every
+ * adapter's settings tab at once.
+ */
+class DemoModeLocksFormTest extends TestCase
+{
+ public function test_demo_mode_disables_every_interactive_control_on_the_adapter_form()
+ {
+ config()->set('app.lock_passwords', true);
+
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index'))
+ ->assertOk()
+ ->getContent();
+
+ // Credential + URL inputs render as XXXX-masked disabled text
+ // inputs when locked (so the placeholder ceremony is visible).
+ // Active and heartbeat checkboxes forward :disabled through
+ // <x-form.checkbox-row>. Mapping selects forward :disabled
+ // through <x-input.select> via attribute merge.
+ $this->assertStringContainsString('name="fleet_url"', $html);
+ $this->assertMatchesRegularExpression('/name="fleet_url"[^>]*disabled/', $html);
+ $this->assertMatchesRegularExpression('/name="fleet_token"[^>]*disabled/', $html);
+ $this->assertMatchesRegularExpression('/name="fleet_active"[^>]*disabled/', $html);
+ $this->assertMatchesRegularExpression('/name="fleet_log_heartbeats"[^>]*disabled/', $html);
+ $this->assertMatchesRegularExpression('/name="fleet_mapping\[hostname\]"[^>]*disabled/', $html);
+
+ // Same coverage for the key-pair auth shape (Addigy). Different
+ // partial, same shell, so locking must apply symmetrically.
+ $this->assertMatchesRegularExpression('/name="addigy_key_id"[^>]*disabled/', $html);
+ $this->assertMatchesRegularExpression('/name="addigy_key_secret"[^>]*disabled/', $html);
+ $this->assertMatchesRegularExpression('/name="addigy_active"[^>]*disabled/', $html);
+
+ // Save button lives in the footer, disabled by the index page
+ // directly. Sync Now lives per-pane in the sync-adapter-panel
+ // component and the panel disables it when locked, so every
+ // pane's sync button carries the disabled attribute.
+ $this->assertMatchesRegularExpression('/id="adapter-save-button"[^>]*disabled/', $html);
+ $this->assertStringContainsString(trans('admin/settings/general.sync_adapter_sync_now'), $html);
+
+ // Walk every <button>...</button> in the rendered HTML: any
+ // that contains the sync-now label must also carry a disabled
+ // attribute. Catches a regression that would leave the button
+ // clickable in demo mode.
+ $label = trans('admin/settings/general.sync_adapter_sync_now');
+ preg_match_all('/<button\b(?<attrs>[^>]*)>(?<body>.*?)<\/button>/s', $html, $buttons, PREG_SET_ORDER);
+ $syncButtonsSeen = 0;
+ foreach ($buttons as $match) {
+ if (! str_contains($match['body'], $label)) {
+ continue;
+ }
+ $syncButtonsSeen++;
+ $this->assertStringContainsString('disabled', $match['attrs'], 'Sync-now button rendered without disabled attribute in demo mode.');
+ }
+ $this->assertGreaterThan(0, $syncButtonsSeen);
+ }
+
+ public function test_non_demo_mode_leaves_interactive_controls_enabled()
+ {
+ config()->set('app.lock_passwords', false);
+
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index'))
+ ->assertOk()
+ ->getContent();
+
+ // Guardrail: if the shell ever accidentally hardcodes `disabled`
+ // instead of gating on the config, this catches it.
+ $this->assertDoesNotMatchRegularExpression('/name="fleet_url"[^>]*disabled/', $html);
+ $this->assertDoesNotMatchRegularExpression('/name="fleet_active"[^>]*disabled/', $html);
+ $this->assertDoesNotMatchRegularExpression('/name="fleet_mapping\[hostname\]"[^>]*disabled/', $html);
+ }
+}
diff --git a/tests/Feature/SyncAdapters/ExternalInventorySurfaceTest.php b/tests/Feature/SyncAdapters/ExternalInventorySurfaceTest.php
new file mode 100644
index 0000000000..e5f58214ad
--- /dev/null
+++ b/tests/Feature/SyncAdapters/ExternalInventorySurfaceTest.php
@@ -0,0 +1,162 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters;
+
+use App\Models\Asset;
+use App\Models\AssetExternalSource;
+use App\Models\User;
+use Illuminate\Support\Facades\DB;
+use Tests\TestCase;
+
+/**
+ * Covers the surfacing of sync-adapter-populated external inventory
+ * (MAC / IP / OS / OS version / last seen) on the asset detail view
+ * and via the assets API transformer.
+ *
+ * The write path is already covered by the per-adapter tests +
+ * SyncHostFromAdapter integration tests. This file is the read path.
+ */
+class ExternalInventorySurfaceTest extends TestCase
+{
+ public function test_asset_detail_view_renders_external_inventory_rows_when_data_exists()
+ {
+ $asset = Asset::factory()->create();
+
+ DB::table('asset_external_sources')->insert([
+ 'asset_id' => $asset->id,
+ 'source' => 'fleet',
+ 'external_id' => 'fleet-'.$asset->id,
+ 'primary_mac' => 'aa:bb:cc:dd:ee:ff',
+ 'primary_ip' => '10.0.0.42',
+ 'os' => 'macOS',
+ 'os_version' => '14.2.1',
+ 'last_seen' => now(),
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('hardware.show', $asset))
+ ->assertOk()
+ ->getContent();
+
+ // Each value appears in the rendered detail view.
+ $this->assertStringContainsString('aa:bb:cc:dd:ee:ff', $html);
+ $this->assertStringContainsString('10.0.0.42', $html);
+ $this->assertStringContainsString('macOS', $html);
+ $this->assertStringContainsString('14.2.1', $html);
+ }
+
+ public function test_asset_detail_view_omits_external_inventory_block_when_no_row_exists()
+ {
+ $asset = Asset::factory()->create();
+
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('hardware.show', $asset))
+ ->assertOk()
+ ->getContent();
+
+ // Each external-inventory data-row emits an
+ // `js-copy-external_*` CSS class on its copy-to-clipboard
+ // span. Their absence proves the block did not render.
+ // Asserting on the field labels alone is unreliable because
+ // the same lang keys leak into the assets-table column-picker
+ // JSON on other pages, causing false-positive matches.
+ $this->assertStringNotContainsString('js-copy-external_mac', $html);
+ $this->assertStringNotContainsString('js-copy-external_ip', $html);
+ $this->assertStringNotContainsString('js-copy-external_last_seen', $html);
+ }
+
+ public function test_asset_detail_view_omits_individual_rows_for_null_fields()
+ {
+ // Partial data: only MAC present, other fields null.
+ $asset = Asset::factory()->create();
+ DB::table('asset_external_sources')->insert([
+ 'asset_id' => $asset->id,
+ 'source' => 'fleet',
+ 'external_id' => 'fleet-'.$asset->id,
+ 'primary_mac' => 'aa:bb:cc:dd:ee:ff',
+ 'primary_ip' => null,
+ 'os' => null,
+ 'os_version' => null,
+ 'last_seen' => null,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('hardware.show', $asset))
+ ->assertOk()
+ ->getContent();
+
+ // MAC row rendered (marker + value both present).
+ $this->assertStringContainsString('js-copy-external_mac', $html);
+ $this->assertStringContainsString('aa:bb:cc:dd:ee:ff', $html);
+ // Null-valued rows did not render (no marker).
+ $this->assertStringNotContainsString('js-copy-external_ip', $html);
+ $this->assertStringNotContainsString('js-copy-external_os', $html);
+ $this->assertStringNotContainsString('js-copy-external_last_seen', $html);
+ }
+
+ public function test_assets_api_exposes_external_inventory_fields_as_flat_keys()
+ {
+ $asset = Asset::factory()->create();
+ DB::table('asset_external_sources')->insert([
+ 'asset_id' => $asset->id,
+ 'source' => 'fleet',
+ 'external_id' => 'fleet-'.$asset->id,
+ 'primary_mac' => 'aa:bb:cc:dd:ee:ff',
+ 'primary_ip' => '10.0.0.42',
+ 'os' => 'macOS',
+ 'os_version' => '14.2.1',
+ 'last_seen' => now(),
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ $response = $this->actingAs(User::factory()->superuser()->create(), 'api')
+ ->getJson(route('api.assets.show', $asset));
+
+ $response->assertOk()
+ ->assertJsonPath('primary_mac', 'aa:bb:cc:dd:ee:ff')
+ ->assertJsonPath('primary_ip', '10.0.0.42')
+ ->assertJsonPath('external_os', 'macOS')
+ ->assertJsonPath('external_os_version', '14.2.1');
+
+ // last_seen is present as a formatted datetime string, not
+ // asserting the exact value (locale + timezone would make it
+ // brittle) but confirming the key is populated.
+ $this->assertNotEmpty($response->json('last_seen'));
+ }
+
+ public function test_assets_api_returns_null_external_inventory_keys_when_no_row_exists()
+ {
+ $asset = Asset::factory()->create();
+
+ $response = $this->actingAs(User::factory()->superuser()->create(), 'api')
+ ->getJson(route('api.assets.show', $asset));
+
+ // Keys still present in the payload shape (so bs-table's
+ // column bindings never throw missing-property errors), just
+ // null when no external inventory row exists.
+ $response->assertOk()
+ ->assertJsonPath('primary_mac', null)
+ ->assertJsonPath('primary_ip', null)
+ ->assertJsonPath('external_os', null)
+ ->assertJsonPath('external_os_version', null);
+ }
+
+ public function test_asset_external_source_belongs_to_asset()
+ {
+ $asset = Asset::factory()->create();
+ $source = AssetExternalSource::create([
+ 'asset_id' => $asset->id,
+ 'source' => 'fleet',
+ 'external_id' => 'fleet-'.$asset->id,
+ 'primary_mac' => 'aa:bb:cc:dd:ee:ff',
+ ]);
+
+ $this->assertTrue($asset->externalSource->is($source));
+ $this->assertTrue($source->asset->is($asset));
+ }
+}
diff --git a/tests/Feature/SyncAdapters/ExtraFieldMappingTest.php b/tests/Feature/SyncAdapters/ExtraFieldMappingTest.php
new file mode 100644
index 0000000000..445e821a89
--- /dev/null
+++ b/tests/Feature/SyncAdapters/ExtraFieldMappingTest.php
@@ -0,0 +1,280 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters;
+
+use App\Models\CustomField;
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\Fleet\FleetAdapter;
+use App\SyncAdapters\Support\MappingTargets;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * Coverage for the extra-field mapping flow: adapters declare
+ * vendor-specific extra keys via extraFields(), admin maps each to a
+ * custom field, and SyncHostFromAdapter writes the (stringified) value
+ * onto the asset's dynamic column.
+ *
+ * Fleet is the workhorse here because its normalize() emits a mix of
+ * scalar (fleet_team, fleet_uuid, fleet_status) and array
+ * (fleet_labels) values, exercising the stringifier for both shapes.
+ */
+class ExtraFieldMappingTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_extra_field_mapping_writes_scalar_value_to_custom_field()
+ {
+ $customField = CustomField::factory()->create(['element' => 'text']);
+ $fleet = $this->configuredFleetInstance();
+
+ // Map fleet_team (scalar string) to the custom field.
+ SyncAdapterConfig::put($fleet->id, 'mapping.fleet_team', 'custom:'.$customField->id);
+
+ Http::fake([
+ '*/api/latest/fleet/hosts*' => Http::sequence()
+ ->push(['hosts' => [
+ $this->fleetHost(id: 1, hostname: 'wksn-01', hardware_model: 'MacBook Pro', team_name: 'Engineering'),
+ ]])
+ ->push(['hosts' => []]),
+ ]);
+
+ $adapter = new FleetAdapter($fleet);
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseHas('assets', [
+ 'name' => 'wksn-01',
+ $customField->db_column => 'Engineering',
+ ]);
+ }
+
+ public function test_extra_field_mapping_stringifies_array_to_comma_joined()
+ {
+ $customField = CustomField::factory()->create(['element' => 'text']);
+ $fleet = $this->configuredFleetInstance();
+
+ // Map fleet_labels (an array from the vendor) to the custom field.
+ SyncAdapterConfig::put($fleet->id, 'mapping.fleet_labels', 'custom:'.$customField->id);
+
+ Http::fake([
+ '*/api/latest/fleet/hosts*' => Http::sequence()
+ ->push(['hosts' => [
+ $this->fleetHost(
+ id: 2,
+ hostname: 'wksn-02',
+ hardware_model: 'MacBook Pro',
+ labels: ['production', 'us-east', 'engineering'],
+ ),
+ ]])
+ ->push(['hosts' => []]),
+ ]);
+
+ $adapter = new FleetAdapter($fleet);
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseHas('assets', [
+ 'name' => 'wksn-02',
+ $customField->db_column => 'production, us-east, engineering',
+ ]);
+ }
+
+ public function test_skipped_extra_fields_do_not_touch_the_asset()
+ {
+ $customField = CustomField::factory()->create(['element' => 'text']);
+ $fleet = $this->configuredFleetInstance();
+
+ // No mapping stored: default is skip for extras.
+ Http::fake([
+ '*/api/latest/fleet/hosts*' => Http::sequence()
+ ->push(['hosts' => [
+ $this->fleetHost(id: 3, hostname: 'wksn-03', hardware_model: 'MacBook Pro', team_name: 'Engineering'),
+ ]])
+ ->push(['hosts' => []]),
+ ]);
+
+ $adapter = new FleetAdapter($fleet);
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ // Asset created, but the custom-field column stays null since
+ // no mapping was stored.
+ $this->assertDatabaseHas('assets', [
+ 'name' => 'wksn-03',
+ $customField->db_column => null,
+ ]);
+ }
+
+ public function test_extra_field_options_only_include_skip_and_custom_targets()
+ {
+ CustomField::factory()->create(['element' => 'text', 'name' => 'Team Assignment']);
+
+ $options = MappingTargets::optionsForExtra();
+
+ // First option is always skip.
+ $this->assertArrayHasKey('skip', $options);
+
+ // Every non-skip option is a custom:{id} target. Extras never
+ // route to native or external targets by design.
+ foreach ($options as $target => $label) {
+ if ($target === 'skip') {
+ continue;
+ }
+ $this->assertStringStartsWith('custom:', $target);
+ }
+ }
+
+ public function test_asset_tag_native_target_overwrites_the_asset_tag_column()
+ {
+ $fleet = $this->configuredFleetInstance();
+
+ // Standard-field mapping: route the normalized asset_tag field
+ // to Snipe-IT's native asset_tag column. Fleet doesn't emit
+ // asset_tag in its normalize() so we simulate a source that
+ // does by using the extra->normalized path elsewhere. Here we
+ // exercise the mapping wiring itself.
+ SyncAdapterConfig::put($fleet->id, 'mapping.asset_tag', 'native:asset_tag');
+
+ // Build a record with an explicit assetTag so writeNative
+ // picks it up.
+ $record = new \App\SyncAdapters\HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'tag-test-1',
+ hostname: 'host-with-tag',
+ hardwareSerial: 'SN-1',
+ hardwareModel: 'MacBook Pro',
+ assetTag: 'VENDOR-TAG-001',
+ );
+
+ SyncHostFromAdapter::run($record);
+
+ $this->assertDatabaseHas('assets', [
+ 'name' => 'host-with-tag',
+ 'asset_tag' => 'VENDOR-TAG-001',
+ ]);
+ }
+
+ public function test_asset_tag_defaults_to_native_and_writes_vendor_tag()
+ {
+ $fleet = $this->configuredFleetInstance();
+
+ // No explicit mapping stored: default target for asset_tag is
+ // native:asset_tag so the vendor's tag flows into Snipe-IT's
+ // asset_tag column. Common case is admins want the printed
+ // sticker in Kandji / Jamf to match the Snipe-IT tag.
+ $record = new \App\SyncAdapters\HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'tag-test-2',
+ hostname: 'host-default-mapping',
+ hardwareSerial: 'SN-2',
+ hardwareModel: 'MacBook Pro',
+ assetTag: 'VENDOR-TAG-002',
+ );
+
+ SyncHostFromAdapter::run($record);
+
+ $this->assertDatabaseHas('assets', [
+ 'name' => 'host-default-mapping',
+ 'asset_tag' => 'VENDOR-TAG-002',
+ ]);
+ }
+
+ public function test_asset_tag_skip_target_preserves_curated_snipe_it_tag()
+ {
+ $fleet = $this->configuredFleetInstance();
+
+ // Admins who want to protect curated tags flip the mapping
+ // to skip. Vendor tag ignored on both create and update.
+ SyncAdapterConfig::put($fleet->id, 'mapping.asset_tag', 'skip');
+
+ $record = new \App\SyncAdapters\HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'tag-test-3',
+ hostname: 'host-skipped-mapping',
+ hardwareSerial: 'SN-3',
+ hardwareModel: 'MacBook Pro',
+ assetTag: 'VENDOR-TAG-003',
+ );
+
+ SyncHostFromAdapter::run($record);
+
+ $created = \App\Models\Asset::where('name', 'host-skipped-mapping')->firstOrFail();
+ $this->assertNotSame('VENDOR-TAG-003', $created->asset_tag);
+ }
+
+ public function test_adapter_declares_extra_fields_via_schema()
+ {
+ $fleet = $this->configuredFleetInstance();
+ $adapter = new FleetAdapter($fleet);
+
+ $extras = $adapter->extraFields();
+
+ // Fleet's normalize() emits these four extras. the declaration
+ // matches so the mapping UI knows to render dropdowns for
+ // them.
+ $this->assertArrayHasKey('fleet_team', $extras);
+ $this->assertArrayHasKey('fleet_labels', $extras);
+ $this->assertArrayHasKey('fleet_uuid', $extras);
+ $this->assertArrayHasKey('fleet_status', $extras);
+ }
+
+ public function test_settings_page_renders_extra_field_dropdowns_for_adapter_that_declares_them()
+ {
+ $customField = CustomField::factory()->create(['element' => 'text', 'name' => 'Team Assignment']);
+
+ $html = $this->actingAs(\App\Models\User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index', ['adapter' => 'fleet']))
+ ->assertOk()
+ ->getContent();
+
+ // Rows for each Fleet extra field appear on the page.
+ $this->assertStringContainsString('fleet_mapping[fleet_team]', $html);
+ $this->assertStringContainsString('fleet_mapping[fleet_labels]', $html);
+ // Custom field is offered as a target for extras.
+ $this->assertStringContainsString('Team Assignment', $html);
+ }
+
+ private function configuredFleetInstance(): SyncAdapterInstance
+ {
+ $instance = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://example.com/fleet');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-token'));
+
+ return $instance->fresh();
+ }
+
+ /**
+ * @param array<int, string>|null $labels
+ * @return array<string, mixed>
+ */
+ private function fleetHost(
+ int $id,
+ string $hostname = 'host',
+ string $hardware_model = 'Generic Laptop',
+ ?string $team_name = null,
+ ?array $labels = null,
+ ): array {
+ return [
+ 'id' => $id,
+ 'hostname' => $hostname,
+ 'hardware_model' => $hardware_model,
+ 'team_name' => $team_name,
+ 'labels' => $labels,
+ 'uuid' => 'fleet-uuid-'.$id,
+ 'status' => 'online',
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Fleet/FleetAdapterTest.php b/tests/Feature/SyncAdapters/Fleet/FleetAdapterTest.php
new file mode 100644
index 0000000000..fe80ada12b
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Fleet/FleetAdapterTest.php
@@ -0,0 +1,223 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Fleet;
+
+use App\Models\Asset;
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\Fleet\FleetAdapter;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the Fleet adapter through SyncHostFromAdapter.
+ * Mocks Fleet's REST API, asserts assets + asset_external_sources + side-
+ * table rows land correctly, and that a re-run updates instead of
+ * duplicating.
+ *
+ * The adapter is instance-bound, so every test creates a
+ * SyncAdapterInstance first and populates its URL + token via
+ * SyncAdapterConfig.
+ */
+class FleetAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ // SyncHostFromAdapter picks a deployable status label or falls
+ // back to any status label. Seed at least one so create doesn't
+ // blow up.
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_hosts_from_fleet_and_creates_assets()
+ {
+ $adapter = $this->configuredFleetAdapter();
+
+ Http::fake([
+ '*/api/latest/fleet/hosts*' => Http::sequence()
+ ->push($this->fleetHostsResponse([
+ // primary_mac included so the external-inventory
+ // side-table row is actually written. the mapping
+ // path skips null values by design to keep partial
+ // syncs from blanking existing data.
+ $this->fleetHost(id: 42, hostname: 'workstation-01', hardware_model: 'MacBook Pro 16-inch (M3)', primary_mac: 'aa:bb:cc:00:00:42'),
+ $this->fleetHost(id: 99, hostname: 'workstation-02', hardware_model: 'ThinkPad X1 Carbon', primary_mac: 'aa:bb:cc:00:00:99'),
+ ])),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'fleet', 'external_id' => '42']);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'fleet', 'external_id' => '99']);
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+
+ $this->assertDatabaseHas('assets', ['name' => 'workstation-01']);
+ $this->assertDatabaseHas('assets', ['name' => 'workstation-02']);
+ }
+
+ public function test_re_running_updates_existing_assets_instead_of_duplicating()
+ {
+ $adapter = $this->configuredFleetAdapter();
+
+ Http::fake([
+ '*/api/latest/fleet/hosts*' => Http::sequence()
+ // first pull: one host
+ ->push($this->fleetHostsResponse([
+ $this->fleetHost(id: 42, hostname: 'workstation-01', hardware_model: 'MacBook Pro'),
+ ]))
+ // second pull: same host with a new hostname + IP
+ ->push($this->fleetHostsResponse([
+ $this->fleetHost(id: 42, hostname: 'workstation-01-renamed', hardware_model: 'MacBook Pro', primary_ip: '10.0.0.42'),
+ ])),
+ ]);
+
+ // First pull.
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 1);
+ $this->assertDatabaseCount('assets', 1);
+ $originalAssetId = Asset::where('name', 'workstation-01')->firstOrFail()->id;
+
+ // Second pull.
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ // No new rows: same host, same identity link.
+ $this->assertDatabaseCount('asset_external_sources', 1);
+ $this->assertDatabaseCount('assets', 1);
+
+ $updated = Asset::find($originalAssetId);
+ $this->assertSame('workstation-01-renamed', $updated->name);
+
+ $inventory = DB::table('asset_external_sources')->where('asset_id', $originalAssetId)->first();
+ $this->assertSame('10.0.0.42', $inventory->primary_ip);
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredFleetAdapter();
+
+ Http::fake([
+ '*/api/latest/fleet/hosts*' => Http::sequence()
+ ->push($this->fleetHostsResponse([
+ $this->fleetHost(
+ id: 7,
+ hostname: 'zed',
+ hardware_model: 'Framework 13',
+ hardware_serial: 'FW-ABC-123',
+ hardware_vendor: 'Framework',
+ primary_mac: 'aa:bb:cc:dd:ee:ff',
+ primary_ip: '192.168.1.7',
+ platform: 'linux',
+ os_version: 'Ubuntu 24.04',
+ seen_time: '2026-01-15T10:00:00Z',
+ ),
+ ])),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('fleet', $record->sourceKey);
+ $this->assertSame('7', $record->sourceId);
+ $this->assertSame('zed', $record->hostname);
+ $this->assertSame('Framework 13', $record->hardwareModel);
+ $this->assertSame('FW-ABC-123', $record->hardwareSerial);
+ $this->assertSame('Framework', $record->manufacturer);
+ $this->assertSame('aa:bb:cc:dd:ee:ff', $record->primaryMac);
+ $this->assertSame('192.168.1.7', $record->primaryIp);
+ $this->assertSame('linux', $record->os);
+ $this->assertSame('Ubuntu 24.04', $record->osVersion);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ public function test_synced_assets_inherit_the_instance_company_id()
+ {
+ $company = \App\Models\Company::factory()->create();
+ $instance = SyncAdapterInstance::create([
+ 'adapter_type' => 'fleet',
+ 'label' => 'Company Fleet',
+ 'company_id' => $company->id,
+ ]);
+ SyncAdapterConfig::put($instance->id, 'url', 'https://fleet.example.test');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-token'));
+
+ Http::fake([
+ '*/api/latest/fleet/hosts*' => Http::sequence()
+ ->push($this->fleetHostsResponse([
+ $this->fleetHost(id: 1, hostname: 'scoped-host', hardware_model: 'MacBook'),
+ ])),
+ ]);
+
+ $adapter = new FleetAdapter($instance);
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseHas('assets', ['name' => 'scoped-host', 'company_id' => $company->id]);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => $instance->slug, 'company_id' => $company->id]);
+ }
+
+ /** Builds and hydrates the built-in Fleet instance seeded by the migration. */
+ private function configuredFleetAdapter(): FleetAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://fleet.example.test');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-token'));
+
+ return new FleetAdapter($instance->fresh());
+ }
+
+ /**
+ * @param array<int, array<string, mixed>> $hosts
+ * @return array<string, mixed>
+ */
+ private function fleetHostsResponse(array $hosts): array
+ {
+ return ['hosts' => $hosts];
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function fleetHost(
+ int $id,
+ string $hostname = 'host',
+ string $hardware_model = 'Generic Laptop',
+ ?string $hardware_serial = null,
+ ?string $hardware_vendor = null,
+ ?string $primary_mac = null,
+ ?string $primary_ip = null,
+ ?string $platform = null,
+ ?string $os_version = null,
+ ?string $seen_time = null,
+ ): array {
+ return [
+ 'id' => $id,
+ 'hostname' => $hostname,
+ 'hardware_model' => $hardware_model,
+ 'hardware_serial' => $hardware_serial,
+ 'hardware_vendor' => $hardware_vendor,
+ 'primary_mac' => $primary_mac,
+ 'primary_ip' => $primary_ip,
+ 'platform' => $platform,
+ 'os_version' => $os_version,
+ 'seen_time' => $seen_time,
+ 'uuid' => 'fleet-uuid-'.$id,
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Fleet/FleetSettingsPageTest.php b/tests/Feature/SyncAdapters/Fleet/FleetSettingsPageTest.php
new file mode 100644
index 0000000000..939fb16545
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Fleet/FleetSettingsPageTest.php
@@ -0,0 +1,135 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Fleet;
+
+use App\Models\Category;
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\Models\User;
+use Illuminate\Support\Facades\Crypt;
+use Tests\TestCase;
+
+/**
+ * Coverage for the Fleet section of the shared Settings → Sync Adapters
+ * page. Access gates, form render, persistence with encryption, and
+ * preserve-existing-token behavior when the admin leaves the token
+ * field blank on a subsequent save. Also covers ExternalUrl SSRF-guard
+ * enforcement so a superadmin can't point the adapter at 127.0.0.1.
+ */
+class FleetSettingsPageTest extends TestCase
+{
+ /**
+ * default_category_id + default_status_id are now required by
+ * postAdapterConfig, so every settings-save happy-path test needs
+ * to seed them. Kept in one place so a rename or additional
+ * required field lands in one spot rather than 13.
+ *
+ * @return array<string, int>
+ */
+ private function requiredFields(string $slug): array
+ {
+ return [
+ $slug.'_default_category_id' => Category::factory()->create()->id,
+ $slug.'_default_status_id' => Statuslabel::factory()->rtd()->create()->id,
+ ];
+ }
+
+ public function test_superuser_sees_the_fleet_section_on_the_adapters_page()
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index'))
+ ->assertOk()
+ ->assertSee('Fleet') // tab label
+ ->assertSee('Base URL') // generic field label
+ ->assertSee('API Token'); // generic field label
+ }
+
+ public function test_non_superuser_gets_forbidden()
+ {
+ $this->actingAs(User::factory()->create())
+ ->get(route('settings.adapters.index'))
+ ->assertForbidden();
+ }
+
+ public function test_saving_persists_url_and_encrypts_token()
+ {
+ $fleet = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('settings.adapters.save', $fleet), $this->requiredFields('fleet') + [
+ 'fleet_url' => 'https://example.com/fleet',
+ 'fleet_token' => 'plaintext-token-value',
+ ])
+ ->assertRedirect(route('settings.adapters.index', ['adapter' => $fleet->slug]));
+
+ $this->assertSame('https://example.com/fleet', SyncAdapterConfig::get($fleet->id, 'url'));
+
+ $storedToken = SyncAdapterConfig::get($fleet->id, 'token');
+ $this->assertNotSame('plaintext-token-value', $storedToken);
+ $this->assertSame('plaintext-token-value', Crypt::decrypt($storedToken));
+ }
+
+ public function test_blank_token_on_save_fails_validation()
+ {
+ $fleet = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+ SyncAdapterConfig::put($fleet->id, 'url', 'https://example.com/fleet-old');
+ SyncAdapterConfig::put($fleet->id, 'token', Crypt::encrypt('previously-saved-token'));
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->from(route('settings.adapters.index', ['adapter' => $fleet->slug]))
+ ->post(route('settings.adapters.save', $fleet), $this->requiredFields('fleet') + [
+ 'fleet_url' => 'https://example.com/fleet-new',
+ 'fleet_token' => '',
+ ])
+ ->assertRedirect()
+ ->assertSessionHasErrors('fleet_token');
+
+ // URL didn't persist because the whole save was rejected.
+ // Stored token stays put too.
+ $this->assertSame('https://example.com/fleet-old', SyncAdapterConfig::get($fleet->id, 'url'));
+ $this->assertSame('previously-saved-token', Crypt::decrypt(SyncAdapterConfig::get($fleet->id, 'token')));
+ }
+
+ public function test_unknown_instance_on_save_returns_404()
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('settings.adapters.save', 'does-not-exist'), [
+ 'fleet_url' => 'https://example.com/fleet',
+ ])
+ ->assertNotFound();
+ }
+
+ public function test_loopback_url_is_rejected_by_external_url_guard()
+ {
+ $fleet = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->from(route('settings.adapters.index', ['adapter' => $fleet->slug]))
+ ->post(route('settings.adapters.save', $fleet), $this->requiredFields('fleet') + [
+ 'fleet_url' => 'http://127.0.0.1:9999/api',
+ 'fleet_token' => 'secret',
+ ])
+ ->assertRedirect()
+ ->assertSessionHasErrors('fleet_url');
+
+ // Config didn't persist the loopback URL.
+ $this->assertNull(SyncAdapterConfig::get($fleet->id, 'url'));
+ }
+
+ public function test_metadata_ip_is_rejected_by_external_url_guard()
+ {
+ $fleet = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->from(route('settings.adapters.index', ['adapter' => $fleet->slug]))
+ ->post(route('settings.adapters.save', $fleet), $this->requiredFields('fleet') + [
+ 'fleet_url' => 'http://169.254.169.254/latest/meta-data/',
+ 'fleet_token' => 'secret',
+ ])
+ ->assertRedirect()
+ ->assertSessionHasErrors('fleet_url');
+
+ $this->assertNull(SyncAdapterConfig::get($fleet->id, 'url'));
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Fleet/FleetSyncNowTest.php b/tests/Feature/SyncAdapters/Fleet/FleetSyncNowTest.php
new file mode 100644
index 0000000000..efdd33d8e5
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Fleet/FleetSyncNowTest.php
@@ -0,0 +1,138 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Fleet;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\Models\User;
+use Illuminate\Support\Facades\Artisan;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * Covers the "Sync Now" button flow (POST /admin/adapters/fleet/sync)
+ * and the snipeit:sync-inventory artisan command targeted at Fleet.
+ * Both share the same adapter plumbing, but each entry point is tested
+ * independently so a regression in either path is caught.
+ */
+class FleetSyncNowTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_sync_button_returns_error_when_fleet_is_not_configured()
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('settings.adapters.sync', 'fleet'))
+ ->assertRedirect(route('settings.adapters.index', ['adapter' => 'fleet']))
+ ->assertSessionHas('error');
+ }
+
+ public function test_sync_button_runs_the_adapter_when_configured()
+ {
+ $fleet = $this->configureFleet();
+
+ Http::fake([
+ '*/api/latest/fleet/hosts*' => Http::response([
+ 'hosts' => [
+ ['id' => 42, 'hostname' => 'wksn-01', 'hardware_model' => 'MacBook Pro'],
+ ['id' => 99, 'hostname' => 'wksn-02', 'hardware_model' => 'ThinkPad X1'],
+ ],
+ ]),
+ ]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('settings.adapters.sync', $fleet->slug))
+ ->assertRedirect(route('settings.adapters.index', ['adapter' => $fleet->slug]))
+ ->assertSessionHas('success');
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseCount('assets', 2);
+
+ // Status columns persist the last-run outcome so page reload
+ // after a browser tab close mid-sync still shows what happened
+ // server-side.
+ $fleet->refresh();
+ $this->assertNotNull($fleet->last_synced_at);
+ $this->assertStringContainsString('Synced 2', $fleet->last_sync_result);
+ }
+
+ public function test_non_superuser_cannot_trigger_sync()
+ {
+ $this->actingAs(User::factory()->create())
+ ->post(route('settings.adapters.sync', 'fleet'))
+ ->assertForbidden();
+ }
+
+ public function test_unknown_instance_returns_404()
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('settings.adapters.sync', 'does-not-exist'))
+ ->assertNotFound();
+ }
+
+ public function test_artisan_command_fails_when_fleet_is_not_configured()
+ {
+ $exit = Artisan::call('snipeit:sync-inventory', ['adapter' => 'fleet']);
+
+ $this->assertSame(1, $exit);
+ }
+
+ public function test_artisan_command_fails_with_unknown_instance()
+ {
+ $exit = Artisan::call('snipeit:sync-inventory', ['adapter' => 'nope']);
+
+ $this->assertSame(1, $exit);
+ $this->assertStringContainsString('Unknown adapter instance', Artisan::output());
+ }
+
+ public function test_artisan_command_pulls_and_upserts_hosts()
+ {
+ $fleet = $this->configureFleet();
+
+ Http::fake([
+ '*/api/latest/fleet/hosts*' => Http::response([
+ 'hosts' => [
+ ['id' => 42, 'hostname' => 'wksn-01', 'hardware_model' => 'MacBook Pro'],
+ ],
+ ]),
+ ]);
+
+ $exit = Artisan::call('snipeit:sync-inventory', ['adapter' => $fleet->slug]);
+
+ $this->assertSame(0, $exit);
+ $this->assertDatabaseCount('asset_external_sources', 1);
+ // CLI + UI now share the sync_adapter_sync_complete phrasing so
+ // scheduled runs render the same status admins see on the
+ // settings page.
+ $this->assertStringContainsString('Synced 1', Artisan::output());
+
+ // CLI runs write the same status columns as the interactive
+ // button so scheduled cron jobs surface in the UI.
+ $fleet->refresh();
+ $this->assertNotNull($fleet->last_synced_at);
+ $this->assertStringContainsString('Synced 1', $fleet->last_sync_result);
+ }
+
+ /**
+ * Set the built-in Fleet instance to active and populate its
+ * credentials. Returns the fresh instance.
+ */
+ private function configureFleet(): SyncAdapterInstance
+ {
+ $fleet = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+ $fleet->active = true;
+ $fleet->save();
+
+ SyncAdapterConfig::put($fleet->id, 'url', 'https://fleet.example.test');
+ SyncAdapterConfig::put($fleet->id, 'token', Crypt::encrypt('fake-token'));
+
+ return $fleet->fresh();
+ }
+}
diff --git a/tests/Feature/SyncAdapters/GroupCompanyMappingTest.php b/tests/Feature/SyncAdapters/GroupCompanyMappingTest.php
new file mode 100644
index 0000000000..e27be0a91f
--- /dev/null
+++ b/tests/Feature/SyncAdapters/GroupCompanyMappingTest.php
@@ -0,0 +1,220 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters;
+
+use App\Models\Asset;
+use App\Models\Category;
+use App\Models\Company;
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\HostInventoryRecord;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Tests\TestCase;
+
+/**
+ * Coverage for per-adapter vendor-group to Snipe-IT-company mapping.
+ * When an adapter supports group scoping and the sync record carries
+ * a vendorGroupId that has a mapping, the resulting asset lands in
+ * the mapped company. Otherwise it falls back to the instance's own
+ * company_id. Uses Fleet as the exemplar because Fleet Teams are a
+ * clean semantic match for company scoping.
+ */
+class GroupCompanyMappingTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_mapped_group_lands_asset_in_mapped_company()
+ {
+ $customerA = Company::factory()->create(['name' => 'Customer A']);
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'group_mapping.42', (string) $customerA->id);
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'gc-1',
+ hostname: 'customer-a-laptop',
+ hardwareSerial: 'SN-A',
+ hardwareModel: 'MacBook Pro',
+ vendorGroupId: '42',
+ ));
+
+ $asset = Asset::where('name', 'customer-a-laptop')->firstOrFail();
+ $this->assertSame($customerA->id, (int) $asset->company_id);
+ }
+
+ public function test_unmapped_group_falls_back_to_instance_company()
+ {
+ $customerA = Company::factory()->create();
+ $fleet = $this->configuredFleet($customerA->id);
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'gc-2',
+ hostname: 'unmapped-group-laptop',
+ hardwareSerial: 'SN-B',
+ hardwareModel: 'MacBook Pro',
+ vendorGroupId: '999',
+ ));
+
+ $asset = Asset::where('name', 'unmapped-group-laptop')->firstOrFail();
+ $this->assertSame($customerA->id, (int) $asset->company_id);
+ }
+
+ public function test_missing_vendor_group_id_uses_instance_company()
+ {
+ $customerA = Company::factory()->create();
+ $fleet = $this->configuredFleet($customerA->id);
+ SyncAdapterConfig::put($fleet->id, 'group_mapping.42', '999');
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'gc-3',
+ hostname: 'no-group-laptop',
+ hardwareSerial: 'SN-C',
+ hardwareModel: 'MacBook Pro',
+ vendorGroupId: null,
+ ));
+
+ $asset = Asset::where('name', 'no-group-laptop')->firstOrFail();
+ $this->assertSame($customerA->id, (int) $asset->company_id);
+ }
+
+ public function test_group_change_on_subsequent_sync_reassigns_company()
+ {
+ $customerA = Company::factory()->create();
+ $customerB = Company::factory()->create();
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'group_mapping.10', (string) $customerA->id);
+ SyncAdapterConfig::put($fleet->id, 'group_mapping.20', (string) $customerB->id);
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'gc-4',
+ hostname: 'moving-laptop',
+ hardwareSerial: 'SN-D',
+ hardwareModel: 'MacBook Pro',
+ vendorGroupId: '10',
+ ));
+
+ $asset = Asset::where('name', 'moving-laptop')->firstOrFail();
+ $this->assertSame($customerA->id, (int) $asset->company_id);
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'gc-4',
+ hostname: 'moving-laptop',
+ hardwareSerial: 'SN-D',
+ hardwareModel: 'MacBook Pro',
+ vendorGroupId: '20',
+ ));
+
+ $asset->refresh();
+ $this->assertSame($customerB->id, (int) $asset->company_id);
+ }
+
+ public function test_group_mappings_accessor_returns_id_to_id_map()
+ {
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'group_mapping.10', '5');
+ SyncAdapterConfig::put($fleet->id, 'group_mapping.20', '7');
+
+ $adapter = $fleet->fresh()->adapter();
+ $this->assertSame(['10' => 5, '20' => 7], $adapter->groupMappings());
+ }
+
+ public function test_refresh_groups_endpoint_fetches_and_caches_vendor_groups()
+ {
+ $fleet = $this->configuredFleet();
+
+ \Illuminate\Support\Facades\Http::fake([
+ '*/api/latest/fleet/teams' => \Illuminate\Support\Facades\Http::response([
+ 'teams' => [
+ ['id' => 5, 'name' => 'Engineering'],
+ ['id' => 7, 'name' => 'Sales'],
+ ],
+ ]),
+ ]);
+
+ $this->actingAs(\App\Models\User::factory()->superuser()->create())
+ ->post(route('settings.adapters.refresh_groups', $fleet->slug))
+ ->assertRedirect()
+ ->assertSessionHas('success');
+
+ $stored = json_decode(SyncAdapterConfig::get($fleet->id, 'cached_groups'), true);
+ $this->assertSame(['id' => '5', 'label' => 'Engineering'], $stored[0]);
+ $this->assertSame(['id' => '7', 'label' => 'Sales'], $stored[1]);
+ }
+
+ public function test_refresh_groups_endpoint_404s_for_adapter_without_group_scoping()
+ {
+ // Zentral is a stub adapter that doesn't opt into group scoping,
+ // so its refresh-groups endpoint is not reachable.
+ $zentral = SyncAdapterInstance::where('slug', 'zentral')->firstOrFail();
+ SyncAdapterConfig::put($zentral->id, 'url', 'https://zentral.example.test');
+ SyncAdapterConfig::put($zentral->id, 'token', Crypt::encrypt('t'));
+ $zentral->active = true;
+ $zentral->save();
+
+ $this->actingAs(\App\Models\User::factory()->superuser()->create())
+ ->post(route('settings.adapters.refresh_groups', 'zentral'))
+ ->assertNotFound();
+ }
+
+ public function test_saving_adapter_persists_group_mappings_from_form()
+ {
+ $customerA = Company::factory()->create();
+ $fleet = $this->configuredFleet();
+
+ $this->actingAs(\App\Models\User::factory()->superuser()->create())
+ ->post(route('settings.adapters.save', $fleet), [
+ 'fleet_url' => 'https://example.com/fleet',
+ 'fleet_token' => 'fake-token',
+ 'fleet_group_mapping' => ['42' => (string) $customerA->id],
+ 'fleet_default_category_id' => Category::factory()->create()->id,
+ 'fleet_default_status_id' => Statuslabel::factory()->rtd()->create()->id,
+ ])
+ ->assertRedirect();
+
+ $this->assertSame(
+ (string) $customerA->id,
+ SyncAdapterConfig::get($fleet->id, 'group_mapping.42'),
+ );
+ }
+
+ public function test_blank_mapping_clears_an_existing_group_mapping()
+ {
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'group_mapping.42', '99');
+
+ $this->actingAs(\App\Models\User::factory()->superuser()->create())
+ ->post(route('settings.adapters.save', $fleet), [
+ 'fleet_url' => 'https://example.com/fleet',
+ 'fleet_token' => 'fake-token',
+ 'fleet_group_mapping' => ['42' => ''],
+ 'fleet_default_category_id' => Category::factory()->create()->id,
+ 'fleet_default_status_id' => Statuslabel::factory()->rtd()->create()->id,
+ ])
+ ->assertRedirect();
+
+ $this->assertNull(SyncAdapterConfig::get($fleet->id, 'group_mapping.42'));
+ }
+
+ private function configuredFleet(?int $companyId = null): SyncAdapterInstance
+ {
+ $instance = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+ $instance->company_id = $companyId;
+ $instance->active = true;
+ $instance->save();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://example.com/fleet');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-token'));
+
+ return $instance->fresh();
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Intune/IntuneAdapterTest.php b/tests/Feature/SyncAdapters/Intune/IntuneAdapterTest.php
new file mode 100644
index 0000000000..ef315ad9c8
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Intune/IntuneAdapterTest.php
@@ -0,0 +1,116 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Intune;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\Intune\IntuneAdapter;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the Microsoft Intune adapter through
+ * SyncHostFromAdapter. Fakes the OAuth token exchange and the
+ * Graph managedDevices endpoint.
+ */
+class IntuneAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_devices_from_graph_and_creates_assets()
+ {
+ $adapter = $this->configuredAdapter();
+
+ Http::fake([
+ '*/oauth2/v2.0/token' => Http::response(['access_token' => 'stub-bearer', 'expires_in' => 3600]),
+ '*/v1.0/deviceManagement/managedDevices*' => Http::response([
+ 'value' => [
+ $this->intuneDevice(id: 'guid-42', name: 'intune-host-1'),
+ $this->intuneDevice(id: 'guid-99', name: 'intune-host-2'),
+ ],
+ ]),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'intune', 'external_id' => 'guid-42']);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'intune', 'external_id' => 'guid-99']);
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredAdapter();
+
+ Http::fake([
+ '*/oauth2/v2.0/token' => Http::response(['access_token' => 'stub-bearer', 'expires_in' => 3600]),
+ '*/v1.0/deviceManagement/managedDevices*' => Http::response([
+ 'value' => [
+ [
+ 'id' => 'guid-7',
+ 'deviceName' => 'intune-mbp',
+ 'serialNumber' => 'FW-ABC-123',
+ 'model' => 'Surface Pro 9',
+ 'manufacturer' => 'Microsoft',
+ 'ethernetMacAddress' => 'aa:bb:cc:dd:ee:ff',
+ 'wiFiMacAddress' => '11:22:33:44:55:66',
+ 'operatingSystem' => 'Windows',
+ 'osVersion' => '11.24H2',
+ 'lastSyncDateTime' => '2026-01-15T10:00:00Z',
+ 'userPrincipalName' => '[email protected]',
+ 'complianceState' => 'compliant',
+ ],
+ ],
+ ]),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('intune', $record->sourceKey);
+ $this->assertSame('guid-7', $record->sourceId);
+ $this->assertSame('intune-mbp', $record->hostname);
+ $this->assertSame('FW-ABC-123', $record->hardwareSerial);
+ $this->assertSame('Surface Pro 9', $record->hardwareModel);
+ $this->assertSame('Microsoft', $record->manufacturer);
+ $this->assertSame('aa:bb:cc:dd:ee:ff', $record->primaryMac);
+ $this->assertSame('Windows', $record->os);
+ $this->assertSame('11.24H2', $record->osVersion);
+ $this->assertSame('[email protected]', $record->assignedUserEmail);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ private function configuredAdapter(): IntuneAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'intune')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://graph.microsoft.com');
+ SyncAdapterConfig::put($instance->id, 'tenant_id', 'stub-tenant');
+ SyncAdapterConfig::put($instance->id, 'client_id', 'stub-client');
+ SyncAdapterConfig::put($instance->id, 'client_secret', Crypt::encrypt('fake-secret'));
+
+ return new IntuneAdapter($instance->fresh());
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function intuneDevice(string $id, string $name): array
+ {
+ return [
+ 'id' => $id,
+ 'deviceName' => $name,
+ 'serialNumber' => 'SN-'.$id,
+ 'model' => 'Generic Model',
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Intune/IntuneSettingsPageTest.php b/tests/Feature/SyncAdapters/Intune/IntuneSettingsPageTest.php
new file mode 100644
index 0000000000..b3df0992cb
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Intune/IntuneSettingsPageTest.php
@@ -0,0 +1,87 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Intune;
+
+use App\Models\Category;
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\Models\User;
+use Illuminate\Support\Facades\Crypt;
+use Tests\TestCase;
+
+/**
+ * Smoke coverage for the Intune adapter's registration + settings-page
+ * presence. Intune's schema mixes non-secret fields (tenant_id,
+ * client_id) with a secret field (client_secret), so this test proves
+ * the ConfigurableAdapter base's per-schema encryption dispatch works:
+ * secrets get encrypted at rest, plain-text fields stay plain. pull()
+ * is still a stub so no sync coverage.
+ */
+class IntuneSettingsPageTest extends TestCase
+{
+ public function test_intune_tab_renders_on_the_adapters_page()
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index'))
+ ->assertOk()
+ ->assertSee('Microsoft Intune') // tab label
+ ->assertSee('Tenant ID') // schema-declared label
+ ->assertSee('Client ID')
+ ->assertSee('Client Secret');
+ }
+
+ public function test_saving_intune_encrypts_only_the_secret_field()
+ {
+ $intune = SyncAdapterInstance::where('slug', 'intune')->firstOrFail();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('settings.adapters.save', $intune), [
+ 'intune_url' => 'https://graph.microsoft.com',
+ 'intune_tenant_id' => 'contoso.onmicrosoft.com',
+ 'intune_client_id' => '00000000-0000-0000-0000-000000000001',
+ 'intune_client_secret' => 'plaintext-client-secret',
+ 'intune_default_category_id' => Category::factory()->create()->id,
+ 'intune_default_status_id' => Statuslabel::factory()->rtd()->create()->id,
+ ])
+ ->assertRedirect(route('settings.adapters.index', ['adapter' => $intune->slug]));
+
+ // Non-secret fields land as plain text so admins can audit
+ // which Azure app registration is in use without a decrypt
+ // step.
+ $this->assertSame('contoso.onmicrosoft.com', SyncAdapterConfig::get($intune->id, 'tenant_id'));
+ $this->assertSame('00000000-0000-0000-0000-000000000001', SyncAdapterConfig::get($intune->id, 'client_id'));
+
+ // Secret field is Crypt-encrypted at rest. Regression that
+ // stored it plain would surface here.
+ $storedSecret = SyncAdapterConfig::get($intune->id, 'client_secret');
+ $this->assertNotSame('plaintext-client-secret', $storedSecret);
+ $this->assertSame('plaintext-client-secret', Crypt::decrypt($storedSecret));
+ }
+
+ public function test_blank_credentials_on_save_fail_validation()
+ {
+ $intune = SyncAdapterInstance::where('slug', 'intune')->firstOrFail();
+ SyncAdapterConfig::put($intune->id, 'tenant_id', 'existing-tenant');
+ SyncAdapterConfig::put($intune->id, 'client_id', 'existing-client-id');
+ SyncAdapterConfig::put($intune->id, 'client_secret', Crypt::encrypt('existing-client-secret'));
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->from(route('settings.adapters.index', ['adapter' => $intune->slug]))
+ ->post(route('settings.adapters.save', $intune), [
+ 'intune_url' => 'https://graph.microsoft.us',
+ 'intune_tenant_id' => '',
+ 'intune_client_id' => '',
+ 'intune_client_secret' => '',
+ 'intune_default_category_id' => Category::factory()->create()->id,
+ 'intune_default_status_id' => Statuslabel::factory()->rtd()->create()->id,
+ ])
+ ->assertRedirect()
+ ->assertSessionHasErrors(['intune_tenant_id', 'intune_client_id', 'intune_client_secret']);
+
+ // Nothing persisted since the whole save was rejected.
+ $this->assertSame('existing-tenant', SyncAdapterConfig::get($intune->id, 'tenant_id'));
+ $this->assertSame('existing-client-id', SyncAdapterConfig::get($intune->id, 'client_id'));
+ $this->assertSame('existing-client-secret', Crypt::decrypt(SyncAdapterConfig::get($intune->id, 'client_secret')));
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Jamf/JamfAdapterTest.php b/tests/Feature/SyncAdapters/Jamf/JamfAdapterTest.php
new file mode 100644
index 0000000000..7fefd2b126
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Jamf/JamfAdapterTest.php
@@ -0,0 +1,134 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Jamf;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\Jamf\JamfAdapter;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the Jamf Pro adapter through
+ * SyncHostFromAdapter. Mocks the Jamf Pro API, asserts assets +
+ * asset_external_sources land correctly, and that the totalCount /
+ * pagination termination works.
+ */
+class JamfAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_computers_from_jamf_and_creates_assets()
+ {
+ $adapter = $this->configuredJamfAdapter();
+
+ Http::fake([
+ '*/api/v1/computers-inventory*' => Http::response([
+ 'totalCount' => 2,
+ 'results' => [
+ $this->jamfComputer(id: 42, name: 'lab-mac-01', model: 'iMac'),
+ $this->jamfComputer(id: 99, name: 'lab-mac-02', model: 'Mac mini'),
+ ],
+ ]),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'jamf', 'external_id' => '42']);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'jamf', 'external_id' => '99']);
+ $this->assertDatabaseHas('assets', ['name' => 'lab-mac-01']);
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredJamfAdapter();
+
+ Http::fake([
+ '*/api/v1/computers-inventory*' => Http::response([
+ 'totalCount' => 1,
+ 'results' => [
+ $this->jamfComputer(
+ id: 7,
+ name: 'design-mbp',
+ model: 'MacBook Pro (16-inch, M3)',
+ serialNumber: 'C02XYZ',
+ macAddress: 'aa:bb:cc:11:22:33',
+ osName: 'macOS',
+ osVersion: '14.2.1',
+ lastContactTime: '2026-01-15T10:00:00.000Z',
+ ),
+ ],
+ ]),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('jamf', $record->sourceKey);
+ $this->assertSame('7', $record->sourceId);
+ $this->assertSame('design-mbp', $record->hostname);
+ $this->assertSame('MacBook Pro (16-inch, M3)', $record->hardwareModel);
+ $this->assertSame('C02XYZ', $record->hardwareSerial);
+ $this->assertSame('Apple', $record->manufacturer);
+ $this->assertSame('aa:bb:cc:11:22:33', $record->primaryMac);
+ $this->assertSame('macOS', $record->os);
+ $this->assertSame('14.2.1', $record->osVersion);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ private function configuredJamfAdapter(): JamfAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'jamf')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://example.jamfcloud.com');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-jamf-token'));
+
+ return new JamfAdapter($instance->fresh());
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function jamfComputer(
+ int $id,
+ string $name = 'computer',
+ string $model = 'MacBook',
+ ?string $serialNumber = null,
+ ?string $macAddress = null,
+ ?string $osName = 'macOS',
+ ?string $osVersion = null,
+ ?string $lastContactTime = null,
+ ): array {
+ return [
+ 'id' => (string) $id,
+ 'udid' => 'jamf-udid-'.$id,
+ 'general' => [
+ 'name' => $name,
+ 'lastContactTime' => $lastContactTime,
+ 'lastEnrolledDate' => null,
+ ],
+ 'hardware' => [
+ 'make' => 'Apple',
+ 'model' => $model,
+ 'modelIdentifier' => 'Mac'.$id,
+ 'serialNumber' => $serialNumber,
+ 'macAddress' => $macAddress,
+ ],
+ 'operatingSystem' => [
+ 'name' => $osName,
+ 'version' => $osVersion,
+ ],
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/JamfSchool/JamfSchoolAdapterTest.php b/tests/Feature/SyncAdapters/JamfSchool/JamfSchoolAdapterTest.php
new file mode 100644
index 0000000000..a627292b94
--- /dev/null
+++ b/tests/Feature/SyncAdapters/JamfSchool/JamfSchoolAdapterTest.php
@@ -0,0 +1,153 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\JamfSchool;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\JamfSchool\JamfSchoolAdapter;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the Jamf School adapter. Mocks the Jamf
+ * School (ex-ZuluDesk) API, asserts assets + external ids land
+ * correctly, and that HTTP Basic auth (network_id + api_key) goes out
+ * on the request instead of the bearer-token pattern Jamf Pro uses.
+ */
+class JamfSchoolAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_devices_from_jamf_school_and_creates_assets()
+ {
+ $adapter = $this->configuredJamfSchoolAdapter();
+
+ Http::fake([
+ '*/devices*' => Http::response([
+ 'count' => 2,
+ 'devices' => [
+ $this->jamfSchoolDevice(udid: 'edu-udid-1', name: 'classroom-ipad-01', modelName: 'iPad (10th generation)'),
+ $this->jamfSchoolDevice(udid: 'edu-udid-2', name: 'classroom-ipad-02', modelName: 'iPad Air'),
+ ],
+ ]),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'jamf_school', 'external_id' => 'edu-udid-1']);
+ $this->assertDatabaseHas('assets', ['name' => 'classroom-ipad-01']);
+ }
+
+ public function test_basic_auth_is_sent_not_bearer()
+ {
+ $adapter = $this->configuredJamfSchoolAdapter();
+
+ Http::fake([
+ '*/devices*' => Http::response(['count' => 0, 'devices' => []]),
+ ]);
+
+ iterator_to_array($adapter->pull());
+
+ // Jamf School's API uses HTTP Basic auth with network_id as
+ // username and api_key as password. Distinct from Jamf Pro's
+ // bearer-token model. a copy-paste regression to withToken()
+ // would 401 against a real tenant.
+ Http::assertSent(function ($request) {
+ $expected = 'Basic '.base64_encode('school-network-id:fake-jamf-school-key');
+
+ return $request->hasHeader('Authorization', $expected);
+ });
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredJamfSchoolAdapter();
+
+ Http::fake([
+ '*/devices*' => Http::response([
+ 'count' => 1,
+ 'devices' => [
+ $this->jamfSchoolDevice(
+ udid: 'edu-udid-7',
+ name: 'chem-lab-ipad',
+ modelName: 'iPad Pro 12.9',
+ serial: 'JSCHOOL-XYZ',
+ wifiMac: 'aa:bb:cc:11:22:33',
+ os: 'iPadOS',
+ osVersion: '17.2',
+ lastCheckin: '2026-01-15 10:00:00',
+ ),
+ ],
+ ]),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('jamf_school', $record->sourceKey);
+ $this->assertSame('edu-udid-7', $record->sourceId);
+ $this->assertSame('chem-lab-ipad', $record->hostname);
+ $this->assertSame('iPad Pro 12.9', $record->hardwareModel);
+ $this->assertSame('JSCHOOL-XYZ', $record->hardwareSerial);
+ $this->assertSame('Apple', $record->manufacturer);
+ $this->assertSame('aa:bb:cc:11:22:33', $record->primaryMac);
+ $this->assertSame('iPadOS', $record->os);
+ $this->assertSame('17.2', $record->osVersion);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ private function configuredJamfSchoolAdapter(): JamfSchoolAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'jamf_school')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://example.jamfcloud.com/api');
+ // Non-secret field stored as plain text (matches Intune's
+ // tenant_id / client_id shape).
+ SyncAdapterConfig::put($instance->id, 'network_id', 'school-network-id');
+ SyncAdapterConfig::put($instance->id, 'api_key', Crypt::encrypt('fake-jamf-school-key'));
+
+ return new JamfSchoolAdapter($instance->fresh());
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function jamfSchoolDevice(
+ string $udid,
+ string $name = 'device',
+ string $modelName = 'iPad',
+ ?string $serial = null,
+ ?string $wifiMac = null,
+ ?string $os = 'iPadOS',
+ ?string $osVersion = null,
+ ?string $lastCheckin = null,
+ ): array {
+ return [
+ 'UDID' => $udid,
+ 'name' => $name,
+ 'serialNumber' => $serial,
+ 'model' => [
+ 'name' => $modelName,
+ 'identifier' => 'iPad12,1',
+ ],
+ 'os' => $os,
+ 'osVersion' => $osVersion,
+ 'wifiMacAddress' => $wifiMac,
+ 'lastCheckin' => $lastCheckin,
+ 'isSupervised' => true,
+ 'assetTag' => null,
+ 'locationId' => 1,
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/JumpCloud/JumpCloudAdapterTest.php b/tests/Feature/SyncAdapters/JumpCloud/JumpCloudAdapterTest.php
new file mode 100644
index 0000000000..2cad8c46e7
--- /dev/null
+++ b/tests/Feature/SyncAdapters/JumpCloud/JumpCloudAdapterTest.php
@@ -0,0 +1,146 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\JumpCloud;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\JumpCloud\JumpCloudAdapter;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the JumpCloud adapter through
+ * SyncHostFromAdapter. Mocks the JumpCloud Systems API, asserts assets
+ * + asset_external_sources land correctly, and that the x-api-key header
+ * shape (not bearer) actually goes out on the request.
+ */
+class JumpCloudAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_systems_from_jumpcloud_and_creates_assets()
+ {
+ $adapter = $this->configuredJumpCloudAdapter();
+
+ Http::fake([
+ '*/systems*' => Http::response([
+ 'totalCount' => 2,
+ 'results' => [
+ $this->jumpcloudSystem(id: 'sys-1', hostname: 'workstation-01', systemType: 'Mac'),
+ $this->jumpcloudSystem(id: 'sys-2', hostname: 'workstation-02', systemType: 'Windows'),
+ ],
+ ]),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'jumpcloud', 'external_id' => 'sys-1']);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'jumpcloud', 'external_id' => 'sys-2']);
+ $this->assertDatabaseHas('assets', ['name' => 'workstation-01']);
+ }
+
+ public function test_x_api_key_header_is_sent_not_bearer()
+ {
+ $adapter = $this->configuredJumpCloudAdapter();
+
+ Http::fake([
+ '*/systems*' => Http::response(['totalCount' => 0, 'results' => []]),
+ ]);
+
+ iterator_to_array($adapter->pull());
+
+ // JumpCloud uses a non-standard x-api-key header instead of the
+ // Authorization: Bearer convention the other adapters follow.
+ // Regressing to withToken() would silently 401 against a real
+ // tenant. this catches it before it ships.
+ Http::assertSent(function ($request) {
+ return $request->hasHeader('x-api-key', 'fake-jumpcloud-key')
+ && ! $request->hasHeader('Authorization');
+ });
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredJumpCloudAdapter();
+
+ Http::fake([
+ '*/systems*' => Http::response([
+ 'totalCount' => 1,
+ 'results' => [
+ $this->jumpcloudSystem(
+ id: 'sys-7',
+ hostname: 'dev-linux',
+ systemType: 'Linux',
+ osFamily: 'linux',
+ os: 'Ubuntu',
+ version: '24.04',
+ serialNumber: 'LNX-999',
+ lastContact: '2026-01-15T10:00:00.000Z',
+ ),
+ ],
+ ]),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('jumpcloud', $record->sourceKey);
+ $this->assertSame('sys-7', $record->sourceId);
+ $this->assertSame('dev-linux', $record->hostname);
+ $this->assertSame('Linux', $record->hardwareModel);
+ $this->assertSame('LNX-999', $record->hardwareSerial);
+ $this->assertNull($record->manufacturer); // only darwin infers to Apple
+ $this->assertSame('Ubuntu', $record->os);
+ $this->assertSame('24.04', $record->osVersion);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ private function configuredJumpCloudAdapter(): JumpCloudAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'jumpcloud')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://console.jumpcloud.com/api');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-jumpcloud-key'));
+
+ return new JumpCloudAdapter($instance->fresh());
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function jumpcloudSystem(
+ string $id,
+ string $hostname = 'system',
+ string $systemType = 'Mac',
+ ?string $osFamily = 'darwin',
+ ?string $os = 'Mac OS X',
+ ?string $version = null,
+ ?string $serialNumber = null,
+ ?string $lastContact = null,
+ ): array {
+ return [
+ '_id' => $id,
+ 'hostname' => $hostname,
+ 'displayName' => $hostname,
+ 'systemType' => $systemType,
+ 'osFamily' => $osFamily,
+ 'os' => $os,
+ 'version' => $version,
+ 'arch' => 'arm64',
+ 'serialNumber' => $serialNumber,
+ 'agentVersion' => '1.0.0',
+ 'lastContact' => $lastContact,
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Kandji/KandjiAdapterTest.php b/tests/Feature/SyncAdapters/Kandji/KandjiAdapterTest.php
new file mode 100644
index 0000000000..91a06e1a94
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Kandji/KandjiAdapterTest.php
@@ -0,0 +1,121 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Kandji;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\Kandji\KandjiAdapter;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the Kandji adapter through SyncHostFromAdapter.
+ * Mocks Kandji's REST API, asserts assets + asset_external_sources land
+ * correctly, and that pagination termination on a short page works.
+ */
+class KandjiAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_devices_from_kandji_and_creates_assets()
+ {
+ $adapter = $this->configuredKandjiAdapter();
+
+ Http::fake([
+ '*/api/v1/devices*' => Http::sequence()
+ ->push([
+ $this->kandjiDevice(id: 'aaa-111', device_name: 'workstation-01', model: 'MacBook Pro'),
+ $this->kandjiDevice(id: 'bbb-222', device_name: 'workstation-02', model: 'MacBook Air'),
+ ])
+ // Empty second page terminates the client's pagination loop.
+ ->push([]),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'kandji', 'external_id' => 'aaa-111']);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'kandji', 'external_id' => 'bbb-222']);
+ $this->assertDatabaseHas('assets', ['name' => 'workstation-01']);
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredKandjiAdapter();
+
+ Http::fake([
+ '*/api/v1/devices*' => Http::sequence()
+ ->push([
+ $this->kandjiDevice(
+ id: 'ccc-333',
+ device_name: 'design-mbp',
+ model: 'MacBook Pro (16-inch, M3)',
+ serial_number: 'ABC123',
+ platform: 'Mac',
+ os_version: '14.2.1',
+ last_check_in: '2026-01-15T10:00:00.000Z',
+ ),
+ ])
+ ->push([]),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('kandji', $record->sourceKey);
+ $this->assertSame('ccc-333', $record->sourceId);
+ $this->assertSame('design-mbp', $record->hostname);
+ $this->assertSame('MacBook Pro (16-inch, M3)', $record->hardwareModel);
+ $this->assertSame('ABC123', $record->hardwareSerial);
+ $this->assertSame('Apple', $record->manufacturer); // inferred from platform=Mac
+ $this->assertSame('Mac', $record->os);
+ $this->assertSame('14.2.1', $record->osVersion);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ private function configuredKandjiAdapter(): KandjiAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'kandji')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://example.api.kandji.io');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-kandji-token'));
+
+ return new KandjiAdapter($instance->fresh());
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function kandjiDevice(
+ string $id,
+ string $device_name = 'device',
+ string $model = 'MacBook',
+ ?string $serial_number = null,
+ ?string $platform = 'Mac',
+ ?string $os_version = null,
+ ?string $last_check_in = null,
+ ): array {
+ return [
+ 'device_id' => $id,
+ 'device_name' => $device_name,
+ 'model' => $model,
+ 'serial_number' => $serial_number,
+ 'platform' => $platform,
+ 'os_version' => $os_version,
+ 'last_check_in' => $last_check_in,
+ 'asset_tag' => null,
+ 'assigned_blueprint_id' => null,
+ 'mdm_enabled' => true,
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Kandji/KandjiSettingsPageTest.php b/tests/Feature/SyncAdapters/Kandji/KandjiSettingsPageTest.php
new file mode 100644
index 0000000000..dffc127c8e
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Kandji/KandjiSettingsPageTest.php
@@ -0,0 +1,54 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Kandji;
+
+use App\Models\Category;
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\Models\User;
+use Illuminate\Support\Facades\Crypt;
+use Tests\TestCase;
+
+/**
+ * Smoke coverage for the Kandji adapter's registration + settings-page
+ * presence. Kandji shares the URL+token partial with Fleet, so this
+ * test proves the shared partial works for a second adapter and that
+ * the registry-driven dispatch resolves Kandji correctly. pull() is
+ * still a stub, so no sync-flow coverage here.
+ */
+class KandjiSettingsPageTest extends TestCase
+{
+ public function test_kandji_tab_renders_on_the_adapters_page()
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index'))
+ ->assertOk()
+ ->assertSee('Kandji');
+ }
+
+ public function test_saving_kandji_persists_url_and_encrypts_token()
+ {
+ $kandji = SyncAdapterInstance::where('slug', 'kandji')->firstOrFail();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('settings.adapters.save', $kandji), [
+ 'kandji_url' => 'https://example.api.kandji.io',
+ 'kandji_token' => 'plaintext-kandji-token',
+ 'kandji_default_category_id' => Category::factory()->create()->id,
+ 'kandji_default_status_id' => Statuslabel::factory()->rtd()->create()->id,
+ ])
+ ->assertRedirect(route('settings.adapters.index', ['adapter' => $kandji->slug]));
+
+ $this->assertSame('https://example.api.kandji.io', SyncAdapterConfig::get($kandji->id, 'url'));
+
+ // Independent storage from Fleet. Saving Kandji doesn't touch
+ // Fleet's rows.
+ $fleet = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+ $this->assertNull(SyncAdapterConfig::get($fleet->id, 'url'));
+
+ $storedToken = SyncAdapterConfig::get($kandji->id, 'token');
+ $this->assertNotSame('plaintext-kandji-token', $storedToken);
+ $this->assertSame('plaintext-kandji-token', Crypt::decrypt($storedToken));
+ }
+}
diff --git a/tests/Feature/SyncAdapters/MerakiSystemsManager/MerakiSystemsManagerAdapterTest.php b/tests/Feature/SyncAdapters/MerakiSystemsManager/MerakiSystemsManagerAdapterTest.php
new file mode 100644
index 0000000000..040fe4a681
--- /dev/null
+++ b/tests/Feature/SyncAdapters/MerakiSystemsManager/MerakiSystemsManagerAdapterTest.php
@@ -0,0 +1,114 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\MerakiSystemsManager;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\MerakiSystemsManager\MerakiSystemsManagerAdapter;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the Meraki Systems Manager adapter through
+ * SyncHostFromAdapter. Fakes the org->networks list and per-network
+ * SM devices call.
+ */
+class MerakiSystemsManagerAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_devices_from_meraki_and_creates_assets()
+ {
+ $adapter = $this->configuredAdapter();
+
+ Http::fake([
+ '*/organizations/*/networks*' => Http::response([
+ ['id' => 'N_100', 'productTypes' => ['systemsManager']],
+ ['id' => 'N_200', 'productTypes' => ['wireless']], // filtered out
+ ]),
+ '*/networks/N_100/sm/devices*' => Http::response([
+ $this->merakiDevice(id: 'D_42', name: 'meraki-host-1'),
+ $this->merakiDevice(id: 'D_99', name: 'meraki-host-2'),
+ ]),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'meraki_sm', 'external_id' => 'D_42']);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'meraki_sm', 'external_id' => 'D_99']);
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredAdapter();
+
+ Http::fake([
+ '*/organizations/*/networks*' => Http::response([
+ ['id' => 'N_100', 'productTypes' => ['systemsManager']],
+ ]),
+ '*/networks/N_100/sm/devices*' => Http::response([
+ [
+ 'id' => 'D_7',
+ 'name' => 'meraki-mbp',
+ 'serialNumber' => 'ABC-123',
+ 'systemModel' => 'MacBook Pro',
+ 'manufacturer' => 'Apple',
+ 'wifiMac' => 'aa:bb:cc:dd:ee:ff',
+ 'ip' => '10.0.0.7',
+ 'osName' => 'macOS',
+ 'systemType' => '14.5',
+ 'lastConnectAt' => '2026-01-15T10:00:00Z',
+ 'ownerUsername' => 'alice',
+ ],
+ ]),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('meraki_sm', $record->sourceKey);
+ $this->assertSame('D_7', $record->sourceId);
+ $this->assertSame('meraki-mbp', $record->hostname);
+ $this->assertSame('ABC-123', $record->hardwareSerial);
+ $this->assertSame('MacBook Pro', $record->hardwareModel);
+ $this->assertSame('Apple', $record->manufacturer);
+ $this->assertSame('aa:bb:cc:dd:ee:ff', $record->primaryMac);
+ $this->assertSame('10.0.0.7', $record->primaryIp);
+ $this->assertSame('macOS', $record->os);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ private function configuredAdapter(): MerakiSystemsManagerAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'meraki_sm')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://api.meraki.com/api/v1');
+ SyncAdapterConfig::put($instance->id, 'api_key', Crypt::encrypt('fake-key'));
+ SyncAdapterConfig::put($instance->id, 'organization_id', '111111');
+
+ return new MerakiSystemsManagerAdapter($instance->fresh());
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function merakiDevice(string $id, string $name): array
+ {
+ return [
+ 'id' => $id,
+ 'name' => $name,
+ 'serialNumber' => 'SN-'.$id,
+ 'systemModel' => 'Generic Model',
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Mosyle/MosyleAdapterTest.php b/tests/Feature/SyncAdapters/Mosyle/MosyleAdapterTest.php
new file mode 100644
index 0000000000..a0931c7800
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Mosyle/MosyleAdapterTest.php
@@ -0,0 +1,154 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Mosyle;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\Mosyle\MosyleAdapter;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the Mosyle adapter through
+ * SyncHostFromAdapter. Mocks the Mosyle Manager v2 response shape (the
+ * nested response[0].response.rows form), asserts assets + external
+ * ids land correctly, and that the client sends POST (not GET) since
+ * Mosyle's reads use POST bodies.
+ */
+class MosyleAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_devices_from_mosyle_and_creates_assets()
+ {
+ $adapter = $this->configuredMosyleAdapter();
+
+ Http::fake([
+ '*/devices' => Http::sequence()
+ ->push([
+ 'response' => [[
+ 'response' => [
+ 'rows' => [
+ $this->mosyleDevice(udid: 'aaa-111', name: 'edu-ipad-01', model: 'iPad (10th generation)'),
+ $this->mosyleDevice(udid: 'bbb-222', name: 'edu-ipad-02', model: 'iPad Air'),
+ ],
+ ],
+ ]],
+ ])
+ // Empty second page terminates the client's pagination loop.
+ ->push([
+ 'response' => [['response' => ['rows' => []]]],
+ ]),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'mosyle', 'external_id' => 'aaa-111']);
+ $this->assertDatabaseHas('assets', ['name' => 'edu-ipad-01']);
+ }
+
+ public function test_client_sends_post_not_get()
+ {
+ $adapter = $this->configuredMosyleAdapter();
+
+ Http::fake([
+ '*/devices' => Http::response([
+ 'response' => [['response' => ['rows' => []]]],
+ ]),
+ ]);
+
+ iterator_to_array($adapter->pull());
+
+ // Mosyle's devices endpoint takes POST with a JSON body,
+ // unlike every other adapter here. A regression to GET would
+ // 405 against a real tenant. this catches it before it ships.
+ Http::assertSent(function ($request) {
+ return $request->method() === 'POST';
+ });
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredMosyleAdapter();
+
+ Http::fake([
+ '*/devices' => Http::sequence()
+ ->push([
+ 'response' => [['response' => ['rows' => [
+ $this->mosyleDevice(
+ udid: 'ccc-333',
+ name: 'staff-mbp',
+ model: 'MacBook Pro',
+ serial: 'C02XYZ',
+ os: 'macOS',
+ osversion: '14.2.1',
+ wifi_mac: 'aa:bb:cc:11:22:33',
+ date_info: '2026-01-15T10:00:00.000Z',
+ ),
+ ]]]],
+ ])
+ ->push(['response' => [['response' => ['rows' => []]]]]),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('mosyle', $record->sourceKey);
+ $this->assertSame('ccc-333', $record->sourceId);
+ $this->assertSame('staff-mbp', $record->hostname);
+ $this->assertSame('MacBook Pro', $record->hardwareModel);
+ $this->assertSame('C02XYZ', $record->hardwareSerial);
+ $this->assertSame('Apple', $record->manufacturer);
+ $this->assertSame('aa:bb:cc:11:22:33', $record->primaryMac);
+ $this->assertSame('macOS', $record->os);
+ $this->assertSame('14.2.1', $record->osVersion);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ private function configuredMosyleAdapter(): MosyleAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'mosyle')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://managerapi.mosyle.com/v2');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-mosyle-token'));
+
+ return new MosyleAdapter($instance->fresh());
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function mosyleDevice(
+ string $udid,
+ string $name = 'device',
+ string $model = 'iPad',
+ ?string $serial = null,
+ ?string $os = 'iOS',
+ ?string $osversion = null,
+ ?string $wifi_mac = null,
+ ?string $date_info = null,
+ ): array {
+ return [
+ 'deviceudid' => $udid,
+ 'device_name' => $name,
+ 'device_model_name' => $model,
+ 'serial_number' => $serial,
+ 'os' => $os,
+ 'osversion' => $osversion,
+ 'wifi_mac_address' => $wifi_mac,
+ 'date_info' => $date_info,
+ 'is_supervised' => true,
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/NewBuiltInAdaptersRenderTest.php b/tests/Feature/SyncAdapters/NewBuiltInAdaptersRenderTest.php
new file mode 100644
index 0000000000..97466ef96d
--- /dev/null
+++ b/tests/Feature/SyncAdapters/NewBuiltInAdaptersRenderTest.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters;
+
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Smoke coverage for the three newest built-in adapters (Mosyle, Meraki
+ * Systems Manager, Omnissa Workspace ONE). Each is a schema-only
+ * declaration on top of ConfigurableAdapter. the storage / save /
+ * encryption cycle is already covered by FleetSettingsPageTest,
+ * AddigySettingsPageTest, and IntuneSettingsPageTest for the three
+ * distinct schema shapes (single-secret, key-pair, mixed). This test
+ * just verifies each new adapter is registered with the AdapterRegistry
+ * and its schema-declared labels appear on the settings page.
+ */
+class NewBuiltInAdaptersRenderTest extends TestCase
+{
+ public function test_mosyle_renders_with_its_schema_label()
+ {
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index'))
+ ->assertOk()
+ ->getContent();
+
+ $this->assertStringContainsString('Mosyle', $html);
+ // Schema field label from MosyleAdapter::credentialSchema()
+ $this->assertMatchesRegularExpression('/name="mosyle_token"/', $html);
+ }
+
+ public function test_meraki_systems_manager_renders_with_both_schema_fields()
+ {
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index'))
+ ->assertOk()
+ ->getContent();
+
+ $this->assertStringContainsString('Meraki Systems Manager', $html);
+ // Secret schema field
+ $this->assertMatchesRegularExpression('/name="meraki_sm_api_key"/', $html);
+ // Non-secret schema field
+ $this->assertMatchesRegularExpression('/name="meraki_sm_organization_id"/', $html);
+ }
+
+ public function test_workspace_one_renders_with_all_three_schema_fields()
+ {
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('settings.adapters.index'))
+ ->assertOk()
+ ->getContent();
+
+ $this->assertStringContainsString('Omnissa Workspace ONE', $html);
+ $this->assertMatchesRegularExpression('/name="workspace_one_tenant_code"/', $html);
+ $this->assertMatchesRegularExpression('/name="workspace_one_client_id"/', $html);
+ $this->assertMatchesRegularExpression('/name="workspace_one_client_secret"/', $html);
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Osctrl/OsctrlAdapterTest.php b/tests/Feature/SyncAdapters/Osctrl/OsctrlAdapterTest.php
new file mode 100644
index 0000000000..225a5d4b11
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Osctrl/OsctrlAdapterTest.php
@@ -0,0 +1,163 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Osctrl;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\Osctrl\OsctrlAdapter;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the osctrl adapter through
+ * SyncHostFromAdapter. Mocks the osctrl nodes response, asserts assets
+ * + external ids land correctly, and that the environment schema
+ * field flows into the request URL path (osctrl's API is environment-
+ * scoped).
+ */
+class OsctrlAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_nodes_from_osctrl_and_creates_assets()
+ {
+ $adapter = $this->configuredOsctrlAdapter(environment: 'prod');
+
+ Http::fake([
+ '*/api/v1/nodes/prod/all' => Http::response([
+ $this->osctrlNode(uuid: 'node-1', hostname: 'edge-01', hardware_model: 'Dell OptiPlex'),
+ $this->osctrlNode(uuid: 'node-2', hostname: 'edge-02', hardware_model: 'HP EliteDesk'),
+ ]),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'osctrl', 'external_id' => 'node-1']);
+ $this->assertDatabaseHas('assets', ['name' => 'edge-01']);
+ }
+
+ public function test_environment_schema_field_flows_into_the_request_path()
+ {
+ $adapter = $this->configuredOsctrlAdapter(environment: 'staging');
+
+ Http::fake([
+ '*/api/v1/nodes/*' => Http::response([]),
+ ]);
+
+ iterator_to_array($adapter->pull());
+
+ // Regression: if the adapter forgot to pass the environment
+ // through, the request would hit the wrong path and return
+ // nothing (or 404), silently no-op'ing the sync.
+ Http::assertSent(function ($request) {
+ return str_contains($request->url(), '/api/v1/nodes/staging/all');
+ });
+ }
+
+ public function test_isenabled_returns_false_without_environment_field()
+ {
+ $instance = SyncAdapterInstance::where('slug', 'osctrl')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://osctrl.example.test');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('t'));
+ $instance->active = true;
+ $instance->save();
+
+ $adapter = new OsctrlAdapter($instance->fresh());
+
+ // Environment field is required (default in credentialSchema),
+ // so isEnabled() must refuse until it's present. Prevents the
+ // Sync Now button from becoming clickable on a half-configured
+ // adapter.
+ $this->assertFalse($adapter->isEnabled());
+
+ SyncAdapterConfig::put($instance->id, 'environment', 'prod');
+ $this->assertTrue((new OsctrlAdapter($instance->fresh()))->isEnabled());
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredOsctrlAdapter(environment: 'prod');
+
+ Http::fake([
+ '*/api/v1/nodes/prod/all' => Http::response([
+ $this->osctrlNode(
+ uuid: 'node-7',
+ hostname: 'db-primary',
+ hardware_model: 'PowerEdge R650',
+ hardware_serial: 'PE-XYZ',
+ hardware_vendor: 'Dell Inc.',
+ platform: 'ubuntu',
+ platform_version: '24.04',
+ ip_address: '10.0.0.7',
+ last_seen: '2026-01-15T10:00:00Z',
+ ),
+ ]),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('osctrl', $record->sourceKey);
+ $this->assertSame('node-7', $record->sourceId);
+ $this->assertSame('db-primary', $record->hostname);
+ $this->assertSame('PowerEdge R650', $record->hardwareModel);
+ $this->assertSame('PE-XYZ', $record->hardwareSerial);
+ $this->assertSame('Dell Inc.', $record->manufacturer);
+ $this->assertSame('10.0.0.7', $record->primaryIp);
+ $this->assertSame('ubuntu', $record->os);
+ $this->assertSame('24.04', $record->osVersion);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ private function configuredOsctrlAdapter(string $environment): OsctrlAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'osctrl')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://osctrl.example.test');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-osctrl-token'));
+ SyncAdapterConfig::put($instance->id, 'environment', $environment);
+
+ return new OsctrlAdapter($instance->fresh());
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function osctrlNode(
+ string $uuid,
+ string $hostname = 'node',
+ string $hardware_model = 'Generic Server',
+ ?string $hardware_serial = null,
+ ?string $hardware_vendor = null,
+ ?string $platform = 'ubuntu',
+ ?string $platform_version = null,
+ ?string $ip_address = null,
+ ?string $last_seen = null,
+ ): array {
+ return [
+ 'uuid' => $uuid,
+ 'hostname' => $hostname,
+ 'localname' => $hostname,
+ 'hardware_model' => $hardware_model,
+ 'hardware_serial' => $hardware_serial,
+ 'hardware_vendor' => $hardware_vendor,
+ 'platform' => $platform,
+ 'platform_version' => $platform_version,
+ 'ip_address' => $ip_address,
+ 'last_seen' => $last_seen,
+ 'environment' => 'prod',
+ 'osquery_version' => '5.11.0',
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Unifi/UnifiAdapterTest.php b/tests/Feature/SyncAdapters/Unifi/UnifiAdapterTest.php
new file mode 100644
index 0000000000..b4db94f98e
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Unifi/UnifiAdapterTest.php
@@ -0,0 +1,175 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Unifi;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\SyncHostFromAdapter;
+use App\SyncAdapters\Unifi\UnifiAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the UniFi adapter through
+ * SyncHostFromAdapter. Mocks the UniFi Network Integration API,
+ * asserts assets + asset_external_sources land correctly, and that
+ * the X-API-KEY header (not bearer, not session cookies) goes out on
+ * the request.
+ */
+class UnifiAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_devices_from_unifi_and_creates_assets()
+ {
+ $adapter = $this->configuredUnifiAdapter();
+
+ Http::fake([
+ '*/proxy/network/integration/v1/sites/*/devices*' => Http::response([
+ 'offset' => 0,
+ 'limit' => 100,
+ 'count' => 2,
+ 'totalCount' => 2,
+ 'data' => [
+ $this->unifiDevice(id: 'dev-1', name: 'core-switch-01', model: 'USW-Pro-24-PoE'),
+ $this->unifiDevice(id: 'dev-2', name: 'ap-01', model: 'U6-Pro'),
+ ],
+ ]),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'unifi', 'external_id' => 'dev-1']);
+ $this->assertDatabaseHas('assets', ['name' => 'core-switch-01']);
+ }
+
+ public function test_x_api_key_header_is_sent_not_bearer_or_session()
+ {
+ $adapter = $this->configuredUnifiAdapter();
+
+ Http::fake([
+ '*/proxy/network/integration/v1/sites/*/devices*' => Http::response([
+ 'offset' => 0, 'limit' => 100, 'count' => 0, 'totalCount' => 0, 'data' => [],
+ ]),
+ ]);
+
+ iterator_to_array($adapter->pull());
+
+ // UniFi's modern API uses X-API-KEY. A copy-paste regression
+ // to withToken() (bearer) would fail auth against a real
+ // controller. Also verifies no session-cookie auth leaks in.
+ Http::assertSent(function ($request) {
+ return $request->hasHeader('X-API-KEY', 'fake-unifi-api-key')
+ && ! $request->hasHeader('Authorization')
+ && ! $request->hasHeader('Cookie');
+ });
+ }
+
+ public function test_site_id_flows_into_the_request_path()
+ {
+ $adapter = $this->configuredUnifiAdapter(siteId: 'campus-b');
+
+ Http::fake([
+ '*' => Http::response([
+ 'offset' => 0, 'limit' => 100, 'count' => 0, 'totalCount' => 0, 'data' => [],
+ ]),
+ ]);
+
+ iterator_to_array($adapter->pull());
+
+ Http::assertSent(function ($request) {
+ return str_contains($request->url(), '/proxy/network/integration/v1/sites/campus-b/devices');
+ });
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredUnifiAdapter();
+
+ Http::fake([
+ '*/proxy/network/integration/v1/sites/*/devices*' => Http::response([
+ 'offset' => 0,
+ 'limit' => 100,
+ 'count' => 1,
+ 'totalCount' => 1,
+ 'data' => [
+ $this->unifiDevice(
+ id: 'dev-7',
+ name: 'building-a-gateway',
+ model: 'UDMPRO',
+ serialNumber: 'UBNT-XYZ',
+ macAddress: 'aa:bb:cc:11:22:33',
+ ipAddress: '10.0.0.1',
+ firmwareVersion: '3.2.7',
+ lastSeen: '2026-01-15T10:00:00.000Z',
+ ),
+ ],
+ ]),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('unifi', $record->sourceKey);
+ $this->assertSame('dev-7', $record->sourceId);
+ $this->assertSame('building-a-gateway', $record->hostname);
+ $this->assertSame('UDMPRO', $record->hardwareModel);
+ $this->assertSame('UBNT-XYZ', $record->hardwareSerial);
+ $this->assertSame('Ubiquiti', $record->manufacturer);
+ $this->assertSame('aa:bb:cc:11:22:33', $record->primaryMac);
+ $this->assertSame('10.0.0.1', $record->primaryIp);
+ $this->assertSame('UniFi', $record->os);
+ $this->assertSame('3.2.7', $record->osVersion);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ private function configuredUnifiAdapter(string $siteId = 'default'): UnifiAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'unifi')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://unifi.example.test:8443');
+ SyncAdapterConfig::put($instance->id, 'api_key', Crypt::encrypt('fake-unifi-api-key'));
+ SyncAdapterConfig::put($instance->id, 'site_id', $siteId);
+
+ return new UnifiAdapter($instance->fresh());
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function unifiDevice(
+ string $id,
+ string $name = 'device',
+ string $model = 'U6-Pro',
+ ?string $serialNumber = null,
+ ?string $macAddress = null,
+ ?string $ipAddress = null,
+ ?string $firmwareVersion = null,
+ ?string $lastSeen = null,
+ ): array {
+ return [
+ 'id' => $id,
+ 'name' => $name,
+ 'model' => $model,
+ 'modelKey' => strtolower($model),
+ 'serialNumber' => $serialNumber,
+ 'macAddress' => $macAddress,
+ 'ipAddress' => $ipAddress,
+ 'firmwareVersion' => $firmwareVersion,
+ 'lastSeen' => $lastSeen,
+ 'state' => 'ONLINE',
+ 'adopted' => true,
+ 'uplink' => ['mac' => null],
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/UserAssignmentTest.php b/tests/Feature/SyncAdapters/UserAssignmentTest.php
new file mode 100644
index 0000000000..f7b0f48a92
--- /dev/null
+++ b/tests/Feature/SyncAdapters/UserAssignmentTest.php
@@ -0,0 +1,216 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters;
+
+use App\Events\CheckoutableCheckedOut;
+use App\Models\Actionlog;
+use App\Models\Asset;
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\Models\User;
+use App\SyncAdapters\HostInventoryRecord;
+use App\SyncAdapters\SyncHostFromAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Event;
+use Tests\TestCase;
+
+/**
+ * Coverage for the sync-driven user assignment feature. Adapters
+ * with a user_match_strategy configured look up Snipe-IT users from
+ * the vendor's assigned-user field and check the asset out to the
+ * match. Missing or unresolved users get skipped and logged.
+ * Existing assignments are never cleared by a payload that omits the
+ * user field.
+ */
+class UserAssignmentTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_email_strategy_assigns_asset_to_matched_user()
+ {
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'user_match_strategy', 'email');
+
+ $user = User::factory()->create(['email' => '[email protected]']);
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'ua-1',
+ hostname: 'alices-laptop',
+ hardwareSerial: 'SN-A',
+ hardwareModel: 'MacBook Pro',
+ assignedUserEmail: '[email protected]',
+ ));
+
+ $asset = Asset::where('name', 'alices-laptop')->firstOrFail();
+ $this->assertSame($user->id, (int) $asset->assigned_to);
+ $this->assertSame(User::class, $asset->assigned_type);
+ }
+
+ public function test_username_strategy_assigns_asset_to_matched_user()
+ {
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'user_match_strategy', 'username');
+
+ $user = User::factory()->create(['username' => 'alice.smith']);
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'ua-2',
+ hostname: 'alices-laptop',
+ hardwareSerial: 'SN-A',
+ hardwareModel: 'MacBook Pro',
+ assignedUserName: 'alice.smith',
+ ));
+
+ $asset = Asset::where('name', 'alices-laptop')->firstOrFail();
+ $this->assertSame($user->id, (int) $asset->assigned_to);
+ }
+
+ public function test_none_strategy_leaves_asset_unassigned_even_when_user_field_populated()
+ {
+ $fleet = $this->configuredFleet();
+ // Default strategy is 'none' when nothing is stored.
+ User::factory()->create(['email' => '[email protected]']);
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'ua-3',
+ hostname: 'no-assignment',
+ hardwareSerial: 'SN-A',
+ hardwareModel: 'MacBook Pro',
+ assignedUserEmail: '[email protected]',
+ ));
+
+ $asset = Asset::where('name', 'no-assignment')->firstOrFail();
+ $this->assertNull($asset->assigned_to);
+ }
+
+ public function test_unresolved_email_skips_assignment_and_logs_warning()
+ {
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'user_match_strategy', 'email');
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'ua-4',
+ hostname: 'unmatched-host',
+ hardwareSerial: 'SN-A',
+ hardwareModel: 'MacBook Pro',
+ assignedUserEmail: '[email protected]',
+ ));
+
+ $asset = Asset::where('name', 'unmatched-host')->firstOrFail();
+ $this->assertNull($asset->assigned_to);
+ }
+
+ public function test_missing_user_field_does_not_clear_existing_assignment()
+ {
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'user_match_strategy', 'email');
+
+ $user = User::factory()->create(['email' => '[email protected]']);
+
+ // First sync: assigns to alice.
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'ua-5',
+ hostname: 'alices-laptop',
+ hardwareSerial: 'SN-A',
+ hardwareModel: 'MacBook Pro',
+ assignedUserEmail: '[email protected]',
+ ));
+
+ // Second sync of the same asset with no user email.
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'ua-5',
+ hostname: 'alices-laptop',
+ hardwareSerial: 'SN-A',
+ hardwareModel: 'MacBook Pro',
+ assignedUserEmail: null,
+ ));
+
+ $asset = Asset::where('name', 'alices-laptop')->firstOrFail();
+ $this->assertSame($user->id, (int) $asset->assigned_to);
+ }
+
+ public function test_suppress_notifications_bypasses_event_dispatch()
+ {
+ Event::fake([CheckoutableCheckedOut::class]);
+
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'user_match_strategy', 'email');
+ // Default is suppress = true, verify explicitly.
+ SyncAdapterConfig::put($fleet->id, 'suppress_notifications', '1');
+
+ User::factory()->create(['email' => '[email protected]']);
+
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'ua-6',
+ hostname: 'silent-assign',
+ hardwareSerial: 'SN-A',
+ hardwareModel: 'MacBook Pro',
+ assignedUserEmail: '[email protected]',
+ ));
+
+ // Direct assignment path skips the CheckoutableCheckedOut
+ // event entirely, so no listener (email or otherwise) runs.
+ Event::assertNotDispatched(CheckoutableCheckedOut::class);
+
+ // But the assignment happened and a checkout actionlog was
+ // written manually so the asset history stays accurate.
+ $asset = Asset::where('name', 'silent-assign')->firstOrFail();
+ $this->assertNotNull($asset->assigned_to);
+ $this->assertDatabaseHas('action_logs', [
+ 'item_id' => $asset->id,
+ 'item_type' => Asset::class,
+ 'action_type' => 'checkout',
+ 'created_by' => null,
+ ]);
+ }
+
+ public function test_suppress_off_falls_back_to_silent_when_no_authenticated_admin()
+ {
+ Event::fake([CheckoutableCheckedOut::class]);
+
+ $fleet = $this->configuredFleet();
+ SyncAdapterConfig::put($fleet->id, 'user_match_strategy', 'email');
+ SyncAdapterConfig::put($fleet->id, 'suppress_notifications', '0');
+
+ User::factory()->create(['email' => '[email protected]']);
+
+ // No actingAs(): simulates a CLI-triggered sync where auth()
+ // has no user. Suppress-off would otherwise crash the
+ // CheckoutableCheckedOut event (requires User admin), so the
+ // sync path silently degrades to direct assignment.
+ SyncHostFromAdapter::run(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'ua-8',
+ hostname: 'cli-assign',
+ hardwareSerial: 'SN-A',
+ hardwareModel: 'MacBook Pro',
+ assignedUserEmail: '[email protected]',
+ ));
+
+ Event::assertNotDispatched(CheckoutableCheckedOut::class);
+ $asset = Asset::where('name', 'cli-assign')->firstOrFail();
+ $this->assertNotNull($asset->assigned_to);
+ }
+
+ private function configuredFleet(): SyncAdapterInstance
+ {
+ $instance = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://example.com/fleet');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-token'));
+
+ return $instance->fresh();
+ }
+}
diff --git a/tests/Feature/SyncAdapters/WorkspaceOne/WorkspaceOneAdapterTest.php b/tests/Feature/SyncAdapters/WorkspaceOne/WorkspaceOneAdapterTest.php
new file mode 100644
index 0000000000..60be4deed1
--- /dev/null
+++ b/tests/Feature/SyncAdapters/WorkspaceOne/WorkspaceOneAdapterTest.php
@@ -0,0 +1,124 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\WorkspaceOne;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\SyncHostFromAdapter;
+use App\SyncAdapters\WorkspaceOne\WorkspaceOneAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the Omnissa Workspace ONE adapter through
+ * SyncHostFromAdapter. Fakes the OAuth token exchange and the
+ * /api/mdm/devices/search endpoint.
+ */
+class WorkspaceOneAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_devices_from_workspace_one_and_creates_assets()
+ {
+ $adapter = $this->configuredAdapter();
+
+ Http::fake([
+ '*/connect/token' => Http::response(['access_token' => 'stub-bearer', 'expires_in' => 3600]),
+ '*/api/mdm/devices/search*' => Http::response([
+ 'Total' => 2,
+ 'PageSize' => 500,
+ 'Page' => 0,
+ 'Devices' => [
+ $this->ws1Device(uuid: 'ws1-42', friendlyName: 'ws1-host-1'),
+ $this->ws1Device(uuid: 'ws1-99', friendlyName: 'ws1-host-2'),
+ ],
+ ]),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'workspace_one', 'external_id' => 'ws1-42']);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'workspace_one', 'external_id' => 'ws1-99']);
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredAdapter();
+
+ Http::fake([
+ '*/connect/token' => Http::response(['access_token' => 'stub-bearer', 'expires_in' => 3600]),
+ '*/api/mdm/devices/search*' => Http::response([
+ 'Total' => 1,
+ 'PageSize' => 500,
+ 'Page' => 0,
+ 'Devices' => [
+ [
+ 'Uuid' => 'ws1-7',
+ 'DeviceFriendlyName' => 'ws1-ipad',
+ 'SerialNumber' => 'ABC-123',
+ 'Model' => 'iPad Pro',
+ 'DeviceManufacturer' => 'Apple',
+ 'MacAddress' => 'aa:bb:cc:dd:ee:ff',
+ 'IpAddress' => '10.0.0.7',
+ 'Platform' => 'AppleOsX',
+ 'OperatingSystem' => '17.4',
+ 'LastSeen' => '2026-01-15T10:00:00Z',
+ 'UserEmailAddress' => '[email protected]',
+ 'UserName' => 'alice',
+ 'ComplianceStatus' => 'Compliant',
+ ],
+ ],
+ ]),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('workspace_one', $record->sourceKey);
+ $this->assertSame('ws1-7', $record->sourceId);
+ $this->assertSame('ws1-ipad', $record->hostname);
+ $this->assertSame('ABC-123', $record->hardwareSerial);
+ $this->assertSame('iPad Pro', $record->hardwareModel);
+ $this->assertSame('Apple', $record->manufacturer);
+ $this->assertSame('aa:bb:cc:dd:ee:ff', $record->primaryMac);
+ $this->assertSame('10.0.0.7', $record->primaryIp);
+ $this->assertSame('AppleOsX', $record->os);
+ $this->assertSame('[email protected]', $record->assignedUserEmail);
+ $this->assertSame('alice', $record->assignedUserName);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ private function configuredAdapter(): WorkspaceOneAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'workspace_one')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://as123.awmdm.com');
+ SyncAdapterConfig::put($instance->id, 'tenant_code', 'stub-tenant');
+ SyncAdapterConfig::put($instance->id, 'client_id', 'stub-client');
+ SyncAdapterConfig::put($instance->id, 'client_secret', Crypt::encrypt('fake-secret'));
+
+ return new WorkspaceOneAdapter($instance->fresh());
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function ws1Device(string $uuid, string $friendlyName): array
+ {
+ return [
+ 'Uuid' => $uuid,
+ 'DeviceFriendlyName' => $friendlyName,
+ 'SerialNumber' => 'SN-'.$uuid,
+ 'Model' => 'Generic Model',
+ ];
+ }
+}
diff --git a/tests/Feature/SyncAdapters/Zentral/ZentralAdapterTest.php b/tests/Feature/SyncAdapters/Zentral/ZentralAdapterTest.php
new file mode 100644
index 0000000000..b1c84f14b8
--- /dev/null
+++ b/tests/Feature/SyncAdapters/Zentral/ZentralAdapterTest.php
@@ -0,0 +1,118 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\Zentral;
+
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\SyncHostFromAdapter;
+use App\SyncAdapters\Zentral\ZentralAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\Http;
+use Tests\TestCase;
+
+/**
+ * End-to-end coverage for the Zentral adapter through SyncHostFromAdapter.
+ * Fakes Zentral's /api/inventory/machines/ endpoint, asserts assets +
+ * asset_external_sources rows land correctly.
+ */
+class ZentralAdapterTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_pulls_machines_from_zentral_and_creates_assets()
+ {
+ $adapter = $this->configuredZentralAdapter();
+
+ Http::fake([
+ '*/api/inventory/machines/*' => Http::sequence()
+ ->push($this->zentralResponse([
+ $this->zentralMachine(serial: 'ABC123', hostname: 'zentral-host-1'),
+ $this->zentralMachine(serial: 'XYZ789', hostname: 'zentral-host-2'),
+ ])),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncHostFromAdapter::run($record);
+ }
+
+ $this->assertDatabaseCount('asset_external_sources', 2);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'zentral', 'external_id' => 'ABC123']);
+ $this->assertDatabaseHas('asset_external_sources', ['source' => 'zentral', 'external_id' => 'XYZ789']);
+ $this->assertDatabaseHas('assets', ['name' => 'zentral-host-1']);
+ $this->assertDatabaseHas('assets', ['name' => 'zentral-host-2']);
+ }
+
+ public function test_normalized_record_carries_expected_fields()
+ {
+ $adapter = $this->configuredZentralAdapter();
+
+ Http::fake([
+ '*/api/inventory/machines/*' => Http::sequence()
+ ->push($this->zentralResponse([
+ [
+ 'serial_number' => 'FW-ABC-123',
+ 'computer_name' => 'framework-13',
+ 'system_info' => [
+ 'hardware_model' => 'Framework 13',
+ 'hardware_vendor' => 'Framework',
+ ],
+ 'network_interfaces' => [
+ ['mac' => 'aa:bb:cc:dd:ee:ff', 'address' => '10.0.0.7'],
+ ],
+ 'os_version' => ['name' => 'macOS', 'version' => '14.5'],
+ 'last_seen' => '2026-01-15T10:00:00Z',
+ ],
+ ])),
+ ]);
+
+ $records = iterator_to_array($adapter->pull());
+ $this->assertCount(1, $records);
+
+ $record = $records[0];
+ $this->assertSame('zentral', $record->sourceKey);
+ $this->assertSame('FW-ABC-123', $record->sourceId);
+ $this->assertSame('framework-13', $record->hostname);
+ $this->assertSame('Framework 13', $record->hardwareModel);
+ $this->assertSame('Framework', $record->manufacturer);
+ $this->assertSame('aa:bb:cc:dd:ee:ff', $record->primaryMac);
+ $this->assertSame('10.0.0.7', $record->primaryIp);
+ $this->assertSame('macOS', $record->os);
+ $this->assertSame('14.5', $record->osVersion);
+ $this->assertNotNull($record->lastSeen);
+ }
+
+ private function configuredZentralAdapter(): ZentralAdapter
+ {
+ $instance = SyncAdapterInstance::where('slug', 'zentral')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://zentral.example.test');
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-token'));
+
+ return new ZentralAdapter($instance->fresh());
+ }
+
+ /**
+ * @param array<int, array<string, mixed>> $machines
+ * @return array<string, mixed>
+ */
+ private function zentralResponse(array $machines): array
+ {
+ return ['count' => count($machines), 'results' => $machines];
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ private function zentralMachine(string $serial, string $hostname): array
+ {
+ return [
+ 'serial_number' => $serial,
+ 'computer_name' => $hostname,
+ 'system_info' => ['hardware_model' => 'Generic Model'],
+ ];
+ }
+}