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->server = Server::factory()->create(['team_id' => $this->team->id]); $this->token = $this->user->createToken('server-validation', ['write'])->plainTextToken; Queue::fake(); }); function serverValidationHeaders(): array { return ['Authorization' => 'Bearer '.test()->token]; } it('validates without installing by default', function () { $response = $this->withHeaders(serverValidationHeaders()) ->postJson("/api/v1/servers/{$this->server->uuid}/validate"); $response->assertCreated()->assertJson(['message' => 'Validation started.']); Queue::assertNotPushed(ValidateAndInstallServerJob::class); }); it('validates and installs when explicitly requested', function () { $this->token = $this->user->createToken('server-validation-install', ['write', 'deploy'])->plainTextToken; $response = $this->withHeaders(serverValidationHeaders()) ->postJson("/api/v1/servers/{$this->server->uuid}/validate", ['install' => true]); $response->assertCreated()->assertJson(['message' => 'Validation and installation started.']); Queue::assertPushed( ValidateAndInstallServerJob::class, fn (ValidateAndInstallServerJob $job): bool => $job->server->is($this->server) ); Queue::assertNotPushed(ValidateServer::class); }); it('rejects an invalid install option', function () { $response = $this->withHeaders(serverValidationHeaders()) ->postJson("/api/v1/servers/{$this->server->uuid}/validate", ['install' => 'yes']); $response->assertUnprocessable()->assertJsonValidationErrors('install'); }); it('rejects non-string SSH host keys with 422', function (string $endpoint) { $settingsBefore = $this->server->settings->getAttributes(); $serverCount = Server::count(); $this->withHeaders(serverValidationHeaders()) ->postJson($endpoint === 'create' ? '/api/v1/servers' : "/api/v1/servers/{$this->server->uuid}/host-key/reset", [ 'ssh_host_key' => ['x'], ]) ->assertUnprocessable() ->assertJsonValidationErrors('ssh_host_key'); expect(Server::count())->toBe($serverCount); expect($this->server->settings->fresh()->getAttributes())->toBe($settingsBefore); Queue::assertNothingPushed(); })->with(['create', 'reset']);