eronn
eronn

Reputation: 1830

Symfony Panther authentication seems not to be taken into account

I am using Symfony Panther for some testing.

When I want to log a User like this :

protected function setUpPanther()
{
    $client = static::createPantherClient(['readinessPath' => '/error']);
    $client->manage()->window()->maximize();
    $this->client = $client;
}

protected function loginPantherClient(Client $client, User $user)
{
    $client->request('GET', '/error');

    $session = self::$container->get('session');

    $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
    $session->set('_security_main', serialize($token));
    $session->save();

    $cookie = new Cookie($session->getName(), $session->getId());

    $client->getCookieJar()->set($cookie);
}

public function testUpdateProductDetails()
{
    $this->setUpPanther();
    
    $admin = $this->getSuperAdminAccount();

    $this->loginPantherClient($this->client, $admin);
    $this->client->request('GET', '/');
    $this->assertSelectorExists('div.form-group');
}

A strange phenomenon occurs.

  1. My client goes to the error page to initialize the cookie (required)

  2. And here I have the impression that the authentication of my client is not taken into account, because if you look closely, in my test, I then make a request on

    $this->client->request('GET', '/');

But instead of redirecting me to the requested page, it redirects me to / login, as if during the request, the user was not authenticated, and therefore, was redirected to the login page, while in my code, I authenticate the user before

Has someone already had this problem ?

Upvotes: 1

Views: 1805

Answers (1)

Manzolo
Manzolo

Reputation: 1959

Try to return client from loginPantherClient function:

protected function loginPantherClient(Client $client, User $user)
{
    $client->request('GET', '/error');

    $session = self::$container->get('session');

    $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
    $session->set('_security_main', serialize($token));
    $session->save();

    $cookie = new Cookie($session->getName(), $session->getId());

    $client->getCookieJar()->set($cookie);
    return $client;
}

public function testUpdateProductDetails()
{
    $this->setUpPanther();
    
    $admin = $this->getSuperAdminAccount();

    $this->client = $this->loginPantherClient($this->client, $admin);
    $this->client->request('GET', '/');
    $this->assertSelectorExists('div.form-group');
}

or remove from loginPantherClient function $client argument:

protected function loginPantherClient(User $user)
{
    $this->client->request('GET', '/error');

    $session = self::$container->get('session');

    $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
    $session->set('_security_main', serialize($token));
    $session->save();

    $cookie = new Cookie($session->getName(), $session->getId());

    $this->client->getCookieJar()->set($cookie);
    
}

public function testUpdateProductDetails()
{
    $this->setUpPanther();

    $admin = $this->getSuperAdminAccount();

    $this->loginPantherClient($admin);
    $this->client->request('GET', '/');
    $this->assertSelectorExists('div.form-group');
}

Upvotes: 1

Related Questions