Files
coolify/tests/Feature/ServiceConnectToDockerNetworkApiTest.php
e3d4411ff2 chore: merge main into release/v4.5 (#12192)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Furox <177975472+Furox-Art@users.noreply.github.com>
Co-authored-by: johandenhollander <johan.den.hollander@finalist.nl>
Co-authored-by: João Cruz <161638790+SrJooJ@users.noreply.github.com>
Co-authored-by: Saim Hashmi <hashmisaim037@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-10-18 23:43:20 +02:00

79 lines
3.1 KiB
PHP

<?php
use App\Models\InstanceSettings;
use App\Models\Project;
use App\Models\Server;
use App\Models\Service;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Illuminate\Testing\TestResponse;
uses(RefreshDatabase::class);
beforeEach(function () {
InstanceSettings::unguarded(fn () => InstanceSettings::create(['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->bearerToken = $this->user->createToken('test-token', ['*'])->plainTextToken;
$this->server = Server::factory()->create(['team_id' => $this->team->id]);
$this->project = Project::factory()->create(['team_id' => $this->team->id]);
$this->environment = $this->project->environments()->first();
});
function createServiceWithDockerNetworkFlag(mixed $connectToDockerNetwork, string $bearerToken, array $payload): TestResponse
{
return test()->withHeaders(['Authorization' => 'Bearer '.$bearerToken])
->postJson('/api/v1/services', [
...$payload,
'connect_to_docker_network' => $connectToDockerNetwork,
]);
}
describe('POST /api/v1/services connect_to_docker_network', function () {
test('saves the flag for a docker_compose_raw service', function () {
$response = createServiceWithDockerNetworkFlag(true, $this->bearerToken, [
'project_uuid' => $this->project->uuid,
'environment_uuid' => $this->environment->uuid,
'server_uuid' => $this->server->uuid,
'docker_compose_raw' => base64_encode("services:\n app:\n image: nginx:alpine\n"),
]);
$response->assertCreated();
expect(Service::whereUuid($response->json('uuid'))->firstOrFail()->connect_to_docker_network)->toBeTruthy();
});
test('saves the flag for a one-click service', function () {
Queue::fake();
$response = createServiceWithDockerNetworkFlag(true, $this->bearerToken, [
'type' => 'cyberchef',
'project_uuid' => $this->project->uuid,
'environment_uuid' => $this->environment->uuid,
'server_uuid' => $this->server->uuid,
]);
$response->assertCreated();
expect(Service::whereUuid($response->json('uuid'))->firstOrFail()->connect_to_docker_network)->toBeTruthy();
});
test('rejects a non-boolean value', function () {
$response = createServiceWithDockerNetworkFlag('not-a-boolean', $this->bearerToken, [
'project_uuid' => $this->project->uuid,
'environment_uuid' => $this->environment->uuid,
'server_uuid' => $this->server->uuid,
'docker_compose_raw' => base64_encode("services:\n app:\n image: nginx:alpine\n"),
]);
$response->assertUnprocessable()->assertJsonValidationErrors(['connect_to_docker_network']);
expect($response->json('errors.connect_to_docker_network'))->not->toContain('This field is not allowed.');
});
});