diff --git a/resources/views/components/api-disabled-callout.blade.php b/resources/views/components/api-disabled-callout.blade.php new file mode 100644 index 0000000000..1bc9dec11e --- /dev/null +++ b/resources/views/components/api-disabled-callout.blade.php @@ -0,0 +1,14 @@ +@props(['message' => 'This feature uses the Coolify API.']) + +@unless (instanceSettings()->is_api_enabled) + + {{ $message }} + @if (isInstanceAdmin()) +
+ Enable API access +
+ @else + Ask an instance admin to enable API access. + @endif +
+@endunless diff --git a/resources/views/livewire/project/shared/webhooks.blade.php b/resources/views/livewire/project/shared/webhooks.blade.php index 3e332bf440..8e5483b283 100644 --- a/resources/views/livewire/project/shared/webhooks.blade.php +++ b/resources/views/livewire/project/shared/webhooks.blade.php @@ -43,6 +43,8 @@ + @@ -117,6 +119,8 @@ + diff --git a/resources/views/livewire/security/api-tokens.blade.php b/resources/views/livewire/security/api-tokens.blade.php index ddffd51094..8b05a0679b 100644 --- a/resources/views/livewire/security/api-tokens.blade.php +++ b/resources/views/livewire/security/api-tokens.blade.php @@ -10,13 +10,15 @@ - - - Open settings - - + description="{{ isInstanceAdmin() ? 'Enable API access in instance settings to issue tokens.' : 'Ask an instance admin to enable API access.' }}" + icon-name="keys" size="sm"> + @if (isInstanceAdmin()) + + + Enable API access + + + @endif diff --git a/tests/Feature/ApiDisabledCalloutTest.php b/tests/Feature/ApiDisabledCalloutTest.php new file mode 100644 index 0000000000..7f6ca8f6f8 --- /dev/null +++ b/tests/Feature/ApiDisabledCalloutTest.php @@ -0,0 +1,79 @@ + 0, 'is_api_enabled' => false]); + + $this->rootTeam = Team::factory()->create(['id' => 0]); + $this->instanceAdmin = User::factory()->create(); + $this->rootTeam->members()->attach($this->instanceAdmin->id, ['role' => 'admin']); + + $this->team = Team::factory()->create(); + $this->teamAdmin = User::factory()->create(); + $this->team->members()->attach($this->teamAdmin->id, ['role' => 'admin']); + + $server = Server::factory()->create(['team_id' => $this->team->id]); + $destination = StandaloneDocker::query()->where('server_id', $server->id)->firstOrFail(); + $project = Project::factory()->create(['team_id' => $this->team->id]); + $environment = Environment::factory()->create(['project_id' => $project->id]); + + $this->application = Application::factory()->create([ + 'environment_id' => $environment->id, + 'destination_id' => $destination->id, + 'destination_type' => $destination->getMorphClass(), + ]); +}); + +afterEach(function () { + Server::flushIdentityMap(); +}); + +it('links instance admins to the API settings when the API is disabled', function (string $component) { + $this->actingAs($this->instanceAdmin); + session(['currentTeam' => $this->rootTeam]); + + $params = $component === Webhooks::class ? ['resource' => $this->application] : []; + + Livewire::test($component, $params) + ->assertSee('API access is turned off') + ->assertSee(route('settings.advanced')) + ->assertSee('Enable API access'); +})->with(['webhooks' => Webhooks::class, 'api tokens' => ApiTokens::class]); + +it('tells other users to ask an instance admin when the API is disabled', function (string $component) { + $this->actingAs($this->teamAdmin); + session(['currentTeam' => $this->team]); + + $params = $component === Webhooks::class ? ['resource' => $this->application] : []; + + Livewire::test($component, $params) + ->assertSee('API access is turned off') + ->assertSee('Ask an instance admin to enable API access.') + ->assertDontSee(route('settings.advanced')); +})->with(['webhooks' => Webhooks::class, 'api tokens' => ApiTokens::class]); + +it('does not show the API notice on the webhooks page when the API is enabled', function () { + InstanceSettings::query()->whereKey(0)->update(['is_api_enabled' => true]); + Once::flush(); + $this->actingAs($this->teamAdmin); + session(['currentTeam' => $this->team]); + + Livewire::test(Webhooks::class, ['resource' => $this->application]) + ->assertDontSee('API access is turned off'); +});