ltsecurityinc安防产品在线商城代码
This commit is contained in:
+94
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*
|
||||
*/
|
||||
namespace Magento\Integration\Block\Adminhtml\Integration\Activate\Permissions\Tab;
|
||||
|
||||
use Magento\Integration\Controller\Adminhtml\Integration as IntegrationController;
|
||||
use Magento\Integration\Model\Integration;
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Integration/_files/integration_all_permissions.php
|
||||
*/
|
||||
class WebapiTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/** @var \Magento\Framework\Registry */
|
||||
protected $registry;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->registry = $objectManager->get(\Magento\Framework\Registry::class);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->registry->unregister(IntegrationController::REGISTRY_KEY_CURRENT_INTEGRATION);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testGetSelectedResourcesJsonEmpty()
|
||||
{
|
||||
$expectedResult = '[]';
|
||||
$this->assertEquals($expectedResult, $this->createApiTabBlock()->getSelectedResourcesJson());
|
||||
}
|
||||
|
||||
public function testGetSelectedResourcesJson()
|
||||
{
|
||||
$expectedResult = '["Magento_Backend::dashboard",';
|
||||
$this->registry->register(
|
||||
IntegrationController::REGISTRY_KEY_CURRENT_INTEGRATION,
|
||||
$this->getFixtureIntegration()->getData()
|
||||
);
|
||||
$this->assertStringContainsString($expectedResult, $this->createApiTabBlock()->getSelectedResourcesJson());
|
||||
}
|
||||
|
||||
public function testGetResourcesTreeJson()
|
||||
{
|
||||
$expectedResult = '[{"id":"Magento_Backend::dashboard","li_attr":{"data-id":"Magento_Backend::dashboard"},' .
|
||||
'"text":"Dashboard","children":[],"state":';
|
||||
$this->registry->register(
|
||||
IntegrationController::REGISTRY_KEY_CURRENT_INTEGRATION,
|
||||
$this->getFixtureIntegration()->getData()
|
||||
);
|
||||
$this->assertStringContainsString($expectedResult, $this->createApiTabBlock()->getResourcesTreeJson());
|
||||
}
|
||||
|
||||
public function testCanShowTabNegative()
|
||||
{
|
||||
$this->assertFalse($this->createApiTabBlock()->canShowTab());
|
||||
}
|
||||
|
||||
public function testCanShowTabPositive()
|
||||
{
|
||||
$integrationData = $this->getFixtureIntegration()->getData();
|
||||
$integrationData[Integration::SETUP_TYPE] = Integration::TYPE_CONFIG;
|
||||
$this->registry->register(IntegrationController::REGISTRY_KEY_CURRENT_INTEGRATION, $integrationData);
|
||||
$this->assertTrue($this->createApiTabBlock()->canShowTab());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Integration\Block\Adminhtml\Integration\Activate\Permissions\Tab\Webapi
|
||||
*/
|
||||
protected function createApiTabBlock()
|
||||
{
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
return $objectManager->create(
|
||||
\Magento\Integration\Block\Adminhtml\Integration\Activate\Permissions\Tab\Webapi::class
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Integration
|
||||
*/
|
||||
protected function getFixtureIntegration()
|
||||
{
|
||||
/** @var $integration Integration */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$integration = $objectManager->create(\Magento\Integration\Model\Integration::class);
|
||||
return $integration->load('Fixture Integration', 'name');
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Integration\Block\Adminhtml\Integration\Edit;
|
||||
|
||||
use Magento\Integration\Controller\Adminhtml\Integration;
|
||||
use Magento\Integration\Model\Integration as IntegrationModel;
|
||||
|
||||
/**
|
||||
* Test for \Magento\Integration\Block\Adminhtml\Integration\Edit\Form
|
||||
*/
|
||||
class FormTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Integration\Block\Adminhtml\Integration\Edit\Form
|
||||
*/
|
||||
private $block;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
|
||||
/** @var $layout \Magento\Framework\View\Layout */
|
||||
$layout = $this->objectManager->create(\Magento\Framework\View\LayoutInterface::class);
|
||||
$this->block = $layout->createBlock(\Magento\Integration\Block\Adminhtml\Integration\Edit\Form::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
public function testToHtmlNoExistingIntegrationData()
|
||||
{
|
||||
$this->assertStringContainsString(
|
||||
'<form id="edit_form" action="" method="post">',
|
||||
$this->block->toHtml()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testToHtmlWithIntegrationData()
|
||||
{
|
||||
/** @var \Magento\Framework\Registry $coreRegistry */
|
||||
$coreRegistry = $this->objectManager->get(\Magento\Framework\Registry::class);
|
||||
$coreRegistry->unregister(Integration::REGISTRY_KEY_CURRENT_INTEGRATION);
|
||||
$id = 'idValue';
|
||||
$integrationData = [
|
||||
\Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info::DATA_ID => $id,
|
||||
];
|
||||
$coreRegistry->register(Integration::REGISTRY_KEY_CURRENT_INTEGRATION, $integrationData);
|
||||
|
||||
$html = $this->block->toHtml();
|
||||
|
||||
$this->assertMatchesRegularExpression(
|
||||
"/<input id=\"integration_id\" name=\"id\".*value=\"$id\".*type=\"hidden\".*>/",
|
||||
$html
|
||||
);
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Integration\Block\Adminhtml\Integration\Edit\Tab;
|
||||
|
||||
use Magento\Integration\Controller\Adminhtml\Integration;
|
||||
use Magento\Integration\Model\Integration as IntegrationModel;
|
||||
|
||||
/**
|
||||
* Test for \Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info
|
||||
*/
|
||||
class InfoTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info
|
||||
*/
|
||||
private $block;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
|
||||
/** @var $layout \Magento\Framework\View\Layout */
|
||||
$layout = $this->objectManager->create(\Magento\Framework\View\LayoutInterface::class);
|
||||
$this->block = $layout->createBlock(\Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info::class);
|
||||
$this->block->addChild('integration_tokens', \Magento\Integration\Block\Adminhtml\Integration\Tokens::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
public function testToHtmlNoExistingIntegrationData()
|
||||
{
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/<input id="integration_properties_name" name="name".*type="text".*>/',
|
||||
$this->block->toHtml()
|
||||
);
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/<input id="integration_properties_email" name="email".*type="text".*>/',
|
||||
$this->block->toHtml()
|
||||
);
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/<input id="integration_properties_endpoint" name="endpoint".*type="text".*>/',
|
||||
$this->block->toHtml()
|
||||
);
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/<input id="integration_properties_identity_link_url" name="identity_link_url".*type="text".*>/',
|
||||
$this->block->toHtml()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
public function testToHtmlWithIntegrationData()
|
||||
{
|
||||
/** @var \Magento\Framework\Registry $coreRegistry */
|
||||
$coreRegistry = $this->objectManager->get(\Magento\Framework\Registry::class);
|
||||
$coreRegistry->unregister(Integration::REGISTRY_KEY_CURRENT_INTEGRATION);
|
||||
$name = 'nameExample';
|
||||
$email = 'admin@example.com';
|
||||
$endpoint = 'exampleEndpoint';
|
||||
$identityLinkUrl = 'identityLinkUrl';
|
||||
$integrationData = [
|
||||
Info::DATA_ID => '1',
|
||||
Info::DATA_NAME => $name,
|
||||
Info::DATA_EMAIL => 'admin@example.com',
|
||||
Info::DATA_ENDPOINT => 'exampleEndpoint',
|
||||
Info::DATA_IDENTITY_LINK_URL => 'identityLinkUrl',
|
||||
Info::DATA_SETUP_TYPE => IntegrationModel::TYPE_MANUAL,
|
||||
];
|
||||
$coreRegistry->register(Integration::REGISTRY_KEY_CURRENT_INTEGRATION, $integrationData);
|
||||
|
||||
$html = $this->block->toHtml();
|
||||
|
||||
$this->assertMatchesRegularExpression(
|
||||
"/<input id=\"integration_properties_name\" name=\"name\".*value=\"$name\".*type=\"text\".*>/",
|
||||
$html
|
||||
);
|
||||
$this->assertMatchesRegularExpression(
|
||||
"/<input id=\"integration_properties_email\" name=\"email\".*value=\"$email\".*type=\"text\".*>/",
|
||||
$html
|
||||
);
|
||||
$this->assertMatchesRegularExpression(
|
||||
"/<input id=\"integration_properties_endpoint\" name=\"endpoint\".*value=\"$endpoint\".*type=\"text\".*>/",
|
||||
$html
|
||||
);
|
||||
$this->assertMatchesRegularExpression(
|
||||
"/<input id=\"integration_properties_identity_link_url\" name=\"identity_link_url\".*"
|
||||
. "value=\"$identityLinkUrl\".*type=\"text\".*>/",
|
||||
$html
|
||||
);
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/<input id="integration_properties_consumer_key" name="consumer_key".*type="text".*>/',
|
||||
$this->block->toHtml()
|
||||
);
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/<input id="integration_properties_consumer_secret" name="consumer_secret".*type="text".*>/',
|
||||
$this->block->toHtml()
|
||||
);
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/<input id="integration_properties_token" name="token".*type="text".*>/',
|
||||
$this->block->toHtml()
|
||||
);
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/<input id="integration_properties_token_secret" name="token_secret".*type="text".*>/',
|
||||
$this->block->toHtml()
|
||||
);
|
||||
}
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Magento\Integration\Block\Adminhtml\Integration;
|
||||
|
||||
use Magento\Integration\Controller\Adminhtml\Integration as IntegrationController;
|
||||
use Magento\Integration\Model\Integration;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* Test class for \Magento\Integration\Block\Adminhtml\Integration\Edit
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class EditTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Integration\Block\Adminhtml\Integration\Edit
|
||||
*/
|
||||
protected $editBlock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->editBlock = Bootstrap::getObjectManager()
|
||||
->create(\Magento\Integration\Block\Adminhtml\Integration\Edit::class);
|
||||
}
|
||||
|
||||
public function testGetHeaderTextNewIntegration()
|
||||
{
|
||||
$this->assertEquals('New Integration', $this->editBlock->getHeaderText()->getText());
|
||||
$buttonList = Bootstrap::getObjectManager()
|
||||
->get(\Magento\Backend\Block\Widget\Context::class)
|
||||
->getButtonList()
|
||||
->getItems();
|
||||
|
||||
// Assert that there is a 'save' and 'activate' button when creating a new integration
|
||||
$haveSaveButton = false;
|
||||
foreach ($buttonList as $button) {
|
||||
foreach ($button as $key => $value) {
|
||||
if ($key === 'save') {
|
||||
$haveSaveButton = true;
|
||||
$this->assertNotNull($value->getDataByKey('options'));
|
||||
$this->assertEquals(
|
||||
'activate',
|
||||
$value->getDataByKey('options')['save_activate']['id'],
|
||||
"'Activate' button is expected when creating a new integration."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->assertTrue($haveSaveButton, "'Save' button is expected when creating a new integration.");
|
||||
}
|
||||
|
||||
public function testGetHeaderTextEditIntegration()
|
||||
{
|
||||
$integrationId = 1;
|
||||
$integrationName = 'Test Name';
|
||||
|
||||
$integrationData = [
|
||||
Integration::ID => $integrationId,
|
||||
Integration::NAME => $integrationName,
|
||||
];
|
||||
|
||||
/** @var \Magento\Framework\Registry $registry */
|
||||
$registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
|
||||
$registry->register(IntegrationController::REGISTRY_KEY_CURRENT_INTEGRATION, $integrationData);
|
||||
|
||||
$headerText = $this->editBlock->getHeaderText();
|
||||
$this->assertEquals("Edit Integration '%1'", $headerText->getText());
|
||||
$this->assertEquals($integrationName, $headerText->getArguments()[0]);
|
||||
|
||||
// Tear down
|
||||
$registry->unregister(IntegrationController::REGISTRY_KEY_CURRENT_INTEGRATION);
|
||||
}
|
||||
|
||||
public function testGetHeaderTextEditIntegrationConfigType()
|
||||
{
|
||||
$integrationId = 2;
|
||||
$integrationName = 'Test Name 2';
|
||||
|
||||
$integrationData = [
|
||||
Integration::ID => $integrationId,
|
||||
Integration::NAME => $integrationName,
|
||||
Integration::SETUP_TYPE => Integration::TYPE_CONFIG
|
||||
];
|
||||
|
||||
/** @var \Magento\Framework\Registry $registry */
|
||||
$registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
|
||||
$registry->register(IntegrationController::REGISTRY_KEY_CURRENT_INTEGRATION, $integrationData);
|
||||
|
||||
/** @var \Magento\Integration\Block\Adminhtml\Integration\Edit $editBlock */
|
||||
$editBlock = Bootstrap::getObjectManager()
|
||||
->create(\Magento\Integration\Block\Adminhtml\Integration\Edit::class);
|
||||
|
||||
$headerText = $editBlock->getHeaderText();
|
||||
$this->assertEquals("Edit Integration '%1'", $headerText->getText());
|
||||
$this->assertEquals($integrationName, $headerText->getArguments()[0]);
|
||||
|
||||
$buttonList = Bootstrap::getObjectManager()
|
||||
->get(\Magento\Backend\Block\Widget\Context::class)
|
||||
->getButtonList()
|
||||
->getItems();
|
||||
|
||||
// Assert that 'save' button is removed for integration of config type
|
||||
foreach ($buttonList as $button) {
|
||||
$this->assertArrayNotHasKey('save', $button);
|
||||
}
|
||||
|
||||
// Tear down
|
||||
$registry->unregister(IntegrationController::REGISTRY_KEY_CURRENT_INTEGRATION);
|
||||
}
|
||||
|
||||
public function testGetFormActionUrl()
|
||||
{
|
||||
$baseUrl = Bootstrap::getObjectManager()->get(\Magento\Framework\Url::class)->getBaseUrl();
|
||||
$this->assertStringContainsString($baseUrl, $this->editBlock->getFormActionUrl());
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Magento\Integration\Block\Adminhtml\Integration;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* Test class for \Magento\Integration\Block\Adminhtml\Integration\Grid
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class GridTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Integration\Block\Adminhtml\Integration\Grid
|
||||
*/
|
||||
protected $gridBlock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->gridBlock = Bootstrap::getObjectManager()
|
||||
->create(\Magento\Integration\Block\Adminhtml\Integration\Grid::class);
|
||||
}
|
||||
|
||||
public function testGetRowClickCallback()
|
||||
{
|
||||
$this->assertEquals('', $this->gridBlock->getRowClickCallback());
|
||||
}
|
||||
|
||||
public function testGetRowInitCallback()
|
||||
{
|
||||
$this->assertEquals('', $this->gridBlock->getRowInitCallback());
|
||||
}
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Magento\Integration\Block\Adminhtml\Integration;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* Test class for \Magento\Integration\Block\Adminhtml\Integration\Tokens
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class TokensTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Integration\Block\Adminhtml\Integration\Tokens
|
||||
*/
|
||||
protected $tokensBlock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->tokensBlock = Bootstrap::getObjectManager()
|
||||
->create(\Magento\Integration\Block\Adminhtml\Integration\Tokens::class);
|
||||
}
|
||||
|
||||
public function testGetFormFields()
|
||||
{
|
||||
$expectedData = [
|
||||
[
|
||||
'name' => Tokens::DATA_CONSUMER_KEY,
|
||||
'type' => 'text',
|
||||
'metadata' => [
|
||||
'label' => __('Consumer Key'),
|
||||
'name' => Tokens::DATA_CONSUMER_KEY,
|
||||
'readonly' => true,
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => Tokens::DATA_CONSUMER_SECRET,
|
||||
'type' => 'text',
|
||||
'metadata' => [
|
||||
'label' => __('Consumer Secret'),
|
||||
'name' => Tokens::DATA_CONSUMER_SECRET,
|
||||
'readonly' => true,
|
||||
]
|
||||
],
|
||||
[
|
||||
'name' => Tokens::DATA_TOKEN,
|
||||
'type' => 'text',
|
||||
'metadata' => ['label' => __('Access Token'), 'name' => Tokens::DATA_TOKEN, 'readonly' => true]
|
||||
],
|
||||
[
|
||||
'name' => Tokens::DATA_TOKEN_SECRET,
|
||||
'type' => 'text',
|
||||
'metadata' => [
|
||||
'label' => __('Access Token Secret'),
|
||||
'name' => Tokens::DATA_TOKEN_SECRET,
|
||||
'readonly' => true,
|
||||
]
|
||||
]
|
||||
];
|
||||
$this->assertEquals($expectedData, $this->tokensBlock->getFormFields());
|
||||
}
|
||||
|
||||
public function testToHtml()
|
||||
{
|
||||
$this->tokensBlock->setNameInLayout('test_block_name');
|
||||
$htmlContent = $this->tokensBlock->toHtml();
|
||||
|
||||
$this->assertStringContainsString('name="consumer_key"', $htmlContent);
|
||||
$this->assertStringContainsString(
|
||||
'<span>Consumer Key</span>',
|
||||
$htmlContent,
|
||||
"HTML content of token block should contain information about 'Consumer Key'."
|
||||
);
|
||||
|
||||
$this->assertStringContainsString('name="consumer_secret"', $htmlContent);
|
||||
$this->assertStringContainsString(
|
||||
'<span>Consumer Secret</span>',
|
||||
$htmlContent,
|
||||
"HTML content of token block should contain information about 'Consumer Secret'."
|
||||
);
|
||||
|
||||
$this->assertStringContainsString('name="token"', $htmlContent);
|
||||
$this->assertStringContainsString(
|
||||
'<span>Access Token</span>',
|
||||
$htmlContent,
|
||||
"HTML content of token block should contain information about 'Access Token'."
|
||||
);
|
||||
|
||||
$this->assertStringContainsString('name="token_secret"', $htmlContent);
|
||||
$this->assertStringContainsString(
|
||||
'<span>Access Token Secret</span>',
|
||||
$htmlContent,
|
||||
"HTML content of token block should contain information about 'Access Token Secret'."
|
||||
);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace Magento\Integration\Block\Adminhtml\System\Config;
|
||||
|
||||
/**
|
||||
* Testing Oauth section in configs.
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class OauthSectionTest extends \Magento\TestFramework\TestCase\AbstractBackendController
|
||||
{
|
||||
/**
|
||||
* Checks that OAuth Section in the system config is loaded
|
||||
*/
|
||||
public function testOAuthSection()
|
||||
{
|
||||
$this->dispatch('backend/admin/system_config/edit/section/oauth/');
|
||||
$body = $this->getResponse()->getBody();
|
||||
$this->assertStringContainsString('id="oauth_access_token_lifetime-head"', $body);
|
||||
$this->assertStringContainsString('id="oauth_cleanup-head"', $body);
|
||||
$this->assertStringContainsString('id="oauth_consumer-head"', $body);
|
||||
$this->assertStringContainsString('id="oauth_authentication_lock-head"', $body);
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*
|
||||
*/
|
||||
namespace Magento\Integration\Block\Adminhtml\Widget\Grid\Column\Renderer\Button;
|
||||
|
||||
use Magento\Integration\Model\Integration;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDataFixture Magento/Integration/_files/integration_all_permissions.php
|
||||
*/
|
||||
class DeleteTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Integration\Block\Adminhtml\Widget\Grid\Column\Renderer\Button\Delete
|
||||
*/
|
||||
protected $deleteButtonBlock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
/** @var \Magento\Framework\App\Request\Http $request */
|
||||
$request = $objectManager->get(\Magento\Framework\App\Request\Http::class);
|
||||
$request->setRouteName('adminhtml')->setControllerName('integration');
|
||||
$this->deleteButtonBlock = $objectManager->create(
|
||||
\Magento\Integration\Block\Adminhtml\Widget\Grid\Column\Renderer\Button\Delete::class
|
||||
);
|
||||
$column = $objectManager->create(\Magento\Backend\Block\Widget\Grid\Column::class);
|
||||
$this->deleteButtonBlock->setColumn($column);
|
||||
}
|
||||
|
||||
public function testRender()
|
||||
{
|
||||
$integration = $this->getFixtureIntegration();
|
||||
$buttonHtml = $this->deleteButtonBlock->render($integration);
|
||||
self::assertStringContainsString('title="Remove"', $buttonHtml);
|
||||
self::assertStringContainsString(
|
||||
'this.setAttribute(\'data-url\', '
|
||||
. '\'http://localhost/index.php/backend/admin/integration/delete/id/'
|
||||
. $integration->getId(),
|
||||
$buttonHtml
|
||||
);
|
||||
$this->assertStringNotContainsString('disabled', $buttonHtml);
|
||||
}
|
||||
|
||||
public function testRenderDisabled()
|
||||
{
|
||||
$integration = $this->getFixtureIntegration();
|
||||
$integration->setSetupType(Integration::TYPE_CONFIG);
|
||||
$buttonHtml = $this->deleteButtonBlock->render($integration);
|
||||
self::assertStringContainsString(
|
||||
'title="' .$this->deleteButtonBlock->escapeHtmlAttr('Uninstall the extension to remove this integration')
|
||||
.'"',
|
||||
$buttonHtml
|
||||
);
|
||||
self::assertStringContainsString(
|
||||
'this.setAttribute(\'data-url\', '
|
||||
. '\'http://localhost/index.php/backend/admin/integration/delete/id/'
|
||||
. $integration->getId(),
|
||||
$buttonHtml
|
||||
);
|
||||
self::assertStringContainsString('disabled="disabled"', $buttonHtml);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Integration
|
||||
*/
|
||||
protected function getFixtureIntegration()
|
||||
{
|
||||
/** @var $integration Integration */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$integration = $objectManager->create(\Magento\Integration\Model\Integration::class);
|
||||
return $integration->load('Fixture Integration', 'name');
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*
|
||||
*/
|
||||
namespace Magento\Integration\Block\Adminhtml\Widget\Grid\Column\Renderer\Button;
|
||||
|
||||
use Magento\Integration\Model\Integration;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDataFixture Magento/Integration/_files/integration_all_permissions.php
|
||||
*/
|
||||
class EditTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Integration\Block\Adminhtml\Widget\Grid\Column\Renderer\Button\Edit
|
||||
*/
|
||||
protected $editButtonBlock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
/** @var \Magento\Framework\App\Request\Http $request */
|
||||
$request = $objectManager->get(\Magento\Framework\App\Request\Http::class);
|
||||
$request->setRouteName('adminhtml')->setControllerName('integration');
|
||||
$this->editButtonBlock = $objectManager->create(
|
||||
\Magento\Integration\Block\Adminhtml\Widget\Grid\Column\Renderer\Button\Edit::class
|
||||
);
|
||||
$column = $objectManager->create(\Magento\Backend\Block\Widget\Grid\Column::class);
|
||||
$this->editButtonBlock->setColumn($column);
|
||||
}
|
||||
|
||||
public function testRenderEdit()
|
||||
{
|
||||
$integration = $this->getFixtureIntegration();
|
||||
$buttonHtml = $this->editButtonBlock->render($integration);
|
||||
$this->assertStringContainsString('title="Edit"', $buttonHtml);
|
||||
$this->assertStringContainsString('class="' .$this->editButtonBlock->escapeHtmlAttr('action edit') .'"', $buttonHtml);
|
||||
$this->assertStringContainsString(
|
||||
'window.location.href=\'http://localhost/index.php/backend/admin/integration/edit/id/'
|
||||
. $integration->getId(),
|
||||
$buttonHtml
|
||||
);
|
||||
}
|
||||
|
||||
public function testRenderView()
|
||||
{
|
||||
$integration = $this->getFixtureIntegration();
|
||||
$integration->setSetupType(Integration::TYPE_CONFIG);
|
||||
$buttonHtml = $this->editButtonBlock->render($integration);
|
||||
$this->assertStringContainsString('title="View"', $buttonHtml);
|
||||
$this->assertStringContainsString('class="' .$this->editButtonBlock->escapeHtmlAttr('action info') .'"', $buttonHtml);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Integration
|
||||
*/
|
||||
protected function getFixtureIntegration()
|
||||
{
|
||||
/** @var $integration Integration */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$integration = $objectManager->create(\Magento\Integration\Model\Integration::class);
|
||||
return $integration->load('Fixture Integration', 'name');
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*
|
||||
*/
|
||||
namespace Magento\Integration\Block\Adminhtml\Widget\Grid\Column\Renderer\Link;
|
||||
|
||||
use Magento\Integration\Model\Integration;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDataFixture Magento/Integration/_files/integration_all_permissions.php
|
||||
*/
|
||||
class ActivateTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Integration\Block\Adminhtml\Widget\Grid\Column\Renderer\Link\Activate
|
||||
*/
|
||||
protected $activateLinkBlock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->activateLinkBlock = $objectManager->create(
|
||||
\Magento\Integration\Block\Adminhtml\Widget\Grid\Column\Renderer\Link\Activate::class
|
||||
);
|
||||
$column = $objectManager->create(\Magento\Backend\Block\Widget\Grid\Column::class);
|
||||
$this->activateLinkBlock->setColumn($column);
|
||||
}
|
||||
|
||||
public function testRenderActivate()
|
||||
{
|
||||
$integration = $this->getFixtureIntegration();
|
||||
$buttonHtml = $this->activateLinkBlock->render($integration);
|
||||
$this->assertStringContainsString('href="javascript:void(0);"', $buttonHtml);
|
||||
$this->assertStringContainsString('title="Activate"', $buttonHtml);
|
||||
$this->assertStringContainsString('data-row-id="' . $integration->getId() . '"', $buttonHtml);
|
||||
$this->assertStringContainsString('data-row-dialog="permissions"', $buttonHtml);
|
||||
$this->assertStringContainsString('data-row-is-reauthorize="0"', $buttonHtml);
|
||||
$this->assertStringContainsString('data-row-is-token-exchange="0"', $buttonHtml);
|
||||
$this->assertStringContainsString('onclick="integration.popup.show(this);', $buttonHtml);
|
||||
$this->assertStringContainsString('>Activate</a>', $buttonHtml);
|
||||
}
|
||||
|
||||
public function testRenderReauthorize()
|
||||
{
|
||||
$integration = $this->getFixtureIntegration();
|
||||
$integration->setStatus(Integration::STATUS_ACTIVE);
|
||||
$buttonHtml = $this->activateLinkBlock->render($integration);
|
||||
$this->assertStringContainsString('href="javascript:void(0);"', $buttonHtml);
|
||||
$this->assertStringContainsString('title="Reauthorize"', $buttonHtml);
|
||||
$this->assertStringContainsString('data-row-id="' . $integration->getId() . '"', $buttonHtml);
|
||||
$this->assertStringContainsString('data-row-dialog="permissions"', $buttonHtml);
|
||||
$this->assertStringContainsString('data-row-is-reauthorize="1"', $buttonHtml);
|
||||
$this->assertStringContainsString('data-row-is-token-exchange="0"', $buttonHtml);
|
||||
$this->assertStringContainsString('onclick="integration.popup.show(this);', $buttonHtml);
|
||||
$this->assertStringContainsString('>Reauthorize</a>', $buttonHtml);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $endpoint
|
||||
* @param string $identityLinkUrl
|
||||
* @param int $expectedResult
|
||||
* @dataProvider renderTokenExchangeProvider
|
||||
*/
|
||||
public function testRenderTokenExchange($endpoint, $identityLinkUrl, $expectedResult)
|
||||
{
|
||||
$integration = $this->getFixtureIntegration();
|
||||
$integration->setStatus(Integration::STATUS_ACTIVE);
|
||||
$integration->setEndpoint($endpoint);
|
||||
$integration->setIdentityLinkUrl($identityLinkUrl);
|
||||
$buttonHtml = $this->activateLinkBlock->render($integration);
|
||||
$this->assertStringContainsString('href="javascript:void(0);"', $buttonHtml);
|
||||
$this->assertStringContainsString('title="Reauthorize"', $buttonHtml);
|
||||
$this->assertStringContainsString('data-row-id="' . $integration->getId() . '"', $buttonHtml);
|
||||
$this->assertStringContainsString('data-row-dialog="permissions"', $buttonHtml);
|
||||
$this->assertStringContainsString('data-row-is-reauthorize="1"', $buttonHtml);
|
||||
$this->assertStringContainsString('data-row-is-token-exchange="' . $expectedResult . '"', $buttonHtml);
|
||||
$this->assertStringContainsString('onclick="integration.popup.show(this);', $buttonHtml);
|
||||
$this->assertStringContainsString('>Reauthorize</a>', $buttonHtml);
|
||||
}
|
||||
|
||||
public function renderTokenExchangeProvider()
|
||||
{
|
||||
return [
|
||||
['http://example.com/endpoint', 'http://example.com/identity', 1],
|
||||
['', null, 0],
|
||||
['http://example.com/endpoint', '', 0],
|
||||
[null, 'http://example.com/identity', 0],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Integration
|
||||
*/
|
||||
protected function getFixtureIntegration()
|
||||
{
|
||||
/** @var $integration Integration */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$integration = $objectManager->create(\Magento\Integration\Model\Integration::class);
|
||||
return $integration->load('Fixture Integration', 'name');
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Integration\Controller\Adminhtml\Integration;
|
||||
|
||||
use Magento\Framework\App\Request\Http as HttpRequest;
|
||||
use Magento\Integration\Api\IntegrationServiceInterface;
|
||||
use Magento\TestFramework\TestCase\AbstractBackendController;
|
||||
|
||||
/**
|
||||
* Test for \Magento\Integration\Controller\Adminhtml\Integration\TokensExchange.
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class TokensExchangeTest extends AbstractBackendController
|
||||
{
|
||||
private const URL = 'backend/admin/integration/tokensExchange';
|
||||
|
||||
/**
|
||||
* @var IntegrationServiceInterface
|
||||
*/
|
||||
private $integrationService;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->integrationService = $this->_objectManager->get(IntegrationServiceInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate integration
|
||||
*
|
||||
* @magentoDataFixture Magento/Integration/_files/integration_all_data.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testActivate()
|
||||
{
|
||||
$integration = $this->integrationService->findByName('Fixture Integration');
|
||||
|
||||
$this->getRequest()->setMethod(HttpRequest::METHOD_GET);
|
||||
$this->getRequest()->setParams(['id' => $integration->getId()]);
|
||||
$this->dispatch(self::URL);
|
||||
|
||||
$this->assertStringContainsString(
|
||||
'Please setup or sign in into your 3rd party account to complete setup of this integration.',
|
||||
$this->getResponse()->getBody()
|
||||
);
|
||||
}
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Magento\Integration\Controller\Adminhtml;
|
||||
|
||||
use Magento\Framework\App\Request\Http as HttpRequest;
|
||||
use Magento\TestFramework\Bootstrap;
|
||||
|
||||
/**
|
||||
* \Magento\Integration\Controller\Adminhtml\Integration
|
||||
*
|
||||
* @magentoDataFixture Magento/Integration/_files/integration_all_permissions.php
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
class IntegrationTest extends \Magento\TestFramework\TestCase\AbstractBackendController
|
||||
{
|
||||
/** @var \Magento\Integration\Model\Integration */
|
||||
private $_integration;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
/** @var $integration \Magento\Integration\Model\Integration */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$integration = $objectManager->create(\Magento\Integration\Model\Integration::class);
|
||||
$this->_integration = $integration->load('Fixture Integration', 'name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test view page.
|
||||
*/
|
||||
public function testIndexAction()
|
||||
{
|
||||
$this->dispatch('backend/admin/integration/index');
|
||||
$response = $this->getResponse()->getBody();
|
||||
|
||||
$this->assertStringContainsString('Integrations', $response);
|
||||
$this->assertEquals(
|
||||
1,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//*[@id="integrationGrid"]',
|
||||
$response
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creation form.
|
||||
*/
|
||||
public function testNewAction()
|
||||
{
|
||||
$this->dispatch('backend/admin/integration/new');
|
||||
$response = $this->getResponse()->getBody();
|
||||
|
||||
$this->assertEquals('new', $this->getRequest()->getActionName());
|
||||
$this->assertStringContainsString('entry-edit form-inline', $response);
|
||||
$this->assertStringContainsString('New Integration', $response);
|
||||
$this->assertEquals(
|
||||
1,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//*[@id="integration_properties_base_fieldset"]',
|
||||
$response
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update form.
|
||||
*/
|
||||
public function testEditAction()
|
||||
{
|
||||
$integrationId = $this->_integration->getId();
|
||||
$this->getRequest()->setParam('id', $integrationId);
|
||||
$this->dispatch('backend/admin/integration/edit');
|
||||
$response = $this->getResponse()->getBody();
|
||||
$saveLink = 'integration/save/';
|
||||
|
||||
$this->assertStringContainsString('entry-edit form-inline', $response);
|
||||
$this->assertStringContainsString(
|
||||
'Edit "' . $this->_integration->getName() . '" Integration',
|
||||
$response
|
||||
);
|
||||
$this->assertStringContainsString($saveLink, $response);
|
||||
$this->assertEquals(
|
||||
1,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//*[@id="integration_properties_base_fieldset"]',
|
||||
$response
|
||||
)
|
||||
);
|
||||
$this->assertEquals(
|
||||
1,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//*[@id="integration_edit_tabs_info_section_content"]',
|
||||
$response
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test saving.
|
||||
*/
|
||||
public function testSaveActionUpdateIntegration()
|
||||
{
|
||||
$integrationId = $this->_integration->getId();
|
||||
$integrationName = $this->_integration->getName();
|
||||
$this->getRequest()->setParam('id', $integrationId);
|
||||
$url = 'http://magento.ll/endpoint_url';
|
||||
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
|
||||
$this->getRequest()->setPostValue(
|
||||
[
|
||||
'name' => $integrationName,
|
||||
'email' => 'test@magento.com',
|
||||
'authentication' => '1',
|
||||
'endpoint' => $url,
|
||||
'current_password' => Bootstrap::ADMIN_PASSWORD,
|
||||
]
|
||||
);
|
||||
$this->dispatch('backend/admin/integration/save');
|
||||
$this->assertSessionMessages(
|
||||
$this->equalTo(["The integration '{$integrationName}' has been saved."]),
|
||||
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
|
||||
);
|
||||
$this->assertRedirect($this->stringContains('backend/admin/integration/index/'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test saving.
|
||||
*/
|
||||
public function testSaveActionNewIntegration()
|
||||
{
|
||||
$url = 'http://magento.ll/endpoint_url';
|
||||
// phpcs:ignore Magento2.Security.InsecureFunction
|
||||
$integrationName = md5(rand());
|
||||
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
|
||||
$this->getRequest()->setPostValue(
|
||||
[
|
||||
'name' => $integrationName,
|
||||
'email' => 'test@magento.com',
|
||||
'authentication' => '1',
|
||||
'endpoint' => $url,
|
||||
'current_password' => Bootstrap::ADMIN_PASSWORD,
|
||||
]
|
||||
);
|
||||
$this->dispatch('backend/admin/integration/save');
|
||||
|
||||
$this->assertSessionMessages(
|
||||
$this->equalTo(["The integration '{$integrationName}' has been saved."]),
|
||||
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
|
||||
);
|
||||
$this->assertRedirect($this->stringContains('backend/admin/integration/index/'));
|
||||
}
|
||||
}
|
||||
@@ -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'));
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
use Magento\Customer\Model\CustomerRegistry;
|
||||
use Magento\Integration\Api\CustomerTokenServiceInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
|
||||
|
||||
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer.php');
|
||||
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
/** @var CustomerRegistry $customerRegistry */
|
||||
$customerRegistry = $objectManager->create(CustomerRegistry::class);
|
||||
$customer = $customerRegistry->retrieve(1);
|
||||
/** @var CustomerTokenServiceInterface $customerTokenService */
|
||||
$customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
|
||||
|
||||
$customerTokenService->createCustomerAccessToken($customer->getEmail(), 'password');
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
|
||||
|
||||
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer_rollback.php');
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
use Magento\Integration\Api\IntegrationServiceInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$integrationService = $objectManager->get(IntegrationServiceInterface::class);
|
||||
|
||||
$data = [
|
||||
'name' => 'Fixture Integration',
|
||||
'email' => 'john.doe@example.com',
|
||||
'endpoint' => 'https://example.com/endpoint',
|
||||
'identity_link_url' => 'https://example.com/link',
|
||||
'all_resources' => 0,
|
||||
];
|
||||
$integrationService->create($data);
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
|
||||
|
||||
Resolver::getInstance()->requireDataFixture('Magento/Integration/_files/integration_all_permissions_rollback.php');
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
/** @var $integration \Magento\Integration\Model\Integration */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$integration = $objectManager->create(\Magento\Integration\Model\Integration::class);
|
||||
$integration->setName('Fixture Integration')->save();
|
||||
|
||||
/** Grant permissions to integrations */
|
||||
/** @var \Magento\Integration\Api\AuthorizationServiceInterface */
|
||||
$authorizationService = $objectManager->create(\Magento\Integration\Api\AuthorizationServiceInterface::class);
|
||||
$authorizationService->grantAllPermissions($integration->getId());
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
/** @var $integration \Magento\Integration\Model\Integration */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$integration = $objectManager->create(\Magento\Integration\Model\Integration::class);
|
||||
$integration->load('Fixture Integration', 'name');
|
||||
if ($integration->getId()) {
|
||||
$integration->delete();
|
||||
}
|
||||
Reference in New Issue
Block a user