diff --git a/composer.json b/composer.json index 33090a1..25e934b 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,9 @@ }, "require-dev": { "phpstan/phpstan": "*", - "spryker/code-sniffer": "*" + "spryker/code-sniffer": "*", + "spryker/propel": "*", + "spryker/testify": "*" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.neon index 42eea45..ffc73d0 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -9,6 +9,7 @@ parameters: - '#Parameter .+ of method .+ has invalid typehint type .+Transfer#' - '#Return typehint of method .+ has invalid type .+Transfer#' - '#Instantiated class .+Transfer not found#' + - '#Instantiated class Orm\\Zed\\AmazonQuicksight\\Persistence\\.+ not found.+#' - '#Return typehint of method .+ has invalid type Orm\\.+#' - '#Call to method .+\(\) on an unknown class Orm\\.+#' - '#Call to static method create\(\) on an unknown class .+Query.#' diff --git a/src/SprykerEco/Shared/AmazonQuicksight/Transfer/amazon_quicksight.transfer.xml b/src/SprykerEco/Shared/AmazonQuicksight/Transfer/amazon_quicksight.transfer.xml index a06f23b..bb6022e 100644 --- a/src/SprykerEco/Shared/AmazonQuicksight/Transfer/amazon_quicksight.transfer.xml +++ b/src/SprykerEco/Shared/AmazonQuicksight/Transfer/amazon_quicksight.transfer.xml @@ -12,9 +12,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/SprykerEco/Zed/AmazonQuicksight/AmazonQuicksightConfig.php b/src/SprykerEco/Zed/AmazonQuicksight/AmazonQuicksightConfig.php index bbf82d3..7260db8 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/AmazonQuicksightConfig.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/AmazonQuicksightConfig.php @@ -16,7 +16,7 @@ class AmazonQuicksightConfig extends AbstractBundleConfig /** * @var string */ - protected const QUICKSIGHT_USER_REGISTER_NAMESPACE = 'default'; + protected const QUICKSIGHT_REGISTER_USER_NAMESPACE = 'default'; /** * @var string @@ -66,9 +66,9 @@ public function getAwsAccountId(): string * * @return string */ - public function getQuicksightUserRegisterNamespace(): string + public function getQuicksightRegisterUserNamespace(): string { - return static::QUICKSIGHT_USER_REGISTER_NAMESPACE; + return static::QUICKSIGHT_REGISTER_USER_NAMESPACE; } /** diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightBusinessFactory.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightBusinessFactory.php index 683b2b9..fa971ee 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightBusinessFactory.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightBusinessFactory.php @@ -8,8 +8,18 @@ namespace SprykerEco\Zed\AmazonQuicksight\Business; use Spryker\Zed\Kernel\Business\AbstractBusinessFactory; +use SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightDependencyProvider; +use SprykerEco\Zed\AmazonQuicksight\Business\ApiClient\AmazonQuicksightApiClient; +use SprykerEco\Zed\AmazonQuicksight\Business\ApiClient\AmazonQuicksightApiClientInterface; +use SprykerEco\Zed\AmazonQuicksight\Business\Creator\QuicksightUserCreator; +use SprykerEco\Zed\AmazonQuicksight\Business\Creator\QuicksightUserCreatorInterface; use SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpander; use SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpanderInterface; +use SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatter; +use SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatterInterface; +use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapper; +use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapperInterface; +use SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface; /** * @method \SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig getConfig() @@ -25,4 +35,52 @@ public function createUserExpander(): UserExpanderInterface { return new UserExpander($this->getRepository()); } + + /** + * @return \SprykerEco\Zed\AmazonQuicksight\Business\Creator\QuicksightUserCreatorInterface + */ + public function createQuicksightUserCreator(): QuicksightUserCreatorInterface + { + return new QuicksightUserCreator( + $this->createAmazonQuicksightApiClient(), + $this->getEntityManager(), + ); + } + + /** + * @return \SprykerEco\Zed\AmazonQuicksight\Business\ApiClient\AmazonQuicksightApiClientInterface + */ + public function createAmazonQuicksightApiClient(): AmazonQuicksightApiClientInterface + { + return new AmazonQuicksightApiClient( + $this->getConfig(), + $this->createQuicksightUserMapper(), + $this->createAmazonQuicksightRequestDataFormatter(), + $this->getAwsQuicksightClient(), + ); + } + + /** + * @return \SprykerEco\Zed\AmazonQuicksight\Business\Mapper\QuicksightUserMapperInterface + */ + public function createQuicksightUserMapper(): QuicksightUserMapperInterface + { + return new QuicksightUserMapper(); + } + + /** + * @return \SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatterInterface + */ + public function createAmazonQuicksightRequestDataFormatter(): AmazonQuicksightRequestDataFormatterInterface + { + return new AmazonQuicksightRequestDataFormatter(); + } + + /** + * @return \SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface + */ + public function getAwsQuicksightClient(): AmazonQuicksightToAwsQuicksightClientInterface + { + return $this->getProvidedDependency(AmazonQuicksightDependencyProvider::AWS_QUICKSIGHT_CLIENT); + } } diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacade.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacade.php index 10ea988..0971368 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacade.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacade.php @@ -7,6 +7,7 @@ namespace SprykerEco\Zed\AmazonQuicksight\Business; +use Generated\Shared\Transfer\UserCollectionResponseTransfer; use Generated\Shared\Transfer\UserCollectionTransfer; use Spryker\Zed\Kernel\Business\AbstractFacade; @@ -31,4 +32,21 @@ public function expandUserCollectionWithQuicksightUsers( ): UserCollectionTransfer { return $this->getFactory()->createUserExpander()->expandUserCollectionWithQuicksightUser($userCollectionTransfer); } + + /** + * {@inheritDoc} + * + * @api + * + * @param \Generated\Shared\Transfer\UserCollectionResponseTransfer $userCollectionResponseTransfer + * + * @return \Generated\Shared\Transfer\UserCollectionResponseTransfer + */ + public function createQuicksightUsersForUserCollectionResponse( + UserCollectionResponseTransfer $userCollectionResponseTransfer + ): UserCollectionResponseTransfer { + return $this->getFactory() + ->createQuicksightUserCreator() + ->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer); + } } diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacadeInterface.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacadeInterface.php index 86069e2..c4071b6 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacadeInterface.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/AmazonQuicksightFacadeInterface.php @@ -7,6 +7,7 @@ namespace SprykerEco\Zed\AmazonQuicksight\Business; +use Generated\Shared\Transfer\UserCollectionResponseTransfer; use Generated\Shared\Transfer\UserCollectionTransfer; interface AmazonQuicksightFacadeInterface @@ -27,4 +28,23 @@ interface AmazonQuicksightFacadeInterface public function expandUserCollectionWithQuicksightUsers( UserCollectionTransfer $userCollectionTransfer ): UserCollectionTransfer; + + /** + * Specification: + * - Expects `UserCollectionResponseTransfer.users.quicksightUser.role` to be set. + * - Does nothing if `UserTransfer.quicksightUser.role` is not set. + * - Sends request to AWS API to register Quicksight users. For more information see {@link https://docs.aws.amazon.com/quicksight/latest/APIReference/API_RegisterUser.html}. + * - Adds errors to `UserCollectionResponseTransfer.errors` if Quicksight user registration failed. + * - Persists successfully registered Quicksight users in the database. + * - Returns `UserCollectionResponseTransfer` with updated `UserTransfers`. + * + * @api + * + * @param \Generated\Shared\Transfer\UserCollectionResponseTransfer $userCollectionResponseTransfer + * + * @return \Generated\Shared\Transfer\UserCollectionResponseTransfer + */ + public function createQuicksightUsersForUserCollectionResponse( + UserCollectionResponseTransfer $userCollectionResponseTransfer + ): UserCollectionResponseTransfer; } diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/ApiClient/AmazonQuicksightApiClient.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/ApiClient/AmazonQuicksightApiClient.php new file mode 100644 index 0000000..f4dddb7 --- /dev/null +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/ApiClient/AmazonQuicksightApiClient.php @@ -0,0 +1,127 @@ +amazonQuicksightConfig = $amazonQuicksightConfig; + $this->quicksightUserMapper = $quicksightUserMapper; + $this->amazonQuicksightRequestDataFormatter = $amazonQuicksightRequestDataFormatter; + $this->amazonQuicksightToAwsQuicksightClient = $amazonQuicksightToAwsQuicksightClient; + } + + /** + * @param \Generated\Shared\Transfer\UserTransfer $userTransfer + * + * @return \Generated\Shared\Transfer\QuicksightUserRegisterResponseTransfer + */ + public function registerUser(UserTransfer $userTransfer): QuicksightUserRegisterResponseTransfer + { + $quicksightUserRegisterRequestTransfer = $this->createQuicksightUserRegisterRequestTransfer(); + $quicksightUserRegisterRequestTransfer = $this->quicksightUserMapper->mapUserTransferToQuicksightUserRegisterRequestTransfer( + $userTransfer, + $quicksightUserRegisterRequestTransfer, + ); + + $requestData = $this->amazonQuicksightRequestDataFormatter->formatRequestData( + $quicksightUserRegisterRequestTransfer->toArray(true, true), + ); + + $quicksightUserRegisterResponseTransfer = new QuicksightUserRegisterResponseTransfer(); + try { + $response = $this->amazonQuicksightToAwsQuicksightClient->registerUser($requestData); + } catch (QuickSightException $quickSightException) { + return $quicksightUserRegisterResponseTransfer->addError( + (new ErrorTransfer())->setMessage($quickSightException->getAwsErrorMessage()), + ); + } + + if (!$response->hasKey(static::RESPONSE_KEY_USER)) { + return $quicksightUserRegisterResponseTransfer->addError( + (new ErrorTransfer())->setMessage(static::ERROR_MESSAGE_USER_REGISTRATION_FAILED), + ); + } + + $quicksightUserTransfer = $this->quicksightUserMapper->mapQuicksightUserDataToQuicksightUserTransfer( + $response->get(static::RESPONSE_KEY_USER), + $userTransfer->getQuicksightUserOrFail(), + ); + + return $quicksightUserRegisterResponseTransfer->setQuicksightUser($quicksightUserTransfer); + } + + /** + * @return \Generated\Shared\Transfer\QuicksightUserRegisterRequestTransfer + */ + protected function createQuicksightUserRegisterRequestTransfer(): QuicksightUserRegisterRequestTransfer + { + return (new QuicksightUserRegisterRequestTransfer()) + ->setIdentityType(static::QUICKSIGHT_USER_IDENTITY_TYPE) + ->setAwsAccountId($this->amazonQuicksightConfig->getAwsAccountId()) + ->setNamespace($this->amazonQuicksightConfig->getQuicksightRegisterUserNamespace()); + } +} diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/ApiClient/AmazonQuicksightApiClientInterface.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/ApiClient/AmazonQuicksightApiClientInterface.php new file mode 100644 index 0000000..d7ebad4 --- /dev/null +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/ApiClient/AmazonQuicksightApiClientInterface.php @@ -0,0 +1,21 @@ +amazonQuicksightApiClient = $amazonQuicksightApiClient; + $this->amazonQuicksightEntityManager = $amazonQuicksightEntityManager; + } + + /** + * @param \Generated\Shared\Transfer\UserCollectionResponseTransfer $userCollectionResponseTransfer + * + * @return \Generated\Shared\Transfer\UserCollectionResponseTransfer + */ + public function createQuicksightUsersForUserCollectionResponse( + UserCollectionResponseTransfer $userCollectionResponseTransfer + ): UserCollectionResponseTransfer { + $userTransfersWithQuicksightUserRole = $this->extractUserTransfersWithQuicksightUserRole( + $userCollectionResponseTransfer->getUsers(), + ); + + if ($userTransfersWithQuicksightUserRole === []) { + return $userCollectionResponseTransfer; + } + + return $this->getTransactionHandler()->handleTransaction(function () use ($userCollectionResponseTransfer, $userTransfersWithQuicksightUserRole) { + return $this->executeCreateQuicksightUsersForUsersTransaction( + $userCollectionResponseTransfer, + $userTransfersWithQuicksightUserRole, + ); + }); + } + + /** + * @param \Generated\Shared\Transfer\UserCollectionResponseTransfer $userCollectionResponseTransfer + * @param array $userTransfers + * + * @return \Generated\Shared\Transfer\UserCollectionResponseTransfer + */ + protected function executeCreateQuicksightUsersForUsersTransaction( + UserCollectionResponseTransfer $userCollectionResponseTransfer, + array $userTransfers + ): UserCollectionResponseTransfer { + foreach ($userTransfers as $entityIdentifier => $userTransfer) { + $quicksightUserRegisterResponseTransfer = $this->amazonQuicksightApiClient->registerUser($userTransfer); + if ($quicksightUserRegisterResponseTransfer->getErrors()->count() !== 0) { + $userCollectionResponseTransfer = $this->addErrorsToUserCollectionResponse( + $userCollectionResponseTransfer, + $quicksightUserRegisterResponseTransfer->getErrors(), + $entityIdentifier, + ); + + continue; + } + + $quicksightUserTransfer = $quicksightUserRegisterResponseTransfer->getQuicksightUserOrFail(); + $quicksightUserTransfer->setFkUser($userTransfer->getIdUserOrFail()); + + $quicksightUserTransfer = $this->amazonQuicksightEntityManager->createQuicksightUser($quicksightUserTransfer); + $userTransfer->setQuicksightUser($quicksightUserTransfer); + } + + return $userCollectionResponseTransfer; + } + + /** + * @param \ArrayObject $userTransfers + * + * @return array + */ + protected function extractUserTransfersWithQuicksightUserRole(ArrayObject $userTransfers): array + { + $extractedUserTransfers = []; + foreach ($userTransfers as $entityIdentifier => $userTransfer) { + if ($userTransfer->getQuicksightUser() && $userTransfer->getQuicksightUserOrFail()->getRole()) { + $extractedUserTransfers[$entityIdentifier] = $userTransfer; + } + } + + return $extractedUserTransfers; + } + + /** + * @param \Generated\Shared\Transfer\UserCollectionResponseTransfer $userCollectionResponseTransfer + * @param \ArrayObject $errorTransfers + * @param string $entityIdentifier + * + * @return \Generated\Shared\Transfer\UserCollectionResponseTransfer + */ + protected function addErrorsToUserCollectionResponse( + UserCollectionResponseTransfer $userCollectionResponseTransfer, + ArrayObject $errorTransfers, + string $entityIdentifier + ): UserCollectionResponseTransfer { + foreach ($errorTransfers as $errorTransfer) { + $errorTransfer->setEntityIdentifier($entityIdentifier); + + $userCollectionResponseTransfer->addError($errorTransfer); + } + + return $userCollectionResponseTransfer; + } +} diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/Creator/QuicksightUserCreatorInterface.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/Creator/QuicksightUserCreatorInterface.php new file mode 100644 index 0000000..ffdc040 --- /dev/null +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/Creator/QuicksightUserCreatorInterface.php @@ -0,0 +1,22 @@ + $requestData + * + * @return array + */ + public function formatRequestData(array $requestData): array + { + $formattedData = []; + foreach ($requestData as $key => $value) { + if (is_array($value)) { + $value = $this->formatRequestData($value); + } + + $formattedData[ucfirst($key)] = $value; + } + + return $formattedData; + } +} diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/Formatter/AmazonQuicksightRequestDataFormatterInterface.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/Formatter/AmazonQuicksightRequestDataFormatterInterface.php new file mode 100644 index 0000000..c5ed2f8 --- /dev/null +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/Formatter/AmazonQuicksightRequestDataFormatterInterface.php @@ -0,0 +1,18 @@ + $requestData + * + * @return array + */ + public function formatRequestData(array $requestData): array; +} diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/Mapper/QuicksightUserMapper.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/Mapper/QuicksightUserMapper.php new file mode 100644 index 0000000..8c01f6a --- /dev/null +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/Mapper/QuicksightUserMapper.php @@ -0,0 +1,44 @@ +setEmail($userTransfer->getUsernameOrFail()) + ->setUserName($userTransfer->getUsernameOrFail()) + ->setUserRole(strtoupper($userTransfer->getQuicksightUserOrFail()->getRoleOrFail())); + } + + /** + * @param array $quicksightUserData + * @param \Generated\Shared\Transfer\QuicksightUserTransfer $quicksightUserTransfer + * + * @return \Generated\Shared\Transfer\QuicksightUserTransfer + */ + public function mapQuicksightUserDataToQuicksightUserTransfer( + array $quicksightUserData, + QuicksightUserTransfer $quicksightUserTransfer + ): QuicksightUserTransfer { + return $quicksightUserTransfer->fromArray($quicksightUserData, true); + } +} diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Business/Mapper/QuicksightUserMapperInterface.php b/src/SprykerEco/Zed/AmazonQuicksight/Business/Mapper/QuicksightUserMapperInterface.php new file mode 100644 index 0000000..09a9d70 --- /dev/null +++ b/src/SprykerEco/Zed/AmazonQuicksight/Business/Mapper/QuicksightUserMapperInterface.php @@ -0,0 +1,37 @@ + $quicksightUserData + * @param \Generated\Shared\Transfer\QuicksightUserTransfer $quicksightUserTransfer + * + * @return \Generated\Shared\Transfer\QuicksightUserTransfer + */ + public function mapQuicksightUserDataToQuicksightUserTransfer( + array $quicksightUserData, + QuicksightUserTransfer $quicksightUserTransfer + ): QuicksightUserTransfer; +} diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Communication/Plugin/User/QuicksightUserPostCreatePlugin.php b/src/SprykerEco/Zed/AmazonQuicksight/Communication/Plugin/User/QuicksightUserPostCreatePlugin.php new file mode 100644 index 0000000..9c473f9 --- /dev/null +++ b/src/SprykerEco/Zed/AmazonQuicksight/Communication/Plugin/User/QuicksightUserPostCreatePlugin.php @@ -0,0 +1,40 @@ +getFacade()->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer); + } +} diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightEntityManager.php b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightEntityManager.php index 1676134..9d8132a 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightEntityManager.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightEntityManager.php @@ -7,6 +7,8 @@ namespace SprykerEco\Zed\AmazonQuicksight\Persistence; +use Generated\Shared\Transfer\QuicksightUserTransfer; +use Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUser; use Spryker\Zed\Kernel\Persistence\AbstractEntityManager; /** @@ -14,4 +16,21 @@ */ class AmazonQuicksightEntityManager extends AbstractEntityManager implements AmazonQuicksightEntityManagerInterface { + /** + * @param \Generated\Shared\Transfer\QuicksightUserTransfer $quicksightUserTransfer + * + * @return \Generated\Shared\Transfer\QuicksightUserTransfer + */ + public function createQuicksightUser(QuicksightUserTransfer $quicksightUserTransfer): QuicksightUserTransfer + { + $quicksightUserEntity = $this->getFactory() + ->createQuicksightUserMapper() + ->mapQuicksightUserTransferToQuicksightUserEntity($quicksightUserTransfer, new SpyQuicksightUser()); + + $quicksightUserEntity->save(); + + return $this->getFactory() + ->createQuicksightUserMapper() + ->mapQuicksightUserEntityToQuicksightUserTransfer($quicksightUserEntity, $quicksightUserTransfer); + } } diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightEntityManagerInterface.php b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightEntityManagerInterface.php index 067d7d0..00e280f 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightEntityManagerInterface.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/AmazonQuicksightEntityManagerInterface.php @@ -7,6 +7,14 @@ namespace SprykerEco\Zed\AmazonQuicksight\Persistence; +use Generated\Shared\Transfer\QuicksightUserTransfer; + interface AmazonQuicksightEntityManagerInterface { + /** + * @param \Generated\Shared\Transfer\QuicksightUserTransfer $quicksightUserTransfer + * + * @return \Generated\Shared\Transfer\QuicksightUserTransfer + */ + public function createQuicksightUser(QuicksightUserTransfer $quicksightUserTransfer): QuicksightUserTransfer; } diff --git a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/Propel/Mapper/QuicksightUserMapper.php b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/Propel/Mapper/QuicksightUserMapper.php index 904cce3..3ca337e 100644 --- a/src/SprykerEco/Zed/AmazonQuicksight/Persistence/Propel/Mapper/QuicksightUserMapper.php +++ b/src/SprykerEco/Zed/AmazonQuicksight/Persistence/Propel/Mapper/QuicksightUserMapper.php @@ -33,13 +33,26 @@ public function mapQuicksightUserEntitiesToQuicksightUserTransfers( return $quicksightUserTransfers; } + /** + * @param \Generated\Shared\Transfer\QuicksightUserTransfer $quicksightUserTransfer + * @param \Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUser $quicksightUserEntity + * + * @return \Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUser + */ + public function mapQuicksightUserTransferToQuicksightUserEntity( + QuicksightUserTransfer $quicksightUserTransfer, + SpyQuicksightUser $quicksightUserEntity + ): SpyQuicksightUser { + return $quicksightUserEntity->fromArray($quicksightUserTransfer->modifiedToArray()); + } + /** * @param \Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUser $quicksightUserEntity * @param \Generated\Shared\Transfer\QuicksightUserTransfer $quicksightUserTransfer * * @return \Generated\Shared\Transfer\QuicksightUserTransfer */ - protected function mapQuicksightUserEntityToQuicksightUserTransfer( + public function mapQuicksightUserEntityToQuicksightUserTransfer( SpyQuicksightUser $quicksightUserEntity, QuicksightUserTransfer $quicksightUserTransfer ): QuicksightUserTransfer { diff --git a/tests/SprykerEcoTest/Zed/AmazonQuicksight/Business/Facade/CreateQuicksightUsersForUserCollectionResponseTest.php b/tests/SprykerEcoTest/Zed/AmazonQuicksight/Business/Facade/CreateQuicksightUsersForUserCollectionResponseTest.php new file mode 100644 index 0000000..3f37464 --- /dev/null +++ b/tests/SprykerEcoTest/Zed/AmazonQuicksight/Business/Facade/CreateQuicksightUsersForUserCollectionResponseTest.php @@ -0,0 +1,253 @@ +tester->haveUserWithNotPersistedQuicksightUserRole(static::QUICKSIGHT_USER_ROLE_READER); + + $this->tester->setDependency( + AmazonQuicksightDependencyProvider::AWS_QUICKSIGHT_CLIENT, + $this->getAwsQuicksightClientMockWithSuccessfulResponse( + $this->createRegisterUserSuccessfulResponse($userTransfer), + ), + ); + + $userCollectionResponseTransfer = (new UserCollectionResponseTransfer())->addUser($userTransfer); + + // Act + $userCollectionResponseTransfer = $this->tester->getFacade() + ->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer); + + // Assert + $this->assertCount(0, $userCollectionResponseTransfer->getErrors()); + $this->assertCount(1, $userCollectionResponseTransfer->getUsers()); + + $quicksightUserEntity = $this->tester->findQuicksightUserByIdUser($userTransfer->getIdUserOrFail()); + $this->assertNotNull($quicksightUserEntity); + + /** @var \Generated\Shared\Transfer\UserTransfer $userTransfer */ + $userTransfer = $userCollectionResponseTransfer->getUsers()->getIterator()->current(); + $this->assertNotNull($userTransfer->getQuicksightUser()); + $this->assertNotNull($userTransfer->getQuicksightUserOrFail()->getIdQuicksightUser()); + $this->assertNotNull($userTransfer->getQuicksightUserOrFail()->getArn()); + $this->assertNotNull($userTransfer->getQuicksightUserOrFail()->getPrincipalId()); + } + + /** + * @return void + */ + public function testShouldReturnErrorWhenUserQuicksightClientThrowsException(): void + { + // Arrange + $userTransfer = $this->tester->haveUserWithNotPersistedQuicksightUserRole(static::QUICKSIGHT_USER_ROLE_READER); + + $this->tester->setDependency( + AmazonQuicksightDependencyProvider::AWS_QUICKSIGHT_CLIENT, + $this->getAwsQuicksightClientMockWithErrorResponse( + $this->getQuicksightExceptionMock(static::ERROR_MESSAGE_QUICKSIGHT_USER_REGISTER_FAILURE), + ), + ); + + $userCollectionResponseTransfer = (new UserCollectionResponseTransfer())->addUser($userTransfer); + + // Act + $userCollectionResponseTransfer = $this->tester->getFacade() + ->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer); + + // Assert + $this->assertCount(1, $userCollectionResponseTransfer->getErrors()); + $this->assertCount(1, $userCollectionResponseTransfer->getUsers()); + + /** @var \Generated\Shared\Transfer\ErrorTransfer $errorTransfer */ + $errorTransfer = $userCollectionResponseTransfer->getErrors()->getIterator()->current(); + $this->assertSame(static::ERROR_MESSAGE_QUICKSIGHT_USER_REGISTER_FAILURE, $errorTransfer->getMessage()); + $this->assertSame((string)$userCollectionResponseTransfer->getUsers()->getIterator()->key(), $errorTransfer->getEntityIdentifier()); + + /** @var \Generated\Shared\Transfer\UserTransfer $userTransfer */ + $userTransfer = $userCollectionResponseTransfer->getUsers()->getIterator()->current(); + $this->assertNotNull($userTransfer->getQuicksightUser()); + $this->assertNull($userTransfer->getQuicksightUserOrFail()->getIdQuicksightUser()); + $this->assertNull($userTransfer->getQuicksightUserOrFail()->getArn()); + $this->assertNull($userTransfer->getQuicksightUserOrFail()->getPrincipalId()); + } + + /** + * @return void + */ + public function testShouldReturnErrorWhenQuicksightUserRegisterResponseDoesNotContainUserKey(): void + { + // Arrange + $userTransfer = $this->tester->haveUserWithNotPersistedQuicksightUserRole(static::QUICKSIGHT_USER_ROLE_READER); + + $this->tester->setDependency( + AmazonQuicksightDependencyProvider::AWS_QUICKSIGHT_CLIENT, + $this->getAwsQuicksightClientMockWithSuccessfulResponse(new Result()), + ); + + $userCollectionResponseTransfer = (new UserCollectionResponseTransfer())->addUser($userTransfer); + + // Act + $userCollectionResponseTransfer = $this->tester->getFacade() + ->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer); + + // Assert + $this->assertCount(1, $userCollectionResponseTransfer->getErrors()); + $this->assertCount(1, $userCollectionResponseTransfer->getUsers()); + + /** @var \Generated\Shared\Transfer\ErrorTransfer $errorTransfer */ + $errorTransfer = $userCollectionResponseTransfer->getErrors()->getIterator()->current(); + $this->assertSame(static::ERROR_MESSAGE_USER_REGISTRATION_FAILED, $errorTransfer->getMessage()); + $this->assertSame((string)$userCollectionResponseTransfer->getUsers()->getIterator()->key(), $errorTransfer->getEntityIdentifier()); + + /** @var \Generated\Shared\Transfer\UserTransfer $userTransfer */ + $userTransfer = $userCollectionResponseTransfer->getUsers()->getIterator()->current(); + $this->assertNotNull($userTransfer->getQuicksightUser()); + $this->assertNull($userTransfer->getQuicksightUserOrFail()->getIdQuicksightUser()); + $this->assertNull($userTransfer->getQuicksightUserOrFail()->getArn()); + $this->assertNull($userTransfer->getQuicksightUserOrFail()->getPrincipalId()); + } + + /** + * @return void + */ + public function testShouldDoNothingWhenQuicksightUserRoleIsNotProvidedForUser(): void + { + // Arrange + $userTransfer = $this->tester->haveUser(); + + $awsQuicksightClientMock = $this->getAwsQuicksightClientMock(); + $awsQuicksightClientMock->expects($this->never())->method('registerUser'); + $this->tester->setDependency( + AmazonQuicksightDependencyProvider::AWS_QUICKSIGHT_CLIENT, + $awsQuicksightClientMock, + ); + + // Act + $userCollectionResponseTransfer = (new UserCollectionResponseTransfer())->addUser($userTransfer); + + // Act + $userCollectionResponseTransfer = $this->tester->getFacade() + ->createQuicksightUsersForUserCollectionResponse($userCollectionResponseTransfer); + + // Assert + $this->assertCount(0, $userCollectionResponseTransfer->getErrors()); + $this->assertCount(1, $userCollectionResponseTransfer->getUsers()); + $this->assertNull($userCollectionResponseTransfer->getUsers()->getIterator()->current()->getQuicksightUser()); + } + + /** + * @param \Generated\Shared\Transfer\UserTransfer $userTransfer + * + * @return \Aws\ResultInterface + */ + protected function createRegisterUserSuccessfulResponse(UserTransfer $userTransfer): ResultInterface + { + $responseData = [ + 'RequestId' => time(), + 'User' => [ + 'Arn' => 'arn:aws:quicksight:eu-central-1:123456789012:user/default/' . $userTransfer->getUsername(), + 'PrincipalId' => '123456789012', + 'Role' => $userTransfer->getQuicksightUserOrFail()->getRoleOrFail(), + ], + ]; + + return new Result($responseData); + } + + /** + * @param \Aws\ResultInterface $result + * + * @return \SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface|\PHPUnit\Framework\MockObject\MockObject + */ + protected function getAwsQuicksightClientMockWithSuccessfulResponse(ResultInterface $result): AmazonQuicksightToAwsQuicksightClientInterface + { + $awsQuicksightClientMock = $this->getAwsQuicksightClientMock(); + $awsQuicksightClientMock->method('registerUser') + ->willReturn($result); + + return $awsQuicksightClientMock; + } + + /** + * @param \Aws\QuickSight\Exception\QuickSightException $quickSightException + * + * @return \SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface|\PHPUnit\Framework\MockObject\MockObject + */ + protected function getAwsQuicksightClientMockWithErrorResponse(QuickSightException $quickSightException): AmazonQuicksightToAwsQuicksightClientInterface + { + $awsQuicksightClientMock = $this->getAwsQuicksightClientMock(); + $awsQuicksightClientMock->method('registerUser') + ->willThrowException($quickSightException); + + return $awsQuicksightClientMock; + } + + /** + * @return \SprykerEco\Zed\AmazonQuicksight\Dependency\External\AmazonQuicksightToAwsQuicksightClientInterface|\PHPUnit\Framework\MockObject\MockObject + */ + protected function getAwsQuicksightClientMock(): AmazonQuicksightToAwsQuicksightClientInterface + { + return $this->getMockBuilder(AmazonQuicksightToAwsQuicksightClientInterface::class)->getMock(); + } + + /** + * @param string $message + * + * @return \Aws\QuickSight\Exception\QuickSightException|\PHPUnit\Framework\MockObject\MockObject + */ + protected function getQuicksightExceptionMock(string $message): QuickSightException + { + $quicksightExceptionMock = $this->getMockBuilder(QuickSightException::class) + ->disableOriginalConstructor() + ->getMock(); + $quicksightExceptionMock->method('getAwsErrorMessage')->willReturn($message); + + return $quicksightExceptionMock; + } +} diff --git a/tests/SprykerEcoTest/Zed/AmazonQuicksight/_support/AmazonQuicksightBusinessTester.php b/tests/SprykerEcoTest/Zed/AmazonQuicksight/_support/AmazonQuicksightBusinessTester.php new file mode 100644 index 0000000..be55dcd --- /dev/null +++ b/tests/SprykerEcoTest/Zed/AmazonQuicksight/_support/AmazonQuicksightBusinessTester.php @@ -0,0 +1,71 @@ +haveUser([ + UserTransfer::QUICKSIGHT_USER => (new QuicksightUserBuilder([ + QuicksightUserTransfer::ROLE => $quicksightUserRole, + ]))->build(), + ]); + } + + /** + * @param int $idUser + * + * @return \Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUser|null + */ + public function findQuicksightUserByIdUser(int $idUser): ?SpyQuicksightUser + { + return $this->getQuicksightUserQuery() + ->filterByFkUser($idUser) + ->findOne(); + } + + /** + * @return \Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUserQuery + */ + protected function getQuicksightUserQuery(): SpyQuicksightUserQuery + { + return SpyQuicksightUserQuery::create(); + } +} diff --git a/tests/SprykerEcoTest/Zed/AmazonQuicksight/codeception.yml b/tests/SprykerEcoTest/Zed/AmazonQuicksight/codeception.yml index 5f849d0..68f8e4f 100644 --- a/tests/SprykerEcoTest/Zed/AmazonQuicksight/codeception.yml +++ b/tests/SprykerEcoTest/Zed/AmazonQuicksight/codeception.yml @@ -1,24 +1,34 @@ namespace: SprykerEcoTest\Zed\AmazonQuicksight + paths: tests: . data: ../../../_data support: _support output: ../../../_output + coverage: enabled: true remote: false - whitelist: { include: ['../../../../src/*'] } + whitelist: + include: + - '../../../../src/*' suites: Business: - path: . - actor: AmazonQuicksightTester + path: Business + actor: AmazonQuicksightBusinessTester modules: enabled: - Asserts - - \SprykerTest\Zed\Testify\Helper\Business\BusinessHelper - - \SprykerTest\Shared\Testify\Helper\DataCleanupHelper - \SprykerTest\Shared\Testify\Helper\Environment - - \SprykerTest\Shared\Testify\Helper\LocatorHelper + - \SprykerTest\Shared\Testify\Helper\ConfigHelper + - \SprykerTest\Shared\Testify\Helper\LocatorHelper: + coreNamespaces: + - 'Spryker' + - 'SprykerEco' + - \SprykerTest\Shared\Testify\Helper\DependencyHelper + - \SprykerTest\Shared\Testify\Helper\DataCleanupHelper - \SprykerTest\Shared\Propel\Helper\TransactionHelper + - \SprykerTest\Zed\Testify\Helper\Business\DependencyProviderHelper + - \SprykerTest\Zed\Testify\Helper\Business\BusinessHelper - \SprykerTest\Shared\User\Helper\UserDataHelper