ltsecurityinc安防产品在线商城代码
This commit is contained in:
+379
@@ -0,0 +1,379 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Integration\Model;
|
||||
|
||||
use Magento\Framework\Exception\InputException;
|
||||
use Magento\Integration\Model\Oauth\Token as TokenModel;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
use Magento\User\Model\User as UserModel;
|
||||
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
|
||||
use Magento\Integration\Model\Oauth\Token\RequestLog\Config as TokenThrottlerConfig;
|
||||
|
||||
/**
|
||||
* api-functional test for \Magento\Integration\Model\AdminTokenService.
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class AdminTokenServiceTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_NAME = "integrationAdminTokenServiceV1";
|
||||
const SERVICE_VERSION = "V1";
|
||||
const RESOURCE_PATH_ADMIN_TOKEN = "/V1/integration/admin/token";
|
||||
|
||||
/**
|
||||
* @var \Magento\Integration\Api\AdminTokenServiceInterface
|
||||
*/
|
||||
private $tokenService;
|
||||
|
||||
/**
|
||||
* @var TokenModel
|
||||
*/
|
||||
private $tokenModel;
|
||||
|
||||
/**
|
||||
* @var UserModel
|
||||
*/
|
||||
private $userModel;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $attemptsCountToLockAccount;
|
||||
|
||||
/**
|
||||
* Setup AdminTokenService
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->markTestSkipped('Skipped until MC-34201 is addressed');
|
||||
$this->_markTestAsRestOnly();
|
||||
$this->tokenService = Bootstrap::getObjectManager()->get(\Magento\Integration\Model\AdminTokenService::class);
|
||||
$this->tokenModel = Bootstrap::getObjectManager()->get(\Magento\Integration\Model\Oauth\Token::class);
|
||||
$this->userModel = Bootstrap::getObjectManager()->get(\Magento\User\Model\User::class);
|
||||
/** @var TokenThrottlerConfig $tokenThrottlerConfig */
|
||||
$tokenThrottlerConfig = Bootstrap::getObjectManager()->get(TokenThrottlerConfig::class);
|
||||
$this->attemptsCountToLockAccount = $tokenThrottlerConfig->getMaxFailuresCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Webapi/_files/webapi_user.php
|
||||
*/
|
||||
public function testCreateAdminAccessToken()
|
||||
{
|
||||
$adminUserNameFromFixture = 'webapi_user';
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH_ADMIN_TOKEN,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
|
||||
],
|
||||
];
|
||||
$requestData = [
|
||||
'username' => $adminUserNameFromFixture,
|
||||
'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
|
||||
];
|
||||
$accessToken = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertToken($adminUserNameFromFixture, $accessToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider to test input validation
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function validationDataProvider()
|
||||
{
|
||||
return [
|
||||
'Check for empty credentials' => ['', ''],
|
||||
'Check for null credentials' => [null, null]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validationDataProvider
|
||||
*/
|
||||
public function testCreateAdminAccessTokenEmptyOrNullCredentials()
|
||||
{
|
||||
$noExceptionOccurred = false;
|
||||
try {
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH_ADMIN_TOKEN,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
|
||||
],
|
||||
];
|
||||
$requestData = ['username' => '', 'password' => ''];
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
$noExceptionOccurred = true;
|
||||
} catch (\Exception $exception) {
|
||||
$this->assertInputExceptionMessages($exception);
|
||||
}
|
||||
if ($noExceptionOccurred) {
|
||||
$this->fail("Exception was expected to be thrown when provided credentials are invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
public function testCreateAdminAccessTokenInvalidCredentials()
|
||||
{
|
||||
$customerUserName = 'invalid';
|
||||
$password = 'invalid';
|
||||
$noExceptionOccurred = false;
|
||||
try {
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH_ADMIN_TOKEN,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
|
||||
],
|
||||
];
|
||||
$requestData = ['username' => $customerUserName, 'password' => $password];
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
$noExceptionOccurred = true;
|
||||
} catch (\Exception $exception) {
|
||||
$this->assertInvalidCredentialsException($exception);
|
||||
}
|
||||
if ($noExceptionOccurred) {
|
||||
$this->fail("Exception was expected to be thrown when provided credentials are invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Webapi/_files/webapi_user.php
|
||||
*/
|
||||
public function testUseAdminAccessTokenInactiveAdmin()
|
||||
{
|
||||
$adminUserNameFromFixture = 'webapi_user';
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH_ADMIN_TOKEN,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
|
||||
],
|
||||
];
|
||||
$requestData = [
|
||||
'username' => $adminUserNameFromFixture,
|
||||
'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
|
||||
];
|
||||
$accessToken = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertToken($adminUserNameFromFixture, $accessToken);
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/store/storeConfigs',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
'token' => $accessToken
|
||||
]
|
||||
];
|
||||
$requestData = [
|
||||
'storeCodes' => ['default'],
|
||||
];
|
||||
$storeConfigs = $this->_webApiCall($serviceInfo, $requestData);
|
||||
$this->assertNotNull($storeConfigs);
|
||||
|
||||
$adminUser = $this->userModel->loadByUsername($adminUserNameFromFixture);
|
||||
$adminUser->setData("is_active", 0);
|
||||
$adminUser->save();
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/store/storeConfigs',
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
|
||||
'token' => $accessToken
|
||||
]
|
||||
];
|
||||
$requestData = [
|
||||
'storeCodes' => ['default'],
|
||||
];
|
||||
|
||||
$noExceptionOccurred = false;
|
||||
try {
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
$noExceptionOccurred = true;
|
||||
} catch (\Exception $exception) {
|
||||
$this->assertUnauthorizedAccessException($exception);
|
||||
}
|
||||
if ($noExceptionOccurred) {
|
||||
$this->fail("Exception was expected to be thrown when provided token is expired.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Webapi/_files/webapi_user.php
|
||||
*/
|
||||
public function testThrottlingMaxAttempts()
|
||||
{
|
||||
$adminUserNameFromFixture = 'webapi_user';
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH_ADMIN_TOKEN,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
|
||||
],
|
||||
];
|
||||
$invalidCredentials = [
|
||||
'username' => $adminUserNameFromFixture,
|
||||
'password' => 'invalid',
|
||||
];
|
||||
$validCredentials = [
|
||||
'username' => $adminUserNameFromFixture,
|
||||
'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
|
||||
];
|
||||
|
||||
/* Try to get token using invalid credentials for 5 times (account is locked after 6 attempts) */
|
||||
$noExceptionOccurred = false;
|
||||
for ($i = 0; $i < ($this->attemptsCountToLockAccount - 1); $i++) {
|
||||
try {
|
||||
$this->_webApiCall($serviceInfo, $invalidCredentials);
|
||||
$noExceptionOccurred = true;
|
||||
} catch (\Exception $exception) {
|
||||
}
|
||||
}
|
||||
if ($noExceptionOccurred) {
|
||||
$this->fail(
|
||||
"Precondition failed: exception should have occurred when token was requested with invalid credentials."
|
||||
);
|
||||
}
|
||||
|
||||
/** On 6th attempt it still should be possible to get token if valid credentials are specified */
|
||||
$accessToken = $this->_webApiCall($serviceInfo, $validCredentials);
|
||||
$this->assertToken($adminUserNameFromFixture, $accessToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Webapi/_files/webapi_user.php
|
||||
*/
|
||||
public function testThrottlingAccountLockout()
|
||||
{
|
||||
$adminUserNameFromFixture = 'webapi_user';
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH_ADMIN_TOKEN,
|
||||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
|
||||
],
|
||||
];
|
||||
$invalidCredentials = [
|
||||
'username' => $adminUserNameFromFixture,
|
||||
'password' => 'invalid',
|
||||
];
|
||||
$validCredentials = [
|
||||
'username' => $adminUserNameFromFixture,
|
||||
'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
|
||||
];
|
||||
|
||||
/* Try to get token using invalid credentials for 5 times (account would be locked after 6 attempts) */
|
||||
$noExceptionOccurred = false;
|
||||
for ($i = 0; $i < $this->attemptsCountToLockAccount; $i++) {
|
||||
try {
|
||||
$this->_webApiCall($serviceInfo, $invalidCredentials);
|
||||
$noExceptionOccurred = true;
|
||||
} catch (\Exception $exception) {
|
||||
$this->assertInvalidCredentialsException($exception);
|
||||
}
|
||||
if ($noExceptionOccurred) {
|
||||
$this->fail("Exception was expected to be thrown when provided credentials are invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
$noExceptionOccurred = false;
|
||||
try {
|
||||
$this->_webApiCall($serviceInfo, $validCredentials);
|
||||
$noExceptionOccurred = true;
|
||||
} catch (\Exception $exception) {
|
||||
$this->assertInvalidCredentialsException($exception);
|
||||
}
|
||||
if ($noExceptionOccurred) {
|
||||
$this->fail("Exception was expected to be thrown because account should have been locked at this point.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert for presence of Input exception messages
|
||||
*
|
||||
* @param \Exception $exception
|
||||
*/
|
||||
private function assertInputExceptionMessages($exception)
|
||||
{
|
||||
$this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $exception->getCode());
|
||||
$exceptionData = $this->processRestExceptionResult($exception);
|
||||
$expectedExceptionData = [
|
||||
'message' => 'One or more input exceptions have occurred.',
|
||||
'errors' => [
|
||||
[
|
||||
'message' => '"%fieldName" is required. Enter and try again.',
|
||||
'parameters' => [
|
||||
'fieldName' => 'username',
|
||||
],
|
||||
],
|
||||
[
|
||||
'message' => '"%fieldName" is required. Enter and try again.',
|
||||
'parameters' => [
|
||||
'fieldName' => 'password',
|
||||
]
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->assertEquals($expectedExceptionData, $exceptionData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that status code and message are correct in case of authentication failure.
|
||||
*
|
||||
* @param \Exception $exception
|
||||
*/
|
||||
private function assertInvalidCredentialsException($exception)
|
||||
{
|
||||
$this->assertEquals(
|
||||
HTTPExceptionCodes::HTTP_UNAUTHORIZED,
|
||||
$exception->getCode(),
|
||||
"Response HTTP code is invalid."
|
||||
);
|
||||
$exceptionData = $this->processRestExceptionResult($exception);
|
||||
$expectedExceptionData = [
|
||||
'message' => 'The account sign-in was incorrect or your account is disabled temporarily. '
|
||||
. 'Please wait and try again later.'
|
||||
];
|
||||
$this->assertEquals($expectedExceptionData, $exceptionData, "Exception message is invalid.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that status code and message are correct in case of authentication failure.
|
||||
*
|
||||
* @param \Exception $exception
|
||||
*/
|
||||
private function assertUnauthorizedAccessException($exception)
|
||||
{
|
||||
$this->assertEquals(
|
||||
HTTPExceptionCodes::HTTP_UNAUTHORIZED,
|
||||
$exception->getCode(),
|
||||
"Response HTTP code is invalid."
|
||||
);
|
||||
$exceptionData = $this->processRestExceptionResult($exception);
|
||||
$expectedExceptionData = [
|
||||
'message' => "The consumer isn't authorized to access %resources.",
|
||||
'parameters' => [
|
||||
'resources' => 'Magento_Backend::store'
|
||||
]
|
||||
];
|
||||
$this->assertEquals($expectedExceptionData, $exceptionData, "Exception message is invalid.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure provided token is valid and belongs to the specified user.
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $accessToken
|
||||
*/
|
||||
private function assertToken($username, $accessToken)
|
||||
{
|
||||
$adminUserId = $this->userModel->loadByUsername($username)->getId();
|
||||
/** @var $token TokenModel */
|
||||
$token = $this->tokenModel
|
||||
->loadByAdminId($adminUserId)
|
||||
->getToken();
|
||||
$this->assertEquals($accessToken, $token);
|
||||
}
|
||||
}
|
||||
+336
@@ -0,0 +1,336 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Integration\Model;
|
||||
|
||||
use Magento\Authorization\Model\UserContextInterface;
|
||||
use Magento\Customer\Api\AccountManagementInterface;
|
||||
use Magento\Framework\Webapi\Rest\Request;
|
||||
use Magento\Integration\Api\UserTokenReaderInterface;
|
||||
use Magento\Integration\Model\Oauth\Token as TokenModel;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\WebapiAbstract;
|
||||
use Magento\User\Model\User as UserModel;
|
||||
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
|
||||
use Magento\Integration\Model\ResourceModel\Oauth\Token\CollectionFactory;
|
||||
use Magento\Integration\Model\Oauth\Token\RequestLog\Config as TokenThrottlerConfig;
|
||||
use Magento\Integration\Api\CustomerTokenServiceInterface;
|
||||
|
||||
/**
|
||||
* api-functional test for \Magento\Integration\Model\CustomerTokenService.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class CustomerTokenServiceTest extends WebapiAbstract
|
||||
{
|
||||
const SERVICE_NAME = "integrationCustomerTokenServiceV1";
|
||||
const SERVICE_VERSION = "V1";
|
||||
const RESOURCE_PATH_CUSTOMER_TOKEN = "/V1/integration/customer/token";
|
||||
|
||||
/**
|
||||
* @var CustomerTokenServiceInterface
|
||||
*/
|
||||
private $tokenService;
|
||||
|
||||
/**
|
||||
* @var AccountManagementInterface
|
||||
*/
|
||||
private $customerAccountManagement;
|
||||
|
||||
/**
|
||||
* @var CollectionFactory
|
||||
*/
|
||||
private $tokenCollection;
|
||||
|
||||
/**
|
||||
* @var UserModel
|
||||
*/
|
||||
private $userModel;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $attemptsCountToLockAccount;
|
||||
|
||||
/**
|
||||
* @var UserTokenReaderInterface
|
||||
*/
|
||||
private $tokenReader;
|
||||
|
||||
/**
|
||||
* Setup CustomerTokenService
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
$this->tokenService = Bootstrap::getObjectManager()->get(
|
||||
\Magento\Integration\Model\CustomerTokenService::class
|
||||
);
|
||||
$this->customerAccountManagement = Bootstrap::getObjectManager()->get(
|
||||
\Magento\Customer\Api\AccountManagementInterface::class
|
||||
);
|
||||
$tokenCollectionFactory = Bootstrap::getObjectManager()->get(
|
||||
\Magento\Integration\Model\ResourceModel\Oauth\Token\CollectionFactory::class
|
||||
);
|
||||
$this->tokenCollection = $tokenCollectionFactory->create();
|
||||
$this->userModel = Bootstrap::getObjectManager()->get(\Magento\User\Model\User::class);
|
||||
/** @var TokenThrottlerConfig $tokenThrottlerConfig */
|
||||
$tokenThrottlerConfig = Bootstrap::getObjectManager()->get(TokenThrottlerConfig::class);
|
||||
$this->attemptsCountToLockAccount = $tokenThrottlerConfig->getMaxFailuresCount();
|
||||
$this->tokenReader = Bootstrap::getObjectManager()->get(UserTokenReaderInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create customer access token
|
||||
*
|
||||
* @dataProvider storesDataProvider
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php
|
||||
*
|
||||
* @param string|null $store
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateCustomerAccessToken(?string $store): void
|
||||
{
|
||||
$userName = 'customer@example.com';
|
||||
$password = 'password';
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH_CUSTOMER_TOKEN,
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
],
|
||||
];
|
||||
$requestData = ['username' => $userName, 'password' => $password];
|
||||
$accessToken = $this->_webApiCall($serviceInfo, $requestData, null, $store);
|
||||
|
||||
$this->assertToken($accessToken, $userName, $password);
|
||||
}
|
||||
|
||||
/**
|
||||
* DataProvider for testCreateCustomerAccessToken
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function storesDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'default store' => [null],
|
||||
'all store view' => ['all'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider validationDataProvider
|
||||
*/
|
||||
public function testCreateCustomerAccessTokenEmptyOrNullCredentials($username, $password)
|
||||
{
|
||||
$noExceptionOccurred = false;
|
||||
try {
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH_CUSTOMER_TOKEN,
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
],
|
||||
];
|
||||
$requestData = ['username' => $username, 'password' => $password];
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
$noExceptionOccurred = true;
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInputExceptionMessages($e);
|
||||
}
|
||||
if ($noExceptionOccurred) {
|
||||
$this->fail("Exception was expected to be thrown when provided credentials are invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
public function testCreateCustomerAccessTokenInvalidCustomer()
|
||||
{
|
||||
$customerUserName = 'invalid';
|
||||
$password = 'invalid';
|
||||
$noExceptionOccurred = false;
|
||||
try {
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH_CUSTOMER_TOKEN,
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
],
|
||||
];
|
||||
$requestData = ['username' => $customerUserName, 'password' => $password];
|
||||
$this->_webApiCall($serviceInfo, $requestData);
|
||||
$noExceptionOccurred = true;
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInvalidCredentialsException($e);
|
||||
}
|
||||
if ($noExceptionOccurred) {
|
||||
$this->fail("Exception was expected to be thrown when provided credentials are invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider to test input validation
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function validationDataProvider()
|
||||
{
|
||||
return [
|
||||
'Check for empty credentials' => ['', ''],
|
||||
'Check for null credentials' => [null, null]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert for presence of Input exception messages
|
||||
*
|
||||
* @param \Exception $e
|
||||
*/
|
||||
private function assertInputExceptionMessages($e)
|
||||
{
|
||||
$this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode());
|
||||
$exceptionData = $this->processRestExceptionResult($e);
|
||||
$expectedExceptionData = [
|
||||
'message' => 'One or more input exceptions have occurred.',
|
||||
'errors' => [
|
||||
[
|
||||
'message' => '"%fieldName" is required. Enter and try again.',
|
||||
'parameters' => [
|
||||
'fieldName' => 'username',
|
||||
],
|
||||
],
|
||||
[
|
||||
'message' => '"%fieldName" is required. Enter and try again.',
|
||||
'parameters' => [
|
||||
'fieldName' => 'password',
|
||||
]
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->assertEquals($expectedExceptionData, $exceptionData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php
|
||||
*/
|
||||
public function testThrottlingMaxAttempts()
|
||||
{
|
||||
$userNameFromFixture = 'customer@example.com';
|
||||
$passwordFromFixture = 'password';
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH_CUSTOMER_TOKEN,
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
],
|
||||
];
|
||||
$invalidCredentials = [
|
||||
'username' => $userNameFromFixture,
|
||||
'password' => 'invalid',
|
||||
];
|
||||
$validCredentials = [
|
||||
'username' => $userNameFromFixture,
|
||||
'password' => $passwordFromFixture,
|
||||
];
|
||||
|
||||
/* Try to get token using invalid credentials for 5 times (account is locked after 6 attempts) */
|
||||
$noExceptionOccurred = false;
|
||||
for ($i = 0; $i < ($this->attemptsCountToLockAccount - 1); $i++) {
|
||||
try {
|
||||
$this->_webApiCall($serviceInfo, $invalidCredentials);
|
||||
$noExceptionOccurred = true;
|
||||
} catch (\Exception $e) {
|
||||
}
|
||||
}
|
||||
if ($noExceptionOccurred) {
|
||||
$this->fail(
|
||||
"Precondition failed: exception should have occurred when token was requested with invalid credentials."
|
||||
);
|
||||
}
|
||||
|
||||
/** On 6th attempt it still should be possible to get token if valid credentials are specified */
|
||||
$accessToken = $this->_webApiCall($serviceInfo, $validCredentials);
|
||||
$this->assertToken($accessToken, $userNameFromFixture, $passwordFromFixture);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php
|
||||
*/
|
||||
public function testThrottlingAccountLockout()
|
||||
{
|
||||
$userNameFromFixture = 'customer@example.com';
|
||||
$passwordFromFixture = 'password';
|
||||
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => self::RESOURCE_PATH_CUSTOMER_TOKEN,
|
||||
'httpMethod' => Request::HTTP_METHOD_POST,
|
||||
],
|
||||
];
|
||||
$invalidCredentials = [
|
||||
'username' => $userNameFromFixture,
|
||||
'password' => 'invalid',
|
||||
];
|
||||
$validCredentials = [
|
||||
'username' => $userNameFromFixture,
|
||||
'password' => $passwordFromFixture,
|
||||
];
|
||||
|
||||
/* Try to get token using invalid credentials for 5 times (account would be locked after 6 attempts) */
|
||||
$noExceptionOccurred = false;
|
||||
for ($i = 0; $i < $this->attemptsCountToLockAccount; $i++) {
|
||||
try {
|
||||
$this->_webApiCall($serviceInfo, $invalidCredentials);
|
||||
$noExceptionOccurred = true;
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInvalidCredentialsException($e);
|
||||
}
|
||||
if ($noExceptionOccurred) {
|
||||
$this->fail("Exception was expected to be thrown when provided credentials are invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
$noExceptionOccurred = false;
|
||||
try {
|
||||
$this->_webApiCall($serviceInfo, $validCredentials);
|
||||
$noExceptionOccurred = true;
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInvalidCredentialsException($e);
|
||||
}
|
||||
if ($noExceptionOccurred) {
|
||||
$this->fail("Exception was expected to be thrown because account should have been locked at this point.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that status code and message are correct in case of authentication failure.
|
||||
*
|
||||
* @param \Exception $e
|
||||
*/
|
||||
private function assertInvalidCredentialsException($e)
|
||||
{
|
||||
$this->assertEquals(HTTPExceptionCodes::HTTP_UNAUTHORIZED, $e->getCode(), "Response HTTP code is invalid.");
|
||||
$exceptionData = $this->processRestExceptionResult($e);
|
||||
$expectedExceptionData = [
|
||||
'message' => 'The account sign-in was incorrect or your account is disabled temporarily. '
|
||||
. 'Please wait and try again later.'
|
||||
];
|
||||
$this->assertEquals($expectedExceptionData, $exceptionData, "Exception message is invalid.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure provided token is valid and belongs to the specified user.
|
||||
*
|
||||
* @param string $accessToken
|
||||
* @param string $userName
|
||||
* @param string $password
|
||||
*/
|
||||
private function assertToken($accessToken, $userName, $password)
|
||||
{
|
||||
$customerData = $this->customerAccountManagement->authenticate($userName, $password);
|
||||
$tokenData = $this->tokenReader->read($accessToken);
|
||||
$this->assertEquals((int) $customerData->getId(), $tokenData->getUserContext()->getUserId());
|
||||
$this->assertEquals(UserContextInterface::USER_TYPE_CUSTOMER, $tokenData->getUserContext()->getUserType());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Integration\Model;
|
||||
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Integration\Api\OauthServiceInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Authentication\OauthHelper;
|
||||
|
||||
class IntegrationTest extends \Magento\TestFramework\TestCase\WebapiAbstract
|
||||
{
|
||||
/** @var \Magento\Integration\Model\Integration */
|
||||
protected $integration;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
/** @var $integrationService \Magento\Integration\Api\IntegrationServiceInterface */
|
||||
$integrationService = $objectManager->get(\Magento\Integration\Api\IntegrationServiceInterface::class);
|
||||
|
||||
$params = [
|
||||
'all_resources' => true,
|
||||
'integration_id' => 1,
|
||||
'status' => Integration::STATUS_ACTIVE,
|
||||
'name' => 'Test Integration1'
|
||||
];
|
||||
$this->integration = $integrationService->update($params);
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->integration = null;
|
||||
OauthHelper::clearApiAccessCredentials();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testConfigBasedIntegrationCreation()
|
||||
{
|
||||
$this->assertEquals('test-integration@magento.com', $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());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test simple request data
|
||||
*
|
||||
* @depends testConfigBasedIntegrationCreation
|
||||
*/
|
||||
public function testGetServiceCall()
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
$itemId = 1;
|
||||
$name = 'Test';
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'resourcePath' => '/V1/testmodule4/' . $itemId,
|
||||
'httpMethod' => \Magento\Webapi\Model\Rest\Config::HTTP_METHOD_GET,
|
||||
],
|
||||
];
|
||||
$item = $this->_webApiCall($serviceInfo, [], null, null, $this->integration);
|
||||
$this->assertEquals($itemId, $item['entity_id'], 'id field returned incorrectly');
|
||||
$this->assertEquals($name, $item['name'], 'name field returned incorrectly');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Integration access token cannot be used as Bearer token by default
|
||||
* @magentoConfigFixture default_store oauth/consumer/enable_integration_as_bearer 0
|
||||
*/
|
||||
public function testIntegrationAsBearerTokenDefault()
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
$oauthService = ObjectManager::getInstance()->get(OauthServiceInterface::class);
|
||||
$accessToken = $oauthService->getAccessToken($this->integration->getConsumerId());
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'token' => $accessToken,
|
||||
'resourcePath' => '/V1/store/storeViews',
|
||||
'httpMethod' => \Magento\Webapi\Model\Rest\Config::HTTP_METHOD_GET,
|
||||
],
|
||||
];
|
||||
self::expectException(\Exception::class);
|
||||
self::expectExceptionMessage('The consumer isn\'t authorized to access %resources.');
|
||||
$this->_webApiCall($serviceInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Integration access token can be used as Bearer token when explicitly enabled
|
||||
*
|
||||
* @doesNotPerformAssertions
|
||||
*/
|
||||
public function testIntegrationAsBearerTokenEnabled()
|
||||
{
|
||||
$this->_markTestAsRestOnly();
|
||||
$oauthService = ObjectManager::getInstance()->get(OauthServiceInterface::class);
|
||||
$accessToken = $oauthService->getAccessToken($this->integration->getConsumerId());
|
||||
$serviceInfo = [
|
||||
'rest' => [
|
||||
'token' => $accessToken->getToken(),
|
||||
'resourcePath' => '/V1/store/storeViews',
|
||||
'httpMethod' => \Magento\Webapi\Model\Rest\Config::HTTP_METHOD_GET,
|
||||
],
|
||||
];
|
||||
$this->_webApiCall($serviceInfo);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user