-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ACP-4589 Added AppConfigForTenantVaidator request validator, updated … (
#21) * ACP-4589 Added AppConfigForTenantVaidator request validator, updated schema request validator to be able to exclude URLs
- Loading branch information
Showing
9 changed files
with
370 additions
and
2 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
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
24 changes: 24 additions & 0 deletions
24
src/Spryker/Glue/AppKernel/Plugin/GlueApplication/AppConfigForTenantValidatorPlugin.php
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace Spryker\Glue\AppKernel\Plugin\GlueApplication; | ||
|
||
use Generated\Shared\Transfer\GlueRequestTransfer; | ||
use Generated\Shared\Transfer\GlueRequestValidationTransfer; | ||
use Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\RequestValidatorPluginInterface; | ||
use Spryker\Glue\Kernel\AbstractPlugin; | ||
|
||
/** | ||
* @method \Spryker\Glue\AppKernel\AppKernelFactory getFactory() | ||
*/ | ||
class AppConfigForTenantValidatorPlugin extends AbstractPlugin implements RequestValidatorPluginInterface | ||
{ | ||
public function validate(GlueRequestTransfer $glueRequestTransfer): GlueRequestValidationTransfer | ||
{ | ||
return $this->getFactory()->createAppConfigForTenantValidator()->validate($glueRequestTransfer); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/Spryker/Glue/AppKernel/Validator/AppConfigForTenantValidator.php
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace Spryker\Glue\AppKernel\Validator; | ||
|
||
use Generated\Shared\Transfer\AppConfigCriteriaTransfer; | ||
use Generated\Shared\Transfer\GlueErrorTransfer; | ||
use Generated\Shared\Transfer\GlueRequestTransfer; | ||
use Generated\Shared\Transfer\GlueRequestValidationTransfer; | ||
use Spryker\Glue\AppKernel\AppKernelConfig; | ||
use Spryker\Glue\AppKernel\Dependency\Facade\AppKernelToAppKernelFacadeInterface; | ||
use Spryker\Shared\Log\LoggerTrait; | ||
use Spryker\Zed\AppKernel\AppKernelConfig as AppKernelAppKernelConfig; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Throwable; | ||
|
||
class AppConfigForTenantValidator | ||
{ | ||
use LoggerTrait; | ||
|
||
public function __construct(protected AppKernelToAppKernelFacadeInterface $appKernelToAppKernelFacade, protected AppKernelConfig $appKernelConfig) | ||
{ | ||
} | ||
|
||
public function validate(GlueRequestTransfer $glueRequestTransfer): GlueRequestValidationTransfer | ||
{ | ||
$glueRequestValidationTransfer = new GlueRequestValidationTransfer(); | ||
$glueRequestValidationTransfer->setIsValid(true); | ||
|
||
if ($this->isPathExcludedFromValidation($glueRequestTransfer)) { | ||
return $glueRequestValidationTransfer; | ||
} | ||
|
||
$tenantIdentifier = $this->getTenantIdentifier($glueRequestTransfer); | ||
|
||
$appConfigCriteriaTransfer = new AppConfigCriteriaTransfer(); | ||
$appConfigCriteriaTransfer->setTenantIdentifier($tenantIdentifier); | ||
|
||
try { | ||
$appConfigTransfer = $this->appKernelToAppKernelFacade->getConfig($appConfigCriteriaTransfer); | ||
if ($appConfigTransfer->getStatus() === AppKernelAppKernelConfig::APP_STATUS_DISCONNECTED) { | ||
$glueErrorTransfer = new GlueErrorTransfer(); | ||
$glueErrorTransfer->setMessage('Tenant is disconnected.'); | ||
|
||
$glueRequestValidationTransfer | ||
->setIsValid(false) | ||
->setStatus(Response::HTTP_FORBIDDEN) | ||
->addError($glueErrorTransfer); | ||
} | ||
} catch (Throwable $throwable) { | ||
$this->getLogger()->error( | ||
$throwable->getMessage(), | ||
$glueRequestTransfer->toArray(), | ||
); | ||
|
||
$glueErrorTransfer = new GlueErrorTransfer(); | ||
$glueErrorTransfer | ||
->setMessage($throwable->getMessage()); | ||
|
||
$glueRequestValidationTransfer | ||
->setIsValid(false) | ||
->addError($glueErrorTransfer) | ||
->setStatus(Response::HTTP_INTERNAL_SERVER_ERROR); | ||
|
||
return $glueRequestValidationTransfer; | ||
} | ||
|
||
return $glueRequestValidationTransfer; | ||
} | ||
|
||
protected function isPathExcludedFromValidation(GlueRequestTransfer $glueRequestTransfer): bool | ||
{ | ||
return in_array($glueRequestTransfer->getPath(), $this->appKernelConfig->getAppConfigRequestValidationExcludedPaths()); | ||
} | ||
|
||
protected function getTenantIdentifier(GlueRequestTransfer $glueRequestTransfer): ?string | ||
{ | ||
$meta = $glueRequestTransfer->getMeta(); | ||
|
||
if (!isset($meta['x-tenant-identifier'])) { | ||
return null; | ||
} | ||
|
||
$tenantIdentifier = $meta['x-tenant-identifier']; | ||
|
||
if (is_array($tenantIdentifier)) { | ||
return $tenantIdentifier[0]; | ||
} | ||
|
||
return $tenantIdentifier; | ||
} | ||
} |
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
177 changes: 177 additions & 0 deletions
177
...rykerTest/Glue/AppKernel/Plugin/GlueApplication/AppConfigForTenantValidatorPluginTest.php
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 |
---|---|---|
@@ -0,0 +1,177 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace SprykerTest\Glue\AppKernel\Plugin\GlueApplication; | ||
|
||
use Codeception\Stub; | ||
use Codeception\Test\Unit; | ||
use Generated\Shared\Transfer\GlueRequestTransfer; | ||
use Ramsey\Uuid\Uuid; | ||
use Spryker\Glue\AppKernel\AppKernelConfig; | ||
use Spryker\Glue\AppKernel\AppKernelDependencyProvider; | ||
use Spryker\Glue\AppKernel\AppKernelFactory; | ||
use Spryker\Glue\AppKernel\Plugin\GlueApplication\AppConfigForTenantValidatorPlugin; | ||
use Spryker\Glue\Kernel\Backend\Container; | ||
use Spryker\Zed\AppKernel\AppKernelConfig as ZedAppKernelConfig; | ||
use SprykerTest\Glue\AppKernel\AppKernelTester; | ||
use SprykerTest\Glue\Testify\Helper\DependencyProviderHelperTrait; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* Auto-generated group annotations | ||
* | ||
* @group SprykerTest | ||
* @group Glue | ||
* @group AppKernel | ||
* @group Plugin | ||
* @group GlueApplication | ||
* @group AppConfigForTenantValidatorPluginTest | ||
* Add your own group annotations below this line | ||
*/ | ||
class AppConfigForTenantValidatorPluginTest extends Unit | ||
{ | ||
use DependencyProviderHelperTrait; | ||
|
||
protected AppKernelTester $tester; | ||
|
||
public function testGivenAPathIsExcludedFromTheValidationWhenTheValidatorIsExecutedThenTheRequestValidationIsSuccessful(): void | ||
{ | ||
// Arrange | ||
$tenantIdentifier = Uuid::uuid4()->toString(); | ||
|
||
$this->tester->haveAppConfigForTenant($tenantIdentifier, status: ZedAppKernelConfig::APP_STATUS_DISCONNECTED); | ||
|
||
$configStub = Stub::make(AppKernelConfig::class, [ | ||
'getAppConfigRequestValidationExcludedPaths' => ['/excluded-endpoint'], | ||
]); | ||
|
||
$container = (new AppKernelDependencyProvider())->provideDependencies(new Container()); | ||
|
||
$factoryStub = Stub::make(AppKernelFactory::class, ['getConfig' => $configStub]); | ||
$factoryStub->setContainer($container); | ||
|
||
$appConfigForTenantValidatorPlugin = new AppConfigForTenantValidatorPlugin(); | ||
$appConfigForTenantValidatorPlugin->setFactory($factoryStub); | ||
|
||
$glueRequestTransfer = new GlueRequestTransfer(); | ||
$glueRequestTransfer | ||
->setMethod('POST') | ||
->setPath('/excluded-endpoint') | ||
->setMeta([ | ||
AppKernelConfig::HEADER_TENANT_IDENTIFIER => $tenantIdentifier, | ||
'content-type' => 'application/json', | ||
]); | ||
|
||
// Act | ||
$glueRequestValidationTransfer = $appConfigForTenantValidatorPlugin->validate($glueRequestTransfer); | ||
|
||
// Assert | ||
$this->assertTrue($glueRequestValidationTransfer->getIsValid()); | ||
} | ||
|
||
public function testGivenAppConfigIsMarkedAsNewWhenTheValidatorIsExecutedThenTheRequestValidationIsSuccessful(): void | ||
{ | ||
// Arrange | ||
$tenantIdentifier = Uuid::uuid4()->toString(); | ||
|
||
$this->tester->haveAppConfigForTenant($tenantIdentifier, status: ZedAppKernelConfig::APP_STATUS_NEW); | ||
|
||
$appConfigForTenantValidatorPlugin = new AppConfigForTenantValidatorPlugin(); | ||
|
||
$glueRequestTransfer = new GlueRequestTransfer(); | ||
$glueRequestTransfer | ||
->setMethod('POST') | ||
->setPath('/existing-endpoint') | ||
->setMeta([ | ||
AppKernelConfig::HEADER_TENANT_IDENTIFIER => $tenantIdentifier, | ||
'content-type' => 'application/json', | ||
]); | ||
|
||
// Act | ||
$glueRequestValidationTransfer = $appConfigForTenantValidatorPlugin->validate($glueRequestTransfer); | ||
|
||
// Assert | ||
$this->assertTrue($glueRequestValidationTransfer->getIsValid()); | ||
} | ||
|
||
public function testGivenAppConfigIsMarkedAsConnectedWhenTheValidatorIsExecutedThenTheRequestValidationIsSuccessful(): void | ||
{ | ||
// Arrange | ||
$tenantIdentifier = Uuid::uuid4()->toString(); | ||
|
||
$this->tester->haveAppConfigForTenant($tenantIdentifier, status: ZedAppKernelConfig::APP_STATUS_CONNECTED); | ||
|
||
$appConfigForTenantValidatorPlugin = new AppConfigForTenantValidatorPlugin(); | ||
|
||
$glueRequestTransfer = new GlueRequestTransfer(); | ||
$glueRequestTransfer | ||
->setMethod('POST') | ||
->setPath('/existing-endpoint') | ||
->setMeta([ | ||
AppKernelConfig::HEADER_TENANT_IDENTIFIER => $tenantIdentifier, | ||
'content-type' => 'application/json', | ||
]); | ||
|
||
// Act | ||
$glueRequestValidationTransfer = $appConfigForTenantValidatorPlugin->validate($glueRequestTransfer); | ||
|
||
// Assert | ||
$this->assertTrue($glueRequestValidationTransfer->getIsValid()); | ||
} | ||
|
||
public function testGivenAppConfigIsMarkedAsDisconnectedWhenTheValidatorIsExecutedThenTheRequestValidationIsFailedAddAStatusCode403IsReturned(): void | ||
{ | ||
// Arrange | ||
$tenantIdentifier = Uuid::uuid4()->toString(); | ||
|
||
$this->tester->haveAppConfigForTenant($tenantIdentifier, status: ZedAppKernelConfig::APP_STATUS_DISCONNECTED); | ||
|
||
$appConfigForTenantValidatorPlugin = new AppConfigForTenantValidatorPlugin(); | ||
|
||
$glueRequestTransfer = new GlueRequestTransfer(); | ||
$glueRequestTransfer | ||
->setMethod('POST') | ||
->setPath('/existing-endpoint') | ||
->setMeta([ | ||
AppKernelConfig::HEADER_TENANT_IDENTIFIER => $tenantIdentifier, | ||
'content-type' => 'application/json', | ||
]); | ||
|
||
// Act | ||
$glueRequestValidationTransfer = $appConfigForTenantValidatorPlugin->validate($glueRequestTransfer); | ||
|
||
// Assert | ||
$this->assertFalse($glueRequestValidationTransfer->getIsValid()); | ||
$this->assertSame(Response::HTTP_FORBIDDEN, $glueRequestValidationTransfer->getStatus()); | ||
} | ||
|
||
public function testGivenAppConfigIsMarkedAsDisconnectedWhenTheValidatorIsExecutedAndTheTenantIdentifierHeaderIsAnArrayThenTheRequestValidationIsFailedAddAStatusCode403IsReturned(): void | ||
{ | ||
// Arrange | ||
$tenantIdentifier = Uuid::uuid4()->toString(); | ||
|
||
$this->tester->haveAppConfigForTenant($tenantIdentifier, status: ZedAppKernelConfig::APP_STATUS_DISCONNECTED); | ||
|
||
$appConfigForTenantValidatorPlugin = new AppConfigForTenantValidatorPlugin(); | ||
|
||
$glueRequestTransfer = new GlueRequestTransfer(); | ||
$glueRequestTransfer | ||
->setMethod('POST') | ||
->setPath('/existing-endpoint') | ||
->setMeta([ | ||
AppKernelConfig::HEADER_TENANT_IDENTIFIER => [$tenantIdentifier], | ||
'content-type' => 'application/json', | ||
]); | ||
|
||
// Act | ||
$glueRequestValidationTransfer = $appConfigForTenantValidatorPlugin->validate($glueRequestTransfer); | ||
|
||
// Assert | ||
$this->assertFalse($glueRequestValidationTransfer->getIsValid()); | ||
$this->assertSame(Response::HTTP_FORBIDDEN, $glueRequestValidationTransfer->getStatus()); | ||
} | ||
} |
Oops, something went wrong.