123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- <?php
- /**
- * Test for \Magento\Integration\Model\OauthService
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Integration\Test\Unit\Model;
- use Magento\Integration\Model\Oauth\Token;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class OauthServiceTest extends \PHPUnit\Framework\TestCase
- {
- const VALUE_CONSUMER_ID = 1;
- const VALUE_CONSUMER_KEY = 'asdfghjklaqwerfdtyuiomnbgfdhbsoi';
- const VALUE_TOKEN_TYPE = 'access';
- /** @var \Magento\Integration\Model\Oauth\ConsumerFactory|\PHPUnit_Framework_MockObject_MockObject */
- protected $_consumerFactory;
- /** @var \Magento\Integration\Model\Oauth\Token\Provider|\PHPUnit_Framework_MockObject_MockObject */
- protected $_tokenProviderMock;
- /** @var \Magento\Integration\Model\Oauth\Consumer|\PHPUnit_Framework_MockObject_MockObject */
- private $_consumerMock;
- /** @var \Magento\Integration\Model\Integration|\PHPUnit_Framework_MockObject_MockObject */
- private $_emptyConsumerMock;
- /**
- * @var \Magento\Integration\Model\Oauth\Token|\PHPUnit_Framework_MockObject_MockObject
- */
- private $_tokenMock;
- /** @var \Magento\Integration\Model\OauthService */
- private $_service;
- /** @var array */
- private $_consumerData;
- /**
- * @var \Magento\Integration\Model\Oauth\TokenFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $_tokenFactoryMock;
- /**
- * @return void
- */
- protected function setUp()
- {
- $this->_consumerFactory = $this->getMockBuilder(\Magento\Integration\Model\Oauth\ConsumerFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->_tokenProviderMock = $this->getMockBuilder(
- \Magento\Integration\Model\Oauth\Token\Provider::class
- )->disableOriginalConstructor()->getMock();
- $this->_tokenMock = $this->getMockBuilder(
- \Magento\Integration\Model\Oauth\Token::class
- )->disableOriginalConstructor()->setMethods(
- ['createVerifierToken', 'getType', '__wakeup', 'delete']
- )->getMock();
- $this->_tokenFactoryMock = $this->createPartialMock(
- \Magento\Integration\Model\Oauth\TokenFactory::class,
- ['create']
- );
- $this->_tokenFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock));
- $this->_consumerMock = $this->getMockBuilder(
- \Magento\Integration\Model\Oauth\Consumer::class
- )->disableOriginalConstructor()->setMethods(
- ['getData', 'getId', 'load', 'save', 'delete', '__wakeup']
- )->getMock();
- $this->_consumerData = [
- '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\Model\OauthService(
- $this->createMock(\Magento\Store\Model\StoreManagerInterface::class),
- $this->_consumerFactory,
- $this->_tokenFactoryMock,
- $this->createMock(\Magento\Integration\Helper\Oauth\Data::class),
- $this->createMock(\Magento\Framework\HTTP\ZendClient::class),
- $this->createMock(\Psr\Log\LoggerInterface::class),
- $this->createMock(\Magento\Framework\Oauth\Helper\Oauth::class),
- $this->_tokenProviderMock
- );
- $this->_emptyConsumerMock = $this->getMockBuilder(
- \Magento\Integration\Model\Integration::class
- )->disableOriginalConstructor()->setMethods(
- ['getData', 'load', 'getId', 'save', 'delete', '__wakeup']
- )->getMock();
- $this->_emptyConsumerMock->expects($this->any())->method('getId')->will($this->returnValue(null));
- }
- /**
- * @return void
- */
- public function testDelete()
- {
- $this->_consumerMock->expects(
- $this->once()
- )->method(
- 'getId'
- )->will(
- $this->returnValue(self::VALUE_CONSUMER_ID)
- );
- $this->_consumerMock->expects(
- $this->once()
- )->method(
- 'load'
- )->with(
- self::VALUE_CONSUMER_ID
- )->will(
- $this->returnValue($this->_consumerMock)
- );
- $this->_consumerMock->expects($this->once())->method('delete')->will($this->returnValue($this->_consumerMock));
- $this->_consumerMock->expects($this->any())->method('getData')->will($this->returnValue($this->_consumerData));
- $consumerData = $this->_service->deleteConsumer(self::VALUE_CONSUMER_ID);
- $this->assertEquals($this->_consumerData['entity_id'], $consumerData['entity_id']);
- }
- /**
- * @return void
- * @expectedException \Magento\Framework\Exception\IntegrationException
- * @expectedExceptionMessage A consumer with ID "1" doesn't exist. Verify the ID and try again.
- */
- public function testDeleteException()
- {
- $this->_consumerMock->expects($this->any())->method('getId')->will($this->returnValue(null));
- $this->_consumerMock->expects($this->once())->method('load')->will($this->returnSelf());
- $this->_consumerMock->expects($this->never())->method('delete');
- $this->_service->deleteConsumer(self::VALUE_CONSUMER_ID);
- }
- /**
- * @return void
- */
- public function testCreateAccessTokenAndClearExisting()
- {
- $this->_consumerMock->expects(
- $this->any()
- )->method(
- 'load'
- )->with(
- self::VALUE_CONSUMER_ID
- )->will(
- $this->returnValue($this->_consumerMock)
- );
- $this->_tokenProviderMock->expects(
- $this->any()
- )->method(
- 'getIntegrationTokenByConsumerId'
- )->will(
- $this->returnValue($this->_tokenMock)
- );
- $this->_tokenProviderMock->expects($this->any())->method('createRequestToken')->with($this->_consumerMock);
- $this->_tokenProviderMock->expects($this->any())->method('getAccessToken')->with($this->_consumerMock);
- $this->_tokenFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock));
- $this->_tokenMock->expects($this->once())->method('delete');
- $this->_tokenMock->expects($this->once())->method('createVerifierToken')->with(self::VALUE_CONSUMER_ID);
- $this->_tokenProviderMock->expects($this->once())->method('createRequestToken')->with($this->_consumerMock);
- $this->_tokenProviderMock->expects($this->once())->method('getAccessToken')->with($this->_consumerMock);
- $this->assertTrue($this->_service->createAccessToken(self::VALUE_CONSUMER_ID, true));
- }
- /**
- * @return void
- */
- public function testCreateAccessTokenWithoutClearingExisting()
- {
- $this->_consumerMock->expects(
- $this->any()
- )->method(
- 'load'
- )->with(
- self::VALUE_CONSUMER_ID
- )->will(
- $this->returnValue($this->_consumerMock)
- );
- $this->_tokenProviderMock->expects(
- $this->any()
- )->method(
- 'getIntegrationTokenByConsumerId'
- )->will(
- $this->returnValue($this->_tokenMock)
- );
- $this->_tokenMock->expects($this->never())->method('delete');
- $this->assertFalse($this->_service->createAccessToken(self::VALUE_CONSUMER_ID, false));
- }
- /**
- * @return void
- */
- public function testCreateAccessTokenInvalidConsumerId()
- {
- $this->_consumerMock->expects(
- $this->any()
- )->method(
- 'load'
- )->with(
- 0
- )->will(
- $this->returnValue($this->_consumerMock)
- );
- $this->_tokenProviderMock->expects(
- $this->any()
- )->method(
- 'getIntegrationTokenByConsumerId'
- )->will(
- $this->throwException(
- new \Magento\Framework\Oauth\Exception(
- __('A token with consumer ID 0 does not exist')
- )
- )
- );
- $this->_tokenMock->expects($this->never())->method('delete');
- $this->_tokenFactoryMock->expects(
- $this->once()
- )->method(
- 'create'
- )->will(
- $this->returnValue($this->_tokenMock)
- );
- $this->_tokenMock->expects($this->once())->method('createVerifierToken');
- $this->_tokenProviderMock->expects($this->once())->method('createRequestToken');
- $this->_tokenProviderMock->expects($this->once())->method('getAccessToken');
- $this->assertTrue($this->_service->createAccessToken(0, false));
- }
- /**
- * @return void
- */
- public function testLoadConsumer()
- {
- $this->_consumerMock->expects(
- $this->once()
- )->method(
- 'load'
- )->with(
- self::VALUE_CONSUMER_ID
- )->will(
- $this->returnValue($this->_consumerMock)
- );
- $this->_consumerMock->expects($this->any())->method('getData')->will($this->returnValue($this->_consumerData));
- $consumer = $this->_service->loadConsumer(self::VALUE_CONSUMER_ID);
- $consumerData = $consumer->getData();
- $this->assertEquals($this->_consumerData['entity_id'], $consumerData['entity_id']);
- }
- /**
- * @return void
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testLoadConsumerException()
- {
- $this->_consumerMock->expects(
- $this->once()
- )->method(
- 'load'
- )->will(
- $this->throwException(
- new \Magento\Framework\Oauth\Exception(
- __(
- "The oAuth consumer account couldn't be loaded due to an unexpected error. "
- . "Please try again later."
- )
- )
- )
- );
- $this->_service->loadConsumer(self::VALUE_CONSUMER_ID);
- $this->expectExceptionMessage(
- "The oAuth consumer account couldn't be loaded due to an unexpected error. Please try again later."
- );
- }
- /**
- * @return void
- */
- public function testLoadConsumerByKey()
- {
- $this->_consumerMock->expects(
- $this->once()
- )->method(
- 'load'
- )->with(
- self::VALUE_CONSUMER_KEY,
- 'key'
- )->will(
- $this->returnValue($this->_consumerMock)
- );
- $this->_consumerMock->expects($this->any())->method('getData')->will($this->returnValue($this->_consumerData));
- $consumer = $this->_service->loadConsumerByKey(self::VALUE_CONSUMER_KEY);
- $consumerData = $consumer->getData();
- $this->assertEquals($this->_consumerData['key'], $consumerData['key']);
- }
- /**
- * @return void
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testLoadConsumerByKeyException()
- {
- $this->_consumerMock->expects(
- $this->once()
- )->method(
- 'load'
- )->will(
- $this->throwException(
- new \Magento\Framework\Oauth\Exception(
- __(
- "The oAuth consumer account couldn't be loaded due to an unexpected error. "
- . "Please try again later."
- )
- )
- )
- );
- $this->_service->loadConsumerByKey(self::VALUE_CONSUMER_KEY);
- $this->expectExceptionMessage(
- "The oAuth consumer account couldn't be loaded due to an unexpected error. Please try again later."
- );
- }
- /**
- * @return void
- */
- public function testDeleteToken()
- {
- $this->_consumerMock->expects(
- $this->any()
- )->method(
- 'load'
- )->with(
- self::VALUE_CONSUMER_ID
- )->will(
- $this->returnValue($this->_consumerMock)
- );
- $this->_tokenProviderMock->expects(
- $this->any()
- )->method(
- 'getIntegrationTokenByConsumerId'
- )->will(
- $this->returnValue($this->_tokenMock)
- );
- $this->_tokenMock->expects($this->once())->method('delete');
- $this->assertTrue($this->_service->deleteIntegrationToken(self::VALUE_CONSUMER_ID));
- }
- /**
- * @return void
- */
- public function testDeleteTokenNegative()
- {
- $this->_consumerMock->expects(
- $this->any()
- )->method(
- 'load'
- )->with(
- self::VALUE_CONSUMER_ID
- )->will(
- $this->returnValue($this->_consumerMock)
- );
- $this->_tokenProviderMock->expects(
- $this->any()
- )->method(
- 'getIntegrationTokenByConsumerId'
- )->will(
- $this->returnValue($this->_tokenMock)
- );
- $this->_tokenMock->expects($this->never())->method('delete');
- $this->assertFalse($this->_service->deleteIntegrationToken(null));
- }
- /**
- * @return void
- */
- public function testGetAccessTokenNoAccess()
- {
- $this->_consumerMock->expects(
- $this->any()
- )->method(
- 'load'
- )->with(
- self::VALUE_CONSUMER_ID
- )->will(
- $this->returnValue($this->_consumerMock)
- );
- $this->_tokenProviderMock->expects(
- $this->any()
- )->method(
- 'getIntegrationTokenByConsumerId'
- )->will(
- $this->returnValue($this->_tokenMock)
- );
- $this->assertFalse($this->_service->getAccessToken(self::VALUE_CONSUMER_ID), false);
- }
- /**
- * @return void
- */
- public function testGetAccessSuccess()
- {
- $this->_consumerMock->expects(
- $this->any()
- )->method(
- 'load'
- )->with(
- self::VALUE_CONSUMER_ID
- )->will(
- $this->returnValue($this->_consumerMock)
- );
- $this->_tokenMock->expects($this->once())->method('getType')->will($this->returnValue(Token::TYPE_ACCESS));
- $this->_tokenProviderMock->expects(
- $this->any()
- )->method(
- 'getIntegrationTokenByConsumerId'
- )->will(
- $this->returnValue($this->_tokenMock)
- );
- $this->assertEquals($this->_service->getAccessToken(self::VALUE_CONSUMER_ID), $this->_tokenMock);
- }
- }
|