Skip to content

Commit

Permalink
Add soft deletes to User model and implement user account deletion fe…
Browse files Browse the repository at this point in the history
…ature with logging
  • Loading branch information
kennedyowusu committed Jan 6, 2025
1 parent 71898ca commit 6d5bfc4
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\SoftDeletes;


class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable, HasApiTokens;
use HasFactory, Notifiable, HasApiTokens, SoftDeletes;

/**
* The attributes that are mass assignable.
Expand Down
2 changes: 2 additions & 0 deletions database/migrations/0001_01_01_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function up(): void
$table->enum('role', ['user', 'admin'])->default('user');
$table->rememberToken();
$table->timestamps();

$table->softDeletes();
});

Schema::create('password_reset_tokens', function (Blueprint $table) {
Expand Down
67 changes: 67 additions & 0 deletions tests/Feature/UserAccountDeletionFeatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class UserAccountDeletionFeatureTest extends TestCase
{
use RefreshDatabase;

/**
* Test authenticated user can delete their account.
*/
public function test_authenticated_user_can_delete_their_account()
{
$user = User::factory()->hasWeightLogs(3)->hasHealthData()->create();

$this->actingAs($user)
->deleteJson('/api/v1/profile')
->assertStatus(200)
->assertJson(['message' => 'User deleted successfully.']);

// Assert that the user no longer exists in the database
$this->assertDatabaseMissing('users', ['id' => $user->id]);

// Assert related data is deleted
$this->assertDatabaseMissing('weight_logs', ['user_id' => $user->id]);
$this->assertDatabaseMissing('health_data', ['user_id' => $user->id]);
}

/**
* Test unauthorized user cannot delete another user's account.
*/
public function test_user_cannot_delete_another_users_account()
{
$user = User::factory()->create();
$otherUser = User::factory()->create();

$this->actingAs($user)
->deleteJson('/api/v1/profile')
->assertStatus(403);

// Assert that the other user still exists in the database
$this->assertDatabaseHas('users', ['id' => $otherUser->id]);
}

/**
* Test deletion logs are created.
*/
public function test_user_deletion_creates_log_entry()
{
$user = User::factory()->create();

$this->actingAs($user)
->deleteJson('/api/v1/profile')
->assertStatus(200);

// Assert that a deletion log was created
$this->assertDatabaseHas('deletion_logs', [
'user_id' => $user->id,
'email' => $user->email,
]);
}
}
52 changes: 52 additions & 0 deletions tests/Unit/UserDeletionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Tests\Unit;

use App\Events\UserDeleted;
use App\Listeners\LogUserDeletion;
use App\Models\User;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;

class UserDeletionTest extends TestCase
{
/**
* Test the UserDeleted event is fired correctly.
*/
public function test_user_deleted_event_is_dispatched()
{
$user = User::factory()->create();

// Mock the event
Event::fake();

// Dispatch the event
event(new UserDeleted($user));

// Assert the event was dispatched
Event::assertDispatched(UserDeleted::class, function ($event) use ($user) {
return $event->user->id === $user->id;
});
}

/**
* Test the LogUserDeletion listener handles the UserDeleted event.
*/
public function test_log_user_deletion_listener_logs_data_correctly()
{
$user = User::factory()->create();

Log::shouldReceive('info')
->once()
->with('User deleted', [
'id' => $user->id,
'name' => $user->name,
'email' => $user->email,
'deleted_at' => now()->toDateTimeString(),
]);

$listener = new LogUserDeletion();
$listener->handle(new UserDeleted($user));
}
}

0 comments on commit 6d5bfc4

Please sign in to comment.