1212namespace Symfony \Component \Security \Http \Tests \RememberMe ;
1313
1414use PHPUnit \Framework \TestCase ;
15- use Symfony \Bridge \PhpUnit \ClockMock ;
1615use Symfony \Component \HttpFoundation \Cookie ;
1716use Symfony \Component \HttpFoundation \Request ;
1817use Symfony \Component \HttpFoundation \RequestStack ;
18+ use Symfony \Component \PropertyAccess \PropertyAccess ;
1919use Symfony \Component \Security \Core \Exception \AuthenticationException ;
20- use Symfony \Component \Security \Core \Signature \Exception \ExpiredSignatureException ;
21- use Symfony \Component \Security \Core \Signature \Exception \InvalidSignatureException ;
2220use Symfony \Component \Security \Core \Signature \SignatureHasher ;
2321use Symfony \Component \Security \Core \User \InMemoryUser ;
2422use Symfony \Component \Security \Core \User \InMemoryUserProvider ;
@@ -36,10 +34,8 @@ class SignatureRememberMeHandlerTest extends TestCase
3634
3735 protected function setUp (): void
3836 {
39- $ this ->signatureHasher = $ this -> createMock ( SignatureHasher::class );
37+ $ this ->signatureHasher = new SignatureHasher (PropertyAccess:: createPropertyAccessor (), [], ' s3cret ' );
4038 $ this ->userProvider = new InMemoryUserProvider ();
41- $ user = new InMemoryUser ('wouter ' , null );
42- $ this ->userProvider ->createUser ($ user );
4339 $ this ->requestStack = new RequestStack ();
4440 $ this ->request = Request::create ('/login ' );
4541 $ this ->requestStack ->push ($ this ->request );
@@ -51,18 +47,17 @@ protected function setUp(): void
5147 */
5248 public function testCreateRememberMeCookie ()
5349 {
54- ClockMock::register (SignatureRememberMeHandler::class);
55-
5650 $ user = new InMemoryUser ('wouter ' , null );
57- $ this ->signatureHasher ->expects ($ this ->once ())->method ('computeSignatureHash ' )->with ($ user , $ expire = time () + 31536000 )->willReturn ('abc ' );
51+ $ signature = $ this ->signatureHasher ->computeSignatureHash ($ user , $ expire = time () + 31536000 );
52+ $ this ->userProvider ->createUser (new InMemoryUser ('wouter ' , null ));
5853
5954 $ this ->handler ->createRememberMeCookie ($ user );
6055
6156 $ this ->assertTrue ($ this ->request ->attributes ->has (ResponseListener::COOKIE_ATTR_NAME ));
6257
6358 /** @var Cookie $cookie */
6459 $ cookie = $ this ->request ->attributes ->get (ResponseListener::COOKIE_ATTR_NAME );
65- $ this ->assertEquals (base64_encode (InMemoryUser::class. ' :d291dGVy: ' .$ expire .':abc ' ) , $ cookie ->getValue ());
60+ $ this ->assertEquals (strtr (InMemoryUser::class, '\\' , ' . ' ). ' :d291dGVy: ' .$ expire .': ' . $ signature , $ cookie ->getValue ());
6661 }
6762
6863 public function testClearRememberMeCookie ()
@@ -76,50 +71,36 @@ public function testClearRememberMeCookie()
7671 $ this ->assertNull ($ cookie ->getValue ());
7772 }
7873
79- /**
80- * @group time-sensitive
81- */
8274 public function testConsumeRememberMeCookieValid ()
8375 {
84- $ this ->signatureHasher ->expects ($ this ->once ())->method ('verifySignatureHash ' )->with ($ user = new InMemoryUser ('wouter ' , null ), 360 , 'signature ' );
85- $ this ->signatureHasher ->expects ($ this ->any ())
86- ->method ('computeSignatureHash ' )
87- ->with ($ user , $ expire = time () + 31536000 )
88- ->willReturn ('newsignature ' );
76+ $ user = new InMemoryUser ('wouter ' , null );
77+ $ signature = $ this ->signatureHasher ->computeSignatureHash ($ user , $ expire = time () + 3600 );
78+ $ this ->userProvider ->createUser (new InMemoryUser ('wouter ' , null ));
8979
90- $ rememberMeDetails = new RememberMeDetails (InMemoryUser::class, 'wouter ' , 360 , ' signature ' );
80+ $ rememberMeDetails = new RememberMeDetails (InMemoryUser::class, 'wouter ' , $ expire , $ signature );
9181 $ this ->handler ->consumeRememberMeCookie ($ rememberMeDetails );
9282
9383 $ this ->assertTrue ($ this ->request ->attributes ->has (ResponseListener::COOKIE_ATTR_NAME ));
9484
9585 /** @var Cookie $cookie */
9686 $ cookie = $ this ->request ->attributes ->get (ResponseListener::COOKIE_ATTR_NAME );
97- $ this ->assertEquals ((new RememberMeDetails (InMemoryUser::class, 'wouter ' , $ expire , ' newsignature ' ))->toString (), $ cookie ->getValue ());
87+ $ this ->assertNotEquals ((new RememberMeDetails (InMemoryUser::class, 'wouter ' , $ expire , $ signature ))->toString (), $ cookie ->getValue ());
9888 }
9989
10090 public function testConsumeRememberMeCookieInvalidHash ()
10191 {
10292 $ this ->expectException (AuthenticationException::class);
10393 $ this ->expectExceptionMessage ('The cookie \'s hash is invalid. ' );
104-
105- $ this ->signatureHasher ->expects ($ this ->any ())
106- ->method ('verifySignatureHash ' )
107- ->with (new InMemoryUser ('wouter ' , null ), 360 , 'badsignature ' )
108- ->will ($ this ->throwException (new InvalidSignatureException ()));
109-
110- $ this ->handler ->consumeRememberMeCookie (new RememberMeDetails (InMemoryUser::class, 'wouter ' , 360 , 'badsignature ' ));
94+ $ this ->handler ->consumeRememberMeCookie (new RememberMeDetails (InMemoryUser::class, 'wouter ' , time () + 600 , 'badsignature ' ));
11195 }
11296
11397 public function testConsumeRememberMeCookieExpired ()
11498 {
99+ $ user = new InMemoryUser ('wouter ' , null );
100+ $ signature = $ this ->signatureHasher ->computeSignatureHash ($ user , 360 );
101+
115102 $ this ->expectException (AuthenticationException::class);
116103 $ this ->expectExceptionMessage ('The cookie has expired. ' );
117-
118- $ this ->signatureHasher ->expects ($ this ->any ())
119- ->method ('verifySignatureHash ' )
120- ->with (new InMemoryUser ('wouter ' , null ), 360 , 'signature ' )
121- ->will ($ this ->throwException (new ExpiredSignatureException ()));
122-
123- $ this ->handler ->consumeRememberMeCookie (new RememberMeDetails (InMemoryUser::class, 'wouter ' , 360 , 'signature ' ));
104+ $ this ->handler ->consumeRememberMeCookie (new RememberMeDetails (InMemoryUser::class, 'wouter ' , 360 , $ signature ));
124105 }
125106}
0 commit comments