From 351b5f69aa5936fa0eb6aaf7043d5cef10c92ec6 Mon Sep 17 00:00:00 2001 From: Anthony Rey Date: Fri, 12 Jan 2018 18:23:11 +1100 Subject: [PATCH 1/2] doc(testing/http_authentication.rst); - [x] Added notes for people using Guard with the PostAuthenticationGuardToken --- testing/http_authentication.rst | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/testing/http_authentication.rst b/testing/http_authentication.rst index ff1f0d49a90..0fe7b0a7c81 100644 --- a/testing/http_authentication.rst +++ b/testing/http_authentication.rst @@ -124,3 +124,64 @@ needs:: $this->client->getCookieJar()->set($cookie); } } + +Note: Are you using Guard? You may adjust the token depending on your application needs. +For example, if you are using Guard for authentication, you would use the `PostAuthenticationGuardToken`: + + // tests/Controller/DefaultControllerTest.php + namespace App\Tests\Controller; + + use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; + use Symfony\Component\BrowserKit\Cookie; + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken; + + class DefaultControllerTest extends WebTestCase + { + private $client = null; + + private $user = null; + + public function setUp() + { + $this->client = static::createClient(); + $this->user = $this->createUser() + } + + private function createUser() { + + // create your user, save it and return it + // ... + + return $user; + } + + public function testSecuredHello() + { + $this->logInAsUser($user); + $crawler = $this->client->request('GET', '/admin'); + + $this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode()); + $this->assertSame('Admin Dashboard', $crawler->filter('h1')->text()); + } + + + protected function logInAsUser(UserInterface $user) + { + $session = $this->client->getContainer()->get('session'); + + // the firewall context defaults to the firewall name + $firewallContext = 'secured_area'; + $token = new PostAuthenticationGuardToken( + $user, + $firewallContext, + array('ROLE_ADMIN') + ); + $session->set('_security_'.$firewallContext, serialize($token)); + $session->save(); + + $cookie = new Cookie($session->getName(), $session->getId()); + $this->client->getCookieJar()->set($cookie); + } + + From 525bbbdba5a6ca4b0af2ec75b640052309e15634 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 24 Jul 2018 11:21:11 +0200 Subject: [PATCH 2/2] Simplify --- testing/http_authentication.rst | 63 ++------------------------------- 1 file changed, 2 insertions(+), 61 deletions(-) diff --git a/testing/http_authentication.rst b/testing/http_authentication.rst index 0fe7b0a7c81..c6abf4d6a8d 100644 --- a/testing/http_authentication.rst +++ b/testing/http_authentication.rst @@ -116,6 +116,8 @@ needs:: // the firewall context defaults to the firewall name $firewallContext = 'secured_area'; + // you may need to use a different token class depending on your application. + // for example, when using Guard authentication you must instantiate PostAuthenticationGuardToken $token = new UsernamePasswordToken('admin', null, $firewallContext, array('ROLE_ADMIN')); $session->set('_security_'.$firewallContext, serialize($token)); $session->save(); @@ -124,64 +126,3 @@ needs:: $this->client->getCookieJar()->set($cookie); } } - -Note: Are you using Guard? You may adjust the token depending on your application needs. -For example, if you are using Guard for authentication, you would use the `PostAuthenticationGuardToken`: - - // tests/Controller/DefaultControllerTest.php - namespace App\Tests\Controller; - - use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; - use Symfony\Component\BrowserKit\Cookie; - use Symfony\Component\HttpFoundation\Response; - use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken; - - class DefaultControllerTest extends WebTestCase - { - private $client = null; - - private $user = null; - - public function setUp() - { - $this->client = static::createClient(); - $this->user = $this->createUser() - } - - private function createUser() { - - // create your user, save it and return it - // ... - - return $user; - } - - public function testSecuredHello() - { - $this->logInAsUser($user); - $crawler = $this->client->request('GET', '/admin'); - - $this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode()); - $this->assertSame('Admin Dashboard', $crawler->filter('h1')->text()); - } - - - protected function logInAsUser(UserInterface $user) - { - $session = $this->client->getContainer()->get('session'); - - // the firewall context defaults to the firewall name - $firewallContext = 'secured_area'; - $token = new PostAuthenticationGuardToken( - $user, - $firewallContext, - array('ROLE_ADMIN') - ); - $session->set('_security_'.$firewallContext, serialize($token)); - $session->save(); - - $cookie = new Cookie($session->getName(), $session->getId()); - $this->client->getCookieJar()->set($cookie); - } - -