73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Unit\Models;
|
|
|
|
use App\Models\Allocation;
|
|
use App\Models\Node;
|
|
use App\Models\Server;
|
|
use App\Tests\TestCase;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
class ServerTest extends TestCase
|
|
{
|
|
#[DataProvider('sftpUrlDataProvider')]
|
|
public function test_sftp_url_points_at_the_given_directory(?string $directory, string $expectedPath): void
|
|
{
|
|
$server = $this->serverWithNode();
|
|
|
|
$this->assertSame("sftp://user.abcdefgh@node.example.com:2022{$expectedPath}", $server->getSftpUrl('user', $directory));
|
|
}
|
|
|
|
public static function sftpUrlDataProvider(): array
|
|
{
|
|
return [
|
|
'no directory' => [null, ''],
|
|
'root' => ['/', ''],
|
|
'single directory' => ['/data', '/data/'],
|
|
'directory without a leading slash' => ['data', '/data/'],
|
|
'directory with a trailing slash' => ['data/', '/data/'],
|
|
'nested directory' => ['/data/world', '/data/world/'],
|
|
'directory with encoded parts' => ['/data/my world', '/data/my%20world/'],
|
|
];
|
|
}
|
|
|
|
public function test_sftp_url_uses_the_node_sftp_alias_when_set(): void
|
|
{
|
|
$server = $this->serverWithNode(sftpAlias: 'sftp.example.com');
|
|
|
|
$this->assertSame('sftp://user.abcdefgh@sftp.example.com:2022/data/', $server->getSftpUrl('user', '/data'));
|
|
}
|
|
|
|
public function test_sftp_url_encodes_the_username(): void
|
|
{
|
|
$server = $this->serverWithNode();
|
|
|
|
$this->assertSame('sftp://user%20name.abcdefgh@node.example.com:2022/data/', $server->getSftpUrl('user name', '/data'));
|
|
}
|
|
|
|
public function test_allocation_mappings_stay_an_object_when_the_server_has_no_allocations(): void
|
|
{
|
|
$server = new Server();
|
|
$server->node_id = 1;
|
|
$server->setRelation('allocation', new Allocation(['node_id' => 1, 'ip' => '127.0.0.1', 'port' => 25565]));
|
|
$server->setRelation('allocations', new Collection());
|
|
|
|
$this->assertSame('{"":[]}', json_encode($server->getAllocationMappings()));
|
|
}
|
|
|
|
private function serverWithNode(string $fqdn = 'node.example.com', ?string $sftpAlias = null): Server
|
|
{
|
|
$node = new Node();
|
|
$node->fqdn = $fqdn;
|
|
$node->daemon_sftp_alias = $sftpAlias;
|
|
$node->daemon_sftp = 2022;
|
|
|
|
$server = new Server();
|
|
$server->uuid_short = 'abcdefgh';
|
|
$server->setRelation('node', $node);
|
|
|
|
return $server;
|
|
}
|
|
}
|