Skip to content
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

Closed
danielebriggi opened this issue Sep 9, 2017 · 6 comments

Comments

@danielebriggi
Copy link

danielebriggi commented Sep 9, 2017

  • Laravel Version: 5.5.0
  • PHP Version: 7.1
  • Database Driver & Version: MySql 5.7.19

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 by Auht::user() the $wasRecentlyCreated is still set to true.

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:

  1. create an endpoint that accepts PATCH
  2. in the endpoint's controller
  • recall the user: Auth::user()
  • update some user's attributes and save
  • return the user's resource model with the updated user
  1. create a unit test
  2. factory an User model and authenticate it with Passport: Passport::actingAs(factory(User::class)->create());
  3. call the endpoint and assert the status is 200: $this->patchJson(...)->assertStatus(200);
  4. it's expected to be 200 instead is returned 201
@themsaid
Copy link
Member

Thanks but this isn't a bug, the user is new thus why this property is true.

@danielebriggi
Copy link
Author

Actually, the user has been created by the unit test.
The controller has just retrieved the user through the Auth facade. It means that, in the real case, the user is fetched from the database therefore it's not new, and this is what I'd expect it to be even in the unit test case.

@devcircus
Copy link
Contributor

Think this may deserve a second look.

@themsaid
Copy link
Member

What do you propose then?

@mateusjatenee
Copy link
Contributor

Perhaps calling fresh() on the actingAs method?

@danielebriggi
Copy link
Author

danielebriggi commented Sep 16, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants