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

[5.7] Fix actingAs #25873

Merged
merged 1 commit into from
Oct 2, 2018
Merged

[5.7] Fix actingAs #25873

merged 1 commit into from
Oct 2, 2018

Conversation

laurencei
Copy link
Contributor

@laurencei laurencei commented Oct 2, 2018

This fixes #25868 (and duplicates #21107, #25775 and #23270)

So fresh install of Laravel 5.7:

Route::get('/test', function () {
    return Auth::user();
});

public function testBasicTest()
{
    $user = factory(\App\User::class)->create();
    $response = $this->actingAs($user)->get('/test');
    $response->assertOk();
}

This test fails - because Response status code [201] does not match expected 200 status code.

This is a bug.

The $user was created before the start of the reqeust. i.e. we did not create a new user and return it inside of /test. The creation of $user is just to setup the requirements of the test.

The point of 201 is the request was successful and you created a resource. But in the test above - I did not create a resource inside of that request.

This PR ensures that anyone pre-authenticated (using actingAs() or be()) is never marked as newly created - since that is logically impossible (you cant be pre-authenticated against a resource that doesnt exist before the request even begins).

In other words - you must already have a preexisting user to be/act as them

@driesvints
Copy link
Member

@laurencei might be worth to send this to master for 5.8 because I think this will probably be a breaking change for some people.

@laurencei
Copy link
Contributor Author

laurencei commented Oct 2, 2018

Whilst I dont disagree it might break - it would only break test cases, which are actually returning incorrect results though? i.e. this is a bug fix of an incorrect value.

If I had a test that I wanted explicitly to get a 201, and after this patch I start getting a 200 on my test - is it correct to hide the error until 5.8 is released? Because the bug is hiding what would be returned in production.

Production returns would be unchanged obviously.

I'll leave it and if Taylor says I can change it - happy either way.

@driesvints
Copy link
Member

@laurencei fair enough :)

@mfn
Copy link
Contributor

mfn commented Oct 2, 2018

Wish the commit would contain the great PR description 😂

@taylorotwell taylorotwell merged commit b65cf42 into laravel:5.7 Oct 2, 2018
@@ -29,6 +29,10 @@ public function actingAs(UserContract $user, $driver = null)
*/
public function be(UserContract $user, $driver = null)
{
if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the same as this?

if ($user->wasRecentlyCreated ?? false) { }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably... 😄

@devcircus
Copy link
Contributor

This happens across the board with testing for any model. Doesn't make sense to just patch this one case. It would've been best to just let the developer account for this, or revert the magic that causes it.

@laurencei
Copy link
Contributor Author

laurencei commented Oct 4, 2018

This happens across the board with testing for any model.

@devcircus - No it doesnt. It only occurs for an auth user - because you directly pass the newly created model into the actingAs and basically insert it into the framework directly (bypassing the normal request cycle).

Any other models created prior to the request are retrieved during the request, and therefore are not wasRecentlyCreated

That's why this was a specific target.

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

Successfully merging this pull request may close these issues.

6 participants