-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor: fix tests for url-safe signed urls (#203)
- Loading branch information
Showing
3 changed files
with
50 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,12 @@ | |
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\UriSigner; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Symfony\Component\HttpKernel\UriSigner as LegacyUriSigner; | ||
use SymfonyCasts\Bundle\VerifyEmail\Generator\VerifyEmailTokenGenerator; | ||
use SymfonyCasts\Bundle\VerifyEmail\Tests\VerifyEmailTestKernel; | ||
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelper; | ||
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface; | ||
|
@@ -32,36 +35,22 @@ final class VerifyEmailAcceptanceTest extends TestCase | |
public function testGenerateSignature(): void | ||
{ | ||
$kernel = $this->getBootedKernel(); | ||
|
||
$container = $kernel->getContainer(); | ||
|
||
/** @var VerifyEmailHelper $helper */ | ||
$helper = $container->get(VerifyEmailAcceptanceFixture::class)->helper; | ||
/** @var VerifyEmailAcceptanceFixture $testHelper */ | ||
$testHelper = $container->get(VerifyEmailAcceptanceFixture::class); | ||
$helper = $testHelper->helper; | ||
|
||
$components = $helper->generateSignature('verify-test', '1234', '[email protected]'); | ||
|
||
$signature = $components->getSignedUrl(); | ||
$expiresAt = $components->getExpiresAt()->getTimestamp(); | ||
|
||
$expectedUserData = json_encode(['1234', '[email protected]']); | ||
|
||
$expectedToken = base64_encode(hash_hmac('sha256', $expectedUserData, 'foo', true)); | ||
|
||
$expectedSignature = base64_encode(hash_hmac( | ||
'sha256', | ||
\sprintf('http://localhost/verify/user?expires=%s&token=%s', $expiresAt, urlencode($expectedToken)), | ||
'foo', | ||
true | ||
$actual = $components->getSignedUrl(); | ||
$expected = $testHelper->uriSigner->sign(\sprintf( | ||
'http://localhost/verify/user?expires=%s&token=%s', | ||
$expiresAt, | ||
$testHelper->generator->createToken('1234', '[email protected]') | ||
)); | ||
|
||
$parsed = parse_url($signature); | ||
parse_str($parsed['query'], $result); | ||
|
||
self::assertTrue(hash_equals($expectedSignature, $result['signature'])); | ||
self::assertSame( | ||
\sprintf('http://localhost/verify/user?expires=%s&signature=%s&token=%s', $expiresAt, urlencode($expectedSignature), urlencode($expectedToken)), | ||
$signature | ||
); | ||
self::assertSame($expected, $actual); | ||
} | ||
|
||
/** @group legacy */ | ||
|
@@ -133,6 +122,8 @@ private function getBootedKernel(): KernelInterface | |
$builder = new ContainerBuilder(); | ||
$builder->autowire(VerifyEmailAcceptanceFixture::class) | ||
->setPublic(true) | ||
->setArgument(1, new Reference('symfonycasts.verify_email.uri_signer')) | ||
->setArgument(2, new Reference('symfonycasts.verify_email.token_generator')) | ||
; | ||
|
||
$kernel = new VerifyEmailTestKernel( | ||
|
@@ -148,10 +139,10 @@ private function getBootedKernel(): KernelInterface | |
|
||
final class VerifyEmailAcceptanceFixture | ||
{ | ||
public $helper; | ||
|
||
public function __construct(VerifyEmailHelperInterface $helper) | ||
{ | ||
$this->helper = $helper; | ||
public function __construct( | ||
public VerifyEmailHelperInterface $helper, | ||
public LegacyUriSigner|UriSigner $uriSigner, | ||
public VerifyEmailTokenGenerator $generator, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
final class VerifyEmailHelperFunctionalTest extends TestCase | ||
{ | ||
private $mockRouter; | ||
private UriSigner|LegacyUriSigner $uriSigner; | ||
private $expiryTimestamp; | ||
|
||
protected function setUp(): void | ||
|
@@ -55,19 +56,10 @@ public function testGenerateSignature(): void | |
->willReturn(\sprintf('/verify?expires=%s&token=%s', $this->expiryTimestamp, urlencode($token))) | ||
; | ||
|
||
$result = $this->getHelper()->generateSignature('app_verify_route', '1234', '[email protected]'); | ||
$actual = $this->getHelper()->generateSignature('app_verify_route', '1234', '[email protected]')->getSignedUrl(); | ||
$expected = $this->uriSigner->sign(\sprintf('/verify?expires=%s&token=%s', $this->expiryTimestamp, urlencode($token))); | ||
|
||
$parsedUri = parse_url($result->getSignedUrl()); | ||
parse_str($parsedUri['query'], $queryParams); | ||
|
||
$knownToken = $token; | ||
$testToken = $queryParams['token']; | ||
|
||
$knownSignature = $this->getTestSignature(); | ||
$testSignature = $queryParams['signature']; | ||
|
||
self::assertTrue(hash_equals($knownToken, $testToken)); | ||
self::assertTrue(hash_equals($knownSignature, $testSignature)); | ||
self::assertSame($expected, $actual); | ||
} | ||
|
||
/** | ||
|
@@ -117,14 +109,14 @@ private function getTestSignedUri(): string | |
private function getHelper(): VerifyEmailHelperInterface | ||
{ | ||
if (class_exists(UriSigner::class)) { | ||
$uriSigner = new UriSigner('foo', 'signature'); | ||
$this->uriSigner = new UriSigner('foo', 'signature'); | ||
} else { | ||
$uriSigner = new LegacyUriSigner('foo', 'signature'); | ||
$this->uriSigner = new LegacyUriSigner('foo', 'signature'); | ||
} | ||
|
||
return new VerifyEmailHelper( | ||
$this->mockRouter, | ||
$uriSigner, | ||
$this->uriSigner, | ||
new VerifyEmailQueryUtility(), | ||
new VerifyEmailTokenGenerator('foo'), | ||
3600 | ||
|