14
14
use Magento \TestFramework \TestCase \WebapiAbstract ;
15
15
use Magento \Framework \Webapi \Exception as HTTPExceptionCodes ;
16
16
use Magento \Framework \Exception \NoSuchEntityException ;
17
+ use Symfony \Component \Config \Definition \Exception \Exception ;
17
18
18
19
/**
19
20
* Test class for Magento\Customer\Api\CustomerRepositoryInterface
@@ -25,6 +26,7 @@ class CustomerRepositoryTest extends WebapiAbstract
25
26
const SERVICE_VERSION = 'V1 ' ;
26
27
const SERVICE_NAME = 'customerCustomerRepositoryV1 ' ;
27
28
const RESOURCE_PATH = '/V1/customers ' ;
29
+ const RESOURCE_PATH_CUSTOMER_TOKEN = "/V1/integration/customer/token " ;
28
30
29
31
/**
30
32
* Sample values for testing
@@ -82,6 +84,11 @@ class CustomerRepositoryTest extends WebapiAbstract
82
84
*/
83
85
private $ dataObjectProcessor ;
84
86
87
+ /**
88
+ * @var TokenModel
89
+ */
90
+ private $ token ;
91
+
85
92
/**
86
93
* Execute per test initialization.
87
94
*/
@@ -141,6 +148,53 @@ public function tearDown()
141
148
unset($ this ->customerRepository );
142
149
}
143
150
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
+
144
198
public function testDeleteCustomer ()
145
199
{
146
200
$ customerData = $ this ->_createCustomer ();
@@ -750,4 +804,25 @@ protected function _createCustomer()
750
804
$ this ->currentCustomerId [] = $ customerData ['id ' ];
751
805
return $ customerData ;
752
806
}
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
+ }
753
828
}
0 commit comments