|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\ReCaptchaCheckout\Test\Api; |
| 9 | + |
| 10 | +use Magento\Framework\Webapi\Rest\Request; |
| 11 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 12 | +use Magento\Quote\Model\Quote; |
| 13 | +use Magento\Quote\Model\QuoteFactory; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use Magento\TestFramework\TestCase\WebapiAbstract; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test that checkout APIs are covered with ReCaptcha |
| 19 | + */ |
| 20 | +class PaymentInformationManagementTest extends WebapiAbstract |
| 21 | +{ |
| 22 | + private const API_ROUTE = '/V1/carts/mine/payment-information'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var QuoteFactory |
| 26 | + */ |
| 27 | + private $quoteFactory; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var CustomerTokenServiceInterface |
| 31 | + */ |
| 32 | + private $tokenService; |
| 33 | + |
| 34 | + /** |
| 35 | + * @inheritDoc |
| 36 | + */ |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + parent::setUp(); |
| 40 | + |
| 41 | + $this->_markTestAsRestOnly(); |
| 42 | + $objectManager = Bootstrap::getObjectManager(); |
| 43 | + $this->quoteFactory = $objectManager->get(QuoteFactory::class); |
| 44 | + $this->tokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @magentoApiDataFixture Magento/Checkout/_files/customer_quote_ready_for_order.php |
| 49 | + * @magentoConfigFixture default_store customer/captcha/enable 0 |
| 50 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key |
| 51 | + * @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key |
| 52 | + * @magentoConfigFixture base_website recaptcha_frontend/type_for/place_order invisible |
| 53 | + */ |
| 54 | + public function testRequired(): void |
| 55 | + { |
| 56 | + $this->expectException(\Throwable::class); |
| 57 | + $this->expectExceptionCode(400); |
| 58 | + $this->expectExceptionMessage('{"message":"ReCaptcha validation failed, please try again"}'); |
| 59 | + |
| 60 | + /** @var Quote $quote */ |
| 61 | + $quote = $this->quoteFactory->create(); |
| 62 | + $quote->load('55555555', 'reserved_order_id'); |
| 63 | + $cartId = $quote->getId(); |
| 64 | + $payment = $quote->getPayment(); |
| 65 | + $address = $quote->getBillingAddress(); |
| 66 | + $addressData = []; |
| 67 | + $addressProperties = [ |
| 68 | + 'city', 'company', 'countryId', 'firstname', 'lastname', 'postcode', |
| 69 | + 'region', 'regionCode', 'regionId', 'saveInAddressBook', 'street', 'telephone', 'email' |
| 70 | + ]; |
| 71 | + foreach ($addressProperties as $property) { |
| 72 | + $method = 'get' . $property; |
| 73 | + $addressData[$property] = $address->$method(); |
| 74 | + } |
| 75 | + $token = $this-> tokenService-> createCustomerAccessToken( '[email protected]', 'password'); |
| 76 | + |
| 77 | + $serviceInfo = [ |
| 78 | + 'rest' => [ |
| 79 | + 'resourcePath' => self::API_ROUTE, |
| 80 | + 'httpMethod' => Request::HTTP_METHOD_POST, |
| 81 | + 'token' => $token |
| 82 | + ], |
| 83 | + ]; |
| 84 | + $requestData = [ |
| 85 | + 'cart_id' => $cartId, |
| 86 | + 'billingAddress' => $addressData, |
| 87 | + 'email' => $quote->getCustomerEmail(), |
| 88 | + 'paymentMethod' => [ |
| 89 | + 'additional_data' => $payment->getAdditionalData(), |
| 90 | + 'method' => $payment->getMethod(), |
| 91 | + 'po_number' => $payment->getPoNumber() |
| 92 | + ] |
| 93 | + ]; |
| 94 | + |
| 95 | + $this->_webApiCall($serviceInfo, $requestData); |
| 96 | + } |
| 97 | +} |
0 commit comments