Skip to content

Commit a602c5a

Browse files
committed
feat(test): (LAR-86) adding test for bannished user
1 parent bf10e0c commit a602c5a

File tree

4 files changed

+40
-26
lines changed

4 files changed

+40
-26
lines changed

app/Filament/Resources/UserResource.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ public static function getPages(): array
123123

124124
public static function BanUserAction(User $record, $reason): void
125125
{
126+
if ($record->banned_at !== null) {
127+
Notification::make()
128+
->warning()
129+
->title(__('Impossible de bannir'))
130+
->body(__('Cet utilisateur est déjà banni.'))
131+
->send();
132+
133+
return;
134+
}
135+
126136
$record->banned_at = Carbon::now();
127137
$record->banned_reason = $reason;
128138
$record->save();

routes/cpanel.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
use App\Http\Controllers\Cpanel;
66
use Illuminate\Support\Facades\Route;
77

8-
Route::group(['middleware' => ['role:admin']], function () {
9-
Route::redirect('/', 'cpanel/home');
10-
Route::get('/home', Cpanel\DashboardController::class)->name('home');
11-
Route::get('/analytics', Cpanel\AnalyticsController::class)->name('analytics');
12-
Route::prefix('users')->as('users.')->group(function (): void {
13-
Route::get('/', Cpanel\UserController::class)->name('browse');
14-
});
8+
Route::redirect('/', 'cpanel/home');
9+
Route::get('/home', Cpanel\DashboardController::class)->name('home');
10+
Route::get('/analytics', Cpanel\AnalyticsController::class)->name('analytics');
11+
Route::prefix('users')->as('users.')->group(function (): void {
12+
Route::get('/', Cpanel\UserController::class)->name('browse');
1513
});

tests/Feature/UserResourceTest.php renamed to tests/Feature/Filament/UserResourceTest.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,31 @@
44

55
use Carbon\Carbon;
66
use App\Models\User;
7+
use function Pest\Laravel\get;
78
use App\Events\UserBannedEvent;
89
use App\Events\UserUnbannedEvent;
910
use Spatie\Permission\Models\Role;
1011
use Illuminate\Support\Facades\Event;
11-
use Illuminate\Support\Facades\Queue;
1212
use App\Filament\Resources\UserResource;
13-
use Illuminate\Support\Facades\Notification;
14-
use App\Filament\Resources\UserResource\Pages\ListUsers;
15-
1613

1714
beforeEach(function (): void {
1815
Event::fake();
19-
Notification::fake();
20-
Queue::fake();
21-
$this->user = $this->login();
16+
$this->user = User::factory(['email' => 'user@laravel.cm'])->create();
17+
Role::create(['name' => 'admin']);
18+
$this->user->assignRole(['admin']);
19+
$this->actingAs($this->user, 'web');
2220
});
2321

2422
describe(UserResource::class, function() {
25-
it('only admin can ban a user and send a ban notification', function () {
23+
it('can render admin page', function (): void {
24+
get(UserResource::getUrl())->assertSuccessful();
25+
});
2626

27-
Role::create(['name' => 'user']);
28-
$admin = $this->user->assignRole('user');
27+
it('only admin can ban a user and send a ban notification', function () {
28+
$this->get('/cp')->assertSuccessful();
2929

3030
$user = User::factory()->create();
3131

32-
// $this->actingAs($admin);
33-
3432
UserResource::BanUserAction($user, 'Violation des règles de la communauté');
3533

3634
$user->refresh();
@@ -42,16 +40,13 @@
4240
});
4341

4442
it('can unban a user and send a unban notification', function () {
45-
Role::create(['name' => 'admin']);
46-
$admin = $this->user->assignRole('admin');
47-
43+
$this->get('/cp')->assertSuccessful();
44+
4845
$user = User::factory()->create([
4946
'banned_at' => now(),
5047
'banned_reason' => 'Violation des règles de la communauté'
5148
]);
5249

53-
$this->actingAs($admin);
54-
5550
UserResource::UnbanUserAction($user);
5651

5752
$user->refresh();
@@ -62,6 +57,17 @@
6257
Event::assertDispatched(UserUnbannedEvent::class);
6358
});
6459

60+
it('does not ban an already banned user', function () {
61+
$this->get('/cp')->assertSuccessful();
62+
63+
$user = User::factory()->create(['banned_at' => now()]);
64+
65+
UserResource::BanUserAction($user, 'Violation des règles');
66+
67+
expect($user->banned_reason)->not->toBe('Violation des règles')
68+
->and($user->banned_at)->not->toBeNull();
69+
});
70+
6571
it('prevents a banned user from logging in', function () {
6672
$user = User::factory()->create([
6773
'banned_at' => now(),
@@ -72,4 +78,4 @@
7278
->assertRedirect(route('login'))
7379
->assertSessionHasErrors(['email']);
7480
});
75-
});
81+
})->group('users');

tests/Feature/Livewire/Pages/Forum/IndexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
->assertViewHas('threads', fn ($threads) => count($threads) === 30)
2525
->assertSee(__('pagination.next'))
2626
->assertStatus(200);
27-
});
27+
});

0 commit comments

Comments
 (0)