Skip to content

Commit b78b3f0

Browse files
author
Tanniru, Murali(mtanniru)
committed
MAGETWO-51862: Automate test to create API Request to access other customer data using customer token.
- Automate manual test MAGETWO-27489.
1 parent 072a1ae commit b78b3f0

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

dev/tests/api-functional/testsuite/Magento/Customer/Api/CustomerRepositoryTest.php

+75
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\TestFramework\TestCase\WebapiAbstract;
1515
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
1616
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Symfony\Component\Config\Definition\Exception\Exception;
1718

1819
/**
1920
* Test class for Magento\Customer\Api\CustomerRepositoryInterface
@@ -25,6 +26,7 @@ class CustomerRepositoryTest extends WebapiAbstract
2526
const SERVICE_VERSION = 'V1';
2627
const SERVICE_NAME = 'customerCustomerRepositoryV1';
2728
const RESOURCE_PATH = '/V1/customers';
29+
const RESOURCE_PATH_CUSTOMER_TOKEN = "/V1/integration/customer/token";
2830

2931
/**
3032
* Sample values for testing
@@ -82,6 +84,11 @@ class CustomerRepositoryTest extends WebapiAbstract
8284
*/
8385
private $dataObjectProcessor;
8486

87+
/**
88+
* @var TokenModel
89+
*/
90+
private $token;
91+
8592
/**
8693
* Execute per test initialization.
8794
*/
@@ -141,6 +148,53 @@ public function tearDown()
141148
unset($this->customerRepository);
142149
}
143150

151+
/**
152+
* validate update by invalid customer.
153+
*
154+
* @expectedException \Exception
155+
* @expectedExceptionMessage Consumer is not authorized to access %resources
156+
*
157+
*/
158+
public function testInvalidCustomerUpdate()
159+
{
160+
//Create customer 1 and retrieve customer token.
161+
$customerData1 = $this->_createCustomer();
162+
$this->resetTokenForCustomer(($customerData1[Customer::EMAIL]), 'test@123');
163+
164+
//Create customer 2 and update lastname.
165+
$customerData = $this->_createCustomer();
166+
$existingCustomerDataObject = $this->_getCustomerData($customerData[Customer::ID]);
167+
$lastName = $existingCustomerDataObject->getLastname();
168+
$customerData[Customer::LASTNAME] = $lastName . 'Updated';
169+
$newCustomerDataObject = $this->customerDataFactory->create();
170+
$this->dataObjectHelper->populateWithArray(
171+
$newCustomerDataObject,
172+
$customerData,
173+
'\Magento\Customer\Api\Data\CustomerInterface'
174+
);
175+
176+
$serviceInfo = [
177+
'rest' => [
178+
'resourcePath' => self::RESOURCE_PATH . "/{$customerData[Customer::ID]}",
179+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
180+
'token' => $this->token,
181+
],
182+
'soap' => [
183+
'service' => self::SERVICE_NAME,
184+
'serviceVersion' => self::SERVICE_VERSION,
185+
'operation' => self::SERVICE_NAME . 'Save',
186+
'token' => $this->token,
187+
],
188+
];
189+
190+
$newCustomerDataObject = $this->dataObjectProcessor->buildOutputDataArray(
191+
$newCustomerDataObject,
192+
'Magento\Customer\Api\Data\CustomerInterface'
193+
);
194+
$requestData = ['customer' => $newCustomerDataObject];
195+
$this->_webApiCall($serviceInfo, $requestData);
196+
}
197+
144198
public function testDeleteCustomer()
145199
{
146200
$customerData = $this->_createCustomer();
@@ -750,4 +804,25 @@ protected function _createCustomer()
750804
$this->currentCustomerId[] = $customerData['id'];
751805
return $customerData;
752806
}
807+
808+
/**
809+
* Sets the test's access token for a particular username and password.
810+
*
811+
* @param string $username
812+
* @param string $password
813+
*/
814+
815+
protected function resetTokenForCustomer($username, $password)
816+
{
817+
818+
// get customer ID token
819+
$serviceInfo = [
820+
'rest' => [
821+
'resourcePath' => self::RESOURCE_PATH_CUSTOMER_TOKEN,
822+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
823+
],
824+
];
825+
$requestData = ['username' => $username, 'password' => $password];
826+
$this->token = $this->_webApiCall($serviceInfo, $requestData);
827+
}
753828
}

0 commit comments

Comments
 (0)