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.0 KB
Raw Blame
<?php

use App\Helpers\SshMultiplexingHelper;
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\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Visus\Cuid2\Cuid2;

uses(RefreshDatabase::class);

beforeEach(function () {
    config(['app.maintenance.driver' => 'file']);
    Storage::fake('ssh-keys');

    InstanceSettings::forceCreate(['id' => 0, 'is_api_enabled' => true]);

    $this->team = Team::factory()->create();
    $this->user = User::factory()->create();
    $this->team->members()->attach($this->user->id, ['role' => 'owner']);

    session(['currentTeam' => $this->team]);

    $this->actingAs($this->user);

    $this->existingPrivateKey = PrivateKey::withoutEvents(fn () => PrivateKey::forceCreate([
        'uuid' => (string) new Cuid2,
        'name' => 'Existing SSH Key',
        'description' => 'Existing SSH Key',
        'private_key' => 'test-private-key',
        'team_id' => $this->team->id,
    ]));
});

it('generates and preselects a new private key without clearing server form data', function () {
    $component = Livewire::test(ByIp::class, [
        'private_keys' => collect([$this->existingPrivateKey]),
        'limit_reached' => false,
    ])
        ->set('name', 'Production Server')
        ->set('description', 'Deploy target')
        ->set('ip', '192.0.2.50')
        ->set('user', 'deploy.user')
        ->set('port', 2222)
        ->set('server_role', 'build')
        ->call('generatePrivateKey', 'ed25519')
        ->assertHasNoErrors()
        ->assertSet('name', 'Production Server')
        ->assertSet('description', 'Deploy target')
        ->assertSet('ip', '192.0.2.50')
        ->assertSet('user', 'deploy.user')
        ->assertSet('port', 2222)
        ->assertSet('server_role', 'build');

    $newPrivateKeyId = $component->get('private_key_id');

    expect($newPrivateKeyId)->not->toBe($this->existingPrivateKey->id)
        ->and(PrivateKey::find($newPrivateKeyId))->not->toBeNull()
        ->and($component->get('private_keys')->pluck('id'))->toContain($newPrivateKeyId);
});

it('preselects a manually added private key without clearing server form data', function () {
    $manualPrivateKey = PrivateKey::withoutEvents(fn () => PrivateKey::forceCreate([
        'uuid' => (string) new Cuid2,
        'name' => 'Manual SSH Key',
        'description' => 'Manual SSH Key',
        'private_key' => 'manual-test-private-key',
        'team_id' => $this->team->id,
    ]));

    Livewire::test(ByIp::class, [
        'private_keys' => collect([$this->existingPrivateKey]),
        'limit_reached' => false,
    ])
        ->set('name', 'Production Server')
        ->set('description', 'Deploy target')
        ->set('ip', '192.0.2.51')
        ->set('user', 'deploy.user')
        ->set('port', 2222)
        ->set('server_role', 'build')
        ->call('handlePrivateKeyCreated', $manualPrivateKey->id)
        ->assertSet('private_key_id', $manualPrivateKey->id)
        ->assertSet('name', 'Production Server')
        ->assertSet('description', 'Deploy target')
        ->assertSet('ip', '192.0.2.51')
        ->assertSet('user', 'deploy.user')
        ->assertSet('port', 2222)
        ->assertSet('server_role', 'build')
        ->assertSee('Manual SSH Key');
});

it('only accepts a private key from the current team when adding a server', function () {
    $otherTeamKey = PrivateKey::factory()->create(['team_id' => Team::factory()->create()->id]);

    Livewire::test(ByIp::class, [
        'private_keys' => collect([$this->existingPrivateKey]),
        'limit_reached' => false,
    ])
        ->set('ip', '192.0.2.60')
        ->set('private_key_id', $otherTeamKey->id)
        ->call('submit')
        ->assertHasErrors(['private_key_id']);

    expect(Server::where('ip', '192.0.2.60')->exists())->toBeFalse();

    Livewire::test(ByIp::class, [
        'private_keys' => collect([$this->existingPrivateKey]),
        'limit_reached' => false,
    ])
        ->set('ip', '192.0.2.61')
        ->set('private_key_id', $this->existingPrivateKey->id)
        ->call('submit')
        ->assertHasNoErrors();

    expect(Server::where('ip', '192.0.2.61')->value('private_key_id'))->toBe($this->existingPrivateKey->id);
});

it('ssh configuration only uses a private key from the server team', function () {
    $otherTeamKey = PrivateKey::factory()->create(['team_id' => Team::factory()->create()->id]);
    $server = Server::factory()->create([
        'team_id' => $this->team->id,
        'private_key_id' => $this->existingPrivateKey->id,
    ]);

    expect(SshMultiplexingHelper::serverSshConfiguration($server)['sshKeyLocation'])
        ->toBe($this->existingPrivateKey->getKeyLocation());

    Server::query()->whereKey($server->id)->update(['private_key_id' => $otherTeamKey->id]);

    expect(fn () => SshMultiplexingHelper::serverSshConfiguration($server->fresh()))
        ->toThrow(ModelNotFoundException::class);
});