Skip to content

[Testing] KernelBrowser::loginUser not working with subsequent requests #42850

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

Closed
crtl opened this issue Sep 2, 2021 · 3 comments
Closed

[Testing] KernelBrowser::loginUser not working with subsequent requests #42850

crtl opened this issue Sep 2, 2021 · 3 comments

Comments

@crtl
Copy link
Contributor

crtl commented Sep 2, 2021

Symfony version(s) affected: 5.3

Description
Using $client->loginUser in KernelTestCase when sending multiple requests will only work for a single request and return 401/Unauthorized for all subsequent requests.

// Helper to login user
    public function loginUser(KernelBrowser $client): KernelBrowser
    {

        /** @var UserRepositoryInterface $repository $repository */
        $repository = static::getContainer()->get(UserRepositoryInterface::class);

        // Get first user
        $user = $repository->find(1);

        $client->loginUser($user, static::$firewallContext);

        return $client;
    }

// Test case

    public function testShouldReturn404() {
        $this->loginUser($this->client);

        $urls = [
            "/api/v1/endpoint",
            "/api/v1/endpoint",
        ];

        $this->client->request("POST", $urls[0]);
        // This assertion succeeds
        $this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);

        $this->client->request("POST", $urls[1]);
        // This assertion fails
        $this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
    }

I have also tried calling loginUser again but it returns the same result

How to reproduce

  1. Create firewall protected endpoint
  2. Send multiple requests to same endpoint with authenticated KernelBrowser
@crtl crtl added the Bug label Sep 2, 2021
@crtl crtl changed the title [Testing] Client::loginUser not working with subsequent requests [Testing] KernelBrowser::loginUser not working with subsequent requests Sep 2, 2021
@xabbuh
Copy link
Member

xabbuh commented Sep 10, 2021

Can you create a small example application that allows to reproduce your issue?

@xabbuh
Copy link
Member

xabbuh commented Sep 21, 2021

I am going to close here for now due to the lack of feedback. Please let us know when you have more information and we can consider to reopen.

@xabbuh xabbuh closed this as completed Sep 21, 2021
@adrienfr
Copy link
Contributor

adrienfr commented Sep 29, 2021

Hi,

I have the same issue since 5.3.8 release and this change.

I created a small reproducer here.

When calling loginUser multiple times within the same test, user doesn't change in the app:

public function testIndex()
    $user1 = new User();
    $user1->setFirstname('Toto');
    $user1->setLastname('XX');
    $user1->setEmail('toto@toto.com');
    $user1->setPassword('...');
    
    $user2 = new User();
    $user2->setFirstname('Tata');
    $user2->setLastname('XX');
    $user2->setEmail('tata@tata.com');
    $user2->setPassword('...');
    
    $em->persist($user1);
    $em->persist($user2);
    $em->flush();
    
    $client->loginUser($user1);
    $crawler = $client->request('GET', '/index');
    $this->assertResponseIsSuccessful();
    $this->assertSame('Hello Toto!', $crawler->filter('h1')->text(null, true));
    
    $client->loginUser($user2);
    $crawler = $client->request('GET', '/index');
    $this->assertResponseIsSuccessful();
    $this->assertSame('Hello Tata!', $crawler->filter('h1')->text(null, true));
}

Do I need to reset something between each loginUser call?

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

No branches or pull requests

4 participants