Files
coolify/tests/Feature/DeployForceBooleanCastTest.php
T
Josh Salway 6eb527d0ab test(deploy): expand force param tests to cover all coercion cases
Adds datasets for falsy (false, 0) and truthy (true, 1) query string
values, covering the full coercion table from the PR description.
2026-05-02 23:37:57 +10:00

98 lines
3.7 KiB
PHP

<?php
use App\Models\Application;
use App\Models\Environment;
use App\Models\Project;
use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->team = Team::factory()->create();
$this->user = User::factory()->create();
$this->team->members()->attach($this->user->id, ['role' => 'owner']);
$plainTextToken = Str::random(40);
$token = $this->user->tokens()->create([
'name' => 'test-token',
'token' => hash('sha256', $plainTextToken),
'abilities' => ['*'],
'team_id' => $this->team->id,
]);
$this->bearerToken = $token->getKey().'|'.$plainTextToken;
$this->server = Server::factory()->create(['team_id' => $this->team->id]);
$this->destination = StandaloneDocker::factory()->create([
'server_id' => $this->server->id,
'network' => 'coolify-'.Str::lower(Str::random(8)),
]);
$this->project = Project::factory()->create(['team_id' => $this->team->id]);
$this->environment = Environment::factory()->create(['project_id' => $this->project->id]);
$this->application = Application::factory()->create([
'uuid' => (string) Str::uuid(),
'environment_id' => $this->environment->id,
'destination_id' => $this->destination->id,
'destination_type' => StandaloneDocker::class,
]);
});
// Regression test: $request->input('force') ?? false coerced the string "false" to bool true,
// causing every API deploy with ?force=false to run with --no-cache and bypass layer caching.
//
// Input | input('force') ?? false (old) | boolean('force') (fix)
// ------------|-------------------------------|----------------------
// "false" | true (bug — non-empty string)| false (correct)
// "true" | true | true
// "0" | false (PHP special-cases "0") | false
// "1" | true | true
// absent/null | false | false
dataset('falsy force values', [
'string false' => ['false'],
'string 0' => ['0'],
]);
dataset('truthy force values', [
'string true' => ['true'],
'string 1' => ['1'],
]);
test('force=<falsy> query string param does not set force_rebuild', function (string $value) {
$response = $this->withHeaders(['Authorization' => 'Bearer '.$this->bearerToken])
->postJson('/api/v1/deploy?uuid='.$this->application->uuid.'&force='.$value);
$response->assertSuccessful();
$deployment = $this->application->deployment_queue()->latest('id')->first();
expect($deployment)->not()->toBeNull();
expect($deployment->force_rebuild)->toBeFalse();
})->with('falsy force values');
test('force=<truthy> query string param sets force_rebuild', function (string $value) {
$response = $this->withHeaders(['Authorization' => 'Bearer '.$this->bearerToken])
->postJson('/api/v1/deploy?uuid='.$this->application->uuid.'&force='.$value);
$response->assertSuccessful();
$deployment = $this->application->deployment_queue()->latest('id')->first();
expect($deployment)->not()->toBeNull();
expect($deployment->force_rebuild)->toBeTrue();
})->with('truthy force values');
test('omitting force param does not set force_rebuild', function () {
$response = $this->withHeaders(['Authorization' => 'Bearer '.$this->bearerToken])
->postJson('/api/v1/deploy?uuid='.$this->application->uuid);
$response->assertSuccessful();
$deployment = $this->application->deployment_queue()->latest('id')->first();
expect($deployment)->not()->toBeNull();
expect($deployment->force_rebuild)->toBeFalse();
});