Symfony 2 Functional Test: Authenticate a User of a User's Own Class

As described in the answer How do I use an authenticated user in a symfony2 functional test? There is a simple solution with Symfony\Component\Security\Core\User\User .

But I have another User class (some required additional fields), and I want to authenticate the user with it.

How to configure options for this?

+4
source share
4 answers

This is the tricky issue discussed here: https://github.com/symfony/symfony/issues/5228 Although it's 2.1, I still happen to 2.2.

This is how I perform a test check:

 // Create a new client to browse the application $client = static::createClient(); $client->getCookieJar()->set(new Cookie(session_name(), true)); // dummy call to bypass the hasPreviousSession check $crawler = $client->request('GET', '/'); $em = $client->getContainer()->get('doctrine')->getEntityManager(); $user = $em->getRepository('MyOwnBundle:User')->findOneByUsername('username'); $token = new UsernamePasswordToken($user, $user->getPassword(), 'main_firewall', $user->getRoles()); self::$kernel->getContainer()->get('security.context')->setToken($token); $session = $client->getContainer()->get('session'); $session->set('_security_' . 'main_firewall', serialize($token)); $session->save(); $crawler = $client->request('GET', '/login/required/page/'); $this->assertTrue(200 === $client->getResponse()->getStatusCode()); // perform tests in the /login/required/page here.. 

Oh, and usage statements:

 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Bundle\FrameworkBundle\Client; use Symfony\Component\BrowserKit\Cookie; 
+11
source

Do you use the registration form? or http security?

when using the login form, which I do in my tests, I just mimic the user logging in via the login form ...

  /** * test of superuser ingelogd geraakt */ public function testSuperAdminLogin() { $crawler = $this->client->request('GET', '/login'); $form = $crawler->selectButton('Sign In')->form(); $user = $this->em->getRepository('NonoAcademyBundle:User') ->findOneByUsername('superadmin'); $crawler = $this->client ->submit($form, array('_username' => $user->getUsername(), '_password' => $user->getPassword())); $this->assertTrue($this->client->getResponse()->isSuccessful()); $this ->assertRegExp('/\/admin\/notifications/', $this->client->getResponse()->getContent()); } 

just use this client and crawler, as they will act as a registered user. Hope this helps you.

+1
source

I have found a solution.

First we need to create a new user: FakeUserProvider , as described here .
It should implement UserProviderInterface .

Its loadUserByUsername should create the necessary user object.

0
source

You may also find them useful, especially if you use the registration form.

 private function doLogin() { $this->client = static::createClient(); $username = 'your-username'; $password = 'your-password'; $crawler = $this->client->request('GET', '/login'); $form = $crawler->filter('your-submit-button-classname')->form(); $crawler = $this->client ->submit($form, array( '_username' => $username, '_password' => $password, ) ); } 
0
source

Source: https://habr.com/ru/post/1480650/


All Articles