15 private $_consumerFactory;
18 private $_nonceFactory;
21 private $_tokenFactory;
24 private $_consumerMock;
30 private $_oauthHelperMock;
36 private $_httpUtilityMock;
48 private $_oauthSecret;
50 private $_oauthVerifier;
58 $this->_consumerFactory = $this->getMockBuilder(\
Magento\Integration\Model\Oauth\ConsumerFactory::class)
59 ->disableOriginalConstructor()
60 ->setMethods([
'create'])
62 $this->_consumerMock = $this->getMockBuilder(\
Magento\Integration\Model\Oauth\Consumer::class)
63 ->disableOriginalConstructor()->setMethods(
73 'isValidForTokenExchange',
78 $this->_consumerFactory->expects($this->any())
80 ->will($this->returnValue($this->_consumerMock));
81 $this->_nonceFactory = $this->getMockBuilder(\
Magento\Integration\Model\Oauth\NonceFactory::class)
82 ->disableOriginalConstructor()
83 ->setMethods([
'create'])
85 $this->_tokenFactory = $this->getMockBuilder(
86 \
Magento\Integration\Model\Oauth\TokenFactory::class
87 )->disableOriginalConstructor()->setMethods([
'create'])->getMock();
88 $this->_tokenMock = $this->getMockBuilder(\
Magento\Integration\Model\Oauth\Token::class)
89 ->disableOriginalConstructor()
98 'createVerifierToken',
104 'loadByConsumerIdAndUserType',
109 $this->_tokenFactory->expects($this->any())->method(
'create')->will($this->returnValue($this->_tokenMock));
110 $this->_oauthHelperMock = $this->getMockBuilder(\
Magento\Framework\Oauth\Helper\Oauth::class)
111 ->setConstructorArgs([
new \
Magento\Framework\Math\Random()])
113 $this->_httpUtilityMock = $this->getMockBuilder(\Zend_Oauth_Http_Utility::class)
114 ->setMethods([
'sign'])
116 $this->_dateMock = $this->getMockBuilder(\
Magento\Framework\Stdlib\DateTime\DateTime::class)
117 ->disableOriginalConstructor()
119 $this->_loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
120 ->disableOriginalConstructor()
123 $nonceGenerator = new \Magento\Integration\Model\Oauth\Nonce\Generator(
124 $this->_oauthHelperMock,
125 $this->_nonceFactory,
128 $tokenProvider = new \Magento\Integration\Model\Oauth\Token\Provider(
129 $this->_consumerFactory,
130 $this->_tokenFactory,
133 $this->_oauth = new \Magento\Framework\Oauth\Oauth(
134 $this->_oauthHelperMock,
137 $this->_httpUtilityMock
139 $this->_oauthToken = $this->_generateRandomString(\
Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN);
140 $this->_oauthSecret = $this->_generateRandomString(\
Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_SECRET);
141 $this->_oauthVerifier = $this->_generateRandomString(
142 \
Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_VERIFIER
148 unset($this->_consumerFactory);
149 unset($this->_nonceFactory);
150 unset($this->_tokenFactory);
151 unset($this->_oauthHelperMock);
152 unset($this->_httpUtilityMock);
153 unset($this->_dateMock);
154 unset($this->_oauth);
164 'oauth_version' =>
'1.0',
165 'oauth_consumer_key' => $this->_generateRandomString(
166 \
Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY
169 'oauth_timestamp' =>
time(),
171 'oauth_signature' =>
'invalid_signature',
174 return array_merge($requiredParams, $amendments);
184 $this->_oauth->getRequestToken(
197 $this->_oauth->getRequestToken(
210 $this->_consumerMock->expects(
215 $this->returnValue(
new \
Magento\Framework\DataObject())
231 ->expects($this->any())
232 ->method(
'isValidForTokenExchange')
233 ->will($this->returnValue(
false));
243 $this->_consumerMock->expects($this->any())->method(
'loadByKey')->will($this->returnSelf());
245 $this->_consumerMock->expects(
250 $this->returnValue(date(
'c', strtotime(
'-1 day')))
254 $this->_consumerMock->expects($this->any())->method(
'load')->will($this->returnSelf());
256 $this->_consumerMock->expects(
261 $this->returnValue(
new \
Magento\Framework\DataObject())
265 $this->_consumerMock->expects($this->any())->method(
'getId')->will($this->returnValue(1));
266 $this->_consumerMock->expects($this->any())->method(
'getSecret')->will($this->returnValue(
'consumer_secret'));
267 $this->_consumerMock->expects(
272 $this->returnValue(
'callback_url')
279 ->expects($this->any())
280 ->method(
'isValidForTokenExchange')
281 ->will($this->returnValue(
true));
295 $this->_oauth->getRequestToken(
309 [
time() + \Magento\Integration\Model\Oauth\Nonce\Generator::TIME_DEVIATION + 86400]
319 $nonceMock = $this->getMockBuilder(
320 \
Magento\Integration\Model\Oauth\Nonce::class
321 )->disableOriginalConstructor()->setMethods(
323 'loadByCompositeKey',
334 $nonceMock->expects($this->any())->method(
'getNonce')->will($this->returnValue($isUsed));
335 $nonceMock->expects($this->any())->method(
'loadByCompositeKey')->will($this->returnSelf());
336 $nonceMock->expects($this->any())->method(
'getTimestamp')->will($this->returnValue($timestamp));
337 $nonceMock->expects($this->any())->method(
'setNonce')->will($this->returnSelf());
338 $nonceMock->expects($this->any())->method(
'setConsumerId')->will($this->returnSelf());
339 $nonceMock->expects($this->any())->method(
'setTimestamp')->will($this->returnSelf());
340 $nonceMock->expects($this->any())->method(
'save')->will($this->returnSelf());
341 $this->_nonceFactory->expects($this->any())->method(
'create')->will($this->returnValue($nonceMock));
365 $this->_consumerMock->expects(
370 $this->returnValue(
new \
Magento\Framework\DataObject())
385 $type = \
Magento\Integration\Model\Oauth\Token::TYPE_VERIFIER,
386 $consumerId = self::CONSUMER_ID,
390 $this->_tokenMock->expects(
395 $this->returnValue($doesExist ? self::CONSUMER_ID :
null)
398 $verifier = $verifier ?: $this->_oauthVerifier;
400 $this->_tokenMock->expects($this->any())->method(
'load')->will($this->returnSelf());
401 $this->_tokenMock->expects($this->any())->method(
'getType')->will($this->returnValue(
$type));
402 $this->_tokenMock->expects($this->any())->method(
'createRequestToken')->will($this->returnSelf());
403 $this->_tokenMock->expects($this->any())->method(
'getToken')->will($this->returnValue($this->_oauthToken));
404 $this->_tokenMock->expects($this->any())->method(
'getSecret')->will($this->returnValue($this->_oauthSecret));
405 $this->_tokenMock->expects($this->any())->method(
'getConsumerId')->will($this->returnValue($consumerId));
406 $this->_tokenMock->expects($this->any())->method(
'getVerifier')->will($this->returnValue($verifier));
407 $this->_tokenMock->expects($this->any())->method(
'convertToAccess')->will($this->returnSelf());
408 $this->_tokenMock->expects($this->any())->method(
'getRevoked')->will($this->returnValue($isRevoked));
409 $this->_tokenMock->expects($this->any())->method(
'loadByConsumerIdAndUserType')->will($this->returnSelf());
424 $signature =
'valid_signature';
425 $this->_httpUtilityMock->expects($this->any())->method(
'sign')->will($this->returnValue($signature));
427 $this->_oauth->getRequestToken(
446 $signature =
'valid_signature';
447 $this->_httpUtilityMock->expects($this->any())->method(
'sign')->will($this->returnValue($signature));
449 $this->_oauth->getRequestToken(
467 $this->_oauth->getRequestToken(
485 $this->_oauth->getRequestToken(
498 $signature =
'valid_signature';
499 $this->_httpUtilityMock->expects($this->any())->method(
'sign')->will($this->returnValue($signature));
501 $requestToken = $this->_oauth->getRequestToken(
507 [
'oauth_token' => $this->_oauthToken,
'oauth_token_secret' => $this->_oauthSecret],
519 $this->_oauth->getAccessToken(
533 $this->_oauth->getAccessToken(
535 'oauth_version' =>
'1.0',
536 'oauth_consumer_key' =>
'',
537 'oauth_signature' =>
'',
538 'oauth_signature_method' =>
'',
540 'oauth_timestamp' =>
'',
555 $this->_oauth->getAccessToken(
568 $this->_oauth->getAccessToken(
615 \
Magento\Integration\Model\Oauth\Token::TYPE_REQUEST,
620 $this->_oauth->getAccessToken(
632 return [[3, 3], [
'wrong_length',
'wrong_length'], [
'verifier',
'doesn\'t match']];
643 [
'oauth_token' => $this->_oauthToken,
'oauth_token_secret' => $this->_oauthSecret],
687 \
Magento\Integration\Model\Oauth\Token::TYPE_ACCESS,
689 $this->_oauthVerifier,
704 $this->_oauth->validateAccessTokenRequest($requiredParams, self::REQUEST_URL),
705 "Consumer ID is invalid." 719 $this->_oauth->validateAccessToken($this->_oauthToken);
732 \
Magento\Integration\Model\Oauth\Token::TYPE_ACCESS,
734 $this->_oauthVerifier,
738 $this->_oauth->validateAccessToken($this->_oauthToken);
751 $this->_oauth->validateAccessToken($this->_oauthToken);
759 $this->assertEquals(1, $this->_oauth->validateAccessToken($this->_oauthToken),
"Consumer ID is invalid.");
764 $signature =
'valid_signature';
765 $this->_httpUtilityMock->expects($this->any())->method(
'sign')->will($this->returnValue($signature));
768 $this->_oauthHelperMock->expects(
771 'generateRandomString' 773 $this->returnValue(
'tyukmnjhgfdcvxstyuioplkmnhtfvert')
777 'oauth_consumer_key' =>
'edf957ef88492f0a32eb7e1731e85da2',
778 'oauth_consumer_secret' =>
'asdawwewefrtyh2f0a32eb7e1731e85d',
779 'oauth_token' =>
'7c0709f789e1f38a17aa4b9a28e1b06c',
780 'oauth_token_secret' =>
'a6agsfrsfgsrjjjjyy487939244ssggg',
781 'custom_param1' =>
'foo',
782 'custom_param2' =>
'bar',
785 $requestUrl =
'http://www.example.com/endpoint';
786 $oauthHeader = $this->_oauth->buildAuthorizationHeader(
$request, $requestUrl);
788 $expectedHeader =
'OAuth oauth_nonce="tyukmnjhgfdcvxstyuioplkmnhtfvert",' .
789 'oauth_timestamp="",' .
790 'oauth_version="1.0",oauth_consumer_key="edf957ef88492f0a32eb7e1731e85da2",' .
791 'oauth_consumer_secret="asdawwewefrtyh2f0a32eb7e1731e85d",' .
792 'oauth_token="7c0709f789e1f38a17aa4b9a28e1b06c",' .
793 'oauth_token_secret="a6agsfrsfgsrjjjjyy487939244ssggg",' .
794 'oauth_signature="valid_signature"';
796 $this->assertEquals($expectedHeader, $oauthHeader,
'Generated Oauth header is incorrect');
804 $this->expectException(\
Magento\Framework\Oauth\OauthInputException::class);
805 $this->expectExceptionMessage($expectedMessage);
806 $this->expectExceptionCode(0);
808 $requestUrl =
'http://www.example.com/endpoint';
809 $this->_oauth->buildAuthorizationHeader(
$request, $requestUrl);
819 'oauth_consumer_key',
821 'oauth_consumer_secret' =>
'asdawwewefrtyh2f0a32eb7e1731e85d',
822 'oauth_token' =>
'7c0709f789e1f38a17aa4b9a28e1b06c',
823 'oauth_token_secret' =>
'a6agsfrsfgsrjjjjyy487939244ssggg',
824 'custom_param1' =>
'foo',
825 'custom_param2' =>
'bar' 829 'oauth_consumer_secret',
831 'oauth_consumer_key' =>
'edf957ef88492f0a32eb7e1731e85d',
833 'oauth_token' =>
'7c0709f789e1f38a17aa4b9a28e1b06c',
834 'oauth_token_secret' =>
'a6agsfrsfgsrjjjjyy487939244ssggg',
835 'custom_param1' =>
'foo',
836 'custom_param2' =>
'bar' 842 'oauth_consumer_key' =>
'edf957ef88492f0a32eb7e1731e85d',
843 'oauth_consumer_secret' =>
'asdawwewefrtyh2f0a32eb7e1731e85d',
845 'oauth_token_secret' =>
'a6agsfrsfgsrjjjjyy487939244ssggg',
846 'custom_param1' =>
'foo',
847 'custom_param2' =>
'bar' 851 'oauth_token_secret',
853 'oauth_consumer_key' =>
'edf957ef88492f0a32eb7e1731e85d',
854 'oauth_consumer_secret' =>
'asdawwewefrtyh2f0a32eb7e1731e85d',
855 'oauth_token' =>
'7c0709f789e1f38a17aa4b9a28e1b06c',
857 'custom_param1' =>
'foo',
858 'custom_param2' =>
'bar' 871 'oauth_consumer_key' => $this->_generateRandomString(
872 \
Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY
874 'oauth_signature' =>
'',
877 'oauth_timestamp' => (string)
time(),
878 'oauth_token' => $this->_generateRandomString(\
Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN),
879 'oauth_verifier' => $this->_oauthVerifier,
882 return array_merge($requiredParams, $amendments);
889 private function _generateRandomString($length)
892 str_shuffle(str_repeat(
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 5)),
testMissingParamForBuildAuthorizationHeader($expectedMessage, $request)
_getAccessTokenRequiredParams($amendments=[])
testGetAccessTokenVerifierInvalid($verifier, $verifierFromToken)
_setupNonce($isUsed=false, $timestamp=0)
_getRequestTokenParams($amendments=[])
testValidateAccessTokenRequest()
testGetAccessTokenTokenRejected()
testValidateAccessToken()
testGetRequestTokenVersionRejected()
dataProviderForGetRequestTokenNonceTimestampRefusedTest()
testValidateAccessTokenRequestTokenRevoked()
testValidateAccessTokenRequestTokenRejectedByType()
testGetAccessTokenConsumerIdDoesntMatch()
_makeValidExpirationPeriod()
testValidateAccessTokenNoConsumer()
testGetAccessTokenParameterAbsent()
testGetAccessTokenTokenUsed()
testGetRequestTokenNonceAlreadyUsed()
testGetRequestTokenTokenRejectedByType()
_setupConsumer($isLoadable=true)
testGetRequestTokenSignatureMethodRejected()
_setupToken( $doesExist=true, $type=\Magento\Integration\Model\Oauth\Token::TYPE_VERIFIER, $consumerId=self::CONSUMER_ID, $verifier=null, $isRevoked=false)
testGetRequestTokenTokenRejected()
testGetAccessTokenSignatureMethodRejected()
dataProviderForGetAccessTokenVerifierInvalidTest()
testValidateAccessTokenRequestTokenRejected()
testGetRequestTokenOauthTimestampRefused($timestamp)
testGetAccessTokenVersionRejected()
testGetRequestTokenOutdatedConsumerKey()
testGetRequestTokenConsumerKeyRejected()
testGetRequestTokenInvalidSignature()
testValidateAccessTokenRejectedByType()
testBuildAuthorizationHeader()
testGetRequestTokenConsumerKeyNotFound()
dataProviderMissingParamForBuildAuthorizationHeaderTest()
testValidateAccessTokenRevoked()
testGetRequestTokenNoConsumer()