-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add soft deletes to User model and implement user account deletion fe…
…ature with logging
- Loading branch information
1 parent
71898ca
commit 6d5bfc4
Showing
4 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |