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:
commit
2240ecfa14
@ -12,6 +12,13 @@
|
||||
namespace Symfony\Component\Security\Http\Tests\Firewall;
|
||||
|
||||
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\Firewall\SwitchUserListener;
|
||||
use Symfony\Component\Security\Http\SecurityEvents;
|
||||
@ -32,14 +39,12 @@ class SwitchUserListenerTest extends TestCase
|
||||
|
||||
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->userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock();
|
||||
$this->accessDecisionManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock();
|
||||
$this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
|
||||
$this->request->query = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')->getMock();
|
||||
$this->request->server = $this->getMockBuilder('Symfony\Component\HttpFoundation\ServerBag')->getMock();
|
||||
$this->event = $this->getEvent($this->request);
|
||||
$this->request = new Request();
|
||||
$this->event = new GetResponseEvent($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $this->request, HttpKernelInterface::MASTER_REQUEST);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -53,13 +58,11 @@ class SwitchUserListenerTest extends TestCase
|
||||
|
||||
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->handle($this->event);
|
||||
|
||||
$this->assertNull($this->event->getResponse());
|
||||
$this->assertNull($this->tokenStorage->getToken());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,10 +70,10 @@ class SwitchUserListenerTest extends TestCase
|
||||
*/
|
||||
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->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('_exit'));
|
||||
$this->tokenStorage->setToken($token);
|
||||
$this->request->query->set('_switch_user', '_exit');
|
||||
|
||||
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
|
||||
$listener->handle($this->event);
|
||||
@ -78,29 +81,19 @@ class SwitchUserListenerTest extends TestCase
|
||||
|
||||
public function testExitUserUpdatesToken()
|
||||
{
|
||||
$originalToken = $this->getToken();
|
||||
$role = $this->getMockBuilder('Symfony\Component\Security\Core\Role\SwitchUserRole')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$role->expects($this->any())->method('getSource')->will($this->returnValue($originalToken));
|
||||
$originalToken = new UsernamePasswordToken('username', '', 'key', array());
|
||||
$this->tokenStorage->setToken(new UsernamePasswordToken('username', '', 'key', array(new SwitchUserRole('ROLE_PREVIOUS', $originalToken))));
|
||||
|
||||
$this->tokenStorage->expects($this->any())
|
||||
->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'));
|
||||
$this->request->query->set('_switch_user', '_exit');
|
||||
|
||||
$listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager);
|
||||
$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()
|
||||
@ -113,38 +106,9 @@ class SwitchUserListenerTest extends TestCase
|
||||
->method('refreshUser')
|
||||
->with($originalUser)
|
||||
->willReturn($refreshedUser);
|
||||
$originalToken = $this->getToken();
|
||||
$originalToken
|
||||
->expects($this->any())
|
||||
->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()));
|
||||
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
|
||||
$this->tokenStorage->setToken(new UsernamePasswordToken('username', '', 'key', array(new SwitchUserRole('ROLE_PREVIOUS', $originalToken))));
|
||||
$this->request->query->set('_switch_user', '_exit');
|
||||
|
||||
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
|
||||
$dispatcher
|
||||
@ -166,41 +130,9 @@ class SwitchUserListenerTest extends TestCase
|
||||
->userProvider
|
||||
->expects($this->never())
|
||||
->method('refreshUser');
|
||||
$originalToken = $this->getToken();
|
||||
$originalToken
|
||||
->expects($this->any())
|
||||
->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('/');
|
||||
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
|
||||
$this->tokenStorage->setToken(new UsernamePasswordToken('username', '', 'key', array(new SwitchUserRole('ROLE_PREVIOUS', $originalToken))));
|
||||
$this->request->query->set('_switch_user', '_exit');
|
||||
|
||||
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
|
||||
$dispatcher
|
||||
@ -217,10 +149,10 @@ class SwitchUserListenerTest extends TestCase
|
||||
*/
|
||||
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->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('kuba'));
|
||||
$this->tokenStorage->setToken($token);
|
||||
$this->request->query->set('_switch_user', 'kuba');
|
||||
|
||||
$this->accessDecisionManager->expects($this->once())
|
||||
->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH'))
|
||||
@ -232,17 +164,11 @@ class SwitchUserListenerTest extends TestCase
|
||||
|
||||
public function testSwitchUser()
|
||||
{
|
||||
$token = $this->getToken(array($this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleInterface')->getMock()));
|
||||
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
|
||||
$user->expects($this->any())->method('getRoles')->will($this->returnValue(array()));
|
||||
$token = new UsernamePasswordToken('username', '', 'key', array('ROLE_FOO'));
|
||||
$user = new User('username', 'password', array());
|
||||
|
||||
$this->tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue($token));
|
||||
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('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->tokenStorage->setToken($token);
|
||||
$this->request->query->set('_switch_user', 'kuba');
|
||||
|
||||
$this->accessDecisionManager->expects($this->once())
|
||||
->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH'))
|
||||
@ -253,25 +179,26 @@ class SwitchUserListenerTest extends TestCase
|
||||
->will($this->returnValue($user));
|
||||
$this->userChecker->expects($this->once())
|
||||
->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->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()
|
||||
{
|
||||
$token = $this->getToken(array($this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleInterface')->getMock()));
|
||||
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
|
||||
$user->expects($this->any())->method('getRoles')->will($this->returnValue(array()));
|
||||
$token = new UsernamePasswordToken('username', '', 'key', array('ROLE_FOO'));
|
||||
$user = new User('username', 'password', array());
|
||||
|
||||
$this->tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue($token));
|
||||
$this->request->expects($this->any())->method('get')->with('_switch_user')->will($this->returnValue('kuba'));
|
||||
$this->request->query->expects($this->once())->method('remove', '_switch_user');
|
||||
$this->request->query->expects($this->any())->method('all')->will($this->returnValue(array('page' => 3, 'section' => 2)));
|
||||
$this->request->expects($this->any())->method('getUri')->will($this->returnValue('/'));
|
||||
$this->request->server->expects($this->once())->method('set')->with('QUERY_STRING', 'page=3§ion=2');
|
||||
$this->tokenStorage->setToken($token);
|
||||
$this->request->query->replace(array(
|
||||
'_switch_user' => 'kuba',
|
||||
'page' => 3,
|
||||
'section' => 2,
|
||||
));
|
||||
|
||||
$this->accessDecisionManager->expects($this->once())
|
||||
->method('decide')->with($token, array('ROLE_ALLOWED_TO_SWITCH'))
|
||||
@ -282,33 +209,11 @@ class SwitchUserListenerTest extends TestCase
|
||||
->will($this->returnValue($user));
|
||||
$this->userChecker->expects($this->once())
|
||||
->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->handle($this->event);
|
||||
}
|
||||
|
||||
private function getEvent($request)
|
||||
{
|
||||
$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;
|
||||
$this->assertSame('page=3§ion=2', $this->request->server->get('QUERY_STRING'));
|
||||
$this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $this->tokenStorage->getToken());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user