Skip to content

Beautiful Open-Source Form Builder

22.8 KB
Raw Blame
<?php

use App\Enums\SettingsKey;
use App\Enterprise\Oidc\Models\IdentityConnection;
use App\Models\Setting;
use App\Models\User;
use App\Models\Workspace;
use App\Service\License\LicenseCheckResult;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;

uses()->group('oidc', 'feature');

function activateSsoLicense(): void
{
    Setting::set(SettingsKey::SELF_HOSTED_LICENSE, [
        'license_key' => Crypt::encryptString('lic_test_oidc_ctrl'),
        'status' => 'active',
        'features' => ['sso' => true],
        'last_checked_at' => now()->format('c'),
        'expires_at' => now()->addYear()->format('c'),
        'cloud_license_id' => '1',
        'activation_id' => '1',
    ]);
    Cache::put('self_hosted_license_check', new LicenseCheckResult(
        status: 'active',
        features: ['sso' => true],
        lastChecked: now(),
        expiresAt: now()->addYear(),
    ), 86400);
}

describe('OidcConnectionController - List Connections', function () {
    it('lists workspace-scoped connections for workspace admin', function () {
        $user = $this->actingAsUser();
        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $connection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
        ]);

        $response = $this->getJson("/open/workspaces/{$workspace->id}/oidc-connections");

        $response->assertSuccessful();
        $response->assertJsonCount(1);
        expect($response->json('0.id'))->toBe($connection->id);
    });

    it('prevents workspace member from listing connections', function () {
        $user = $this->actingAsUser();
        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'member']);

        IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
        ]);

        $response = $this->getJson("/open/workspaces/{$workspace->id}/oidc-connections");

        // Only workspace admins can view connections
        $response->assertForbidden();
    });

    it('includes workspace-scoped and global connections for workspace', function () {
        $user = $this->actingAsUser();
        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $workspaceConnection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
        ]);

        $globalConnection = IdentityConnection::factory()->create([
            'workspace_id' => null,
        ]);

        $otherWorkspace = Workspace::factory()->create();
        IdentityConnection::factory()->create([
            'workspace_id' => $otherWorkspace->id,
        ]);

        $response = $this->getJson("/open/workspaces/{$workspace->id}/oidc-connections");

        $response->assertSuccessful();
        $response->assertJsonCount(2);
        $ids = collect($response->json())->pluck('id')->toArray();
        expect($ids)->toContain($workspaceConnection->id);
        expect($ids)->toContain($globalConnection->id);
    });
});

describe('OidcConnectionController - Create Connection', function () {
    beforeEach(function () {
        config(['app.self_hosted' => true]);
        config(['cashier.key' => null]);
        activateSsoLicense();
    });

    it('creates workspace-scoped connection as enterprise user', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $data = [
            'name' => 'Company SSO',
            'slug' => 'company-sso',
            'domain' => 'company.com',
            'issuer' => 'https://idp.company.com',
            'client_id' => 'client-123',
            'client_secret' => 'secret-456',
            'enabled' => true,
        ];

        $response = $this->postJson("/open/workspaces/{$workspace->id}/oidc-connections", $data);

        $response->assertStatus(201);
        $response->assertJsonStructure([
            'id',
            'name',
            'slug',
            'domain',
            'issuer',
            'client_id',
            'redirect_url',
            'enabled',
        ]);

        expect($response->json('name'))->toBe('Company SSO');
        expect($response->json('workspace_id'))->toBe($workspace->id);
        expect($response->json('client_secret'))->toBeNull(); // Should not expose secret
    });

    it('creates workspace-scoped connection on self-hosted without license', function () {
        Setting::forget(SettingsKey::SELF_HOSTED_LICENSE);
        Cache::forget('self_hosted_license_check');

        $user = User::factory()->create(['email' => '[email protected]']);
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $data = [
            'name' => 'Company SSO',
            'slug' => 'company-sso',
            'domain' => 'company.com',
            'issuer' => 'https://idp.company.com',
            'client_id' => 'client-123',
            'client_secret' => 'secret-456',
            'enabled' => true,
        ];

        $response = $this->postJson("/open/workspaces/{$workspace->id}/oidc-connections", $data);

        $response->assertStatus(201);
        expect($response->json('name'))->toBe('Company SSO');
    });

    it('validates required fields', function () {
        $user = $this->actingAsEnterpriseUser();
        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $response = $this->postJson("/open/workspaces/{$workspace->id}/oidc-connections", []);

        $response->assertStatus(422);
        $response->assertJsonValidationErrors(['name', 'slug', 'domain', 'issuer', 'client_id', 'client_secret']);
    });

    it('validates unique slug', function () {
        $user = $this->actingAsEnterpriseUser();
        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        IdentityConnection::factory()->create(['slug' => 'existing-slug']);

        $data = [
            'name' => 'Test',
            'slug' => 'existing-slug',
            'domain' => 'test.com',
            'issuer' => 'https://idp.test.com',
            'client_id' => 'client-123',
            'client_secret' => 'secret-456',
        ];

        $response = $this->postJson("/open/workspaces/{$workspace->id}/oidc-connections", $data);

        $response->assertStatus(422);
        $response->assertJsonValidationErrors(['slug']);
    });

    it('validates unique domain per workspace', function () {
        $user = $this->actingAsEnterpriseUser();
        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
            'domain' => 'company.com',
        ]);

        $data = [
            'name' => 'Test',
            'slug' => 'test-slug',
            'domain' => 'company.com',
            'issuer' => 'https://idp.test.com',
            'client_id' => 'client-123',
            'client_secret' => 'secret-456',
        ];

        $response = $this->postJson("/open/workspaces/{$workspace->id}/oidc-connections", $data);

        $response->assertStatus(422);
        $response->assertJsonValidationErrors(['domain']);
    });

    it('enforces single connection per workspace', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
            'type' => IdentityConnection::TYPE_OIDC,
            'domain' => 'company.com',
        ]);

        // Try to create second connection with different domain (should still fail)
        $data = [
            'name' => 'Second Connection',
            'slug' => 'second-connection',
            'domain' => 'mail.company.com', // Different domain, but same workspace
            'issuer' => 'https://idp.company.com',
            'client_id' => 'client-123',
            'client_secret' => 'secret-456',
        ];

        $response = $this->postJson("/open/workspaces/{$workspace->id}/oidc-connections", $data);

        $response->assertStatus(422);
        expect($response->json('message'))->toContain('already has an OIDC connection');
    });

    it('validates domain matches user email domain', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $data = [
            'name' => 'Company SSO',
            'slug' => 'company-sso',
            'domain' => 'company.com',
            'issuer' => 'https://idp.company.com',
            'client_id' => 'client-123',
            'client_secret' => 'secret-456',
        ];

        $response = $this->postJson("/open/workspaces/{$workspace->id}/oidc-connections", $data);

        $response->assertStatus(201);
    });

    it('rejects domain that does not match user email domain', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $data = [
            'name' => 'Test SSO',
            'slug' => 'test-sso',
            'domain' => 'other.com',
            'issuer' => 'https://idp.other.com',
            'client_id' => 'client-123',
            'client_secret' => 'secret-456',
        ];

        $response = $this->postJson("/open/workspaces/{$workspace->id}/oidc-connections", $data);

        $response->assertStatus(422);
        $response->assertJsonValidationErrors(['domain']);
        expect($response->json('errors.domain.0'))->toContain('must match your email domain');
    });

    it('rejects blocked email providers', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $data = [
            'name' => 'Test SSO',
            'slug' => 'test-sso',
            'domain' => 'gmail.com',
            'issuer' => 'https://idp.gmail.com',
            'client_id' => 'client-123',
            'client_secret' => 'secret-456',
        ];

        $response = $this->postJson("/open/workspaces/{$workspace->id}/oidc-connections", $data);

        $response->assertStatus(422);
        $response->assertJsonValidationErrors(['domain']);
        expect($response->json('errors.domain.0'))->toContain('Common email providers');
    });

    it('allows root domain match when user has subdomain email', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $data = [
            'name' => 'Company SSO',
            'slug' => 'company-sso',
            'domain' => 'company.com',
            'issuer' => 'https://idp.company.com',
            'client_id' => 'client-123',
            'client_secret' => 'secret-456',
        ];

        $response = $this->postJson("/open/workspaces/{$workspace->id}/oidc-connections", $data);

        $response->assertStatus(201);
    });

    it('allows subdomain match when user has root domain email', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $data = [
            'name' => 'Company SSO',
            'slug' => 'company-sso',
            'domain' => 'mail.company.com',
            'issuer' => 'https://idp.company.com',
            'client_id' => 'client-123',
            'client_secret' => 'secret-456',
        ];

        $response = $this->postJson("/open/workspaces/{$workspace->id}/oidc-connections", $data);

        $response->assertStatus(201);
    });

    it('validates domain on update', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $connection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
            'domain' => 'company.com',
        ]);

        $response = $this->patchJson(
            "/open/workspaces/{$workspace->id}/oidc-connections/{$connection->id}",
            ['domain' => 'other.com']
        );

        $response->assertStatus(422);
        $response->assertJsonValidationErrors(['domain']);
    });

    it('allows updating to matching domain', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $connection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
            'domain' => 'company.com',
        ]);

        $response = $this->patchJson(
            "/open/workspaces/{$workspace->id}/oidc-connections/{$connection->id}",
            ['domain' => 'mail.company.com']
        );

        $response->assertSuccessful();
        expect($connection->fresh()->domain)->toBe('mail.company.com');
    });
});

describe('OidcConnectionController - Update Connection', function () {
    it('updates workspace-scoped connection as enterprise user', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $connection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
            'name' => 'Old Name',
            'domain' => 'company.com',
        ]);

        $response = $this->patchJson(
            "/open/workspaces/{$workspace->id}/oidc-connections/{$connection->id}",
            ['name' => 'New Name']
        );

        $response->assertSuccessful();
        expect($response->json('name'))->toBe('New Name');
        expect($connection->fresh()->name)->toBe('New Name');
    });

    it('updates workspace-scoped connection in self-hosted mode', function () {
        config(['app.self_hosted' => true]);
        config(['cashier.key' => null]);
        activateSsoLicense();

        $user = User::factory()->create(['email' => '[email protected]']);
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $connection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
            'name' => 'Old Name',
            'domain' => 'company.com',
        ]);

        $response = $this->patchJson(
            "/open/workspaces/{$workspace->id}/oidc-connections/{$connection->id}",
            ['name' => 'New Name']
        );

        $response->assertSuccessful();
        expect($connection->fresh()->name)->toBe('New Name');
    });

    it('denies update for pro user - requires enterprise', function () {
        $user = $this->createProUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $connection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
            'name' => 'Old Name',
            'domain' => 'company.com',
        ]);

        $response = $this->patchJson(
            "/open/workspaces/{$workspace->id}/oidc-connections/{$connection->id}",
            ['name' => 'New Name']
        );

        $response->assertForbidden();
    });

    it('does not update client_secret if not provided', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $connection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
            'client_secret' => 'old-secret',
            'domain' => 'company.com',
        ]);

        $response = $this->patchJson(
            "/open/workspaces/{$workspace->id}/oidc-connections/{$connection->id}",
            [
                'name' => 'Updated Name',
                'redirect_path' => 'https://forms.example.com/auth/company-sso/callback',
            ]
        );

        $response->assertSuccessful();
        expect($connection->fresh()->client_secret)->toBe('old-secret');
        expect($connection->fresh()->redirect_path)->toBe('https://forms.example.com/auth/company-sso/callback');
    });

    it('merges options instead of replacing', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $connection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
            'domain' => 'company.com',
            'options' => [
                'field_mappings' => ['email' => 'mail'],
                'custom_field' => 'custom_value',
            ],
        ]);

        $response = $this->patchJson(
            "/open/workspaces/{$workspace->id}/oidc-connections/{$connection->id}",
            [
                'options' => [
                    'field_mappings' => ['email' => 'email_address'],
                ],
            ]
        );

        $response->assertSuccessful();
        $options = $connection->fresh()->options;
        expect($options['field_mappings']['email'])->toBe('email_address');
        expect($options['custom_field'])->toBe('custom_value');
    });
});

describe('OidcConnectionController - Delete Connection', function () {
    it('deletes workspace-scoped connection as enterprise user', function () {
        $user = $this->createEnterpriseUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $connection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
            'domain' => 'company.com',
        ]);

        $response = $this->deleteJson("/open/workspaces/{$workspace->id}/oidc-connections/{$connection->id}");

        $response->assertStatus(204);
        expect(IdentityConnection::find($connection->id))->toBeNull();
    });

    it('deletes workspace-scoped connection in self-hosted mode', function () {
        config(['app.self_hosted' => true]);
        config(['cashier.key' => null]);
        activateSsoLicense();

        $user = User::factory()->create(['email' => '[email protected]']);
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $connection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
            'domain' => 'company.com',
        ]);

        $response = $this->deleteJson("/open/workspaces/{$workspace->id}/oidc-connections/{$connection->id}");

        $response->assertStatus(204);
        expect(IdentityConnection::find($connection->id))->toBeNull();
    });

    it('denies deletion for pro user - requires enterprise', function () {
        $user = $this->createProUser();
        $user->email = '[email protected]';
        $user->save();
        $this->actingAs($user);

        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);

        $connection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
        ]);

        $response = $this->deleteJson("/open/workspaces/{$workspace->id}/oidc-connections/{$connection->id}");

        $response->assertForbidden();
        expect(IdentityConnection::find($connection->id))->not->toBeNull();
    });

    it('prevents deletion by non-admin', function () {
        $user = $this->actingAsEnterpriseUser();
        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'member']);

        $connection = IdentityConnection::factory()->create([
            'workspace_id' => $workspace->id,
        ]);

        $response = $this->deleteJson("/open/workspaces/{$workspace->id}/oidc-connections/{$connection->id}");

        $response->assertStatus(403);
        expect(IdentityConnection::find($connection->id))->not->toBeNull();
    });
});

describe('Global OIDC connection isolation through workspace routes', function () {
    it('refuses workspace administrators updating or deleting a global connection', function () {
        config(['app.self_hosted' => true, 'cashier.key' => null, 'opnform.admin_emails' => []]);
        $user = $this->actingAsUser();
        $workspace = Workspace::factory()->create();
        $user->workspaces()->attach($workspace->id, ['role' => 'admin']);
        $connection = IdentityConnection::factory()->create([
            'workspace_id' => null,
            'name' => 'Global identity provider',
        ]);
        $url = "/open/workspaces/{$workspace->id}/oidc-connections/{$connection->id}";

        $this->patchJson($url, ['name' => 'Hijacked provider'])->assertForbidden();
        $this->deleteJson($url)->assertForbidden();

        expect($connection->fresh()->name)->toBe('Global identity provider');
    });
});