|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Jobs\ApplicationDeploymentJob; |
| 4 | +use App\Models\Application; |
| 5 | +use App\Models\ApplicationDeploymentQueue; |
| 6 | +use App\Models\Environment; |
| 7 | +use App\Models\InstanceSettings; |
| 8 | +use App\Models\Project; |
| 9 | +use App\Models\Server; |
| 10 | +use App\Models\StandaloneDocker; |
| 11 | +use App\Models\Team; |
| 12 | +use App\Models\User; |
| 13 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 14 | +use Illuminate\Support\Facades\Bus; |
| 15 | +use Illuminate\Testing\Fluent\AssertableJson; |
| 16 | + |
| 17 | +uses(RefreshDatabase::class); |
| 18 | + |
| 19 | +beforeEach(function () { |
| 20 | + Bus::fake([ApplicationDeploymentJob::class]); |
| 21 | + |
| 22 | + InstanceSettings::unguarded(fn () => InstanceSettings::query()->updateOrCreate( |
| 23 | + ['id' => 0], |
| 24 | + ['is_api_enabled' => true], |
| 25 | + )); |
| 26 | + |
| 27 | + $this->team = Team::factory()->create(); |
| 28 | + $this->user = User::factory()->create(); |
| 29 | + $this->team->members()->attach($this->user->id, ['role' => 'owner']); |
| 30 | + session(['currentTeam' => $this->team]); |
| 31 | + |
| 32 | + $this->token = $this->user->createToken('deployment-actions-test', ['deploy'])->plainTextToken; |
| 33 | + |
| 34 | + $this->server = Server::factory()->create(['team_id' => $this->team->id]); |
| 35 | + $this->destination = StandaloneDocker::query()->where('server_id', $this->server->id)->firstOrFail(); |
| 36 | + $this->project = Project::factory()->create(['team_id' => $this->team->id]); |
| 37 | + $this->environment = Environment::factory()->create(['project_id' => $this->project->id]); |
| 38 | +}); |
| 39 | + |
| 40 | +function deploymentActionHeaders(string $token): array |
| 41 | +{ |
| 42 | + return [ |
| 43 | + 'Authorization' => 'Bearer '.$token, |
| 44 | + 'Content-Type' => 'application/json', |
| 45 | + ]; |
| 46 | +} |
| 47 | + |
| 48 | +function makeDeploymentActionApplication(Environment $environment, StandaloneDocker $destination): Application |
| 49 | +{ |
| 50 | + return Application::factory()->create([ |
| 51 | + 'environment_id' => $environment->id, |
| 52 | + 'destination_id' => $destination->id, |
| 53 | + 'destination_type' => $destination->getMorphClass(), |
| 54 | + 'git_repository' => 'https://github.com/coollabsio/coolify', |
| 55 | + 'git_branch' => 'main', |
| 56 | + 'git_commit_sha' => 'HEAD', |
| 57 | + ]); |
| 58 | +} |
| 59 | + |
| 60 | +test('application start API returns queued deployment uuid as a string', function () { |
| 61 | + $application = makeDeploymentActionApplication($this->environment, $this->destination); |
| 62 | + |
| 63 | + $response = $this->withHeaders(deploymentActionHeaders($this->token)) |
| 64 | + ->postJson("/api/v1/applications/{$application->uuid}/start"); |
| 65 | + |
| 66 | + $response->assertSuccessful() |
| 67 | + ->assertJson(fn (AssertableJson $json) => $json |
| 68 | + ->where('message', 'Deployment request queued.') |
| 69 | + ->whereType('deployment_uuid', 'string') |
| 70 | + ); |
| 71 | + |
| 72 | + expect(ApplicationDeploymentQueue::query()->where('deployment_uuid', $response->json('deployment_uuid'))->exists())->toBeTrue(); |
| 73 | +}); |
| 74 | + |
| 75 | +test('application restart API returns queued deployment uuid as a string', function () { |
| 76 | + $application = makeDeploymentActionApplication($this->environment, $this->destination); |
| 77 | + |
| 78 | + $response = $this->withHeaders(deploymentActionHeaders($this->token)) |
| 79 | + ->postJson("/api/v1/applications/{$application->uuid}/restart"); |
| 80 | + |
| 81 | + $response->assertSuccessful() |
| 82 | + ->assertJson(fn (AssertableJson $json) => $json |
| 83 | + ->where('message', 'Restart request queued.') |
| 84 | + ->whereType('deployment_uuid', 'string') |
| 85 | + ); |
| 86 | + |
| 87 | + $deployment = ApplicationDeploymentQueue::query() |
| 88 | + ->where('deployment_uuid', $response->json('deployment_uuid')) |
| 89 | + ->first(); |
| 90 | + |
| 91 | + expect($deployment)->not->toBeNull() |
| 92 | + ->and($deployment->restart_only)->toBeTruthy(); |
| 93 | +}); |
| 94 | + |
| 95 | +test('deployment uuid strings are not converted as objects in API and webhook controllers', function () { |
| 96 | + $files = [ |
| 97 | + app_path('Http/Controllers/Api/DeployController.php'), |
| 98 | + app_path('Http/Controllers/Webhook/Bitbucket.php'), |
| 99 | + app_path('Http/Controllers/Webhook/Gitea.php'), |
| 100 | + app_path('Http/Controllers/Webhook/Gitlab.php'), |
| 101 | + ]; |
| 102 | + |
| 103 | + foreach ($files as $file) { |
| 104 | + expect(file_get_contents($file)) |
| 105 | + ->not->toContain('$deployment_uuid->toString()') |
| 106 | + ->not->toContain('$deployment_uuid?->toString()'); |
| 107 | + } |
| 108 | +}); |
0 commit comments