robsontenorio / laravel-keycloak-guard

🔑 Simple Keycloak Guard for Laravel
MIT License
419 stars 137 forks source link

Using actingAsKeycloakUser() with different "user" in the same test #110

Closed vlauciani closed 2 months ago

vlauciani commented 6 months ago

Hi Mr. @tuytoosh

I'm starting to use the new actingAsKeycloakUser() method for test. I'm not using a local DataBase with users and I'd like to ask you if it is possibile to "change" the user during the test. For example:

// keycloak.php
'preferred_username' => 'php-tests-user-writer'
// Test Class
public function test_my_method_with_writer()
{
    // more code here

    $this->actingAsKeycloakUser()->postJson(route('v1.myroute'), $data);

    // . . .
}

public function test_my_method_with_editor()
{
    // more code here

    config(['keycloak.preferred_username' => 'php-tests-user-editor']);
    $this->actingAsKeycloakUser()->patchJson(route('v1.myroute'), $data);

    // . . .
}

The problem is that in the method test_my_method_with_editor() uses the user php-tests-user-writer because it is already authenticated . I tried to implement a logout() method:

class KeycloakGuard implements Guard
{
    // . . .
    public function logout()
    {
        $this->user = null;
    }
    // . . .
}

and re-try with:

public function test_my_method_with_editor()
{
    // more code here

    auth()->logout();
    config(['keycloak.preferred_username' => 'php-tests-user-editor']);
    $this->actingAsKeycloakUser()->patchJson(route('v1.myroute'), $data);

    // . . .
}

but the user doesn't change; could you help me?

Thank you.

RaimoHaikari commented 5 months ago

Hi

Could you provide an example of your test class?

I am trying to use actingAsKeycloakUser() in my Feature test, but when I try to run the test, I get an error:

Call to undefined method Tests\Feature\SeuraUpdateTest::actingAsKeycloakUser()

robsontenorio commented 4 months ago

Check the new 1.6.1 release. Maybe @elnurvl can help with this.