public function testConfigBasedIntegrationCreation() { $this->assertEquals('*****@*****.**', $this->integration->getEmail()); $this->assertEquals('http://example.com/endpoint1', $this->integration->getEndpoint()); $this->assertEquals('Test Integration1', $this->integration->getName()); $this->assertEquals(Integration::TYPE_CONFIG, $this->integration->getSetupType()); }
public function testSaveActionUpdateIntegration() { $integrationId = $this->_integration->getId(); $integrationName = $this->_integration->getName(); $this->getRequest()->setParam('id', $integrationId); $url = 'http://magento.ll/endpoint_url'; $this->getRequest()->setPostValue(['name' => $integrationName, 'email' => '*****@*****.**', '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/')); }
/** * Creates a dummy integration for use in dispatched methods under testing */ private function _createDummyIntegration() { /** @var $factory \Magento\Integration\Model\IntegrationFactory */ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $factory = $objectManager->create('Magento\\Integration\\Model\\IntegrationFactory'); $this->_integration = $factory->create()->setName(md5(rand()))->save(); /** Grant permissions to integrations */ /** @var \Magento\Integration\Service\V1\AuthorizationService $authorizationService */ $authorizationService = $objectManager->create('Magento\\Integration\\Service\\V1\\AuthorizationService'); $authorizationService->grantAllPermissions($this->_integration->getId()); }
protected function setUp() { $this->_consumerFactory = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Consumer\\Factory')->disableOriginalConstructor()->getMock(); $this->_tokenProviderMock = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Token\\Provider')->disableOriginalConstructor()->getMock(); $this->_tokenMock = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Token')->disableOriginalConstructor()->setMethods(array('createVerifierToken', 'getType', '__wakeup', 'delete'))->getMock(); $this->_tokenFactoryMock = $this->getMock('Magento\\Integration\\Model\\Oauth\\Token\\Factory', array(), array(), '', false); $this->_consumerMock = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Consumer')->disableOriginalConstructor()->setMethods(array('getData', 'getId', 'load', 'save', 'delete', '__wakeup'))->getMock(); $this->_consumerData = array('entity_id' => self::VALUE_CONSUMER_ID, 'key' => self::VALUE_CONSUMER_KEY, 'secret' => 'iuyytrfdsdfbnnhbmkkjlkjl', 'created_at' => '', 'updated_at' => '', 'callback_url' => '', 'rejected_callback_url' => ''); $this->_consumerFactory->expects($this->any())->method('create')->will($this->returnValue($this->_consumerMock)); $this->_service = new \Magento\Integration\Service\V1\Oauth($this->getMock('Magento\\Store\\Model\\StoreManagerInterface', array(), array(), '', false), $this->_consumerFactory, $this->_tokenFactoryMock, $this->getMock('Magento\\Integration\\Helper\\Oauth\\Data', array(), array(), '', false), $this->getMock('Magento\\Framework\\HTTP\\ZendClient', array(), array(), '', false), $this->getMock('Magento\\Framework\\Logger', array(), array(), '', false), $this->getMock('Magento\\Framework\\Oauth\\Helper\\Oauth', array(), array(), '', false), $this->_tokenProviderMock); $this->_emptyConsumerMock = $this->getMockBuilder('Magento\\Integration\\Model\\Integration')->disableOriginalConstructor()->setMethods(array('getData', 'load', 'getId', 'save', 'delete', '__wakeup'))->getMock(); $this->_emptyConsumerMock->expects($this->any())->method('getId')->will($this->returnValue(null)); }
/** * Creates a dummy integration for use in dispatched methods under testing */ private function _createDummyIntegration() { /** @var $factory \Magento\Integration\Model\Integration\Factory */ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $factory = $objectManager->create('Magento\\Integration\\Model\\Integration\\Factory'); $this->_integration = $factory->create()->setName(md5(rand()))->save(); /** Grant permissions to integrations */ /** @var \Magento\Authz\Model\UserIdentifier\Factory $userIdentifierFactory */ $userIdentifierFactory = $objectManager->create('Magento\\Authz\\Model\\UserIdentifier\\Factory'); /** @var \Magento\Authz\Service\AuthorizationV1 $authzService */ $userIdentifier = $userIdentifierFactory->create(\Magento\Authz\Model\UserIdentifier::USER_TYPE_INTEGRATION, $this->_integration->getId()); $authzService = $objectManager->create('Magento\\Authz\\Service\\AuthorizationV1'); $authzService->grantAllPermissions($userIdentifier); }
/** * Check whether integration data or assigned resources were changed * * @param Integration $integration * @param array $originalResources * @return bool */ private function hasDataChanged(Integration $integration, $originalResources) { if (!$integration->getOrigData()) { return true; } // Check if resources have changed $newResources = $integration->getData('resource'); $commonResources = array_intersect($originalResources, $newResources); if (count($commonResources) != count($originalResources) || count($commonResources) != count($newResources)) { return true; } // Check if other data has changed $fields = [Integration::ID, Integration::NAME, Integration::EMAIL, Integration::ENDPOINT, Integration::IDENTITY_LINK_URL, Integration::SETUP_TYPE, Integration::CONSUMER_ID]; foreach ($fields as $field) { if ($integration->getOrigData($field) != $integration->getData($field)) { return true; } } return false; }
/** * Persist API permissions. * * Permissions are expected to be set to integration object by 'resource' key. * If 'all_resources' is set and is evaluated to true, permissions to all resources will be granted. * * @param IntegrationModel $integration * @return void */ protected function _saveApiPermissions(IntegrationModel $integration) { if ($integration->getId()) { if ($integration->getData('all_resources')) { $this->integrationAuthorizationService->grantAllPermissions($integration->getId()); } elseif (is_array($integration->getData('resource'))) { $this->integrationAuthorizationService->grantPermissions($integration->getId(), $integration->getData('resource')); } else { $this->integrationAuthorizationService->grantPermissions($integration->getId(), []); } } }
/** * Persist API permissions. * * Permissions are expected to be set to integration object by 'resource' key. * If 'all_resources' is set and is evaluated to true, permissions to all resources will be granted. * * @param IntegrationModel $integration * @return void */ protected function _saveApiPermissions(IntegrationModel $integration) { if ($integration->getId()) { $userIdentifier = $this->_createUserIdentifier($integration->getId()); if ($integration->getData('all_resources')) { $this->_authzService->grantAllPermissions($userIdentifier); } else { if (is_array($integration->getData('resource'))) { $this->_authzService->grantPermissions($userIdentifier, $integration->getData('resource')); } else { $this->_authzService->grantPermissions($userIdentifier, array()); } } } }
public function testGetStatus() { $this->integrationModel->setStatus(1); $this->assertEquals(1, $this->integrationModel->getStatus()); }
public function testLoadActiveIntegrationByConsumerId() { $integration = $this->integration->getResource()->selectActiveIntegrationByConsumerId($this->consumer->getId()); $this->assertEquals($this->integration->getId(), $integration['integration_id']); }
/** * Return available resourses for integration model * * @param IntegrationModel $integration * @return string[] */ private function getIntegrationApiResource(IntegrationModel $integration) { $resources = []; $integrationResources = $this->integrationApiConfig->getIntegrations(); $integrationName = $integration->getData('name'); if (!empty($integrationResources[$integrationName]['resource'])) { $resources = $integrationResources[$integrationName]['resource']; } return $resources; }
/** * Add oAuth token and token secret. * * @param IntegrationModel $integration * @return void */ protected function _addOauthTokenData(IntegrationModel $integration) { if ($integration->getId()) { $accessToken = $this->_oauthService->getAccessToken($integration->getConsumerId()); if ($accessToken) { $integration->setData('token', $accessToken->getToken()); $integration->setData('token_secret', $accessToken->getSecret()); } } }