Skip to content

Commit

Permalink
Merge pull request #578 from thebiggive/DON-706-cont
Browse files Browse the repository at this point in the history
DON-706: Refactor, extract constant PersonManagementAuthMiddleware::P…
  • Loading branch information
bdsl authored Jul 21, 2023
2 parents 44549e5 + 55633e9 commit bbbea9a
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Application/Actions/DeletePaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(
*/
protected function action(Request $request, Response $response, array $args): Response
{
$customerId = $request->getAttribute('pspId');
$customerId = $request->getAttribute(PersonWithPasswordAuthMiddleware::PSP_ATTRIBUTE_NAME);
\assert(is_string($customerId));

$paymentMethodId = $args['payment_method_id'];
Expand Down
3 changes: 2 additions & 1 deletion src/Application/Actions/Donations/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use MatchBot\Application\Actions\ActionPayload;
use MatchBot\Application\Auth\DonationToken;
use MatchBot\Application\Auth\PersonManagementAuthMiddleware;
use MatchBot\Application\Auth\PersonWithPasswordAuthMiddleware;
use MatchBot\Application\HttpModels\DonationCreate;
use MatchBot\Application\HttpModels\DonationCreatedResponse;
use MatchBot\Domain\Campaign;
Expand Down Expand Up @@ -51,7 +52,7 @@ protected function action(Request $request, Response $response, array $args): Re
// as the person, and sets this attribute to the Stripe Customer ID based on JWS claims, all
// in `PersonManagementAuthMiddleware`. If the legacy route was used or if no such ID was in the
// JWS, this is null.
$customerId = $request->getAttribute('pspId');
$customerId = $request->getAttribute(PersonWithPasswordAuthMiddleware::PSP_ATTRIBUTE_NAME);

$body = (string) $request->getBody();

Expand Down
2 changes: 1 addition & 1 deletion src/Application/Actions/GetPaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function action(Request $request, Response $response, array $args): Re
// The route at `/people/{personId}/donations` validates that the donor has permission to act
// as the person, and sets this attribute to the Stripe Customer ID based on JWS claims, all
// in `PersonWithPasswordAuthMiddleware`.
$customerId = $request->getAttribute('pspId');
$customerId = $request->getAttribute(PersonWithPasswordAuthMiddleware::PSP_ATTRIBUTE_NAME);

$paymentMethods = $this->stripeClient->customers->allPaymentMethods(
$customerId,
Expand Down
3 changes: 2 additions & 1 deletion src/Application/Actions/UpdatePaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fig\Http\Message\StatusCodeInterface;
use JetBrains\PhpStorm\Pure;
use MatchBot\Application\Auth\PersonWithPasswordAuthMiddleware;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
Expand All @@ -23,7 +24,7 @@ public function __construct(

protected function action(Request $request, Response $response, array $args): Response
{
$customerId = $request->getAttribute('pspId');
$customerId = $request->getAttribute(PersonWithPasswordAuthMiddleware::PSP_ATTRIBUTE_NAME);
\assert(is_string($customerId));

$paymentMethodId = $args['payment_method_id'];
Expand Down
3 changes: 2 additions & 1 deletion src/Application/Auth/PersonManagementAuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class PersonManagementAuthMiddleware implements MiddlewareInterface
{
use ErrorTrait;

public const PSP_ATTRIBUTE_NAME = 'pspId';
protected ?string $jws = null;

#[Pure]
Expand Down Expand Up @@ -57,7 +58,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

$this->checkCompleteness($request);

return $handler->handle($request->withAttribute('pspId', IdentityToken::getPspId($this->jws)));
return $handler->handle($request->withAttribute(self::PSP_ATTRIBUTE_NAME, IdentityToken::getPspId($this->jws)));
}

protected function checkCompleteness(ServerRequestInterface $request): void
Expand Down
5 changes: 3 additions & 2 deletions tests/Application/Actions/DeletePaymentMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Laminas\Diactoros\ServerRequest;
use MatchBot\Application\Actions\DeletePaymentMethod;
use MatchBot\Application\Auth\PersonWithPasswordAuthMiddleware;
use MatchBot\Tests\TestCase;
use PHPUnit\Framework\MockObject\Stub;
use Prophecy\Argument;
Expand Down Expand Up @@ -33,7 +34,7 @@ public function testItDeletesAPaymentMethod(): void
$sut = new DeletePaymentMethod($fakeStripeClient, new NullLogger());

$request = (new ServerRequest())
->withAttribute('pspId', 'stripe_customer_id_12');
->withAttribute(PersonWithPasswordAuthMiddleware::PSP_ATTRIBUTE_NAME, 'stripe_customer_id_12');

// assert
$stripePaymentMethodServiceProphecy->detach('stripe_payment_method_id_35')
Expand All @@ -58,7 +59,7 @@ public function testItRefusesToToDeletePaymentMethodThatDoesNotBelongToRquester(
$sut = new DeletePaymentMethod($fakeStripeClient, new NullLogger());

$request = (new ServerRequest())
->withAttribute('pspId', 'stripe_customer_id_12');
->withAttribute(PersonWithPasswordAuthMiddleware::PSP_ATTRIBUTE_NAME, 'stripe_customer_id_12');

// assert
$stripePaymentMethodServiceProphecy->detach(Argument::any())->shouldNotBeCalled();
Expand Down
5 changes: 3 additions & 2 deletions tests/Application/Actions/UpdatePaymentMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Laminas\Diactoros\ServerRequest;
use MatchBot\Application\Actions\DeletePaymentMethod;
use MatchBot\Application\Actions\UpdatePaymentMethod;
use MatchBot\Application\Auth\PersonWithPasswordAuthMiddleware;
use MatchBot\Tests\TestCase;
use PHPUnit\Framework\MockObject\Stub;
use Prophecy\Argument;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function testItUpdatesAPaymentMethod(): void
];

$request = $this->createRequest('PUT', '/', \json_encode($updatedBillingDetails))
->withAttribute('pspId', 'stripe_customer_id_12');
->withAttribute(PersonWithPasswordAuthMiddleware::PSP_ATTRIBUTE_NAME, 'stripe_customer_id_12');

// assert
$stripePaymentMethodServiceProphecy->update('stripe_payment_method_id_35', $updatedBillingDetails)
Expand All @@ -70,7 +71,7 @@ public function testItRefusesToToUpdatePaymentMethodThatDoesNotBelongToRquester(
$sut = new DeletePaymentMethod($fakeStripeClient, new NullLogger());

$request = (new ServerRequest())
->withAttribute('pspId', 'stripe_customer_id_12');
->withAttribute(PersonWithPasswordAuthMiddleware::PSP_ATTRIBUTE_NAME, 'stripe_customer_id_12');

// assert
$stripePaymentMethodServiceProphecy->detach(Argument::any())->shouldNotBeCalled();
Expand Down

0 comments on commit bbbea9a

Please sign in to comment.