Skip to content

Commit f986d18

Browse files
committed
LYNX-232: Fix web api tests
1 parent 62af79c commit f986d18

File tree

2 files changed

+54
-47
lines changed

2 files changed

+54
-47
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/GraphQlCache/CacheIdFactorProviders/Customer/IsLoggedInProviderTest.php

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\GraphQl\GraphQlCache\CacheIdFactorProviders\Customer;
99

10-
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
1110
use Magento\TestFramework\TestCase\GraphQlAbstract;
1211

1312
/**
@@ -16,7 +15,7 @@
1615
class IsLoggedInProviderTest extends GraphQlAbstract
1716
{
1817
/**
19-
* Tests that cache id header is generated for generateToken mutation and other post requests
18+
* Tests cache is not generated for generateToken mutation and other post requests
2019
*
2120
* @magentoApiDataFixture Magento/Customer/_files/customer.php
2221
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
@@ -38,10 +37,12 @@ public function testCacheIdHeaderWithIsLoggedIn()
3837
}
3938
MUTATION;
4039
$tokenResponse = $this->graphQlMutationWithResponseHeaders($generateToken);
41-
// Verify that the the cache id is generated for generate token mutation
42-
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $tokenResponse['headers']);
43-
$cacheIdCustomerToken = $tokenResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
44-
$this->assertTrue((boolean)preg_match('/^[0-9a-f]{64}$/i', $cacheIdCustomerToken));
40+
// Verify that the cache is not generated for generate token mutation
41+
$this->assertEquals('no-cache', $tokenResponse['headers']['Pragma']);
42+
$this->assertEquals(
43+
'no-store, no-cache, must-revalidate, max-age=0',
44+
$tokenResponse['headers']['Cache-Control']
45+
);
4546
$this->assertArrayHasKey('generateCustomerToken', $tokenResponse['body']);
4647
$customerToken = $tokenResponse['body']['generateCustomerToken']['token'];
4748
$createEmptyCart = <<<MUTATION
@@ -54,20 +55,21 @@ public function testCacheIdHeaderWithIsLoggedIn()
5455
'',
5556
['Authorization' => 'Bearer ' . $customerToken]
5657
);
57-
//Verify that the the cache id is generated for authorized mutation like createEmptyCart
58-
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $createCustomerCartResponse['headers']);
58+
//Verify that the cache is not generated for authorized mutation like createEmptyCart
59+
$this->assertEquals('no-cache', $createCustomerCartResponse['headers']['Pragma']);
60+
$this->assertEquals(
61+
'no-store, no-cache, must-revalidate, max-age=0',
62+
$createCustomerCartResponse['headers']['Cache-Control']
63+
);
5964
$cartId = $createCustomerCartResponse['body']['createEmptyCart'];
60-
$cacheIdCreateCustomerCart = $createCustomerCartResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
61-
$this->assertTrue((boolean)preg_match('/^[0-9a-f]{64}$/i', $cacheIdCreateCustomerCart));
62-
$this->assertEquals($cacheIdCustomerToken, $cacheIdCreateCustomerCart);
6365

6466
$createGuestCartResponse = $this->graphQlMutationWithResponseHeaders($createEmptyCart);
65-
//Verify that cache id is generated for unauthorized post requests
66-
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $createGuestCartResponse['headers']);
67-
$cacheIdCreateGuestCart = $createGuestCartResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
68-
$this->assertTrue((boolean)preg_match('/^[0-9a-f]{64}$/i', $cacheIdCreateGuestCart));
69-
//Verify that cache id generated for customer and guest are not equal
70-
$this->assertNotEquals($cacheIdCreateCustomerCart, $cacheIdCreateGuestCart);
67+
//Verify that cache is not generated for unauthorized post requests
68+
$this->assertEquals('no-cache', $createGuestCartResponse['headers']['Pragma']);
69+
$this->assertEquals(
70+
'no-store, no-cache, must-revalidate, max-age=0',
71+
$createGuestCartResponse['headers']['Cache-Control']
72+
);
7173
$addProductToCustomerCart = <<<MUTATION
7274
mutation{
7375
addSimpleProductsToCart
@@ -86,11 +88,11 @@ public function testCacheIdHeaderWithIsLoggedIn()
8688
'',
8789
['Authorization' => 'Bearer ' . $customerToken]
8890
);
89-
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $addProductToCustomerCartResponse['headers']);
90-
//Verify that cache id generated for all subsequent operations by the customer remains consistent
91+
//Verify that cache is not generated for addSimpleProductsToCart mutation
92+
$this->assertEquals('no-cache', $addProductToCustomerCartResponse['headers']['Pragma']);
9193
$this->assertEquals(
92-
$cacheIdCreateCustomerCart,
93-
$addProductToCustomerCartResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER]
94+
'no-store, no-cache, must-revalidate, max-age=0',
95+
$addProductToCustomerCartResponse['headers']['Cache-Control']
9496
);
9597
}
9698

@@ -102,12 +104,15 @@ public function testCacheIdHeaderWithIsLoggedIn()
102104
*/
103105
public function testCacheIdHeaderAfterRevokeToken()
104106
{
105-
// Get the guest cache id
107+
// Get the guest headers
106108
$guestCartResponse = $this->graphQlMutationWithResponseHeaders('mutation{createEmptyCart}');
107-
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $guestCartResponse['headers']);
108-
$guestCacheId = $guestCartResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
109+
$this->assertEquals('no-cache', $guestCartResponse['headers']['Pragma']);
110+
$this->assertEquals(
111+
'no-store, no-cache, must-revalidate, max-age=0',
112+
$guestCartResponse['headers']['Cache-Control']
113+
);
109114

110-
// Get the customer cache id and token to send to the revoke mutation
115+
// Get the customer token to send to the revoke mutation
111116
$generateToken = <<<MUTATION
112117
mutation{
113118
generateCustomerToken(email:"customer@example.com", password:"password")
@@ -117,20 +122,24 @@ public function testCacheIdHeaderAfterRevokeToken()
117122
$tokenResponse = $this->graphQlMutationWithResponseHeaders($generateToken);
118123
$this->assertArrayHasKey('generateCustomerToken', $tokenResponse['body']);
119124
$customerToken = $tokenResponse['body']['generateCustomerToken']['token'];
120-
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $tokenResponse['headers']);
121-
$customerCacheId = $tokenResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
122-
$this->assertNotEquals($customerCacheId, $guestCacheId);
125+
$this->assertEquals('no-cache', $tokenResponse['headers']['Pragma']);
126+
$this->assertEquals(
127+
'no-store, no-cache, must-revalidate, max-age=0',
128+
$tokenResponse['headers']['Cache-Control']
129+
);
123130

124-
// Revoke the token and check that it returns the guest cache id
131+
// Revoke the token and check that cache is not generated
125132
$revokeCustomerToken = "mutation{revokeCustomerToken{result}}";
126133
$revokeResponse = $this->graphQlMutationWithResponseHeaders(
127134
$revokeCustomerToken,
128135
[],
129136
'',
130137
['Authorization' => 'Bearer ' . $customerToken]
131138
);
132-
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $revokeResponse['headers']);
133-
$revokeCacheId = $revokeResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
134-
$this->assertEquals($guestCacheId, $revokeCacheId);
139+
$this->assertEquals('no-cache', $revokeResponse['headers']['Pragma']);
140+
$this->assertEquals(
141+
'no-store, no-cache, must-revalidate, max-age=0',
142+
$revokeResponse['headers']['Cache-Control']
143+
);
135144
}
136145
}

dev/tests/api-functional/testsuite/Magento/GraphQl/PageCache/VarnishTest.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,21 @@ public function testCacheResultForCustomer()
207207
$generateToken = $this->generateCustomerToken($email, $password);
208208
$tokenResponse = $this->graphQlMutationWithResponseHeaders($generateToken);
209209

210-
// Obtain the X-Magento-Cache-id from the response and authorization token - customer logs in
211-
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $tokenResponse['headers']);
212-
$cacheIdCustomer = $tokenResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
210+
// Verify cache is not generated for mutations
211+
$this->assertEquals('no-cache', $tokenResponse['headers']['Pragma']);
212+
$this->assertEquals(
213+
'no-store, no-cache, must-revalidate, max-age=0',
214+
$tokenResponse['headers']['Cache-Control']
215+
);
213216
$customerToken = $tokenResponse['body']['generateCustomerToken']['token'];
214217

215218
// Verify we obtain cache MISS the first time we search by this X-Magento-Cache-Id
216219
$this->assertCacheMissAndReturnResponse($query, [
217-
CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer,
218220
'Authorization' => 'Bearer ' . $customerToken
219221
]);
220222

221223
// Verify we obtain cache HIT second time using the same X-Magento-Cache-Id
222224
$this->assertCacheHitAndReturnResponse($query, [
223-
CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer,
224225
'Authorization' => 'Bearer ' . $customerToken
225226
]);
226227
$revokeTokenQuery = $this->revokeCustomerToken();
@@ -230,18 +231,15 @@ public function testCacheResultForCustomer()
230231
$revokeTokenQuery,
231232
[],
232233
'',
233-
[
234-
CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer,
235-
'Authorization' => 'Bearer ' . $customerToken
236-
]
234+
['Authorization' => 'Bearer ' . $customerToken]
237235
);
238236

239-
$cacheIdGuest = $revokeTokenResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
240-
$this->assertNotEquals($cacheIdCustomer, $cacheIdGuest);
241-
242-
//Verify that omitting the Auth token doesn't send cached content for a logged-in customer
243-
$this->assertCacheMissAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer]);
244-
$this->assertCacheMissAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer]);
237+
//Verify cache is not generated for mutations
238+
$this->assertEquals('no-cache', $revokeTokenResponse['headers']['Pragma']);
239+
$this->assertEquals(
240+
'no-store, no-cache, must-revalidate, max-age=0',
241+
$revokeTokenResponse['headers']['Cache-Control']
242+
);
245243
}
246244

247245
/**

0 commit comments

Comments
 (0)