123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Integration\Test\Unit\Oauth;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class OauthTest extends \PHPUnit\Framework\TestCase
- {
- /** @var \Magento\Integration\Model\Oauth\ConsumerFactory */
- private $_consumerFactory;
- /** @var \Magento\Integration\Model\Oauth\NonceFactory */
- private $_nonceFactory;
- /** @var \Magento\Integration\Model\Oauth\TokenFactory */
- private $_tokenFactory;
- /** @var \Magento\Integration\Model\Oauth\Consumer */
- private $_consumerMock;
- /** @var \Magento\Integration\Model\Oauth\Token */
- private $_tokenMock;
- /** @var \Magento\Framework\Oauth\Helper\Oauth */
- private $_oauthHelperMock;
- /** @var \Magento\Framework\Oauth\Oauth */
- private $_oauth;
- /** @var \Zend_Oauth_Http_Utility */
- private $_httpUtilityMock;
- /** @var \Magento\Framework\Stdlib\DateTime\DateTime */
- private $_dateMock;
- /**
- * @var \Psr\Log\LoggerInterface
- */
- private $_loggerMock;
- private $_oauthToken;
- private $_oauthSecret;
- private $_oauthVerifier;
- const CONSUMER_ID = 1;
- const REQUEST_URL = 'http://magento.ll';
- protected function setUp()
- {
- $this->_consumerFactory = $this->getMockBuilder(\Magento\Integration\Model\Oauth\ConsumerFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->_consumerMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\Consumer::class)
- ->disableOriginalConstructor()->setMethods(
- [
- 'getCreatedAt',
- 'loadByKey',
- 'load',
- 'getId',
- 'getSecret',
- 'getCallbackUrl',
- 'save',
- 'getData',
- 'isValidForTokenExchange',
- '__wakeup',
- ]
- )
- ->getMock();
- $this->_consumerFactory->expects($this->any())
- ->method('create')
- ->will($this->returnValue($this->_consumerMock));
- $this->_nonceFactory = $this->getMockBuilder(\Magento\Integration\Model\Oauth\NonceFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->_tokenFactory = $this->getMockBuilder(
- \Magento\Integration\Model\Oauth\TokenFactory::class
- )->disableOriginalConstructor()->setMethods(['create'])->getMock();
- $this->_tokenMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\Token::class)
- ->disableOriginalConstructor()
- ->setMethods(
- [
- 'getId',
- 'load',
- 'getType',
- 'createRequestToken',
- 'getToken',
- 'getSecret',
- 'createVerifierToken',
- 'getVerifier',
- 'getConsumerId',
- 'convertToAccess',
- 'getRevoked',
- 'getResource',
- 'loadByConsumerIdAndUserType',
- '__wakeup',
- ]
- )
- ->getMock();
- $this->_tokenFactory->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock));
- $this->_oauthHelperMock = $this->getMockBuilder(\Magento\Framework\Oauth\Helper\Oauth::class)
- ->setConstructorArgs([new \Magento\Framework\Math\Random()])
- ->getMock();
- $this->_httpUtilityMock = $this->getMockBuilder(\Zend_Oauth_Http_Utility::class)
- ->setMethods(['sign'])
- ->getMock();
- $this->_dateMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->_loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $nonceGenerator = new \Magento\Integration\Model\Oauth\Nonce\Generator(
- $this->_oauthHelperMock,
- $this->_nonceFactory,
- $this->_dateMock
- );
- $tokenProvider = new \Magento\Integration\Model\Oauth\Token\Provider(
- $this->_consumerFactory,
- $this->_tokenFactory,
- $this->_loggerMock
- );
- $this->_oauth = new \Magento\Framework\Oauth\Oauth(
- $this->_oauthHelperMock,
- $nonceGenerator,
- $tokenProvider,
- $this->_httpUtilityMock
- );
- $this->_oauthToken = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN);
- $this->_oauthSecret = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_SECRET);
- $this->_oauthVerifier = $this->_generateRandomString(
- \Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_VERIFIER
- );
- }
- public function tearDown()
- {
- unset($this->_consumerFactory);
- unset($this->_nonceFactory);
- unset($this->_tokenFactory);
- unset($this->_oauthHelperMock);
- unset($this->_httpUtilityMock);
- unset($this->_dateMock);
- unset($this->_oauth);
- }
- /**
- * @param array $amendments
- * @return array
- */
- protected function _getRequestTokenParams($amendments = [])
- {
- $requiredParams = [
- 'oauth_version' => '1.0',
- 'oauth_consumer_key' => $this->_generateRandomString(
- \Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY
- ),
- 'oauth_nonce' => '',
- 'oauth_timestamp' => time(),
- 'oauth_signature_method' => \Magento\Framework\Oauth\OauthInterface::SIGNATURE_SHA1,
- 'oauth_signature' => 'invalid_signature',
- ];
- return array_merge($requiredParams, $amendments);
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_VERSION_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\OauthInputException
- */
- public function testGetRequestTokenVersionRejected()
- {
- $this->_oauth->getRequestToken(
- $this->_getRequestTokenParams(['oauth_version' => '2.0']),
- self::REQUEST_URL
- );
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_CONSUMER_KEY_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testGetRequestTokenConsumerKeyRejected()
- {
- $this->_oauth->getRequestToken(
- $this->_getRequestTokenParams(['oauth_consumer_key' => 'wrong_key_length']),
- self::REQUEST_URL
- );
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_CONSUMER_KEY_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testGetRequestTokenConsumerKeyNotFound()
- {
- $this->_consumerMock->expects(
- $this->once()
- )->method(
- 'loadByKey'
- )->will(
- $this->returnValue(new \Magento\Framework\DataObject())
- );
- $this->_oauth->getRequestToken($this->_getRequestTokenParams(), self::REQUEST_URL);
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_CONSUMER_KEY_INVALID
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testGetRequestTokenOutdatedConsumerKey()
- {
- $this->_setupConsumer();
- $this->_setupNonce();
- $this->_consumerMock
- ->expects($this->any())
- ->method('isValidForTokenExchange')
- ->will($this->returnValue(false));
- $this->_oauth->getRequestToken($this->_getRequestTokenParams(), self::REQUEST_URL);
- }
- /**
- * @param bool $isLoadable
- */
- protected function _setupConsumer($isLoadable = true)
- {
- $this->_consumerMock->expects($this->any())->method('loadByKey')->will($this->returnSelf());
- $this->_consumerMock->expects(
- $this->any()
- )->method(
- 'getCreatedAt'
- )->will(
- $this->returnValue(date('c', strtotime('-1 day')))
- );
- if ($isLoadable) {
- $this->_consumerMock->expects($this->any())->method('load')->will($this->returnSelf());
- } else {
- $this->_consumerMock->expects(
- $this->any()
- )->method(
- 'load'
- )->will(
- $this->returnValue(new \Magento\Framework\DataObject())
- );
- }
- $this->_consumerMock->expects($this->any())->method('getId')->will($this->returnValue(1));
- $this->_consumerMock->expects($this->any())->method('getSecret')->will($this->returnValue('consumer_secret'));
- $this->_consumerMock->expects(
- $this->any()
- )->method(
- 'getCallbackUrl'
- )->will(
- $this->returnValue('callback_url')
- );
- }
- protected function _makeValidExpirationPeriod()
- {
- $this->_consumerMock
- ->expects($this->any())
- ->method('isValidForTokenExchange')
- ->will($this->returnValue(true));
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_TIMESTAMP_REFUSED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- * @dataProvider dataProviderForGetRequestTokenNonceTimestampRefusedTest
- */
- public function testGetRequestTokenOauthTimestampRefused($timestamp)
- {
- $this->_setupConsumer();
- $this->_makeValidExpirationPeriod();
- $this->_oauth->getRequestToken(
- $this->_getRequestTokenParams(['oauth_timestamp' => $timestamp]),
- self::REQUEST_URL
- );
- }
- /**
- * @return array
- */
- public function dataProviderForGetRequestTokenNonceTimestampRefusedTest()
- {
- return [
- [0],
- //Adding one day deviation
- [time() + \Magento\Integration\Model\Oauth\Nonce\Generator::TIME_DEVIATION + 86400]
- ];
- }
- /**
- * @param bool $isUsed
- * @param int $timestamp
- */
- protected function _setupNonce($isUsed = false, $timestamp = 0)
- {
- $nonceMock = $this->getMockBuilder(
- \Magento\Integration\Model\Oauth\Nonce::class
- )->disableOriginalConstructor()->setMethods(
- [
- 'loadByCompositeKey',
- 'getNonce',
- 'getTimestamp',
- 'setNonce',
- 'setConsumerId',
- 'setTimestamp',
- 'save',
- '__wakeup',
- ]
- )->getMock();
- $nonceMock->expects($this->any())->method('getNonce')->will($this->returnValue($isUsed));
- $nonceMock->expects($this->any())->method('loadByCompositeKey')->will($this->returnSelf());
- $nonceMock->expects($this->any())->method('getTimestamp')->will($this->returnValue($timestamp));
- $nonceMock->expects($this->any())->method('setNonce')->will($this->returnSelf());
- $nonceMock->expects($this->any())->method('setConsumerId')->will($this->returnSelf());
- $nonceMock->expects($this->any())->method('setTimestamp')->will($this->returnSelf());
- $nonceMock->expects($this->any())->method('save')->will($this->returnSelf());
- $this->_nonceFactory->expects($this->any())->method('create')->will($this->returnValue($nonceMock));
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_NONCE_USED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testGetRequestTokenNonceAlreadyUsed()
- {
- $this->_setupConsumer();
- $this->_makeValidExpirationPeriod();
- $this->_setupNonce(true);
- $this->_oauth->getRequestToken($this->_getRequestTokenParams(), self::REQUEST_URL);
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_CONSUMER_KEY_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testGetRequestTokenNoConsumer()
- {
- $this->_consumerMock->expects(
- $this->any()
- )->method(
- 'loadByKey'
- )->will(
- $this->returnValue(new \Magento\Framework\DataObject())
- );
- $this->_oauth->getRequestToken($this->_getRequestTokenParams(), self::REQUEST_URL);
- }
- /**
- * @param bool $doesExist
- * @param string $type
- * @param int $consumerId
- * @param null $verifier
- * @param bool $isRevoked
- */
- protected function _setupToken(
- $doesExist = true,
- $type = \Magento\Integration\Model\Oauth\Token::TYPE_VERIFIER,
- $consumerId = self::CONSUMER_ID,
- $verifier = null,
- $isRevoked = false
- ) {
- $this->_tokenMock->expects(
- $this->any()
- )->method(
- 'getId'
- )->will(
- $this->returnValue($doesExist ? self::CONSUMER_ID : null)
- );
- $verifier = $verifier ?: $this->_oauthVerifier;
- $this->_tokenMock->expects($this->any())->method('load')->will($this->returnSelf());
- $this->_tokenMock->expects($this->any())->method('getType')->will($this->returnValue($type));
- $this->_tokenMock->expects($this->any())->method('createRequestToken')->will($this->returnSelf());
- $this->_tokenMock->expects($this->any())->method('getToken')->will($this->returnValue($this->_oauthToken));
- $this->_tokenMock->expects($this->any())->method('getSecret')->will($this->returnValue($this->_oauthSecret));
- $this->_tokenMock->expects($this->any())->method('getConsumerId')->will($this->returnValue($consumerId));
- $this->_tokenMock->expects($this->any())->method('getVerifier')->will($this->returnValue($verifier));
- $this->_tokenMock->expects($this->any())->method('convertToAccess')->will($this->returnSelf());
- $this->_tokenMock->expects($this->any())->method('getRevoked')->will($this->returnValue($isRevoked));
- $this->_tokenMock->expects($this->any())->method('loadByConsumerIdAndUserType')->will($this->returnSelf());
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testGetRequestTokenTokenRejected()
- {
- $this->_setupConsumer();
- $this->_makeValidExpirationPeriod();
- $this->_setupNonce();
- $this->_setupToken(false);
- $signature = 'valid_signature';
- $this->_httpUtilityMock->expects($this->any())->method('sign')->will($this->returnValue($signature));
- $this->_oauth->getRequestToken(
- $this->_getRequestTokenParams(['oauth_signature' => $signature]),
- self::REQUEST_URL
- );
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testGetRequestTokenTokenRejectedByType()
- {
- $this->_setupConsumer();
- $this->_makeValidExpirationPeriod();
- $this->_setupNonce();
- $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST);
- // wrong type
- $signature = 'valid_signature';
- $this->_httpUtilityMock->expects($this->any())->method('sign')->will($this->returnValue($signature));
- $this->_oauth->getRequestToken(
- $this->_getRequestTokenParams(['oauth_signature' => $signature]),
- self::REQUEST_URL
- );
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_SIGNATURE_METHOD_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\OauthInputException
- */
- public function testGetRequestTokenSignatureMethodRejected()
- {
- $this->_setupConsumer();
- $this->_makeValidExpirationPeriod();
- $this->_setupNonce();
- $this->_setupToken();
- $this->_oauth->getRequestToken(
- $this->_getRequestTokenParams(['oauth_signature_method' => 'wrong_method']),
- self::REQUEST_URL
- );
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_SIGNATURE_INVALID
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testGetRequestTokenInvalidSignature()
- {
- $this->_setupConsumer();
- $this->_makeValidExpirationPeriod();
- $this->_setupNonce();
- $this->_setupToken();
- $this->_oauth->getRequestToken(
- $this->_getRequestTokenParams(['oauth_signature' => 'invalid_signature']),
- self::REQUEST_URL
- );
- }
- public function testGetRequestToken()
- {
- $this->_setupConsumer();
- $this->_makeValidExpirationPeriod();
- $this->_setupNonce();
- $this->_setupToken();
- $signature = 'valid_signature';
- $this->_httpUtilityMock->expects($this->any())->method('sign')->will($this->returnValue($signature));
- $requestToken = $this->_oauth->getRequestToken(
- $this->_getRequestTokenParams(['oauth_signature' => $signature]),
- self::REQUEST_URL
- );
- $this->assertEquals(
- ['oauth_token' => $this->_oauthToken, 'oauth_token_secret' => $this->_oauthSecret],
- $requestToken
- );
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_VERSION_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\OauthInputException
- */
- public function testGetAccessTokenVersionRejected()
- {
- $this->_oauth->getAccessToken(
- $this->_getAccessTokenRequiredParams(['oauth_version' => '0.0']),
- self::REQUEST_URL
- );
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_PARAMETER_ABSENT
- *
- * @expectedException \Magento\Framework\Oauth\OauthInputException
- * @expectedExceptionMessage "oauth_verifier" is required. Enter and try again.
- */
- public function testGetAccessTokenParameterAbsent()
- {
- $this->_oauth->getAccessToken(
- [
- 'oauth_version' => '1.0',
- 'oauth_consumer_key' => '',
- 'oauth_signature' => '',
- 'oauth_signature_method' => '',
- 'oauth_nonce' => '',
- 'oauth_timestamp' => '',
- 'oauth_token' => '',
- // oauth_verifier missing
- ],
- self::REQUEST_URL
- );
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\OauthInputException
- */
- public function testGetAccessTokenTokenRejected()
- {
- $this->_oauth->getAccessToken(
- $this->_getAccessTokenRequiredParams(['oauth_token' => 'invalid_token']),
- self::REQUEST_URL
- );
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_SIGNATURE_METHOD_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\OauthInputException
- */
- public function testGetAccessTokenSignatureMethodRejected()
- {
- $this->_oauth->getAccessToken(
- $this->_getAccessTokenRequiredParams(['oauth_signature_method' => 'invalid_method']),
- self::REQUEST_URL
- );
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_USED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testGetAccessTokenTokenUsed()
- {
- $this->_setupConsumer();
- $this->_setupNonce();
- $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_VERIFIER);
- // Wrong type
- $this->_oauth->getAccessToken($this->_getAccessTokenRequiredParams(), self::REQUEST_URL);
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testGetAccessTokenConsumerIdDoesntMatch()
- {
- $this->_setupConsumer();
- $this->_setupNonce();
- $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST, null);
- $this->_oauth->getAccessToken($this->_getAccessTokenRequiredParams(), self::REQUEST_URL);
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_VERIFIER_INVALID
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- * @dataProvider dataProviderForGetAccessTokenVerifierInvalidTest
- */
- public function testGetAccessTokenVerifierInvalid($verifier, $verifierFromToken)
- {
- $this->_setupConsumer();
- $this->_setupNonce();
- $this->_setupToken(
- true,
- \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST,
- self::CONSUMER_ID,
- $verifierFromToken
- );
- $this->_oauth->getAccessToken(
- $this->_getAccessTokenRequiredParams(['oauth_verifier' => $verifier]),
- self::REQUEST_URL
- );
- }
- /**
- * @return array
- */
- public function dataProviderForGetAccessTokenVerifierInvalidTest()
- {
- // Verifier is not a string
- return [[3, 3], ['wrong_length', 'wrong_length'], ['verifier', 'doesn\'t match']];
- }
- public function testGetAccessToken()
- {
- $this->_setupConsumer();
- $this->_setupNonce();
- $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST);
- $token = $this->_oauth->getAccessToken($this->_getAccessTokenRequiredParams(), self::REQUEST_URL);
- $this->assertEquals(
- ['oauth_token' => $this->_oauthToken, 'oauth_token_secret' => $this->_oauthSecret],
- $token
- );
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testValidateAccessTokenRequestTokenRejected()
- {
- $this->_setupConsumer();
- $this->_setupNonce();
- $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS, null);
- $this->_oauth->validateAccessTokenRequest($this->_getAccessTokenRequiredParams(), self::REQUEST_URL);
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testValidateAccessTokenRequestTokenRejectedByType()
- {
- $this->_setupConsumer();
- $this->_setupNonce();
- $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST);
- $this->_oauth->validateAccessTokenRequest($this->_getAccessTokenRequiredParams(), self::REQUEST_URL);
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REVOKED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testValidateAccessTokenRequestTokenRevoked()
- {
- $this->_setupConsumer();
- $this->_setupNonce();
- $this->_setupToken(
- true,
- \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS,
- self::CONSUMER_ID,
- $this->_oauthVerifier,
- true
- );
- $this->_oauth->validateAccessTokenRequest($this->_getAccessTokenRequiredParams(), self::REQUEST_URL);
- }
- public function testValidateAccessTokenRequest()
- {
- $this->_setupConsumer();
- $this->_setupNonce();
- $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS);
- $requiredParams = $this->_getAccessTokenRequiredParams();
- $this->assertEquals(
- 1,
- $this->_oauth->validateAccessTokenRequest($requiredParams, self::REQUEST_URL),
- "Consumer ID is invalid."
- );
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testValidateAccessTokenRejectedByType()
- {
- $this->_setupConsumer();
- $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST);
- $this->_oauth->validateAccessToken($this->_oauthToken);
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REVOKED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testValidateAccessTokenRevoked()
- {
- $this->_setupConsumer();
- $this->_setupToken(
- true,
- \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS,
- self::CONSUMER_ID,
- $this->_oauthVerifier,
- true
- );
- $this->_oauth->validateAccessToken($this->_oauthToken);
- }
- /**
- * \Magento\Framework\Oauth\OauthInterface::ERR_TOKEN_REJECTED
- *
- * @expectedException \Magento\Framework\Oauth\Exception
- */
- public function testValidateAccessTokenNoConsumer()
- {
- $this->_setupConsumer(false);
- $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS);
- $this->_oauth->validateAccessToken($this->_oauthToken);
- }
- public function testValidateAccessToken()
- {
- $this->_setupConsumer();
- $this->_setupToken(true, \Magento\Integration\Model\Oauth\Token::TYPE_ACCESS);
- $this->assertEquals(1, $this->_oauth->validateAccessToken($this->_oauthToken), "Consumer ID is invalid.");
- }
- public function testBuildAuthorizationHeader()
- {
- $signature = 'valid_signature';
- $this->_httpUtilityMock->expects($this->any())->method('sign')->will($this->returnValue($signature));
- $this->_setupConsumer(false);
- $this->_oauthHelperMock->expects(
- $this->any()
- )->method(
- 'generateRandomString'
- )->will(
- $this->returnValue('tyukmnjhgfdcvxstyuioplkmnhtfvert')
- );
- $request = [
- 'oauth_consumer_key' => 'edf957ef88492f0a32eb7e1731e85da2',
- 'oauth_consumer_secret' => 'asdawwewefrtyh2f0a32eb7e1731e85d',
- 'oauth_token' => '7c0709f789e1f38a17aa4b9a28e1b06c',
- 'oauth_token_secret' => 'a6agsfrsfgsrjjjjyy487939244ssggg',
- 'custom_param1' => 'foo',
- 'custom_param2' => 'bar',
- ];
- $requestUrl = 'http://www.example.com/endpoint';
- $oauthHeader = $this->_oauth->buildAuthorizationHeader($request, $requestUrl);
- $expectedHeader = 'OAuth oauth_nonce="tyukmnjhgfdcvxstyuioplkmnhtfvert",' .
- 'oauth_timestamp="",' .
- 'oauth_version="1.0",oauth_consumer_key="edf957ef88492f0a32eb7e1731e85da2",' .
- 'oauth_consumer_secret="asdawwewefrtyh2f0a32eb7e1731e85d",' .
- 'oauth_token="7c0709f789e1f38a17aa4b9a28e1b06c",' .
- 'oauth_token_secret="a6agsfrsfgsrjjjjyy487939244ssggg",' .
- 'oauth_signature="valid_signature"';
- $this->assertEquals($expectedHeader, $oauthHeader, 'Generated Oauth header is incorrect');
- }
- /**
- * @dataProvider dataProviderMissingParamForBuildAuthorizationHeaderTest
- */
- public function testMissingParamForBuildAuthorizationHeader($expectedMessage, $request)
- {
- $this->expectException(\Magento\Framework\Oauth\OauthInputException::class);
- $this->expectExceptionMessage($expectedMessage);
- $this->expectExceptionCode(0);
- $requestUrl = 'http://www.example.com/endpoint';
- $this->_oauth->buildAuthorizationHeader($request, $requestUrl);
- }
- /**
- * @return array
- */
- public function dataProviderMissingParamForBuildAuthorizationHeaderTest()
- {
- return [
- [
- 'oauth_consumer_key',
- [ //'oauth_consumer_key' => 'edf957ef88492f0a32eb7e1731e85d',
- 'oauth_consumer_secret' => 'asdawwewefrtyh2f0a32eb7e1731e85d',
- 'oauth_token' => '7c0709f789e1f38a17aa4b9a28e1b06c',
- 'oauth_token_secret' => 'a6agsfrsfgsrjjjjyy487939244ssggg',
- 'custom_param1' => 'foo',
- 'custom_param2' => 'bar'
- ],
- ],
- [
- 'oauth_consumer_secret',
- [
- 'oauth_consumer_key' => 'edf957ef88492f0a32eb7e1731e85d',
- //'oauth_consumer_secret' => 'asdawwewefrtyh2f0a32eb7e1731e85d',
- 'oauth_token' => '7c0709f789e1f38a17aa4b9a28e1b06c',
- 'oauth_token_secret' => 'a6agsfrsfgsrjjjjyy487939244ssggg',
- 'custom_param1' => 'foo',
- 'custom_param2' => 'bar'
- ]
- ],
- [
- 'oauth_token',
- [
- 'oauth_consumer_key' => 'edf957ef88492f0a32eb7e1731e85d',
- 'oauth_consumer_secret' => 'asdawwewefrtyh2f0a32eb7e1731e85d',
- //'oauth_token' => '7c0709f789e1f38a17aa4b9a28e1b06c',
- 'oauth_token_secret' => 'a6agsfrsfgsrjjjjyy487939244ssggg',
- 'custom_param1' => 'foo',
- 'custom_param2' => 'bar'
- ]
- ],
- [
- 'oauth_token_secret',
- [
- 'oauth_consumer_key' => 'edf957ef88492f0a32eb7e1731e85d',
- 'oauth_consumer_secret' => 'asdawwewefrtyh2f0a32eb7e1731e85d',
- 'oauth_token' => '7c0709f789e1f38a17aa4b9a28e1b06c',
- //'oauth_token_secret' => 'a6agsfrsfgsrjjjjyy487939244ssggg',
- 'custom_param1' => 'foo',
- 'custom_param2' => 'bar'
- ]
- ]
- ];
- }
- /**
- * @param array $amendments
- * @return array
- */
- protected function _getAccessTokenRequiredParams($amendments = [])
- {
- $requiredParams = [
- 'oauth_consumer_key' => $this->_generateRandomString(
- \Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY
- ),
- 'oauth_signature' => '',
- 'oauth_signature_method' => \Magento\Framework\Oauth\OauthInterface::SIGNATURE_SHA1,
- 'oauth_nonce' => '',
- 'oauth_timestamp' => (string)time(),
- 'oauth_token' => $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN),
- 'oauth_verifier' => $this->_oauthVerifier,
- ];
- return array_merge($requiredParams, $amendments);
- }
- /**
- * @param $length
- * @return bool|string
- */
- private function _generateRandomString($length)
- {
- return substr(
- str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 5)),
- 0,
- $length
- );
- }
- }
|