AbstractRememberMeServicesTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Http\Tests\RememberMe;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Security\Http\RememberMe\AbstractRememberMeServices;
  15. use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
  16. class AbstractRememberMeServicesTest extends TestCase
  17. {
  18. public function testGetRememberMeParameter()
  19. {
  20. $service = $this->getService(null, array('remember_me_parameter' => 'foo'));
  21. $this->assertEquals('foo', $service->getRememberMeParameter());
  22. }
  23. public function testGetSecret()
  24. {
  25. $service = $this->getService();
  26. $this->assertEquals('foosecret', $service->getSecret());
  27. }
  28. public function testAutoLoginReturnsNullWhenNoCookie()
  29. {
  30. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
  31. $this->assertNull($service->autoLogin(new Request()));
  32. }
  33. /**
  34. * @expectedException \RuntimeException
  35. */
  36. public function testAutoLoginThrowsExceptionWhenImplementationDoesNotReturnUserInterface()
  37. {
  38. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
  39. $request = new Request();
  40. $request->cookies->set('foo', 'foo');
  41. $service
  42. ->expects($this->once())
  43. ->method('processAutoLoginCookie')
  44. ->will($this->returnValue(null))
  45. ;
  46. $service->autoLogin($request);
  47. }
  48. public function testAutoLogin()
  49. {
  50. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
  51. $request = new Request();
  52. $request->cookies->set('foo', 'foo');
  53. $user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
  54. $user
  55. ->expects($this->once())
  56. ->method('getRoles')
  57. ->will($this->returnValue(array()))
  58. ;
  59. $service
  60. ->expects($this->once())
  61. ->method('processAutoLoginCookie')
  62. ->will($this->returnValue($user))
  63. ;
  64. $returnedToken = $service->autoLogin($request);
  65. $this->assertSame($user, $returnedToken->getUser());
  66. $this->assertSame('foosecret', $returnedToken->getSecret());
  67. $this->assertSame('fookey', $returnedToken->getProviderKey());
  68. }
  69. /**
  70. * @dataProvider provideOptionsForLogout
  71. */
  72. public function testLogout(array $options)
  73. {
  74. $service = $this->getService(null, $options);
  75. $request = new Request();
  76. $response = new Response();
  77. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  78. $service->logout($request, $response, $token);
  79. $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
  80. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Cookie', $cookie);
  81. $this->assertTrue($cookie->isCleared());
  82. $this->assertSame($options['name'], $cookie->getName());
  83. $this->assertSame($options['path'], $cookie->getPath());
  84. $this->assertSame($options['domain'], $cookie->getDomain());
  85. $this->assertSame($options['secure'], $cookie->isSecure());
  86. $this->assertSame($options['httponly'], $cookie->isHttpOnly());
  87. }
  88. public function provideOptionsForLogout()
  89. {
  90. return array(
  91. array(array('name' => 'foo', 'path' => '/', 'domain' => null, 'secure' => false, 'httponly' => true)),
  92. array(array('name' => 'foo', 'path' => '/bar', 'domain' => 'baz.com', 'secure' => true, 'httponly' => false)),
  93. );
  94. }
  95. public function testLoginFail()
  96. {
  97. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
  98. $request = new Request();
  99. $service->loginFail($request);
  100. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  101. }
  102. public function testLoginSuccessIsNotProcessedWhenTokenDoesNotContainUserInterfaceImplementation()
  103. {
  104. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true, 'path' => null, 'domain' => null));
  105. $request = new Request();
  106. $response = new Response();
  107. $account = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
  108. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  109. $token
  110. ->expects($this->once())
  111. ->method('getUser')
  112. ->will($this->returnValue('foo'))
  113. ;
  114. $service
  115. ->expects($this->never())
  116. ->method('onLoginSuccess')
  117. ;
  118. $this->assertFalse($request->request->has('foo'));
  119. $service->loginSuccess($request, $response, $token);
  120. }
  121. public function testLoginSuccessIsNotProcessedWhenRememberMeIsNotRequested()
  122. {
  123. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo', 'path' => null, 'domain' => null));
  124. $request = new Request();
  125. $response = new Response();
  126. $account = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
  127. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  128. $token
  129. ->expects($this->once())
  130. ->method('getUser')
  131. ->will($this->returnValue($account))
  132. ;
  133. $service
  134. ->expects($this->never())
  135. ->method('onLoginSuccess')
  136. ->will($this->returnValue(null))
  137. ;
  138. $this->assertFalse($request->request->has('foo'));
  139. $service->loginSuccess($request, $response, $token);
  140. }
  141. public function testLoginSuccessWhenRememberMeAlwaysIsTrue()
  142. {
  143. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true, 'path' => null, 'domain' => null));
  144. $request = new Request();
  145. $response = new Response();
  146. $account = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
  147. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  148. $token
  149. ->expects($this->once())
  150. ->method('getUser')
  151. ->will($this->returnValue($account))
  152. ;
  153. $service
  154. ->expects($this->once())
  155. ->method('onLoginSuccess')
  156. ->will($this->returnValue(null))
  157. ;
  158. $service->loginSuccess($request, $response, $token);
  159. }
  160. /**
  161. * @dataProvider getPositiveRememberMeParameterValues
  162. */
  163. public function testLoginSuccessWhenRememberMeParameterWithPathIsPositive($value)
  164. {
  165. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo[bar]', 'path' => null, 'domain' => null));
  166. $request = new Request();
  167. $request->request->set('foo', array('bar' => $value));
  168. $response = new Response();
  169. $account = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
  170. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  171. $token
  172. ->expects($this->once())
  173. ->method('getUser')
  174. ->will($this->returnValue($account))
  175. ;
  176. $service
  177. ->expects($this->once())
  178. ->method('onLoginSuccess')
  179. ->will($this->returnValue(true))
  180. ;
  181. $service->loginSuccess($request, $response, $token);
  182. }
  183. /**
  184. * @dataProvider getPositiveRememberMeParameterValues
  185. */
  186. public function testLoginSuccessWhenRememberMeParameterIsPositive($value)
  187. {
  188. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo', 'path' => null, 'domain' => null));
  189. $request = new Request();
  190. $request->request->set('foo', $value);
  191. $response = new Response();
  192. $account = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
  193. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  194. $token
  195. ->expects($this->once())
  196. ->method('getUser')
  197. ->will($this->returnValue($account))
  198. ;
  199. $service
  200. ->expects($this->once())
  201. ->method('onLoginSuccess')
  202. ->will($this->returnValue(true))
  203. ;
  204. $service->loginSuccess($request, $response, $token);
  205. }
  206. public function getPositiveRememberMeParameterValues()
  207. {
  208. return array(
  209. array('true'),
  210. array('1'),
  211. array('on'),
  212. array('yes'),
  213. array(true),
  214. );
  215. }
  216. public function testEncodeCookieAndDecodeCookieAreInvertible()
  217. {
  218. $cookieParts = array('aa', 'bb', 'cc');
  219. $service = $this->getService();
  220. $encoded = $this->callProtected($service, 'encodeCookie', array($cookieParts));
  221. $this->assertInternalType('string', $encoded);
  222. $decoded = $this->callProtected($service, 'decodeCookie', array($encoded));
  223. $this->assertSame($cookieParts, $decoded);
  224. }
  225. /**
  226. * @expectedException \InvalidArgumentException
  227. * @expectedExceptionMessage cookie delimiter
  228. */
  229. public function testThereShouldBeNoCookieDelimiterInCookieParts()
  230. {
  231. $cookieParts = array('aa', 'b'.AbstractRememberMeServices::COOKIE_DELIMITER.'b', 'cc');
  232. $service = $this->getService();
  233. $this->callProtected($service, 'encodeCookie', array($cookieParts));
  234. }
  235. protected function getService($userProvider = null, $options = array(), $logger = null)
  236. {
  237. if (null === $userProvider) {
  238. $userProvider = $this->getProvider();
  239. }
  240. return $this->getMockForAbstractClass('Symfony\Component\Security\Http\RememberMe\AbstractRememberMeServices', array(
  241. array($userProvider), 'foosecret', 'fookey', $options, $logger,
  242. ));
  243. }
  244. protected function getProvider()
  245. {
  246. $provider = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock();
  247. $provider
  248. ->expects($this->any())
  249. ->method('supportsClass')
  250. ->will($this->returnValue(true))
  251. ;
  252. return $provider;
  253. }
  254. private function callProtected($object, $method, array $args)
  255. {
  256. $reflection = new \ReflectionClass(\get_class($object));
  257. $reflectionMethod = $reflection->getMethod($method);
  258. $reflectionMethod->setAccessible(true);
  259. return $reflectionMethod->invokeArgs($object, $args);
  260. }
  261. }