Skip to content

Commit

Permalink
bug #52 Fix request token event (ajgarlag)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 0.1-dev branch.

Discussion
----------

Fix request token event

I think #49 was merged too soon.

This PR tries to fix the following flaws:

  1. The test expectation is wrong.
  2. The event listener in the test is being added after the event dispatch.
  3. The response set to the event is being discarded by the controller.

Commits
-------

f14c02d Fix request token event
  • Loading branch information
chalasr committed Oct 7, 2021
2 parents 464dfa8 + f14c02d commit d9e7d7f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Controller/TokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public function indexAction(Request $request): Response
$renderedResponse = $this->httpFoundationFactory->createResponse($response);

/** @var TokenRequestResolveEvent $event */
$this->eventDispatcher->dispatch(
$event = $this->eventDispatcher->dispatch(
new TokenRequestResolveEvent($renderedResponse),
OAuth2Events::TOKEN_REQUEST_RESOLVE
);

return $renderedResponse;
return $event->getResponse();
}
}
29 changes: 21 additions & 8 deletions tests/Acceptance/TokenEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,32 @@ public function testSuccessfulRefreshTokenRequest(): void
->get(RefreshTokenManagerInterface::class)
->find(FixtureFactory::FIXTURE_REFRESH_TOKEN);

$this->client->request('POST', '/token', [
'client_id' => 'foo',
'client_secret' => 'secret',
'grant_type' => 'refresh_token',
'refresh_token' => TestHelper::generateEncryptedPayload($refreshToken),
]);

$this->client
->getContainer()
->get('event_dispatcher')
->addListener(OAuth2Events::TOKEN_REQUEST_RESOLVE, static function (TokenRequestResolveEvent $event): void {
$event->getResponse()->headers->set('foo', 'bar');
});

$this->client
->getContainer()
->get('event_dispatcher')
->addListener(OAuth2Events::TOKEN_REQUEST_RESOLVE, static function (TokenRequestResolveEvent $event): void {
if ('bar' === $event->getResponse()->headers->get('foo')) {
$newResponse = clone $event->getResponse();
$newResponse->headers->remove('foo');
$newResponse->headers->set('baz', 'qux');
$event->setResponse($newResponse);
}
}, -1);

$this->client->request('POST', '/token', [
'client_id' => 'foo',
'client_secret' => 'secret',
'grant_type' => 'refresh_token',
'refresh_token' => TestHelper::generateEncryptedPayload($refreshToken),
]);

$response = $this->client->getResponse();

$this->assertSame(200, $response->getStatusCode());
Expand All @@ -131,7 +143,8 @@ public function testSuccessfulRefreshTokenRequest(): void
$this->assertGreaterThan(0, $jsonResponse['expires_in']);
$this->assertNotEmpty($jsonResponse['access_token']);
$this->assertNotEmpty($jsonResponse['refresh_token']);
$this->assertEmpty($response->headers->get('foo'), 'bar');
$this->assertFalse($response->headers->has('foo'));
$this->assertSame($response->headers->get('baz'), 'qux');
}

public function testSuccessfulAuthorizationCodeRequest(): void
Expand Down

0 comments on commit d9e7d7f

Please sign in to comment.