|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2023 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * *********************************************************************** |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\GraphQl\Customer; |
| 20 | + |
| 21 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 22 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 23 | +use Magento\Customer\Test\Fixture\Customer; |
| 24 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 25 | +use Magento\TestFramework\Fixture\DataFixture; |
| 26 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 27 | +use Magento\TestFramework\Helper\Bootstrap; |
| 28 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 29 | +use Magento\TestFramework\TestCase\HttpClient\CurlClient; |
| 30 | + |
| 31 | +/** |
| 32 | + * Test customer authentication responses |
| 33 | + */ |
| 34 | +class AuthenticationTest extends GraphQlAbstract |
| 35 | +{ |
| 36 | + private const QUERY_ACCESSIBLE_BY_GUEST = <<<QUERY |
| 37 | +{ |
| 38 | + isEmailAvailable(email: "customer@example.com") { |
| 39 | + is_email_available |
| 40 | + } |
| 41 | +} |
| 42 | +QUERY; |
| 43 | + |
| 44 | + private const QUERY_REQUIRE_AUTHENTICATION = <<<QUERY |
| 45 | +{ |
| 46 | + customer { |
| 47 | + email |
| 48 | + } |
| 49 | +} |
| 50 | +QUERY; |
| 51 | + |
| 52 | + private $tokenService; |
| 53 | + |
| 54 | + protected function setUp(): void |
| 55 | + { |
| 56 | + $this->tokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class); |
| 57 | + } |
| 58 | + |
| 59 | + public function testNoToken() |
| 60 | + { |
| 61 | + $response = $this->graphQlQuery(self::QUERY_ACCESSIBLE_BY_GUEST); |
| 62 | + |
| 63 | + self::assertArrayHasKey('isEmailAvailable', $response); |
| 64 | + self::assertArrayHasKey('is_email_available', $response['isEmailAvailable']); |
| 65 | + } |
| 66 | + |
| 67 | + public function testInvalidToken() |
| 68 | + { |
| 69 | + $this->expectExceptionCode(401); |
| 70 | + Bootstrap::getObjectManager()->get(CurlClient::class)->get( |
| 71 | + rtrim(TESTS_BASE_URL, '/') . '/graphql', |
| 72 | + [ |
| 73 | + 'query' => self::QUERY_ACCESSIBLE_BY_GUEST |
| 74 | + ], |
| 75 | + [ |
| 76 | + 'Authorization: Bearer invalid_token' |
| 77 | + ] |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + #[ |
| 82 | + DataFixture(Customer::class, as: 'customer'), |
| 83 | + ] |
| 84 | + public function testRevokedTokenPublicQuery() |
| 85 | + { |
| 86 | + /** @var CustomerInterface $customer */ |
| 87 | + $customer = DataFixtureStorageManager::getStorage()->get('customer'); |
| 88 | + $token = $this->tokenService->createCustomerAccessToken($customer->getEmail(), 'password'); |
| 89 | + |
| 90 | + $response = $this->graphQlQuery( |
| 91 | + self::QUERY_ACCESSIBLE_BY_GUEST, |
| 92 | + [], |
| 93 | + '', |
| 94 | + [ |
| 95 | + 'Authorization' => 'Bearer ' . $token |
| 96 | + ] |
| 97 | + ); |
| 98 | + |
| 99 | + self::assertArrayHasKey('isEmailAvailable', $response); |
| 100 | + self::assertArrayHasKey('is_email_available', $response['isEmailAvailable']); |
| 101 | + |
| 102 | + $this->tokenService->revokeCustomerAccessToken($customer->getId()); |
| 103 | + |
| 104 | + $this->expectExceptionCode(401); |
| 105 | + Bootstrap::getObjectManager()->get(CurlClient::class)->get( |
| 106 | + rtrim(TESTS_BASE_URL, '/') . '/graphql', |
| 107 | + [ |
| 108 | + 'query' => self::QUERY_ACCESSIBLE_BY_GUEST |
| 109 | + ], |
| 110 | + [ |
| 111 | + 'Authorization: Bearer ' . $token |
| 112 | + ] |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + #[ |
| 117 | + DataFixture(Customer::class, as: 'customer'), |
| 118 | + ] |
| 119 | + public function testRevokedTokenProtectedQuery() |
| 120 | + { |
| 121 | + /** @var CustomerInterface $customer */ |
| 122 | + $customer = DataFixtureStorageManager::getStorage()->get('customer'); |
| 123 | + $token = $this->tokenService->createCustomerAccessToken($customer->getEmail(), 'password'); |
| 124 | + |
| 125 | + $response = $this->graphQlQuery( |
| 126 | + self::QUERY_REQUIRE_AUTHENTICATION, |
| 127 | + [], |
| 128 | + '', |
| 129 | + [ |
| 130 | + 'Authorization' => 'Bearer ' . $token |
| 131 | + ] |
| 132 | + ); |
| 133 | + |
| 134 | + self::assertEquals( |
| 135 | + [ |
| 136 | + 'customer' => [ |
| 137 | + 'email' => $customer->getEmail() |
| 138 | + ] |
| 139 | + ], |
| 140 | + $response |
| 141 | + ); |
| 142 | + |
| 143 | + $this->tokenService->revokeCustomerAccessToken($customer->getId()); |
| 144 | + |
| 145 | + $this->expectExceptionCode(401); |
| 146 | + Bootstrap::getObjectManager()->get(CurlClient::class)->get( |
| 147 | + rtrim(TESTS_BASE_URL, '/') . '/graphql', |
| 148 | + [ |
| 149 | + 'query' => self::QUERY_REQUIRE_AUTHENTICATION |
| 150 | + ], |
| 151 | + [ |
| 152 | + 'Authorization: Bearer ' . $token |
| 153 | + ] |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + #[ |
| 158 | + DataFixture(Customer::class, as: 'customer'), |
| 159 | + DataFixture( |
| 160 | + Customer::class, |
| 161 | + [ |
| 162 | + 'addresses' => [ |
| 163 | + [ |
| 164 | + 'country_id' => 'US', |
| 165 | + 'region_id' => 32, |
| 166 | + 'city' => 'Boston', |
| 167 | + 'street' => ['10 Milk Street'], |
| 168 | + 'postcode' => '02108', |
| 169 | + 'telephone' => '1234567890', |
| 170 | + 'default_billing' => true, |
| 171 | + 'default_shipping' => true |
| 172 | + ] |
| 173 | + ] |
| 174 | + ], |
| 175 | + as: 'customer2' |
| 176 | + ), |
| 177 | + ] |
| 178 | + public function testForbidden() |
| 179 | + { |
| 180 | + /** @var CustomerInterface $customer2 */ |
| 181 | + $customer2Data = DataFixtureStorageManager::getStorage()->get('customer2'); |
| 182 | + $customer2 = Bootstrap::getObjectManager() |
| 183 | + ->get(CustomerRepositoryInterface::class) |
| 184 | + ->get($customer2Data->getEmail()); |
| 185 | + $addressId = $customer2->getDefaultBilling(); |
| 186 | + $mutation |
| 187 | + = <<<MUTATION |
| 188 | +mutation { |
| 189 | + deleteCustomerAddress(id: {$addressId}) |
| 190 | +} |
| 191 | +MUTATION; |
| 192 | + |
| 193 | + /** @var CustomerInterface $customer */ |
| 194 | + $customer = DataFixtureStorageManager::getStorage()->get('customer'); |
| 195 | + $token = $this->tokenService->createCustomerAccessToken($customer->getEmail(), 'password'); |
| 196 | + |
| 197 | + $this->expectExceptionCode(403); |
| 198 | + Bootstrap::getObjectManager()->get(CurlClient::class)->post( |
| 199 | + rtrim(TESTS_BASE_URL, '/') . '/graphql', |
| 200 | + json_encode(['query' => $mutation]), |
| 201 | + [ |
| 202 | + 'Authorization: Bearer ' . $token, |
| 203 | + 'Accept: application/json', |
| 204 | + 'Content-Type: application/json' |
| 205 | + ] |
| 206 | + ); |
| 207 | + } |
| 208 | +} |
0 commit comments