ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Integration\Model;
|
||||
|
||||
use Magento\Framework\Exception\InputException;
|
||||
use Magento\Integration\Model\Oauth\Token as TokenModel;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\User\Model\User as UserModel;
|
||||
|
||||
/**
|
||||
* Test class for \Magento\Integration\Model\AdminTokenService.
|
||||
*/
|
||||
class AdminTokenServiceTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Integration\Api\AdminTokenServiceInterface
|
||||
*/
|
||||
private $tokenService;
|
||||
|
||||
/**
|
||||
* @var TokenModel
|
||||
*/
|
||||
private $tokenModel;
|
||||
|
||||
/**
|
||||
* @var UserModel
|
||||
*/
|
||||
private $userModel;
|
||||
|
||||
/**
|
||||
* Setup AdminTokenService
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->tokenService = Bootstrap::getObjectManager()->get(\Magento\Integration\Model\AdminTokenService::class);
|
||||
$this->userModel = Bootstrap::getObjectManager()->get(\Magento\User\Model\User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/User/_files/user_with_role.php
|
||||
*/
|
||||
public function testCreateAdminAccessToken()
|
||||
{
|
||||
$adminUserNameFromFixture = 'adminUser';
|
||||
$accessToken = $this->tokenService->createAdminAccessToken(
|
||||
$adminUserNameFromFixture,
|
||||
\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
|
||||
);
|
||||
$this->assertNotNull($accessToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Security/_files/expired_users.php
|
||||
*/
|
||||
public function testCreateAdminAccessTokenExpiredUser()
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\AuthenticationException::class);
|
||||
|
||||
$adminUserNameFromFixture = 'adminUserExpired';
|
||||
$this->tokenService->createAdminAccessToken(
|
||||
$adminUserNameFromFixture,
|
||||
\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
|
||||
);
|
||||
|
||||
$this->expectExceptionMessage(
|
||||
'The account sign-in was incorrect or your account is disabled temporarily. '
|
||||
. 'Please wait and try again later.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validationDataProvider
|
||||
*/
|
||||
public function testCreateAdminAccessTokenEmptyOrNullCredentials($username, $password)
|
||||
{
|
||||
try {
|
||||
$this->tokenService->createAdminAccessToken($username, $password);
|
||||
} catch (InputException $e) {
|
||||
$this->assertInputExceptionMessages($e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function testCreateAdminAccessTokenInvalidCustomer()
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\AuthenticationException::class);
|
||||
|
||||
$adminUserName = 'invalid';
|
||||
$password = 'invalid';
|
||||
$this->tokenService->createAdminAccessToken($adminUserName, $password);
|
||||
|
||||
$this->expectExceptionMessage(
|
||||
'The account sign-in was incorrect or your account is disabled temporarily. '
|
||||
. 'Please wait and try again later.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider to test input validation
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function validationDataProvider()
|
||||
{
|
||||
return [
|
||||
'Check for empty credentials' => ['', ''],
|
||||
'Check for null credentials' => [null, null]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert for presence of Input exception messages
|
||||
*
|
||||
* @param InputException $e
|
||||
*/
|
||||
private function assertInputExceptionMessages($e)
|
||||
{
|
||||
$this->assertEquals('One or more input exceptions have occurred.', $e->getMessage());
|
||||
$errors = $e->getErrors();
|
||||
$this->assertCount(2, $errors);
|
||||
$this->assertEquals('"username" is required. Enter and try again.', $errors[0]->getLogMessage());
|
||||
$this->assertEquals('"password" is required. Enter and try again.', $errors[1]->getLogMessage());
|
||||
}
|
||||
}
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Integration\Model;
|
||||
|
||||
use Magento\Authorization\Model\UserContextInterface;
|
||||
|
||||
/**
|
||||
* Integration authorization service test.
|
||||
*/
|
||||
class AuthorizationServiceTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/** @var AuthorizationService */
|
||||
protected $_service;
|
||||
|
||||
/** @var \Magento\Framework\Authorization */
|
||||
protected $libAuthorization;
|
||||
|
||||
/** @var \Magento\Authorization\Model\UserContextInterface|\PHPUnit\Framework\MockObject\MockObject */
|
||||
protected $userContextMock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->disableOriginalConstructor()->getMock();
|
||||
$loggerMock->expects($this->any())->method('critical')->willReturnSelf();
|
||||
$this->_service = $objectManager->create(
|
||||
\Magento\Integration\Model\AuthorizationService::class,
|
||||
[
|
||||
'logger' => $loggerMock
|
||||
]
|
||||
);
|
||||
|
||||
$this->userContextMock = $this->getMockForAbstractClass(
|
||||
\Magento\Authorization\Model\UserContextInterface::class
|
||||
);
|
||||
$this->userContextMock
|
||||
->expects($this->any())
|
||||
->method('getUserType')
|
||||
->willReturn(UserContextInterface::USER_TYPE_INTEGRATION);
|
||||
$roleLocator = $objectManager->create(
|
||||
\Magento\Webapi\Model\WebapiRoleLocator::class,
|
||||
['userContext' => $this->userContextMock]
|
||||
);
|
||||
$this->libAuthorization = $objectManager->create(
|
||||
\Magento\Framework\Authorization::class,
|
||||
['roleLocator' => $roleLocator]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testGrantPermissions()
|
||||
{
|
||||
$integrationId = rand(1, 1000);
|
||||
$resources = ['Magento_Sales::create', 'Magento_Cms::page', 'Magento_Backend::dashboard'];
|
||||
/** Preconditions check */
|
||||
$this->_ensurePermissionsAreNotGranted($integrationId, $resources);
|
||||
|
||||
$this->_service->grantPermissions($integrationId, $resources);
|
||||
|
||||
/** Validate that access to the specified resources is granted */
|
||||
$this->_ensurePermissionsAreGranted($integrationId, $resources);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $integrationId
|
||||
* @param string[] $initialResources
|
||||
* @param string[] $newResources
|
||||
* @magentoDbIsolation enabled
|
||||
* @dataProvider changePermissionsProvider
|
||||
*/
|
||||
public function testChangePermissions($integrationId, $initialResources, $newResources)
|
||||
{
|
||||
$this->_service->grantPermissions($integrationId, $initialResources);
|
||||
/** Preconditions check */
|
||||
$this->_ensurePermissionsAreGranted($integrationId, $initialResources);
|
||||
$this->_ensurePermissionsAreNotGranted($integrationId, $newResources);
|
||||
|
||||
$this->_service->grantPermissions($integrationId, $newResources);
|
||||
|
||||
/** Check the results of permissions change */
|
||||
$this->_ensurePermissionsAreGranted($integrationId, $newResources);
|
||||
$this->_ensurePermissionsAreNotGranted($integrationId, $initialResources);
|
||||
}
|
||||
|
||||
public function changePermissionsProvider()
|
||||
{
|
||||
return [
|
||||
'integration' => [
|
||||
'integrationId' => rand(1, 1000),
|
||||
'initialResources' => ['Magento_Cms::page', 'Magento_Backend::dashboard'],
|
||||
'newResources' => ['Magento_Sales::cancel', 'Magento_Cms::page_delete'],
|
||||
],
|
||||
'integration clear permissions' => [
|
||||
'integrationId' => rand(1, 1000),
|
||||
'initialResources' => ['Magento_Sales::capture', 'Magento_Cms::page_delete'],
|
||||
'newResources' => [],
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testGrantAllPermissions()
|
||||
{
|
||||
$integrationId = rand(1, 1000);
|
||||
$this->_service->grantAllPermissions($integrationId);
|
||||
$this->_ensurePermissionsAreGranted($integrationId, ['Magento_Backend::all']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has access to the specified resources.
|
||||
*
|
||||
* @param int $integrationId
|
||||
* @param string[] $resources
|
||||
*/
|
||||
protected function _ensurePermissionsAreGranted($integrationId, $resources)
|
||||
{
|
||||
$this->userContextMock
|
||||
->expects($this->any())
|
||||
->method('getUserId')
|
||||
->willReturn($integrationId);
|
||||
foreach ($resources as $resource) {
|
||||
$this->assertTrue(
|
||||
$this->libAuthorization->isAllowed($resource),
|
||||
"Access to resource '{$resource}' is prohibited while it is expected to be granted."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if access to the specified resources is prohibited to the user.
|
||||
*
|
||||
* @param int $integrationId
|
||||
* @param string[] $resources
|
||||
*/
|
||||
protected function _ensurePermissionsAreNotGranted($integrationId, $resources)
|
||||
{
|
||||
$this->userContextMock
|
||||
->expects($this->any())
|
||||
->method('getUserId')
|
||||
->willReturn($integrationId);
|
||||
foreach ($resources as $resource) {
|
||||
$this->assertFalse(
|
||||
$this->libAuthorization->isAllowed($resource),
|
||||
"Access to resource '{$resource}' is expected to be prohibited."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*
|
||||
*/
|
||||
namespace Magento\Integration\Model\Config\Consolidated;
|
||||
|
||||
use Magento\Integration\Model\Config\Consolidated\Reader as ConfigReader;
|
||||
|
||||
/**
|
||||
* Integration config reader test.
|
||||
*/
|
||||
class ReaderTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
||||
protected $fileResolverMock;
|
||||
|
||||
/** @var ConfigReader */
|
||||
protected $configReader;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->fileResolverMock = $this->getMockBuilder(\Magento\Framework\Config\FileResolverInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods([])
|
||||
->getMock();
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->configReader = $objectManager->create(
|
||||
\Magento\Integration\Model\Config\Consolidated\Reader::class,
|
||||
['fileResolver' => $this->fileResolverMock]
|
||||
);
|
||||
}
|
||||
|
||||
public function testRead()
|
||||
{
|
||||
$configFiles = [
|
||||
file_get_contents(realpath(__DIR__ . '/_files/integrationA.xml')),
|
||||
file_get_contents(realpath(__DIR__ . '/_files/integrationB.xml'))
|
||||
];
|
||||
$this->fileResolverMock->expects($this->any())->method('get')->willReturn($configFiles);
|
||||
|
||||
$expectedResult = require __DIR__ . '/_files/integration.php';
|
||||
$this->assertEquals($expectedResult, $this->configReader->read(), 'Error happened during config reading.');
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
return [
|
||||
'TestIntegration1' => [
|
||||
'email' => 'test-integration1@example.com',
|
||||
'endpoint_url' => 'http://example.com/endpoint1',
|
||||
'identity_link_url' => 'http://www.example.com/identity1',
|
||||
'resource' => [
|
||||
'Magento_Backend::admin',
|
||||
'Magento_Customer::customer',
|
||||
'Magento_Customer::manage',
|
||||
'Magento_Sales::sales',
|
||||
'Magento_Sales::sales_operation',
|
||||
'Magento_Sales::sales_order',
|
||||
'Magento_Sales::actions',
|
||||
'Magento_Sales::capture',
|
||||
'Magento_Backend::marketing',
|
||||
'Magento_CatalogRule::promo',
|
||||
'Magento_SalesRule::quote'
|
||||
]
|
||||
],
|
||||
'TestIntegration2' => [
|
||||
'email' => 'test-integration2@example.com',
|
||||
'endpoint_url' => 'http://example.com/integration2',
|
||||
'identity_link_url' => 'http://www.example.com/identity2',
|
||||
'resource' => [
|
||||
'Magento_Backend::admin',
|
||||
'Magento_Sales::sales',
|
||||
'Magento_Sales::sales_operation',
|
||||
'Magento_Sales::sales_order',
|
||||
'Magento_Sales::actions',
|
||||
'Magento_Sales::capture',
|
||||
'Magento_Backend::stores',
|
||||
'Magento_Backend::stores_settings',
|
||||
'Magento_Config::config',
|
||||
'Magento_SalesRule::config_promo'
|
||||
]
|
||||
],
|
||||
'TestIntegration3' => [
|
||||
'email' => 'test-integration3@example.com',
|
||||
'resource' => [
|
||||
'Magento_Backend::admin',
|
||||
'Magento_Sales::sales',
|
||||
'Magento_Sales::sales_operation',
|
||||
'Magento_Sales::sales_order',
|
||||
'Magento_Sales::actions',
|
||||
'Magento_Sales::create',
|
||||
'Magento_Backend::marketing',
|
||||
'Magento_CatalogRule::promo',
|
||||
'Magento_SalesRule::quote'
|
||||
]
|
||||
]
|
||||
];
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
-->
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Integration:etc/integration/integration.xsd">
|
||||
<integration name="TestIntegration1">
|
||||
<email>test-integration1@example.com</email>
|
||||
<endpoint_url>http://example.com/endpoint1</endpoint_url>
|
||||
<identity_link_url>http://www.example.com/identity1</identity_link_url>
|
||||
<resources>
|
||||
<resource name="Magento_Customer::manage" />
|
||||
<resource name="Magento_Sales::capture" />
|
||||
<resource name="Magento_SalesRule::quote" />
|
||||
</resources>
|
||||
</integration>
|
||||
<integration name="TestIntegration2">
|
||||
<email>test-integration2@example.com</email>
|
||||
<resources>
|
||||
<resource name="Magento_Sales::capture" />
|
||||
</resources>
|
||||
</integration>
|
||||
<integration name="TestIntegration3">
|
||||
<email>test-integration3@example.com</email>
|
||||
<resources>
|
||||
<resource name="Magento_Sales::create" />
|
||||
<resource name="Magento_SalesRule::quote" />
|
||||
</resources>
|
||||
</integration>
|
||||
</config>
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
-->
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Integration:etc/integration/integration_file.xsd">
|
||||
<integration name="TestIntegration2">
|
||||
<!--Extension of data provided in integrationA.xml-->
|
||||
<endpoint_url>http://example.com/integration2</endpoint_url>
|
||||
<identity_link_url>http://www.example.com/identity2</identity_link_url>
|
||||
<resources>
|
||||
<resource name="Magento_SalesRule::config_promo" />
|
||||
</resources>
|
||||
</integration>
|
||||
</config>
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*
|
||||
*/
|
||||
namespace Magento\Integration\Model\Config\Integration;
|
||||
|
||||
use Magento\Integration\Model\Config\Integration\Reader as ConfigReader;
|
||||
|
||||
/**
|
||||
* Integration API config reader test.
|
||||
*/
|
||||
class ReaderTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
||||
protected $_fileResolverMock;
|
||||
|
||||
/** @var ConfigReader */
|
||||
protected $_configReader;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->_fileResolverMock = $this->createMock(\Magento\Framework\Config\FileResolverInterface::class);
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->_configReader = $objectManager->create(
|
||||
\Magento\Integration\Model\Config\Integration\Reader::class,
|
||||
['fileResolver' => $this->_fileResolverMock]
|
||||
);
|
||||
}
|
||||
|
||||
public function testRead()
|
||||
{
|
||||
$configFiles = [
|
||||
file_get_contents(realpath(__DIR__ . '/_files/apiA.xml')),
|
||||
file_get_contents(realpath(__DIR__ . '/_files/apiB.xml')),
|
||||
];
|
||||
$this->_fileResolverMock->expects($this->any())->method('get')->willReturn($configFiles);
|
||||
|
||||
$expectedResult = require __DIR__ . '/_files/api.php';
|
||||
$this->assertEquals($expectedResult, $this->_configReader->read(), 'Error happened during config reading.');
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
return [
|
||||
'TestIntegration1' => [
|
||||
'resource' => [
|
||||
'Magento_Customer::manage',
|
||||
'Magento_Customer::online',
|
||||
'Magento_Sales::capture',
|
||||
'Magento_SalesRule::quote',
|
||||
],
|
||||
],
|
||||
'TestIntegration2' => [
|
||||
'resource' => ['Magento_Catalog::product_read', 'Magento_SalesRule::config_promo'],
|
||||
],
|
||||
'TestIntegration3' => [
|
||||
'resource' => ['Magento_Catalog::product_read', 'Magento_Sales::create', 'Magento_SalesRule::quote'],
|
||||
]
|
||||
];
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
-->
|
||||
<integrations>
|
||||
<integration name="TestIntegration1">
|
||||
<!-- List of API resources required by the integration. These are resource ids defined
|
||||
in etc/acl.xml of modules and subsequently mapped to web APIs in webapi.xml. -->
|
||||
<resources>
|
||||
<resource name="Magento_Customer::manage" />
|
||||
<resource name="Magento_Customer::online" />
|
||||
<resource name="Magento_Sales::capture" />
|
||||
<resource name="Magento_SalesRule::quote" />
|
||||
</resources>
|
||||
</integration>
|
||||
<integration name="TestIntegration2">
|
||||
<resources>
|
||||
<resource name="Magento_Catalog::product_read" />
|
||||
</resources>
|
||||
</integration>
|
||||
</integrations>
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
-->
|
||||
<integrations>
|
||||
<integration name="TestIntegration2">
|
||||
<resources>
|
||||
<!--Extension of resources list defined in apiA.xml-->
|
||||
<resource name="Magento_SalesRule::config_promo" />
|
||||
</resources>
|
||||
</integration>
|
||||
<integration name="TestIntegration3">
|
||||
<resources>
|
||||
<resource name="Magento_Catalog::product_read" />
|
||||
<resource name="Magento_Sales::create" />
|
||||
<resource name="Magento_SalesRule::quote" />
|
||||
</resources>
|
||||
</integration>
|
||||
</integrations>
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*
|
||||
*/
|
||||
namespace Magento\Integration\Model\Config;
|
||||
|
||||
use Magento\Integration\Model\Config\Reader as ConfigReader;
|
||||
|
||||
/**
|
||||
* Integration config reader test.
|
||||
*/
|
||||
class ReaderTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
||||
protected $_fileResolverMock;
|
||||
|
||||
/** @var ConfigReader */
|
||||
protected $_configReader;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->_fileResolverMock = $this->createMock(\Magento\Framework\Config\FileResolverInterface::class);
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->_configReader = $objectManager->create(
|
||||
\Magento\Integration\Model\Config\Reader::class,
|
||||
['fileResolver' => $this->_fileResolverMock]
|
||||
);
|
||||
}
|
||||
|
||||
public function testRead()
|
||||
{
|
||||
$configFiles = [
|
||||
file_get_contents(realpath(__DIR__ . '/_files/configA.xml')),
|
||||
file_get_contents(realpath(__DIR__ . '/_files/configB.xml')),
|
||||
];
|
||||
$this->_fileResolverMock->expects($this->any())->method('get')->willReturn($configFiles);
|
||||
|
||||
$expectedResult = require __DIR__ . '/_files/integration.php';
|
||||
$this->assertEquals($expectedResult, $this->_configReader->read(), 'Error happened during config reading.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
-->
|
||||
<integrations>
|
||||
<integration name="TestIntegration1">
|
||||
<email>test-integration1@example.com</email>
|
||||
<endpoint_url>http://example.com/endpoint1</endpoint_url>
|
||||
<identity_link_url>http://www.example.com/identity1</identity_link_url>
|
||||
</integration>
|
||||
<integration name="TestIntegration2">
|
||||
<email>test-integration2@example.com</email>
|
||||
</integration>
|
||||
</integrations>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
-->
|
||||
<integrations>
|
||||
<integration name="TestIntegration2">
|
||||
<!--Extension of data provided in integrationA.xml-->
|
||||
<endpoint_url>http://example.com/integration2</endpoint_url>
|
||||
<identity_link_url>http://www.example.com/identity2</identity_link_url>
|
||||
</integration>
|
||||
<integration name="TestIntegration3">
|
||||
<email>test-integration3@example.com</email>
|
||||
</integration>
|
||||
</integrations>
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
return [
|
||||
'TestIntegration1' => [
|
||||
'email' => 'test-integration1@example.com',
|
||||
'endpoint_url' => 'http://example.com/endpoint1',
|
||||
'identity_link_url' => 'http://www.example.com/identity1',
|
||||
],
|
||||
'TestIntegration2' => [
|
||||
'email' => 'test-integration2@example.com',
|
||||
'endpoint_url' => 'http://example.com/integration2',
|
||||
'identity_link_url' => 'http://www.example.com/identity2',
|
||||
],
|
||||
'TestIntegration3' => ['email' => 'test-integration3@example.com']
|
||||
];
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Integration\Model;
|
||||
|
||||
/**
|
||||
* Test class for \Magento\Integration\Model\ConfigBasedIntegrationManager.php.
|
||||
*/
|
||||
class ConfigBasedIntegrationManagerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
protected $consolidatedMock;
|
||||
|
||||
/**
|
||||
* @var \Magento\Integration\Model\ConfigBasedIntegrationManager
|
||||
*/
|
||||
protected $integrationManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Integration\Api\IntegrationServiceInterface
|
||||
*/
|
||||
protected $integrationService;
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\ObjectManager
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->consolidatedMock = $this->createMock(\Magento\Integration\Model\ConsolidatedConfig::class);
|
||||
$this->objectManager->addSharedInstance(
|
||||
$this->consolidatedMock,
|
||||
\Magento\Integration\Model\ConsolidatedConfig::class
|
||||
);
|
||||
$this->integrationManager = $this->objectManager->create(
|
||||
\Magento\Integration\Model\ConfigBasedIntegrationManager::class,
|
||||
[]
|
||||
);
|
||||
$this->integrationService = $this->objectManager->create(
|
||||
\Magento\Integration\Api\IntegrationServiceInterface::class,
|
||||
[]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->objectManager->removeSharedInstance(\Magento\Integration\Model\ConsolidatedConfig::class);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testProcessConfigBasedIntegrations()
|
||||
{
|
||||
$newIntegrations = require __DIR__ . '/Config/Consolidated/_files/integration.php';
|
||||
$this->consolidatedMock
|
||||
->expects($this->any())
|
||||
->method('getIntegrations')
|
||||
->willReturn($newIntegrations);
|
||||
|
||||
// Check that the integrations do not exist already
|
||||
foreach ($newIntegrations as $integrationName => $integrationData) {
|
||||
$integration = $this->integrationService->findByName($integrationName);
|
||||
$this->assertNull($integration->getId(), 'Integration already exists');
|
||||
}
|
||||
|
||||
// Create new integrations
|
||||
$this->assertEquals(
|
||||
$newIntegrations,
|
||||
$this->integrationManager->processConfigBasedIntegrations($newIntegrations),
|
||||
'Error processing config based integrations.'
|
||||
);
|
||||
$createdIntegrations = [];
|
||||
|
||||
// Check that the integrations are new with "inactive" status
|
||||
foreach ($newIntegrations as $integrationName => $integrationData) {
|
||||
$integration = $this->integrationService->findByName($integrationName);
|
||||
$this->assertNotEmpty($integration->getId(), 'Integration was not created');
|
||||
$this->assertEquals(
|
||||
$integration::STATUS_INACTIVE,
|
||||
$integration->getStatus(),
|
||||
'Integration is not created with "inactive" status'
|
||||
);
|
||||
$createdIntegrations[$integrationName] = $integration;
|
||||
}
|
||||
|
||||
// Rerun integration creation with the same data (data has not changed)
|
||||
$this->assertEquals(
|
||||
$newIntegrations,
|
||||
$this->integrationManager->processConfigBasedIntegrations($newIntegrations),
|
||||
'Error processing config based integrations.'
|
||||
);
|
||||
|
||||
// Check that the integrations are not recreated when data has not actually changed
|
||||
foreach ($newIntegrations as $integrationName => $integrationData) {
|
||||
$integration = $this->integrationService->findByName($integrationName);
|
||||
$this->assertEquals(
|
||||
$createdIntegrations[$integrationName]->getId(),
|
||||
$integration->getId(),
|
||||
'Integration ID has changed'
|
||||
);
|
||||
$this->assertEquals(
|
||||
$createdIntegrations[$integrationName]->getStatus(),
|
||||
$integration->getStatus(),
|
||||
'Integration status has changed'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Integration\Model;
|
||||
|
||||
use Magento\Customer\Api\AccountManagementInterface;
|
||||
use Magento\Framework\Exception\InputException;
|
||||
use Magento\Integration\Model\Oauth\Token as TokenModel;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* Test class for \Magento\Integration\Model\CustomerTokenService.
|
||||
*/
|
||||
class CustomerTokenServiceTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var CustomerTokenServiceInterface
|
||||
*/
|
||||
private $tokenService;
|
||||
|
||||
/**
|
||||
* @var AccountManagementInterface
|
||||
*/
|
||||
private $accountManagement;
|
||||
|
||||
/**
|
||||
* @var TokenModel
|
||||
*/
|
||||
private $tokenModel;
|
||||
|
||||
/**
|
||||
* Setup CustomerTokenService
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->tokenService = Bootstrap::getObjectManager()->get(
|
||||
\Magento\Integration\Model\CustomerTokenService::class
|
||||
);
|
||||
$this->accountManagement = Bootstrap::getObjectManager()->get(
|
||||
\Magento\Customer\Api\AccountManagementInterface::class
|
||||
);
|
||||
$this->tokenModel = Bootstrap::getObjectManager()->get(\Magento\Integration\Model\Oauth\Token::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
*/
|
||||
public function testCreateCustomerAccessToken()
|
||||
{
|
||||
$customerUserName = 'customer@example.com';
|
||||
$password = 'password';
|
||||
$accessToken = $this->tokenService->createCustomerAccessToken($customerUserName, $password);
|
||||
$this->assertNotNull($accessToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validationDataProvider
|
||||
*/
|
||||
public function testCreateCustomerAccessTokenEmptyOrNullCredentials($username, $password)
|
||||
{
|
||||
try {
|
||||
$this->tokenService->createCustomerAccessToken($username, $password);
|
||||
} catch (InputException $e) {
|
||||
$this->assertInputExceptionMessages($e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function testCreateCustomerAccessTokenInvalidCustomer()
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\AuthenticationException::class);
|
||||
|
||||
$customerUserName = 'invalid';
|
||||
$password = 'invalid';
|
||||
$this->tokenService->createCustomerAccessToken($customerUserName, $password);
|
||||
|
||||
$this->expectExceptionMessage(
|
||||
'The account sign-in was incorrect or your account is disabled temporarily. '
|
||||
. 'Please wait and try again later.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider to test input validation
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function validationDataProvider()
|
||||
{
|
||||
return [
|
||||
'Check for empty credentials' => ['', ''],
|
||||
'Check for null credentials' => [null, null]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert for presence of Input exception messages
|
||||
*
|
||||
* @param InputException $e
|
||||
*/
|
||||
private function assertInputExceptionMessages($e)
|
||||
{
|
||||
$this->assertEquals('One or more input exceptions have occurred.', $e->getMessage());
|
||||
$errors = $e->getErrors();
|
||||
$this->assertCount(2, $errors);
|
||||
$this->assertEquals('"username" is required. Enter and try again.', $errors[0]->getLogMessage());
|
||||
$this->assertEquals('"password" is required. Enter and try again.', $errors[1]->getLogMessage());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Integration\Model\OpaqueToken;
|
||||
|
||||
use Magento\Authorization\Model\UserContextInterface;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Integration\Model\CustomUserContext;
|
||||
use Magento\Integration\Model\UserToken\UserTokenParameters;
|
||||
use Magento\Integration\Model\UserToken\UserTokenParametersFactory;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Magento\User\Model\User as UserModel;
|
||||
|
||||
class IssuerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Issuer
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* @var CustomerRepositoryInterface
|
||||
*/
|
||||
private $customerRepo;
|
||||
|
||||
/**
|
||||
* @var UserModel
|
||||
*/
|
||||
private $userModel;
|
||||
|
||||
/**
|
||||
* @var UserTokenParametersFactory
|
||||
*/
|
||||
private $paramsFactory;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
|
||||
$this->model = $objectManager->get(Issuer::class);
|
||||
$this->customerRepo = $objectManager->get(CustomerRepositoryInterface::class);
|
||||
$this->userModel = $objectManager->create(UserModel::class);
|
||||
$this->paramsFactory = $objectManager->get(UserTokenParametersFactory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a token can be issued for a customer.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Throwable
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
*/
|
||||
public function testIssueForCustomer(): void
|
||||
{
|
||||
$customer = $this->customerRepo->get('customer@example.com');
|
||||
/** @var UserTokenParameters $params */
|
||||
$params = $this->paramsFactory->create();
|
||||
$token = $this->model->create(
|
||||
new CustomUserContext((int) $customer->getId(), UserContextInterface::USER_TYPE_CUSTOMER),
|
||||
$params
|
||||
);
|
||||
|
||||
$this->assertNotEmpty($token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a token can be issued for an admin user.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Throwable
|
||||
* @magentoDataFixture Magento/User/_files/user_with_role.php
|
||||
*/
|
||||
public function testIssueForAdmin(): void
|
||||
{
|
||||
$admin = $this->userModel->loadByUsername('adminUser');
|
||||
/** @var UserTokenParameters $params */
|
||||
$params = $this->paramsFactory->create();
|
||||
$token = $this->model->create(
|
||||
new CustomUserContext((int) $admin->getId(), UserContextInterface::USER_TYPE_ADMIN),
|
||||
$params
|
||||
);
|
||||
|
||||
$this->assertNotEmpty($token);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Integration\Model\OpaqueToken;
|
||||
|
||||
use Magento\Authorization\Model\UserContextInterface;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Integration\Model\CustomUserContext;
|
||||
use Magento\Integration\Model\UserToken\UserTokenParameters;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\User\Model\User as UserModel;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Magento\Integration\Model\UserToken\UserTokenParametersFactory;
|
||||
|
||||
class ReaderTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Reader
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* @var CustomerRepositoryInterface
|
||||
*/
|
||||
private $customerRepo;
|
||||
|
||||
/**
|
||||
* @var UserModel
|
||||
*/
|
||||
private $userModel;
|
||||
|
||||
/**
|
||||
* @var UserTokenParametersFactory
|
||||
*/
|
||||
private $paramsFactory;
|
||||
|
||||
/**
|
||||
* @var Issuer
|
||||
*/
|
||||
private $issuer;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
|
||||
$this->model = $objectManager->get(Reader::class);
|
||||
$this->customerRepo = $objectManager->get(CustomerRepositoryInterface::class);
|
||||
$this->userModel = $objectManager->create(UserModel::class);
|
||||
$this->paramsFactory = $objectManager->get(UserTokenParametersFactory::class);
|
||||
$this->issuer = $objectManager->get(Issuer::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a token can be accepted for a customer.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Throwable
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
*/
|
||||
public function testReadingCustomer(): void
|
||||
{
|
||||
//Preparing the token
|
||||
$customer = $this->customerRepo->get('customer@example.com');
|
||||
/** @var UserTokenParameters $params */
|
||||
$params = $this->paramsFactory->create(
|
||||
['issued' => $issued = (new \DateTimeImmutable())->sub(new \DateInterval('PT1H'))]
|
||||
);
|
||||
$token = $this->issuer->create(
|
||||
new CustomUserContext((int) $customer->getId(), UserContextInterface::USER_TYPE_CUSTOMER),
|
||||
$params
|
||||
);
|
||||
|
||||
$data = $this->model->read($token);
|
||||
$this->assertEquals(UserContextInterface::USER_TYPE_CUSTOMER, $data->getUserContext()->getUserType());
|
||||
$this->assertEquals((int) $customer->getId(), $data->getUserContext()->getUserId());
|
||||
$this->assertEquals($issued->format('Y-m-d H:i:s'), $data->getData()->getIssued()->format('Y-m-d H:i:s'));
|
||||
$this->assertGreaterThan($issued, $data->getData()->getExpires());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a token can be accepted for an admin user.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Throwable
|
||||
* @magentoDataFixture Magento/User/_files/user_with_role.php
|
||||
*/
|
||||
public function testRadingAdmin(): void
|
||||
{
|
||||
//Preparing the token
|
||||
$admin = $this->userModel->loadByUsername('adminUser');
|
||||
/** @var UserTokenParameters $params */
|
||||
$params = $this->paramsFactory->create();
|
||||
$token = $this->issuer->create(
|
||||
new CustomUserContext((int) $admin->getId(), UserContextInterface::USER_TYPE_ADMIN),
|
||||
$params
|
||||
);
|
||||
|
||||
$data = $this->model->read($token);
|
||||
$this->assertEquals(UserContextInterface::USER_TYPE_ADMIN, $data->getUserContext()->getUserType());
|
||||
$this->assertEquals((int) $admin->getId(), $data->getUserContext()->getUserId());
|
||||
$this->assertGreaterThan($data->getData()->getIssued(), $data->getData()->getExpires());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Integration\Model\OpaqueToken;
|
||||
|
||||
use Magento\Authorization\Model\UserContextInterface;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Integration\Api\Exception\UserTokenException;
|
||||
use Magento\Integration\Model\CustomUserContext;
|
||||
use Magento\Integration\Model\UserToken\UserTokenParameters;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\User\Model\User as UserModel;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Magento\Integration\Model\UserToken\UserTokenParametersFactory;
|
||||
|
||||
class RevokerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Revoker
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* @var Reader
|
||||
*/
|
||||
private $reader;
|
||||
|
||||
/**
|
||||
* @var CustomerRepositoryInterface
|
||||
*/
|
||||
private $customerRepo;
|
||||
|
||||
/**
|
||||
* @var UserModel
|
||||
*/
|
||||
private $userModel;
|
||||
|
||||
/**
|
||||
* @var UserTokenParametersFactory
|
||||
*/
|
||||
private $paramsFactory;
|
||||
|
||||
/**
|
||||
* @var Issuer
|
||||
*/
|
||||
private $issuer;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
|
||||
$this->model = $objectManager->get(Revoker::class);
|
||||
$this->reader = $objectManager->get(Reader::class);
|
||||
$this->customerRepo = $objectManager->get(CustomerRepositoryInterface::class);
|
||||
$this->userModel = $objectManager->create(UserModel::class);
|
||||
$this->paramsFactory = $objectManager->get(UserTokenParametersFactory::class);
|
||||
$this->issuer = $objectManager->get(Issuer::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that tokens are actually being revoked.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Throwable
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
*/
|
||||
public function testRevoking(): void
|
||||
{
|
||||
//Preparing the token
|
||||
$customer = $this->customerRepo->get('customer@example.com');
|
||||
/** @var UserTokenParameters $params */
|
||||
$params = $this->paramsFactory->create();
|
||||
$token = $this->issuer->create(
|
||||
new CustomUserContext((int) $customer->getId(), UserContextInterface::USER_TYPE_CUSTOMER),
|
||||
$params
|
||||
);
|
||||
|
||||
$this->model->revokeFor(
|
||||
new CustomUserContext((int) $customer->getId(), UserContextInterface::USER_TYPE_CUSTOMER)
|
||||
);
|
||||
$this->expectException(UserTokenException::class);
|
||||
$this->reader->read($token);
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Integration\Model\ResourceModel;
|
||||
|
||||
/**
|
||||
* Integration test for \Magento\Integration\Model\ResourceModel\Integration
|
||||
*/
|
||||
class IntegrationTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Integration\Model\Integration
|
||||
*/
|
||||
protected $integration;
|
||||
|
||||
/**
|
||||
* @var \Magento\Integration\Model\Oauth\Consumer
|
||||
*/
|
||||
protected $consumer;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->consumer = $objectManager->create(\Magento\Integration\Model\Oauth\Consumer::class);
|
||||
$this->consumer->setData(
|
||||
[
|
||||
// md5() here just to generate unique string
|
||||
// phpcs:disable Magento2.Security.InsecureFunction
|
||||
'key' => md5(uniqid()),
|
||||
'secret' => md5(uniqid()),
|
||||
// phpcs:enable
|
||||
'callback_url' => 'http://example.com/callback',
|
||||
'rejected_callback_url' => 'http://example.com/rejectedCallback'
|
||||
]
|
||||
)->save();
|
||||
$this->integration = $objectManager->create(\Magento\Integration\Model\Integration::class);
|
||||
$this->integration->setName('Test Integration')
|
||||
->setConsumerId($this->consumer->getId())
|
||||
->setStatus(\Magento\Integration\Model\Integration::STATUS_ACTIVE)
|
||||
->save();
|
||||
}
|
||||
|
||||
public function testLoadActiveIntegrationByConsumerId()
|
||||
{
|
||||
$integration = $this->integration->getResource()->selectActiveIntegrationByConsumerId($this->consumer->getId());
|
||||
$this->assertEquals($this->integration->getId(), $integration['integration_id']);
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
namespace Magento\Integration\Model\ResourceModel\Oauth;
|
||||
|
||||
use Magento\Framework\Oauth\Helper\Oauth;
|
||||
use Magento\Integration\Model\Oauth\Consumer as ConsumerModel;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
|
||||
/**
|
||||
* Integration test for @see \Magento\Integration\Model\ResourceModel\Oauth\Consumer
|
||||
*
|
||||
*/
|
||||
class ConsumerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ConsumerModel
|
||||
*/
|
||||
private $consumerModel;
|
||||
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var Oauth
|
||||
*/
|
||||
private $oauthHelper;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->oauthHelper = $this->objectManager->create(Oauth::class);
|
||||
$this->consumerModel = $this->objectManager->create(ConsumerModel::class);
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testSave(): void
|
||||
{
|
||||
$consumerSecret = $this->oauthHelper->generateConsumerSecret();
|
||||
$consumerKey = $this->oauthHelper->generateConsumerKey();
|
||||
$this->consumerModel->setData(
|
||||
[
|
||||
'key' => $consumerKey,
|
||||
'secret' => $consumerSecret,
|
||||
]
|
||||
)->save();
|
||||
|
||||
$consumerResourceModel = $this->consumerModel->getResource();
|
||||
|
||||
$this->assertEquals($consumerSecret, $this->consumerModel->getSecret());
|
||||
$this->assertNotEquals(
|
||||
$this->consumerModel->getSecret(),
|
||||
$consumerResourceModel->load($this->consumerModel, 'secret')
|
||||
);
|
||||
}
|
||||
}
|
||||
+311
@@ -0,0 +1,311 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Integration\Model\ResourceModel\Oauth;
|
||||
|
||||
use Magento\Authorization\Model\UserContextInterface;
|
||||
use Magento\Framework\Oauth\Helper\Oauth;
|
||||
use Magento\Integration\Model\Oauth\Token;
|
||||
|
||||
/**
|
||||
* Integration test for @see \Magento\Integration\Model\ResourceModel\Oauth\Token
|
||||
*
|
||||
* Also tests @see \Magento\Integration\Cron\CleanExpiredTokens
|
||||
*/
|
||||
class TokenTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public const TOKEN_LIFETIME = 1; // in hours
|
||||
|
||||
public const BASE_CREATED_AT_TIMESTAMP = 100000;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $generatedTokens;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Stdlib\DateTime\DateTime | \PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
private $dateTimeMock;
|
||||
|
||||
/**
|
||||
* @var \Magento\Integration\Model\ResourceModel\Oauth\Token
|
||||
*/
|
||||
private $tokenResourceModel;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Integration\Model\Oauth\TokenFactory
|
||||
*/
|
||||
private $tokenFactory;
|
||||
|
||||
/**
|
||||
* @var Oauth
|
||||
*/
|
||||
private $oauthHelper;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->tokenFactory = $this->objectManager->create(\Magento\Integration\Model\Oauth\TokenFactory::class);
|
||||
|
||||
/** Mock date model to be able to specify "current timestamp" and avoid dependency on real timestamp */
|
||||
$this->dateTimeMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
/** @var \Magento\Integration\Model\ResourceModel\Oauth\Token $tokenResourceModel */
|
||||
$this->tokenResourceModel = $this->objectManager->create(
|
||||
\Magento\Integration\Model\ResourceModel\Oauth\Token::class,
|
||||
['date' => $this->dateTimeMock]
|
||||
);
|
||||
$this->oauthHelper = $this->objectManager->create(Oauth::class);
|
||||
|
||||
$this->generatedTokens = $this->generateTokens();
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function generateTokens()
|
||||
{
|
||||
/** Generate several tokens with different user types and created at combinations */
|
||||
$tokensToBeGenerated = [
|
||||
'#1' => [
|
||||
'userType' => UserContextInterface::USER_TYPE_ADMIN,
|
||||
'createdAt' => self::BASE_CREATED_AT_TIMESTAMP
|
||||
],
|
||||
'#2' => [
|
||||
'userType' => UserContextInterface::USER_TYPE_ADMIN,
|
||||
'createdAt' => self::BASE_CREATED_AT_TIMESTAMP + 5
|
||||
],
|
||||
'#3' => [
|
||||
'userType' => UserContextInterface::USER_TYPE_CUSTOMER,
|
||||
'createdAt' => self::BASE_CREATED_AT_TIMESTAMP
|
||||
],
|
||||
'#4' => [
|
||||
'userType' => UserContextInterface::USER_TYPE_CUSTOMER,
|
||||
'createdAt' => self::BASE_CREATED_AT_TIMESTAMP - 5
|
||||
],
|
||||
'#5' => [
|
||||
'userType' => UserContextInterface::USER_TYPE_INTEGRATION,
|
||||
'createdAt' => self::BASE_CREATED_AT_TIMESTAMP
|
||||
],
|
||||
'#6' => [
|
||||
'userType' => UserContextInterface::USER_TYPE_INTEGRATION,
|
||||
'createdAt' => self::BASE_CREATED_AT_TIMESTAMP + 5
|
||||
],
|
||||
];
|
||||
/** @var \Magento\Framework\Stdlib\DateTime $dateTimeUtils */
|
||||
$dateTimeUtils = $this->objectManager->get(\Magento\Framework\Stdlib\DateTime::class);
|
||||
foreach ($tokensToBeGenerated as &$tokenData) {
|
||||
$token = $this->tokenFactory->create();
|
||||
$token->setType(Token::TYPE_ACCESS)
|
||||
->setUserType($tokenData['userType'])
|
||||
->setToken(rand(1, PHP_INT_MAX))
|
||||
->setCreatedAt($dateTimeUtils->formatDate($tokenData['createdAt']));
|
||||
$this->tokenResourceModel->save($token);
|
||||
$tokenData['tokenId'] = $token->getId();
|
||||
}
|
||||
return $tokensToBeGenerated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that @see \Magento\Integration\Cron\CleanExpiredTokens cleans tokens correctly per configuration
|
||||
*
|
||||
* 1. Generate several tokens with different user type and creation time
|
||||
* 2. Emulate current time stamp to be equal to (expiration period + some adjustment)
|
||||
* 3. Run clean up
|
||||
* 4. Make sure that clean up removed tokens that were expected to be removed,
|
||||
* and those tokens which were not expected to be removed are still there
|
||||
*
|
||||
* @param int $secondsAfterBaseCreatedTimestamp
|
||||
* @param array $expectedRemovedTokenNumbers
|
||||
* @param array $expectedPreservedTokenNumbers
|
||||
*
|
||||
* @dataProvider deleteExpiredTokenUsingObserverDataProvider
|
||||
* @covers \Magento\Integration\Cron\CleanExpiredTokens::execute
|
||||
*/
|
||||
public function testDeleteExpiredTokenUsingObserver(
|
||||
$secondsAfterBaseCreatedTimestamp,
|
||||
$expectedRemovedTokenNumbers,
|
||||
$expectedPreservedTokenNumbers
|
||||
) {
|
||||
/** @var \Magento\Integration\Cron\CleanExpiredTokens $cleanExpiredTokensModel */
|
||||
$cleanExpiredTokensModel = $this->objectManager->create(
|
||||
\Magento\Integration\Cron\CleanExpiredTokens::class,
|
||||
['tokenResourceModel' => $this->tokenResourceModel]
|
||||
);
|
||||
|
||||
$emulatedCurrentTimestamp = self::BASE_CREATED_AT_TIMESTAMP + $secondsAfterBaseCreatedTimestamp;
|
||||
$this->dateTimeMock->method('gmtTimestamp')->willReturn($emulatedCurrentTimestamp);
|
||||
$cleanExpiredTokensModel->execute();
|
||||
$this->assertTokenCleanUp(
|
||||
$expectedRemovedTokenNumbers,
|
||||
$expectedPreservedTokenNumbers,
|
||||
$this->generatedTokens
|
||||
);
|
||||
}
|
||||
|
||||
public function deleteExpiredTokenUsingObserverDataProvider()
|
||||
{
|
||||
return [
|
||||
"Clean up long before default admin and default customer token life time" => [
|
||||
3600 - 6, // time passed after base creation time
|
||||
[], // expected to be removed
|
||||
['#1', '#2', '#3', '#4', '#5', '#6'], // expected to exist
|
||||
],
|
||||
"Clean up just before default admin and default customer token life time" => [
|
||||
3600 - 1, // time passed after base creation time
|
||||
['#4'], // expected to be removed
|
||||
['#1', '#2', '#3', '#5', '#6'], // expected to exist
|
||||
],
|
||||
"Clean up after default admin token life time, but before default customer token life time" => [
|
||||
3600 + 1, // time passed after base creation time
|
||||
['#3', '#4'], // expected to be removed
|
||||
['#1', '#2', '#5', '#6'], // expected to exist
|
||||
],
|
||||
"Clean up after default customer and default admin token life time" => [
|
||||
14400 + 1, // time passed after base creation time
|
||||
['#1', '#3', '#4'], // expected to be removed
|
||||
['#2', '#5', '#6'], // expected to exist
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that expired tokens removal works as expected, @see \Magento\Integration\Model\ResourceModel\Oauth\Token
|
||||
*
|
||||
* 1. Generate several tokens with different user type and creation time
|
||||
* 2. Emulate current time stamp to be equal to (expiration period + some adjustment)
|
||||
* 3. Run clean up for some token types
|
||||
* 4. Make sure that clean up removed tokens that were expected to be removed,
|
||||
* and those tokens which were not expected to be removed are still there
|
||||
*
|
||||
* @param $secondsAfterBaseCreatedTimestamp
|
||||
* @param $tokenTypesToClean
|
||||
* @param $expectedRemovedTokenNumbers
|
||||
* @param $expectedPreservedTokenNumbers
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
* @dataProvider deleteExpiredTokensDataProvider
|
||||
* @covers \Magento\Integration\Model\ResourceModel\Oauth\Token::deleteExpiredTokens
|
||||
*/
|
||||
public function testDeleteExpiredTokens(
|
||||
$secondsAfterBaseCreatedTimestamp,
|
||||
$tokenTypesToClean,
|
||||
$expectedRemovedTokenNumbers,
|
||||
$expectedPreservedTokenNumbers
|
||||
) {
|
||||
/** Run clean up for tokens of {$tokenTypesToClean} type, created {$secondsAfterBaseCreatedTimestamp} ago */
|
||||
$emulatedCurrentTimestamp = self::BASE_CREATED_AT_TIMESTAMP + $secondsAfterBaseCreatedTimestamp;
|
||||
$this->dateTimeMock->method('gmtTimestamp')->willReturn($emulatedCurrentTimestamp);
|
||||
$this->tokenResourceModel->deleteExpiredTokens(self::TOKEN_LIFETIME, $tokenTypesToClean);
|
||||
$this->assertTokenCleanUp(
|
||||
$expectedRemovedTokenNumbers,
|
||||
$expectedPreservedTokenNumbers,
|
||||
$this->generatedTokens
|
||||
);
|
||||
}
|
||||
|
||||
public function deleteExpiredTokensDataProvider()
|
||||
{
|
||||
return [
|
||||
"Clean up for admin tokens which were created ('token_lifetime' + 1 second) ago" => [
|
||||
self::TOKEN_LIFETIME * 60 * 60 + 1, // time passed after base creation time
|
||||
[UserContextInterface::USER_TYPE_ADMIN], // token types to clean up
|
||||
['#1'], // expected to be removed
|
||||
['#2', '#3', '#4', '#5', '#6'], // expected to exist
|
||||
],
|
||||
"Clean up for admin, integration, guest tokens which were created ('token_lifetime' + 6 second) ago" => [
|
||||
self::TOKEN_LIFETIME * 60 * 60 + 6, // time passed after base creation time
|
||||
[ // token types to clean up
|
||||
UserContextInterface::USER_TYPE_ADMIN,
|
||||
UserContextInterface::USER_TYPE_INTEGRATION,
|
||||
UserContextInterface::USER_TYPE_GUEST
|
||||
],
|
||||
['#1', '#2', '#5', '#6'], // expected to be removed
|
||||
['#3', '#4'], // expected to exist
|
||||
],
|
||||
"Clean up for admin, integration, customer tokens which were created ('token_lifetime' + 6 second) ago" => [
|
||||
self::TOKEN_LIFETIME * 60 * 60 + 6, // time passed after base creation time
|
||||
[ // token types to clean up
|
||||
UserContextInterface::USER_TYPE_ADMIN,
|
||||
UserContextInterface::USER_TYPE_INTEGRATION,
|
||||
UserContextInterface::USER_TYPE_CUSTOMER
|
||||
],
|
||||
['#1', '#2', '#3', '#4', '#5', '#6'], // expected to be removed
|
||||
[], // expected to exist
|
||||
],
|
||||
"Clean up for admin, integration, customer tokens which were created ('token_lifetime' + 1 second) ago" => [
|
||||
self::TOKEN_LIFETIME * 60 * 60 + 1, // time passed after base creation time
|
||||
[ // token types to clean up
|
||||
UserContextInterface::USER_TYPE_ADMIN,
|
||||
UserContextInterface::USER_TYPE_INTEGRATION,
|
||||
UserContextInterface::USER_TYPE_CUSTOMER
|
||||
],
|
||||
['#1', '#3', '#4', '#5'], // expected to be removed
|
||||
['#2', '#6'], // expected to exist
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Make that only expired tokens were cleaned up
|
||||
*
|
||||
* @param array $expectedRemovedTokenNumbers
|
||||
* @param array $expectedPreservedTokenNumbers
|
||||
* @param array $generatedTokens
|
||||
*/
|
||||
private function assertTokenCleanUp(
|
||||
$expectedRemovedTokenNumbers,
|
||||
$expectedPreservedTokenNumbers,
|
||||
$generatedTokens
|
||||
) {
|
||||
foreach ($expectedRemovedTokenNumbers as $tokenNumber) {
|
||||
$token = $this->tokenFactory->create();
|
||||
$this->tokenResourceModel->load($token, $generatedTokens[$tokenNumber]['tokenId']);
|
||||
$this->assertEmpty(
|
||||
$token->getId(),
|
||||
"Token {$tokenNumber} was expected to be deleted after clean up"
|
||||
);
|
||||
}
|
||||
foreach ($expectedPreservedTokenNumbers as $tokenNumber) {
|
||||
$token = $this->tokenFactory->create();
|
||||
$this->tokenResourceModel->load($token, $generatedTokens[$tokenNumber]['tokenId']);
|
||||
$this->assertEquals(
|
||||
$generatedTokens[$tokenNumber]['tokenId'],
|
||||
$token->getId(),
|
||||
"Token {$tokenNumber} was NOT expected to be deleted after clean up"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function testSave(): void
|
||||
{
|
||||
$token = $this->oauthHelper->generateToken();
|
||||
$tokenSecret = $this->oauthHelper->generateTokenSecret();
|
||||
$model = $this->tokenFactory->create();
|
||||
$model->setData(
|
||||
[
|
||||
'token' => $token,
|
||||
'secret' => $tokenSecret,
|
||||
'type' => Token::TYPE_ACCESS
|
||||
]
|
||||
);
|
||||
$model->save();
|
||||
|
||||
$tokenResourceModel = $model->getResource();
|
||||
|
||||
$this->assertEquals($tokenSecret, $model->getSecret());
|
||||
$this->assertNotEquals($model->getSecret(), $tokenResourceModel->load($model, 'secret'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user