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