minor #22049 [Security] simplify the SwitchUserListenerTest (xabbuh)

This PR was merged into the 2.7 branch.

Discussion
----------

[Security] simplify the SwitchUserListenerTest

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

While working on #22048 I noticed that the `SwitchUserListenerTest` was more complicated than necessary by mocking a lot of stuff that didn't need to be mocked.

Commits
-------

923bbdbf9f [Security] simplify the SwitchUserListenerTest
This commit is contained in:
Fabien Potencier 2017-03-22 13:38:16 -07:00
commit 2240ecfa14

View File

@ -12,6 +12,13 @@
namespace Symfony\Component\Security\Http\Tests\Firewall; namespace Symfony\Component\Security\Http\Tests\Firewall;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Http\Event\SwitchUserEvent; use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Component\Security\Http\Firewall\SwitchUserListener; use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
use Symfony\Component\Security\Http\SecurityEvents; use Symfony\Component\Security\Http\SecurityEvents;
@ -32,14 +39,12 @@ class SwitchUserListenerTest extends TestCase
protected function setUp() protected function setUp()
{ {
$this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $this->tokenStorage = new TokenStorage();
$this->userProvider = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock(); $this->userProvider = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock();
$this->userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock(); $this->userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock();
$this->accessDecisionManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock(); $this->accessDecisionManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock();
$this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock(); $this->request = new Request();
$this->request->query = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')->getMock(); $this->event = new GetResponseEvent($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $this->request, HttpKernelInterface::MASTER_REQUEST);
$this->request->server = $this->getMockBuilder('Symfony\Component\HttpFoundation\ServerBag')->getMock();
$this->event = $this->getEvent($this->request);
} }
/** /**
@ -53,13 +58,11 @@ class SwitchUserListenerTest extends TestCase
public function testEventIsIgnoredIfUsernameIsNotPassedWithTheRequest() public function testEventIsIgnoredIfUsernameIsNotPassedWithTheRequest()
{ {
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue(null));
$this->event->expects($this->never())->method('setResponse');
$this->tokenStorage->expects($this->never())->method('setToken');
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager); $listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event); $listener->handle($this->event);
$this->assertNull($this->event->getResponse());
$this->assertNull($this->tokenStorage->getToken());
} }
/** /**
@ -67,10 +70,10 @@ class SwitchUserListenerTest extends TestCase
*/ */
public function testExitUserThrowsAuthenticationExceptionIfOriginalTokenCannotBeFound() public function testExitUserThrowsAuthenticationExceptionIfOriginalTokenCannotBeFound()
{ {
$token = $this->getToken(array($this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleInterface')->getMock())); $token = new UsernamePasswordToken('username', '', 'key', array('ROLE_FOO'));
$this->tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue($token)); $this->tokenStorage->setToken($token);
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('_exit')); $this->request->query->set('_switch_user', '_exit');
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager); $listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event); $listener->handle($this->event);
@ -78,29 +81,19 @@ class SwitchUserListenerTest extends TestCase
public function testExitUserUpdatesToken() public function testExitUserUpdatesToken()
{ {
$originalToken = $this->getToken(); $originalToken = new UsernamePasswordToken('username', '', 'key', array());
$role = $this->getMockBuilder('Symfony\Component\Security\Core\Role\SwitchUserRole') $this->tokenStorage->setToken(new UsernamePasswordToken('username', '', 'key', array(new SwitchUserRole('ROLE_PREVIOUS', $originalToken))));
->disableOriginalConstructor()
->getMock();
$role->expects($this->any())->method('getSource')->will($this->returnValue($originalToken));
$this->tokenStorage->expects($this->any()) $this->request->query->set('_switch_user', '_exit');
->method('getToken')
->will($this->returnValue($this->getToken(array($role))));
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('_exit'));
$this->request->expects($this->any())->method('getUri')->will($this->returnValue('/'));
$this->request->query->expects($this->once())->method('remove', '_switch_user');
$this->request->query->expects($this->any())->method('all')->will($this->returnValue(array()));
$this->request->server->expects($this->once())->method('set')->with('QUERY_STRING', '');
$this->tokenStorage->expects($this->once())
->method('setToken')->with($originalToken);
$this->event->expects($this->once())
->method('setResponse')->with($this->isInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse'));
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager); $listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event); $listener->handle($this->event);
$this->assertSame(array(), $this->request->query->all());
$this->assertSame('', $this->request->server->get('QUERY_STRING'));
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $this->event->getResponse());
$this->assertSame($this->request->getUri(), $this->event->getResponse()->getTargetUrl());
$this->assertSame($originalToken, $this->tokenStorage->getToken());
} }
public function testExitUserDispatchesEventWithRefreshedUser() public function testExitUserDispatchesEventWithRefreshedUser()
@ -113,38 +106,9 @@ class SwitchUserListenerTest extends TestCase
->method('refreshUser') ->method('refreshUser')
->with($originalUser) ->with($originalUser)
->willReturn($refreshedUser); ->willReturn($refreshedUser);
$originalToken = $this->getToken(); $originalToken = new UsernamePasswordToken($originalUser, '', 'key');
$originalToken $this->tokenStorage->setToken(new UsernamePasswordToken('username', '', 'key', array(new SwitchUserRole('ROLE_PREVIOUS', $originalToken))));
->expects($this->any()) $this->request->query->set('_switch_user', '_exit');
->method('getUser')
->willReturn($originalUser);
$role = $this
->getMockBuilder('Symfony\Component\Security\Core\Role\SwitchUserRole')
->disableOriginalConstructor()
->getMock();
$role->expects($this->any())->method('getSource')->willReturn($originalToken);
$this
->tokenStorage
->expects($this->any())
->method('getToken')
->willReturn($this->getToken(array($role)));
$this
->request
->expects($this->any())
->method('get')
->with('_switch_user')
->willReturn('_exit');
$this
->request
->expects($this->any())
->method('getUri')
->willReturn('/');
$this
->request
->query
->expects($this->any())
->method('all')
->will($this->returnValue(array()));
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
$dispatcher $dispatcher
@ -166,41 +130,9 @@ class SwitchUserListenerTest extends TestCase
->userProvider ->userProvider
->expects($this->never()) ->expects($this->never())
->method('refreshUser'); ->method('refreshUser');
$originalToken = $this->getToken(); $originalToken = new UsernamePasswordToken($originalUser, '', 'key');
$originalToken $this->tokenStorage->setToken(new UsernamePasswordToken('username', '', 'key', array(new SwitchUserRole('ROLE_PREVIOUS', $originalToken))));
->expects($this->any()) $this->request->query->set('_switch_user', '_exit');
->method('getUser')
->willReturn($originalUser);
$role = $this
->getMockBuilder('Symfony\Component\Security\Core\Role\SwitchUserRole')
->disableOriginalConstructor()
->getMock();
$role
->expects($this->any())
->method('getSource')
->willReturn($originalToken);
$this
->tokenStorage
->expects($this->any())
->method('getToken')
->willReturn($this->getToken(array($role)));
$this
->request
->expects($this->any())
->method('get')
->with('_switch_user')
->willReturn('_exit');
$this
->request
->query
->expects($this->any())
->method('all')
->will($this->returnValue(array()));
$this
->request
->expects($this->any())
->method('getUri')
->willReturn('/');
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
$dispatcher $dispatcher
@ -217,10 +149,10 @@ class SwitchUserListenerTest extends TestCase
*/ */
public function testSwitchUserIsDisallowed() public function testSwitchUserIsDisallowed()
{ {
$token = $this->getToken(array($this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleInterface')->getMock())); $token = new UsernamePasswordToken('username', '', 'key', array('ROLE_FOO'));
$this->tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue($token)); $this->tokenStorage->setToken($token);
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('kuba')); $this->request->query->set('_switch_user', 'kuba');
$this->accessDecisionManager->expects($this->once()) $this->accessDecisionManager->expects($this->once())
->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH')) ->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH'))
@ -232,17 +164,11 @@ class SwitchUserListenerTest extends TestCase
public function testSwitchUser() public function testSwitchUser()
{ {
$token = $this->getToken(array($this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleInterface')->getMock())); $token = new UsernamePasswordToken('username', '', 'key', array('ROLE_FOO'));
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock(); $user = new User('username', 'password', array());
$user->expects($this->any())->method('getRoles')->will($this->returnValue(array()));
$this->tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue($token)); $this->tokenStorage->setToken($token);
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('kuba')); $this->request->query->set('_switch_user', 'kuba');
$this->request->query->expects($this->once())->method('remove', '_switch_user');
$this->request->query->expects($this->any())->method('all')->will($this->returnValue(array()));
$this->request->expects($this->any())->method('getUri')->will($this->returnValue('/'));
$this->request->server->expects($this->once())->method('set')->with('QUERY_STRING', '');
$this->accessDecisionManager->expects($this->once()) $this->accessDecisionManager->expects($this->once())
->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH')) ->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH'))
@ -253,25 +179,26 @@ class SwitchUserListenerTest extends TestCase
->will($this->returnValue($user)); ->will($this->returnValue($user));
$this->userChecker->expects($this->once()) $this->userChecker->expects($this->once())
->method('checkPostAuth')->with($user); ->method('checkPostAuth')->with($user);
$this->tokenStorage->expects($this->once())
->method('setToken')->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken'));
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager); $listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event); $listener->handle($this->event);
$this->assertSame(array(), $this->request->query->all());
$this->assertSame('', $this->request->server->get('QUERY_STRING'));
$this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $this->tokenStorage->getToken());
} }
public function testSwitchUserKeepsOtherQueryStringParameters() public function testSwitchUserKeepsOtherQueryStringParameters()
{ {
$token = $this->getToken(array($this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleInterface')->getMock())); $token = new UsernamePasswordToken('username', '', 'key', array('ROLE_FOO'));
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock(); $user = new User('username', 'password', array());
$user->expects($this->any())->method('getRoles')->will($this->returnValue(array()));
$this->tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue($token)); $this->tokenStorage->setToken($token);
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('kuba')); $this->request->query->replace(array(
$this->request->query->expects($this->once())->method('remove', '_switch_user'); '_switch_user' => 'kuba',
$this->request->query->expects($this->any())->method('all')->will($this->returnValue(array('page' => 3, 'section' => 2))); 'page' => 3,
$this->request->expects($this->any())->method('getUri')->will($this->returnValue('/')); 'section' => 2,
$this->request->server->expects($this->once())->method('set')->with('QUERY_STRING', 'page=3&section=2'); ));
$this->accessDecisionManager->expects($this->once()) $this->accessDecisionManager->expects($this->once())
->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH')) ->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH'))
@ -282,33 +209,11 @@ class SwitchUserListenerTest extends TestCase
->will($this->returnValue($user)); ->will($this->returnValue($user));
$this->userChecker->expects($this->once()) $this->userChecker->expects($this->once())
->method('checkPostAuth')->with($user); ->method('checkPostAuth')->with($user);
$this->tokenStorage->expects($this->once())
->method('setToken')->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken'));
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager); $listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
$listener->handle($this->event); $listener->handle($this->event);
}
private function getEvent($request) $this->assertSame('page=3&section=2', $this->request->server->get('QUERY_STRING'));
{ $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $this->tokenStorage->getToken());
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
->disableOriginalConstructor()
->getMock();
$event->expects($this->any())
->method('getRequest')
->will($this->returnValue($request));
return $event;
}
private function getToken(array $roles = array())
{
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
$token->expects($this->any())
->method('getRoles')
->will($this->returnValue($roles));
return $token;
} }
} }