anyone?
Laravel testing with Auth::once() permanently authenticates user
Hello,
does anyone have experience with testing methods where they use Auth::once()? I have the following problem.
When I make a request in a test to a resource that makes use of Auth::once() and the authentication is successful, it seems the test context stores the authenticated state of the user permanently. This means in subsequent calls, assertions such as assertGuest() are always false. In the actual app, Auth::once() obviously only authenticates the user for the current request. The test context does not seem to honour that the authentication should only be valid for a single request.
I can get around the storage of auth status by manually using auth()->logout() in the test after the HTTP request to the resource but that is obviously not ideal since that is what the application does and it should not be required as part of the test.
A specific situation where this is a problem: I want to authenticate the user once to check on some user database fields and then continue as guest (e.g. for two factor authentication). In combination with the guest middleware to only access the two factor page as guest, this works in the real application but since the user stays authenticated after the call to Auth::once(), the two factor page is not reachable since the test context thinks the user is already fully authenticated.
Is there a way I can call some method to refresh the authentication status within a test or something similar?
Thanks for any help.
Please or to participate in this conversation.