Skip to content

Beautiful Open-Source Form Builder

7.0 KB
Raw Blame
<?php

use App\Enums\SettingsKey;
use App\Models\Setting;
use App\Service\License\LicenseCheckResult;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;

beforeEach(function () {
    config(['app.self_hosted' => true]);
    config(['cashier.key' => null]);
    $this->user = $this->actingAsUser();
    $this->workspace = $this->createUserWorkspace($this->user);
});

function activateLicenseWithFeatures(array $features): void
{
    Setting::set(SettingsKey::SELF_HOSTED_LICENSE, [
        'license_key' => Crypt::encryptString('lic_test_enterprise_12345'),
        'status' => 'active',
        'features' => $features,
        '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: $features,
        lastChecked: now(),
        expiresAt: now()->addYear(),
    ), 86400);
}

/*
|--------------------------------------------------------------------------
| Custom SMTP (Email Settings)
|--------------------------------------------------------------------------
*/
describe('custom SMTP / email settings', function () {
    $validEmailPayload = [
        'host' => 'smtp.example.com',
        'port' => 587,
        'encryption' => 'tls',
        'username' => '[email protected]',
        'password' => 'secret123',
        'sender_address' => '[email protected]',
    ];

    it('blocks email settings without license', function () use (&$validEmailPayload) {
        $response = $this->putJson(
            route('open.workspaces.save-email-settings', ['workspace' => $this->workspace]),
            $validEmailPayload,
        );

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

    it('blocks email settings with license missing custom_smtp feature', function () use (&$validEmailPayload) {
        activateLicenseWithFeatures(['sso' => true, 'multiOrg' => true]);

        $response = $this->putJson(
            route('open.workspaces.save-email-settings', ['workspace' => $this->workspace]),
            $validEmailPayload,
        );

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

    it('allows email settings with license having custom_smtp feature', function () use (&$validEmailPayload) {
        activateLicenseWithFeatures(['sso' => true, 'custom_smtp' => true]);

        $response = $this->putJson(
            route('open.workspaces.save-email-settings', ['workspace' => $this->workspace]),
            $validEmailPayload,
        );

        $response->assertSuccessful();
    });

    it('allows clearing email settings with license having custom_smtp', function () {
        activateLicenseWithFeatures(['custom_smtp' => true]);

        $response = $this->putJson(
            route('open.workspaces.save-email-settings', ['workspace' => $this->workspace]),
            [],
        );

        $response->assertSuccessful();
    });
});

/*
|--------------------------------------------------------------------------
| Custom Code & CSS
|--------------------------------------------------------------------------
*/
describe('custom code and CSS settings', function () {
    it('blocks custom code without license', function () {
        $response = $this->putJson(
            route('open.workspaces.save-custom-code-settings', ['workspace' => $this->workspace]),
            ['custom_code' => '<script>console.log("hello")</script>'],
        );

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

    it('blocks custom CSS without license', function () {
        $response = $this->putJson(
            route('open.workspaces.save-custom-code-settings', ['workspace' => $this->workspace]),
            ['custom_css' => 'body { color: red; }'],
        );

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

    it('blocks custom code with license missing custom_code feature', function () {
        activateLicenseWithFeatures(['sso' => true, 'custom_smtp' => true]);

        $response = $this->putJson(
            route('open.workspaces.save-custom-code-settings', ['workspace' => $this->workspace]),
            ['custom_code' => '<script>console.log("hello")</script>'],
        );

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

    it('blocks custom code with whitelabel license only', function () {
        activateLicenseWithFeatures(['whitelabel' => true]);

        $response = $this->putJson(
            route('open.workspaces.save-custom-code-settings', ['workspace' => $this->workspace]),
            ['custom_code' => '<script>console.log("hello")</script>'],
        );

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

    it('allows custom code with license having custom_code feature', function () {
        activateLicenseWithFeatures(['custom_code' => true]);

        $response = $this->putJson(
            route('open.workspaces.save-custom-code-settings', ['workspace' => $this->workspace]),
            ['custom_code' => '<script>console.log("hello")</script>'],
        );

        $response->assertSuccessful();
    });

    it('allows custom CSS with license having custom_code feature', function () {
        activateLicenseWithFeatures(['custom_code' => true]);

        $response = $this->putJson(
            route('open.workspaces.save-custom-code-settings', ['workspace' => $this->workspace]),
            ['custom_css' => 'body { color: red; }'],
        );

        $response->assertSuccessful();
    });

    it('allows both custom code and CSS together', function () {
        activateLicenseWithFeatures(['custom_code' => true]);

        $response = $this->putJson(
            route('open.workspaces.save-custom-code-settings', ['workspace' => $this->workspace]),
            [
                'custom_code' => '<script>console.log("hello")</script>',
                'custom_css' => '.form-container { background: #fff; }',
            ],
        );

        $response->assertSuccessful();
    });

    it('allows clearing custom code with license', function () {
        activateLicenseWithFeatures(['custom_code' => true]);

        $response = $this->putJson(
            route('open.workspaces.save-custom-code-settings', ['workspace' => $this->workspace]),
            ['custom_code' => null, 'custom_css' => null],
        );

        $response->assertSuccessful();
    });
});

/*
|--------------------------------------------------------------------------
| Custom Domain (always allowed on self-hosted — is_pro is true)
|--------------------------------------------------------------------------
*/
describe('custom domain on self-hosted', function () {
    it('allows saving custom domain without license (is_pro always true)', function () {
        $this->mock(\App\Service\CustomDomainOwnershipService::class)
            ->shouldReceive('verify')->with($this->workspace->id, 'forms.example.com')->once()->andReturnTrue();
        $response = $this->putJson(
            route('open.workspaces.save-custom-domains', ['workspace' => $this->workspace]),
            ['custom_domains' => ['forms.example.com']],
        );

        $response->assertSuccessful();
    });
});