123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Integration\Test\Unit\Model\Oauth;
- use Magento\Integration\Model\Oauth\Consumer\Validator\KeyLengthFactory;
- use Magento\Integration\Model\Oauth\Token;
- use Magento\Framework\Oauth\Helper\Oauth as OauthHelper;
- use Magento\Authorization\Model\UserContextInterface;
- use Magento\Framework\TestFramework\Unit\Matcher\MethodInvokedAtIndex;
- /**
- * Unit test for \Magento\Integration\Model\Oauth\Nonce
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class TokenTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Integration\Model\Oauth\Token
- */
- protected $tokenModel;
- /**
- * @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $contextMock;
- /**
- * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $registryMock;
- /**
- * @var KeyLengthFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $keyLengthFactoryMock;
- /**
- * @var \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLength|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $validatorKeyLengthMock;
- /**
- * @var \Magento\Framework\Url\Validator|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $validatorMock;
- /**
- * @var \Magento\Integration\Model\Oauth\ConsumerFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $consumerFactoryMock;
- /**
- * @var \Magento\Integration\Helper\Oauth\Data|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $oauthDataMock;
- /**
- * @var \Magento\Framework\Oauth\Helper\Oauth|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $oauthHelperMock;
- /**
- * @var \Magento\Framework\Model\ResourceModel\AbstractResource|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resourceMock;
- protected function setUp()
- {
- $this->contextMock = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
- ->setMethods(['getEventDispatcher'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->validatorKeyLengthMock = $this->getMockBuilder(
- \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLength::class
- )
- ->setMethods(['isValid', 'setLength', 'setName', 'getMessages'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->keyLengthFactoryMock = $this->getMockBuilder(
- \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLengthFactory::class
- )
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->validatorMock = $this->getMockBuilder(\Magento\Framework\Url\Validator::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->consumerFactoryMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\ConsumerFactory::class)
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->oauthDataMock = $this->getMockBuilder(\Magento\Integration\Helper\Oauth\Data::class)
- ->setMethods(['isCleanupProbability', 'getCleanupExpirationPeriod'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->oauthHelperMock = $this->getMockBuilder(\Magento\Framework\Oauth\Helper\Oauth::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->resourceMock = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\AbstractResource::class)
- ->setMethods(
- [
- 'getIdFieldName',
- 'deleteOldEntries',
- '_construct',
- 'getConnection',
- 'selectTokenByType',
- 'save',
- 'selectTokenByConsumerIdAndUserType',
- 'selectTokenByAdminId',
- 'selectTokenByCustomerId',
- 'load'
- ]
- )
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->resourceMock->expects($this->any())
- ->method('getIdFieldName')
- ->willReturn('id');
- $eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
- ->setMethods(['dispatch'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->contextMock->expects($this->once())
- ->method('getEventDispatcher')
- ->willReturn($eventManagerMock);
- $this->tokenModel = new \Magento\Integration\Model\Oauth\Token(
- $this->contextMock,
- $this->registryMock,
- $this->keyLengthFactoryMock,
- $this->validatorMock,
- $this->consumerFactoryMock,
- $this->oauthDataMock,
- $this->oauthHelperMock,
- $this->resourceMock
- );
- }
- public function testAfterSave()
- {
- $this->oauthDataMock->expects($this->once())->method('isCleanupProbability')->willReturn(true);
- $this->oauthDataMock->expects($this->once())->method('getCleanupExpirationPeriod')->willReturn(30);
- $this->resourceMock->expects($this->once())->method('deleteOldEntries')->with(30);
- $this->assertEquals($this->tokenModel, $this->tokenModel->afterSave());
- }
- public function testAfterSaveNoCleanupProbability()
- {
- $this->oauthDataMock->expects($this->once())->method('isCleanupProbability')->willReturn(false);
- $this->oauthDataMock->expects($this->never())->method('getCleanupExpirationPeriod');
- $this->resourceMock->expects($this->never())->method('deleteOldEntries');
- $this->assertEquals($this->tokenModel, $this->tokenModel->afterSave());
- }
- public function testCreateVerifierToken()
- {
- $consumerId = 1;
- $this->resourceMock->expects($this->once())
- ->method('selectTokenByType')
- ->with($consumerId, Token::TYPE_VERIFIER)
- ->willReturn(['id' => 123]);
- $this->oauthHelperMock->expects($this->never())->method('generateToken');
- $this->oauthHelperMock->expects($this->never())->method('generateTokenSecret');
- $this->oauthHelperMock->expects($this->never())->method('generateVerifier');
- $this->validatorMock->expects($this->never())->method('isValid');
- $this->keyLengthFactoryMock->expects($this->never())->method('create');
- $this->resourceMock->expects($this->never())->method('save');
- $this->assertEquals($this->tokenModel, $this->tokenModel->createVerifierToken($consumerId));
- }
- public function testCreateVerifierTokenIfNoTokenId()
- {
- $consumerId = 1;
- $secret = 'secret';
- $token = 'token';
- $verifier = 'verifier';
- $this->oauthHelperMock->expects($this->once())->method('generateTokenSecret')->willReturn($secret);
- $this->oauthHelperMock->expects($this->once())->method('generateToken')->willReturn($token);
- $this->oauthHelperMock->expects($this->once())->method('generateVerifier')->willReturn($verifier);
- $this->resourceMock->expects($this->once())
- ->method('selectTokenByType')
- ->with($consumerId, Token::TYPE_VERIFIER)
- ->willReturn([]);
- $this->tokenModel->setCallbackUrl(OauthHelper::CALLBACK_ESTABLISHED);
- $this->keyLengthFactoryMock->expects($this->once())->method('create')->willReturn(
- $this->validatorKeyLengthMock
- );
- $this->validatorKeyLengthMock->expects($this->exactly(3))->method('setLength');
- $this->validatorKeyLengthMock->expects($this->exactly(3))->method('setName');
- $this->validatorKeyLengthMock->expects($this->exactly(3))->method('isValid')->willReturn(true);
- $this->resourceMock->expects($this->once())->method('save');
- $this->assertEquals($this->tokenModel, $this->tokenModel->createVerifierToken($consumerId));
- }
- /**
- * @expectedException \Magento\Framework\Oauth\Exception
- * @expectedExceptionMessage Cannot convert to access token due to token is not request type
- */
- public function testConvertToAccessIfIsNotRequestType()
- {
- $this->tokenModel->setType('isNotRequestType');
- $this->tokenModel->convertToAccess();
- }
- public function testConvertToAccess()
- {
- $token = 'token';
- $secret = 'secret';
- $this->tokenModel->setType(Token::TYPE_REQUEST);
- $this->oauthHelperMock->expects($this->once())->method('generateToken')->willReturn($token);
- $this->oauthHelperMock->expects($this->once())->method('generateTokenSecret')->willReturn($secret);
- $this->resourceMock->expects($this->once())->method('save');
- $result = $this->tokenModel->convertToAccess();
- $this->assertEquals($this->tokenModel, $result);
- $this->assertEquals($token, $result->getToken());
- $this->assertEquals($secret, $result->getSecret());
- $this->assertEquals(UserContextInterface::USER_TYPE_INTEGRATION, $result->getUserType());
- }
- public function testCreateAdminToken()
- {
- $userId = 1;
- $token = 'token';
- $secret = 'secret';
- $this->oauthHelperMock->expects($this->once())->method('generateToken')->willReturn($token);
- $this->oauthHelperMock->expects($this->once())->method('generateTokenSecret')->willReturn($secret);
- $this->resourceMock->expects($this->once())->method('save');
- $result = $this->tokenModel->createAdminToken($userId);
- $this->assertEquals($this->tokenModel, $result);
- $this->assertEquals($token, $result->getToken());
- $this->assertEquals($secret, $result->getSecret());
- $this->assertEquals($userId, $result->getAdminId());
- $this->assertEquals(UserContextInterface::USER_TYPE_ADMIN, $result->getUserType());
- }
- public function testCreateCustomerToken()
- {
- $userId = 1;
- $token = 'token';
- $secret = 'secret';
- $this->oauthHelperMock->expects($this->once())->method('generateToken')->willReturn($token);
- $this->oauthHelperMock->expects($this->once())->method('generateTokenSecret')->willReturn($secret);
- $this->resourceMock->expects($this->once())->method('save');
- $result = $this->tokenModel->createCustomerToken($userId);
- $this->assertEquals($this->tokenModel, $result);
- $this->assertEquals($token, $result->getToken());
- $this->assertEquals($secret, $result->getSecret());
- $this->assertEquals($userId, $result->getCustomerId());
- $this->assertNotEquals($userId, $result->getAdminId());
- $this->assertEquals(UserContextInterface::USER_TYPE_CUSTOMER, $result->getUserType());
- }
- public function testCreateRequestToken()
- {
- $entityId = 1;
- $callbackUrl = OauthHelper::CALLBACK_ESTABLISHED;
- $token = 'token';
- $secret = 'secret';
- $this->oauthHelperMock->expects($this->once())->method('generateTokenSecret')->willReturn($secret);
- $this->oauthHelperMock->expects($this->once())->method('generateToken')->willReturn($token);
- $this->tokenModel->setCallbackUrl($callbackUrl);
- $this->keyLengthFactoryMock->expects($this->once())->method('create')->willReturn(
- $this->validatorKeyLengthMock
- );
- $this->validatorKeyLengthMock->expects($this->exactly(2))->method('setLength');
- $this->validatorKeyLengthMock->expects($this->exactly(2))->method('setName');
- $this->validatorKeyLengthMock->expects($this->exactly(2))->method('isValid')->willReturn(true);
- $this->resourceMock->expects($this->once())->method('save');
- $actualToken = $this->tokenModel->createRequestToken($entityId, $callbackUrl);
- $this->assertEquals($this->tokenModel, $actualToken);
- $this->assertEquals($this->tokenModel->getSecret(), $actualToken->getSecret());
- $this->assertEquals($this->tokenModel->getToken(), $actualToken->getToken());
- }
- public function testToString()
- {
- $token = 'token';
- $secret = 'secret';
- $expectedResponse = "oauth_token={$token}&oauth_token_secret={$secret}";
- $this->tokenModel->setToken($token)->setSecret($secret);
- $this->assertEquals($expectedResponse, sprintf($this->tokenModel));
- }
- public function testBeforeSave()
- {
- $this->assertEquals($this->tokenModel, $this->tokenModel->beforeSave());
- }
- public function testGetVerifier()
- {
- $verifier = 'testVerifier';
- $this->tokenModel->setData('verifier', $verifier);
- $this->assertEquals($verifier, $this->tokenModel->getVerifier());
- }
- public function testLoadByConsumerIdAndUserType()
- {
- $consumerId = 1;
- $userType = 1;
- $tokenData = 'testToken';
- $data = ['token' => $tokenData];
- $this->resourceMock->expects($this->once())->method('selectTokenByConsumerIdAndUserType')->willReturn($data);
- $actualToken = $this->tokenModel->loadByConsumerIdAndUserType($consumerId, $userType);
- $this->assertEquals($this->tokenModel, $actualToken);
- $this->assertEquals($tokenData, $actualToken->getToken());
- }
- public function testLoadByAdminId()
- {
- $adminId = 1;
- $tokenData = 'testToken';
- $data = ['token' => $tokenData];
- $this->resourceMock->expects($this->once())->method('selectTokenByAdminId')->willReturn($data);
- $actualToken = $this->tokenModel->loadByAdminId($adminId);
- $this->assertEquals($this->tokenModel, $actualToken);
- $this->assertEquals($tokenData, $actualToken->getToken());
- }
- public function testLoadByCustomerId()
- {
- $customerId = 1;
- $tokenData = 'testToken';
- $data = ['token' => $tokenData];
- $this->resourceMock->expects($this->once())->method('selectTokenByCustomerId')->willReturn($data);
- $actualToken = $this->tokenModel->loadByCustomerId($customerId);
- $this->assertEquals($this->tokenModel, $actualToken);
- $this->assertEquals($tokenData, $actualToken->getToken());
- }
- public function testLoad()
- {
- $token = 'testToken';
- $this->resourceMock->expects($this->once())->method('load');
- $actualToken = $this->tokenModel->loadByToken($token);
- $this->assertEquals($this->tokenModel, $actualToken);
- }
- public function testValidateIfNotCallbackEstablishedAndNotValid()
- {
- $exceptionMessage = 'exceptionMessage';
- $this->tokenModel->setCallbackUrl('notCallbackEstablished');
- $this->validatorMock->expects($this->once())->method('isValid')->willReturn(false);
- $this->validatorMock->expects($this->once())->method('getMessages')->willReturn([$exceptionMessage]);
- $this->expectException(\Magento\Framework\Oauth\Exception::class);
- $this->expectExceptionMessage($exceptionMessage);
- $this->tokenModel->validate();
- }
- public function testValidateIfSecretNotValid()
- {
- $exceptionMessage = 'exceptionMessage';
- $this->tokenModel->setCallbackUrl('notCallbackEstablished');
- $this->validatorMock->expects($this->once())->method('isValid')->willReturn(true);
- $this->keyLengthFactoryMock->expects($this->once())->method('create')->willReturn(
- $this->validatorKeyLengthMock
- );
- $this->validatorKeyLengthMock->expects($this->once())->method('isValid')->willReturn(false);
- $this->validatorKeyLengthMock->expects($this->once())->method('getMessages')->willReturn([$exceptionMessage]);
- $this->expectException(\Magento\Framework\Oauth\Exception::class);
- $this->expectExceptionMessage($exceptionMessage);
- $this->tokenModel->validate();
- }
- public function testValidateIfTokenNotValid()
- {
- $exceptionMessage = 'exceptionMessage';
- $token = 'token';
- $secret = 'secret';
- $this->tokenModel->setCallbackUrl('notCallbackEstablished');
- $this->validatorMock->expects($this->once())->method('isValid')->willReturn(true);
- $this->keyLengthFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->validatorKeyLengthMock);
- $this->tokenModel->setSecret($secret);
- $this->tokenModel->setToken($token);
- $this->validatorKeyLengthMock->expects($this->exactly(2))->method('isValid')->willReturnMap(
- [
- [$secret, true],
- [$token, false]
- ]
- );
- $this->validatorKeyLengthMock->expects($this->once())->method('getMessages')->willReturn([$exceptionMessage]);
- $this->expectException(\Magento\Framework\Oauth\Exception::class);
- $this->expectExceptionMessage($exceptionMessage);
- $this->tokenModel->validate();
- }
- public function testValidateIfVerifierNotValid()
- {
- $exceptionMessage = 'exceptionMessage';
- $secret = 'secret';
- $token = 'token';
- $verifier = 'isSetAndNotValid';
- $this->tokenModel->setCallbackUrl('notCallbackEstablished');
- $this->validatorMock->expects($this->once())->method('isValid')->willReturn(true);
- $this->keyLengthFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->validatorKeyLengthMock);
- $this->tokenModel->setSecret($secret);
- $this->tokenModel->setToken($token);
- $this->tokenModel->setData('verifier', $verifier);
- $this->validatorKeyLengthMock->expects($this->exactly(3))->method('isValid')->willReturnMap(
- [
- [$secret, true],
- [$token, true],
- [$verifier, false],
- ]
- );
- $this->validatorKeyLengthMock->expects($this->once())->method('getMessages')->willReturn([$exceptionMessage]);
- $this->expectException(\Magento\Framework\Oauth\Exception::class);
- $this->expectExceptionMessage($exceptionMessage);
- $this->tokenModel->validate();
- }
- public function testValidateIfVerifierIsNotSet()
- {
- $token = 'token';
- $secret = 'secret';
- $verifier = null;
- $this->tokenModel->setCallbackUrl('notCallbackEstablished');
- $this->validatorMock->expects($this->once())->method('isValid')->willReturn(true);
- $this->keyLengthFactoryMock->expects($this->once())->method('create')->willReturn(
- $this->validatorKeyLengthMock
- );
- $this->tokenModel->setSecret($secret);
- $this->tokenModel->setToken($token);
- $this->tokenModel->setData('verifier', $verifier);
- $this->validatorKeyLengthMock->expects($this->exactly(2))->method('isValid')->willReturnMap(
- [
- [$secret, true],
- [$token, true],
- ]
- );
- $this->assertTrue($this->tokenModel->validate());
- }
- public function testValidate()
- {
- $token = 'token';
- $secret = 'secret';
- $verifier = 'verifier';
- $this->tokenModel->setCallbackUrl('notCallbackEstablished');
- $this->validatorMock->expects($this->once())->method('isValid')->willReturn(true);
- $this->keyLengthFactoryMock->expects($this->once())->method('create')->willReturn(
- $this->validatorKeyLengthMock
- );
- $this->tokenModel->setSecret($secret);
- $this->tokenModel->setToken($token);
- $this->tokenModel->setData('verifier', $verifier);
- $this->validatorKeyLengthMock->expects($this->exactly(3))->method('isValid')->willReturnMap(
- [
- [$secret, true],
- [$token, true],
- [$verifier, true],
- ]
- );
- $this->assertTrue($this->tokenModel->validate());
- }
- }
|