Skip to content

Commit fe23996

Browse files
committed
Add validation for additonal data input
1 parent ba459a1 commit fe23996

File tree

5 files changed

+120
-1
lines changed

5 files changed

+120
-1
lines changed

app/code/Magento/BraintreeGraphQl/Model/BraintreeDataProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ public function getData(array $args): array
3232
);
3333
}
3434

35+
if (!isset($args[static::PATH_ADDITIONAL_DATA]['payment_method_nonce'])) {
36+
throw new GraphQlInputException(
37+
__('Required parameter "payment_method_nonce" for "braintree" is missing.')
38+
);
39+
}
40+
41+
if (!isset($args[static::PATH_ADDITIONAL_DATA]['is_active_payment_token_enabler'])) {
42+
throw new GraphQlInputException(
43+
__('Required parameter "is_active_payment_token_enabler" for "braintree" is missing.')
44+
);
45+
}
46+
3547
return $args[static::PATH_ADDITIONAL_DATA];
3648
}
3749
}

app/code/Magento/BraintreeGraphQl/Model/BraintreeVaultDataProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public function getData(array $args): array
3131
);
3232
}
3333

34+
if (!isset($args[static::PATH_ADDITIONAL_DATA]['public_hash'])) {
35+
throw new GraphQlInputException(
36+
__('Required parameter "public_hash" for "braintree_cc_vault" is missing.')
37+
);
38+
}
39+
3440
return $args[static::PATH_ADDITIONAL_DATA];
3541
}
3642
}

app/code/Magento/BraintreeGraphQl/Plugin/SetVaultPaymentNonce.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function beforeExecute(
5555
): array {
5656
if ($paymentData['code'] !== ConfigProvider::CC_VAULT_CODE
5757
|| !isset($paymentData[ConfigProvider::CC_VAULT_CODE])
58+
|| !isset($paymentData[ConfigProvider::CC_VAULT_CODE]['public_hash'])
5859
) {
5960
return [$quote, $paymentData];
6061
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Braintree/Customer/SetPaymentMethodTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,33 @@ public function testSetPaymentMethodInvalidInput(string $methodCode)
210210
$this->graphQlMutation($setPaymentQuery, [], '', $this->getHeaderMap());
211211
}
212212

213+
/**
214+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
215+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
216+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
217+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
218+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
219+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
220+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
221+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
222+
* @magentoApiDataFixture Magento/GraphQl/Braintree/_files/enable_braintree_payment.php
223+
* @dataProvider dataProviderTestSetPaymentMethodInvalidInput
224+
* @expectedException \Exception
225+
* @param string $methodCode
226+
*/
227+
public function testSetPaymentMethodInvalidMethodInput(string $methodCode)
228+
{
229+
$reservedOrderId = 'test_quote';
230+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
231+
232+
$setPaymentQuery = $this->getSetPaymentBraintreeQueryInvalidMethodInput(
233+
$maskedQuoteId,
234+
$methodCode
235+
);
236+
$this->expectExceptionMessage("for \"$methodCode\" is missing.");
237+
$this->graphQlMutation($setPaymentQuery, [], '', $this->getHeaderMap());
238+
}
239+
213240
public function dataProviderTestSetPaymentMethodInvalidInput(): array
214241
{
215242
return [
@@ -320,6 +347,32 @@ private function getSetPaymentBraintreeQueryInvalidInput(string $maskedQuoteId,
320347
QUERY;
321348
}
322349

350+
/**
351+
* @param string $maskedQuoteId
352+
* @param string $methodCode
353+
* @return string
354+
*/
355+
private function getSetPaymentBraintreeQueryInvalidMethodInput(string $maskedQuoteId, string $methodCode): string
356+
{
357+
return <<<QUERY
358+
mutation {
359+
setPaymentMethodOnCart(input:{
360+
cart_id:"{$maskedQuoteId}"
361+
payment_method:{
362+
code:"{$methodCode}"
363+
{$methodCode}: {}
364+
}
365+
}) {
366+
cart {
367+
selected_payment_method {
368+
code
369+
}
370+
}
371+
}
372+
}
373+
QUERY;
374+
}
375+
323376
/**
324377
* @param string $maskedQuoteId
325378
* @return string

dev/tests/api-functional/testsuite/Magento/GraphQl/Braintree/Guest/SetPaymentMethodTest.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ public function testSetPaymentMethodInvalidInput()
121121
$this->graphQlMutation($setPaymentQuery);
122122
}
123123

124+
/**
125+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
126+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
127+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
128+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
129+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
130+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
131+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
132+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
133+
* @magentoApiDataFixture Magento/GraphQl/Braintree/_files/enable_braintree_payment.php
134+
* @expectedException \Exception
135+
*/
136+
public function testSetPaymentMethodInvalidMethodInput()
137+
{
138+
$reservedOrderId = 'test_quote';
139+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
140+
141+
$setPaymentQuery = $this->getSetPaymentBraintreeQueryInvalidMethodInput($maskedQuoteId);
142+
$this->expectExceptionMessage("for \"braintree\" is missing.");
143+
$this->graphQlMutation($setPaymentQuery);
144+
}
145+
124146
private function assertPlaceOrderResponse(array $response, string $reservedOrderId): void
125147
{
126148
self::assertArrayHasKey('placeOrder', $response);
@@ -152,7 +174,7 @@ private function getSetPaymentBraintreeQuery(string $maskedQuoteId, string $nonc
152174
code:"braintree"
153175
braintree:{
154176
is_active_payment_token_enabler:false
155-
payment_method_nonce:"fake-valid-nonce"
177+
payment_method_nonce:"{$nonce}"
156178
}
157179
}
158180
}) {
@@ -190,6 +212,31 @@ private function getSetPaymentBraintreeQueryInvalidInput(string $maskedQuoteId):
190212
QUERY;
191213
}
192214

215+
/**
216+
* @param string $maskedQuoteId
217+
* @return string
218+
*/
219+
private function getSetPaymentBraintreeQueryInvalidMethodInput(string $maskedQuoteId): string
220+
{
221+
return <<<QUERY
222+
mutation {
223+
setPaymentMethodOnCart(input:{
224+
cart_id:"{$maskedQuoteId}"
225+
payment_method:{
226+
code:"braintree"
227+
braintree: {}
228+
}
229+
}) {
230+
cart {
231+
selected_payment_method {
232+
code
233+
}
234+
}
235+
}
236+
}
237+
QUERY;
238+
}
239+
193240
/**
194241
* @param string $maskedQuoteId
195242
* @return string

0 commit comments

Comments
 (0)