Files
coolify/tests/v4/Feature/DangerDeleteResourceTest.php
T
Andras Bacsai 3c23e2d30f refactor(auth): remove OAuth re-authentication for destructive actions
Drop the OAuth confirm/callback flow that let users confirm destructive
actions by re-authenticating through a linked provider. This removes the
confirm route and session handling in OauthController, the middleware
bypass for those routes, OauthSetting::providerName(),
User::oauthConfirmationProviders(),
OauthLoginService::identityBelongsToUser() and the
socialite_reauthentication_parameters() helper.

Users with a linked OAuth identity no longer need a password step and
only use the dialog's typed confirmation. Password confirmation is now
required only for users who have a password and no SSO identity. The
confirmation modal and tests are updated to match.
2026-09-30 21:37:02 +02:00

178 lines
7.0 KiB
PHP

<?php
use App\Jobs\DeleteResourceJob;
use App\Livewire\Project\Shared\Danger;
use App\Models\Application;
use App\Models\Environment;
use App\Models\InstanceSettings;
use App\Models\OauthIdentity;
use App\Models\Project;
use App\Models\Server;
use App\Models\Service;
use App\Models\StandaloneDocker;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Route;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
InstanceSettings::forceCreate(['id' => 0]);
Queue::fake();
$this->user = User::factory()->create([
'password' => Hash::make('test-password'),
]);
$this->team = Team::factory()->create();
$this->user->teams()->attach($this->team, ['role' => 'owner']);
$this->server = Server::factory()->create(['team_id' => $this->team->id]);
$this->destination = StandaloneDocker::factory()->create([
'server_id' => $this->server->id,
'network' => 'test-network-'.fake()->unique()->word(),
]);
$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([
'environment_id' => $this->environment->id,
'destination_id' => $this->destination->id,
'destination_type' => $this->destination->getMorphClass(),
]);
$this->actingAs($this->user);
session(['currentTeam' => $this->team]);
// Bind route parameters so get_route_parameters() works in the Danger component
$route = Route::get('/test/{project_uuid}/{environment_uuid}', fn () => '')->name('test.danger');
$request = Request::create("/test/{$this->project->uuid}/{$this->environment->uuid}");
$route->bind($request);
app('router')->setRoutes(app('router')->getRoutes());
Route::dispatch($request);
});
test('delete returns error string when password is incorrect', function () {
Livewire::test(Danger::class, ['resource' => $this->application])
->call('delete', 'wrong-password')
->assertReturned('The provided password is incorrect.');
// Resource should NOT be deleted
expect(Application::find($this->application->id))->not->toBeNull();
});
test('delete redirects before dispatching resource cleanup after the response', function () {
$service = Service::factory()->create([
'environment_id' => $this->environment->id,
'server_id' => $this->server->id,
'destination_id' => $this->destination->id,
'destination_type' => $this->destination->getMorphClass(),
]);
$component = Livewire::test(Danger::class, ['resource' => $service])
->set('projectUuid', $this->project->uuid)
->set('environmentUuid', $this->environment->uuid)
->call('delete', 'test-password')
->assertHasNoErrors()
->assertRedirectToRoute('project.resource.index', [
'project_uuid' => $this->project->uuid,
'environment_uuid' => $this->environment->uuid,
]);
expect($component->effects)->toHaveKey('redirectUsingNavigate', true);
expect(Service::find($service->id))->not->toBeNull();
Queue::assertPushed(DeleteResourceJob::class, fn (DeleteResourceJob $job) => $job->resource->is($service));
});
test('delete succeeds without a password for a user with a linked oauth identity', function () {
OauthIdentity::create([
'user_id' => $this->user->id,
'provider' => 'oidc',
'issuer' => 'https://idp.example.com',
'provider_user_id' => 'oauth-user-id',
]);
Livewire::test(Danger::class, ['resource' => $this->application])
->set('projectUuid', $this->project->uuid)
->set('environmentUuid', $this->environment->uuid)
->call('delete', '')
->assertHasNoErrors()
->assertRedirectToRoute('project.resource.index', [
'project_uuid' => $this->project->uuid,
'environment_uuid' => $this->environment->uuid,
]);
Queue::assertPushed(DeleteResourceJob::class, fn (DeleteResourceJob $job) => $job->resource->is($this->application));
});
test('delete still asks a user with a linked oauth identity for the typed confirmation but not for a password', function () {
OauthIdentity::create([
'user_id' => $this->user->id,
'provider' => 'oidc',
'issuer' => 'https://idp.example.com',
'provider_user_id' => 'oauth-user-id',
]);
Livewire::test(Danger::class, ['resource' => $this->application])
->assertSee('x-model="userConfirmationText"', false)
->assertSee('confirmWithText: true', false)
->assertSee('confirmWithPassword: false', false)
->assertDontSee('type="password"', false)
->assertDontSee('Confirm with', false);
});
test('delete rejects an empty password for a user without a linked oauth identity', function () {
Livewire::test(Danger::class, ['resource' => $this->application])
->call('delete', '')
->assertHasErrors('password')
->assertReturned('The provided password is incorrect.');
Queue::assertNotPushed(DeleteResourceJob::class);
});
test('delete asks a user without a linked oauth identity for the password', function () {
Livewire::test(Danger::class, ['resource' => $this->application])
->assertSee('confirmWithPassword: true', false)
->assertSee('type="password"', false);
});
test('delete applies selectedActions from checkbox state', function () {
$component = Livewire::test(Danger::class, ['resource' => $this->application])
->call('delete', 'test-password', ['delete_configurations', 'docker_cleanup']);
expect($component->get('delete_volumes'))->toBeFalse();
expect($component->get('delete_connected_networks'))->toBeFalse();
expect($component->get('delete_configurations'))->toBeTrue();
expect($component->get('docker_cleanup'))->toBeTrue();
});
test('service can be removed from Coolify without remote cleanup', function () {
$service = Service::factory()->create([
'environment_id' => $this->environment->id,
'server_id' => $this->server->id,
'destination_id' => $this->destination->id,
'destination_type' => $this->destination->getMorphClass(),
]);
Livewire::test(Danger::class, ['resource' => $service])
->set('projectUuid', $this->project->uuid)
->set('environmentUuid', $this->environment->uuid)
->assertSee('Server is not reachable')
->assertSee('Remove from Coolify only')
->call('deleteFromCoolifyOnly', 'test-password')
->assertHasNoErrors()
->assertRedirectToRoute('project.resource.index', [
'project_uuid' => $this->project->uuid,
'environment_uuid' => $this->environment->uuid,
]);
Queue::assertPushed(
DeleteResourceJob::class,
fn (DeleteResourceJob $job): bool => $job->resource->is($service) && $job->deleteFromCoolifyOnly
);
});