Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

5.1 KB
Raw Blame
<?php

use App\Livewire\Settings\Advanced;
use App\Models\InstanceSettings;
use App\Models\Server;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Once;
use Livewire\Livewire;

uses(RefreshDatabase::class);

test('instance admin can toggle registration via listbox instantSave', function () {
    $rootTeam = Team::find(0) ?? Team::factory()->create(['id' => 0]);
    Server::factory()->create(['id' => 0, 'team_id' => $rootTeam->id]);
    $settings = InstanceSettings::forceCreate([
        'id' => 0,
        'is_registration_enabled' => false,
        'disable_two_step_confirmation' => false,
    ]);
    Once::flush();

    $user = User::factory()->create();
    $rootTeam->members()->attach($user->id, ['role' => 'admin']);

    $this->actingAs($user);
    session(['currentTeam' => ['id' => $rootTeam->id]]);

    Livewire::test(Advanced::class)
        ->assertSet('is_registration_enabled', false)
        ->assertSee('Registration')
        ->assertSee('Destructive action confirmation')
        ->assertDontSee('Only administrators can create accounts.')
        ->set('is_registration_enabled', true)
        ->call('instantSave')
        ->assertDispatched('success');

    expect((bool) $settings->fresh()->is_registration_enabled)->toBeTrue();
});

test('instance admin can toggle two-step confirmation via listbox instantSave', function () {
    $rootTeam = Team::find(0) ?? Team::factory()->create(['id' => 0]);
    Server::factory()->create(['id' => 0, 'team_id' => $rootTeam->id]);
    $settings = InstanceSettings::forceCreate([
        'id' => 0,
        'is_registration_enabled' => true,
        'disable_two_step_confirmation' => false,
    ]);
    Once::flush();

    $user = User::factory()->create();
    $rootTeam->members()->attach($user->id, ['role' => 'admin']);

    $this->actingAs($user);
    session(['currentTeam' => ['id' => $rootTeam->id]]);

    Livewire::test(Advanced::class)
        ->assertSet('disable_two_step_confirmation', false)
        ->set('disable_two_step_confirmation', true)
        ->call('instantSave')
        ->assertDispatched('success');

    expect((bool) $settings->fresh()->disable_two_step_confirmation)->toBeTrue();
});

test('instance admin can configure the image CDN URL at runtime', function () {
    $rootTeam = Team::find(0) ?? Team::factory()->create(['id' => 0]);
    Server::factory()->create(['id' => 0, 'team_id' => $rootTeam->id]);
    $settings = InstanceSettings::forceCreate(['id' => 0]);
    Once::flush();

    $user = User::factory()->create();
    $rootTeam->members()->attach($user->id, ['role' => 'admin']);

    $this->actingAs($user);
    session(['currentTeam' => ['id' => $rootTeam->id]]);

    Livewire::test(Advanced::class)
        ->assertSee('Image CDN URL')
        ->set('image_cdn_url', 'https://images.example.com/media/')
        ->call('submit')
        ->assertHasNoErrors()
        ->assertDispatched('success');

    expect($settings->fresh()->image_cdn_url)->toBe('https://images.example.com/media');
});

test('open API allowlist warning is hidden when API access is disabled', function () {
    $rootTeam = Team::find(0) ?? Team::factory()->create(['id' => 0]);
    Server::factory()->create(['id' => 0, 'team_id' => $rootTeam->id]);
    InstanceSettings::forceCreate([
        'id' => 0,
        'is_api_enabled' => false,
        'allowed_ips' => null,
    ]);
    Once::flush();

    $user = User::factory()->create();
    $rootTeam->members()->attach($user->id, ['role' => 'admin']);

    $this->actingAs($user);
    session(['currentTeam' => ['id' => $rootTeam->id]]);

    Livewire::test(Advanced::class)
        ->assertSet('is_api_enabled', false)
        ->assertDontSee('API access is open to every source');
});

test('open API allowlist warning is shown when API access is enabled without allowlist', function () {
    $rootTeam = Team::find(0) ?? Team::factory()->create(['id' => 0]);
    Server::factory()->create(['id' => 0, 'team_id' => $rootTeam->id]);
    InstanceSettings::forceCreate([
        'id' => 0,
        'is_api_enabled' => true,
        'allowed_ips' => null,
    ]);
    Once::flush();

    $user = User::factory()->create();
    $rootTeam->members()->attach($user->id, ['role' => 'admin']);

    $this->actingAs($user);
    session(['currentTeam' => ['id' => $rootTeam->id]]);

    Livewire::test(Advanced::class)
        ->assertSet('is_api_enabled', true)
        ->assertSee('API access is open to every source');
});

test('MCP endpoint callout links to API token management', function () {
    $rootTeam = Team::find(0) ?? Team::factory()->create(['id' => 0]);
    Server::factory()->create(['id' => 0, 'team_id' => $rootTeam->id]);
    InstanceSettings::forceCreate([
        'id' => 0,
        'is_api_enabled' => false,
        'is_mcp_server_enabled' => true,
    ]);
    Once::flush();

    $user = User::factory()->create();
    $rootTeam->members()->attach($user->id, ['role' => 'admin']);

    $this->actingAs($user);
    session(['currentTeam' => ['id' => $rootTeam->id]]);

    Livewire::test(Advanced::class)
        ->assertSee(url('/mcp'))
        ->assertSee('href="'.route('security.api-tokens').'"', false);
});