-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
minor: fix tests for url-safe signed urls #203
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, why no port? Do we run it w/o port on CI? Will it fail if we run it with port locally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the default for Symfony, without any modifications,
http://localhost
is the base url. So yes, it passes locally.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, great!