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.

3.5 KB
Raw Blame
<?php

use App\Livewire\Server\New\ByIp;
use App\Models\InstanceSettings;
use App\Models\PrivateKey;
use App\Models\Server;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

beforeEach(function () {
    InstanceSettings::forceCreate(['id' => 0]);

    $this->user = User::factory()->create();
    $this->team = Team::factory()->create();
    $this->user->teams()->attach($this->team, ['role' => 'owner']);
    $this->actingAs($this->user);
    session(['currentTeam' => $this->team]);

    $this->privateKey = PrivateKey::factory()->create([
        'team_id' => $this->team->id,
    ]);
});

it('detects duplicate ip within the same team', function () {
    Server::factory()->create([
        'ip' => '1.2.3.4',
        'team_id' => $this->team->id,
        'private_key_id' => $this->privateKey->id,
    ]);

    $foundServer = Server::whereIp('1.2.3.4')->first();

    expect($foundServer)->not->toBeNull();
    expect($foundServer->team_id)->toBe($this->team->id);
});

it('detects duplicate ip from another team', function () {
    $otherTeam = Team::factory()->create();

    Server::factory()->create([
        'ip' => '5.6.7.8',
        'team_id' => $otherTeam->id,
    ]);

    $foundServer = Server::whereIp('5.6.7.8')->first();

    expect($foundServer)->not->toBeNull();
    expect($foundServer->team_id)->not->toBe($this->team->id);
});

it('rejects a duplicate ip from the same team when adding a server', function () {
    Server::factory()->create([
        'ip' => '1.2.3.4',
        'team_id' => $this->team->id,
        'private_key_id' => $this->privateKey->id,
    ]);

    Livewire::test(ByIp::class, [
        'private_keys' => collect([$this->privateKey]),
        'limit_reached' => false,
    ])
        ->set('ip', '1.2.3.4')
        ->set('private_key_id', $this->privateKey->id)
        ->call('submit')
        ->assertDispatched('error', 'A server with this IP/Domain already exists.');

    expect(Server::where('ip', '1.2.3.4')->count())->toBe(1);
});

it('rejects a duplicate ip from another team without revealing the owner team', function () {
    $otherTeam = Team::factory()->create();

    Server::factory()->create([
        'ip' => '5.6.7.8',
        'team_id' => $otherTeam->id,
    ]);

    Livewire::test(ByIp::class, [
        'private_keys' => collect([$this->privateKey]),
        'limit_reached' => false,
    ])
        ->set('ip', '5.6.7.8')
        ->set('private_key_id', $this->privateKey->id)
        ->call('submit')
        ->assertDispatched('error', 'A server with this IP/Domain already exists.')
        ->assertNotDispatched('error', 'A server with this IP/Domain is already in use by another team.');

    expect(Server::where('ip', '5.6.7.8')->count())->toBe(1);
});

it('allows adding ip that does not exist globally', function () {
    $foundServer = Server::whereIp('10.20.30.40')->first();

    expect($foundServer)->toBeNull();
});

it('enforces global uniqueness not just team-scoped', function () {
    $otherTeam = Team::factory()->create();

    Server::factory()->create([
        'ip' => '9.8.7.6',
        'team_id' => $otherTeam->id,
    ]);

    // Global check finds the server even though it belongs to another team
    $foundServer = Server::whereIp('9.8.7.6')->first();
    expect($foundServer)->not->toBeNull();

    // Team-scoped check would miss it - this is why global check is needed
    $teamScopedServer = Server::where('team_id', $this->team->id)
        ->where('ip', '9.8.7.6')
        ->first();
    expect($teamScopedServer)->toBeNull();
});