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

"actingAs" helper is broken in unit tests #22808

Closed
timoschwarzer opened this issue Jan 15, 2018 · 7 comments
Closed

"actingAs" helper is broken in unit tests #22808

timoschwarzer opened this issue Jan 15, 2018 · 7 comments

Comments

@timoschwarzer
Copy link
Contributor

  • Laravel Version: 5.5.29
  • PHP Version: 7.0, 7.1, 7.2
  • Database Driver & Version:

Description:

In unit tests, the user is reset to null even after using the actingAs method when performing a simulated request with the get method.
See: #22649

@abler98
Copy link
Contributor

abler98 commented Jan 15, 2018

I think we need two properties for the setUser and request instance. But this may be cause new issues. Perhaps we should revert my changes.

@timoschwarzer
Copy link
Contributor Author

Yes, this commit should be reverted soon as it likely breaks many unit tests.

@crynobone
Copy link
Member

Could someone create a failing tests for this (Laravel does have Integration tests) to mimick the issues. Would really benefit to see when and why it broken. I'm using actingAs() without issue on the latest stable.

@abler98
Copy link
Contributor

abler98 commented Jan 16, 2018

@crynobone default guards are not using RequestGuard. Those who use the Passport (or other guards based on RequestGuard) have this issue.

When sending an internal request from the tests, Laravel creates HttpKernel instance and send request to it:

trait MakesHttpRequests
{
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
    {
        $kernel = $this->app->make(HttpKernel::class);

        ...

        // This invoke rebind `request` in app container and RequestGuard set user to null
        $response = $kernel->handle(
            $request = Request::createFromBase($symfonyRequest)
        );

        ...

        return $this->createTestResponse($response);
    }

@crynobone
Copy link
Member

crynobone commented Jan 16, 2018

So wouldn't it make more sense to try to fix actingAs() to support this scenario since it doesn't change actual app behaviour, correct?

@abler98
Copy link
Contributor

abler98 commented Jan 16, 2018

@crynobone in this case, we need to come up with a new way to authorize in tests. I need to write tests and have to come up with something to solve this problem.
P.S. Sorry for my bad English

@crynobone
Copy link
Member

I already writen a testcase for actingAs, and bases on Auth::viaRequest() test which uses RequestGuard I still was unable to replicate above claim.

Without proper tests that cover above scenarios anyone might redo the same mistake of sending similar PR and it still going to return green.

To bad if you guys don't see the benefits of adding test to prevent regression bug.

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