Skip to content

A free open source IT asset/license management system

7.4 KB
Raw Blame
<?php

namespace Tests\Feature\SyncAdapters\WorkspaceOne;

use App\Models\Statuslabel;
use App\Models\SyncAdapterConfig;
use App\Models\SyncAdapterInstance;
use App\SyncAdapters\SyncAdapter;
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
 * SyncAdapter. 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) {
            SyncAdapter::syncFromRecord($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);
    }

    /**
     * Regression for issue #20003. The vendor renamed from VMware to
     * Omnissa and the OAuth token hosts migrated from
     * *.vmwservices.com to *.workspaceone.com. The old hosts no longer
     * resolve, which left every pre-fix tenant failing with a cURL 28
     * DNS-timeout on first token exchange. Assert the per-region auth
     * host that goes out on the wire so a future rename or typo in the
     * derived URL gets caught at test time.
     *
     * Each case covers one branch of the match in
     * WorkspaceOneAdapter::deriveAuthBaseUrl, keyed off the first
     * three chars of the API host.
     *
     * @return array<string, array{apiHost: string, expectedAuthHost: string}>
     */
    public static function regionAuthHostCases(): array
    {
        return [
            'na default (asXXX)' => [
                'apiHost' => 'as123.awmdm.com',
                'expectedAuthHost' => 'na.uemauth.workspaceone.com',
            ],
            'na default (dsXXX)' => [
                'apiHost' => 'ds456.awmdm.com',
                'expectedAuthHost' => 'na.uemauth.workspaceone.com',
            ],
            'apac' => [
                'apiHost' => 'apac789.awmdm.com',
                'expectedAuthHost' => 'apac.uemauth.workspaceone.com',
            ],
            'emea' => [
                'apiHost' => 'emea101.awmdm.com',
                'expectedAuthHost' => 'emea.uemauth.workspaceone.com',
            ],
            'uat' => [
                'apiHost' => 'uat202.awmdm.com',
                'expectedAuthHost' => 'uat.uemauth.workspaceone.com',
            ],
        ];
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('regionAuthHostCases')]
    public function test_oauth_token_request_hits_the_rebranded_omnissa_host(string $apiHost, string $expectedAuthHost): void
    {
        $instance = SyncAdapterInstance::where('slug', 'workspace_one')->firstOrFail();
        SyncAdapterConfig::put($instance->id, 'url', 'https://'.$apiHost);
        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'));
        $adapter = new WorkspaceOneAdapter($instance->fresh());

        Http::fake([
            '*/connect/token' => Http::response(['access_token' => 'stub-bearer', 'expires_in' => 3600]),
            '*/api/mdm/devices/search*' => Http::response(['Total' => 0, 'PageSize' => 500, 'Page' => 0, 'Devices' => []]),
        ]);

        iterator_to_array($adapter->pull());

        Http::assertSent(fn ($request) => str_contains($request->url(), 'https://'.$expectedAuthHost.'/connect/token'));
        Http::assertNotSent(fn ($request) => str_contains($request->url(), 'vmwservices.com'));
    }

    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',
        ];
    }
}