-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User factored model for authentication persists wasRecentlyCreated to true when recalled by the endpoint's controller #21107
Comments
Thanks but this isn't a bug, the user is new thus why this property is true. |
Actually, the user has been created by the unit test. |
Think this may deserve a second look. |
What do you propose then? |
Perhaps calling |
It works by refreshing the user before passing it to the `actingAs` method. It was my solution.
… On 11 Sep 2017, at 23:46, Mateus Guimarães ***@***.***> wrote:
Perhaps calling fresh() on the actingAs method?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#21107 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AF3nDIkHOK1uP9yzfGDwP1F8AXv8E0glks5shaotgaJpZM4PSImY>.
|
Description:
Context: unit testing.
For unit testing API that requires the Passport authentication is suggested to factory an user instance and give it to Passport:
Passport::actingAs(factory(User::class)->create());
In my case I needed to test the
PATCH /users
API.I did as suggested but the response from the user resource's response returned by the enpoint's controller was 201 instead of 200.
The problem was related to the
Model::$wasRecentlyCreated
property (https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Model.html).When user is factored the property is set to
true
. When the unit test calls$this->patchJson(...)
endpoint and the enpoint's controller recalls the logged user byAuht::user()
the$wasRecentlyCreated
is still set totrue
.The way to fix the test is to
fresh()
the user model:Passport::actingAs($user->fresh())
.I'm not sure it's a bug but it's an unexpected behavior to me. I'd expected that model retrieved from, eg, the Auth facade, be in a fresh state just like the request is coming from a real request.
Steps To Reproduce:
Auth::user()
Passport::actingAs(factory(User::class)->create());
$this->patchJson(...)->assertStatus(200);
The text was updated successfully, but these errors were encountered: