mstdmstd
mstdmstd

Reputation: 3071

How making tests to refer to middleware checks?

In laravel 11 / livewire app I make check if user has field status active

like having middleware CheckAuthPermissionsMiddleware.php in bootstrap/app.php file and condition in this middleware :

    if (auth()->user()->status !== UserStatusEnum::ACTIVE) {
        Auth::logout();

        return redirect()->route('login')->with('status', 'Please, check if your account is active.');
    }

in web.php file there is rule :

Route::prefix('admin')->name('admin.')->middleware(['checkAuthPermissions'])->group(function () { Route::get('dashboard', Dashboard::Class)->name('dashboard'); ...

That works ok in the browser, but reading docs at https://livewire.laravel.com/docs/testing when I make tests with pestphp/pest: 3.7

#[Test]
public function check_admin_user_has_access_to_admin_dashboard()
{

    $user = User::factory()->create();
    $adminPermission = Permission::findByName(PERMISSION_APP_ADMIN_LABEL);
givePermissionTo($adminPermission);-->
    Livewire::actingAs($user)->test(Dashboard::class)
        ->assertSeeText('Dashboard: ' . app(AppGeneralSettings::class)->site_name)
        ->assertSeeText('Content of the site')
        ->assertStatus(200);

Leaving log lines in the middleware I see it is not logged in my case so no checks are run when givePermissionTo is commented as above.

How have I to remake my app/tests to run tests in the middleware ?

Upvotes: 1

Views: 13

Answers (0)

Related Questions