Skip to content

Commit 4999bdc

Browse files
committed
Add 6-hour TTL for location cache, add test for universe domain skip
1 parent f564f87 commit 4999bdc

3 files changed

Lines changed: 73 additions & 6 deletions

File tree

src/CacheTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ private function getCachedValue($k)
6666
*
6767
* @param mixed $k
6868
* @param mixed $v
69+
* @param int|null $lifetime
6970
* @return mixed
7071
*/
71-
private function setCachedValue($k, $v)
72+
private function setCachedValue($k, $v, ?int $lifetime = null)
7273
{
7374
if (is_null($this->cache)) {
7475
return null;
@@ -81,7 +82,7 @@ private function setCachedValue($k, $v)
8182

8283
$cacheItem = $this->cache->getItem($key);
8384
$cacheItem->set($v);
84-
$cacheItem->expiresAfter($this->cacheConfig['lifetime']);
85+
$cacheItem->expiresAfter($lifetime ?? $this->cacheConfig['lifetime']);
8586
return $this->cache->save($cacheItem);
8687
}
8788

src/TrustBoundaryTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ private function getTrustBoundary(
5858
}
5959

6060
// Save to cache
61-
$this->setCachedValue($this->getCacheKey() . ':trustboundary', $trustBoundary);
61+
$tbLifetime = 6 * 60 * 60; // 6-hour cache TTL
62+
$this->setCachedValue($this->getCacheKey() . ':trustboundary', $trustBoundary, $tbLifetime);
6263

6364
return $trustBoundary;
6465
}

tests/TrustBoundaryTraitTest.php

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
namespace Google\Auth\Tests;
44

55
use Google\Auth\Cache\MemoryCacheItemPool;
6+
use Google\Auth\GetUniverseDomainInterface;
67
use Google\Auth\HttpHandler\HttpHandlerFactory;
78
use Google\Auth\TrustBoundaryTrait;
89
use GuzzleHttp\Client;
910
use GuzzleHttp\Handler\MockHandler;
1011
use GuzzleHttp\Psr7\Response;
1112
use PHPUnit\Framework\TestCase;
13+
use Prophecy\Argument;
14+
use Prophecy\PhpUnit\ProphecyTrait;
15+
use Psr\Cache\CacheItemInterface;
16+
use Psr\Cache\CacheItemPoolInterface;
1217

1318
class TrustBoundaryTraitTest extends TestCase
1419
{
20+
use ProphecyTrait;
21+
1522
private $impl;
1623

1724
public function setUp(): void
@@ -55,22 +62,79 @@ public function testRefreshTrustBoundaryWithCache()
5562
$cache = new MemoryCacheItemPool();
5663
$this->impl->setCache($cache);
5764
$responseBody =
58-
'{"locations": ["us-central1", "us-east1", "europe-west1", "asia-east1"], "enodedLocations": ""0xA30"}';
65+
'{"locations": ["us-central1", "us-east1", "europe-west1", "asia-east1"], "encodedLocations": "0xA30"}';
5966
$mock = new MockHandler([
6067
new Response(200, [], $responseBody),
6168
]);
6269
$handler = HttpHandlerFactory::build(new Client(['handler' => $mock]));
6370

6471
// First call, should fetch and cache
65-
$result1 = $this->impl->getTrustBoundary('universe.domain', $handler, 'default', []);
72+
$result1 = $this->impl->getTrustBoundary(
73+
GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN,
74+
$handler,
75+
'default',
76+
['authorization' => ['xyz']]
77+
);
6678
$this->assertEquals(json_decode($responseBody, true), $result1);
6779

6880
// Second call, should return from cache
6981
$mock->reset();
7082
$mock->append(new Response(500)); // This should not be called
71-
$result2 = $this->impl->getTrustBoundary('universe.domain', $handler, 'default', []);
83+
$result2 = $this->impl->getTrustBoundary(
84+
GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN,
85+
$handler,
86+
'default',
87+
[]
88+
);
7289
$this->assertEquals(json_decode($responseBody, true), $result2);
7390
}
91+
92+
public function testCacheLifetime()
93+
{
94+
$cacheItem = $this->prophesize(CacheItemInterface::class);
95+
$cacheItem->isHit()->shouldBeCalledOnce()->willReturn(false);
96+
$cacheItem->set(Argument::any())->shouldBeCalledOnce();
97+
$cacheItem->expiresAfter(6 * 60 * 60)->shouldBeCalledOnce();
98+
99+
$cache = $this->prophesize(CacheItemPoolInterface::class);
100+
$cache->getItem(Argument::type('string'))
101+
->shouldBeCalledTimes(2)
102+
->willReturn($cacheItem->reveal());
103+
$cache->save($cacheItem->reveal())->shouldBeCalledOnce();
104+
105+
$this->impl->setCache($cache->reveal());
106+
107+
$responseBody =
108+
'{"locations": ["us-central1", "us-east1", "europe-west1", "asia-east1"], "encodedLocations": "0xA30"}';
109+
$mock = new MockHandler([
110+
new Response(200, [], $responseBody),
111+
]);
112+
$handler = HttpHandlerFactory::build(new Client(['handler' => $mock]));
113+
114+
// First call, should fetch and cache
115+
$result1 = $this->impl->getTrustBoundary(
116+
GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN,
117+
$handler,
118+
'default',
119+
['authorization' => ['xyz']]
120+
);
121+
122+
$this->assertNotNull($result1);
123+
$this->assertEquals(json_decode($responseBody, true), $result1);
124+
}
125+
126+
public function testSkipLookupOutsideDefaultUniverseDomain()
127+
{
128+
// First call, should fetch and cache
129+
$result1 = $this->impl->getTrustBoundary(
130+
'universe.domain',
131+
fn () => throw new \Exception('Should not be called'),
132+
'default',
133+
['authorization' => ['xyz']]
134+
);
135+
136+
$this->assertNull($result1);
137+
}
74138
}
75139

76140
class TrustBoundaryTraitImpl
@@ -90,6 +154,7 @@ public function __construct(array $config = [])
90154
'prefix' => '',
91155
'lifetime' => 1000,
92156
];
157+
$this->enableTrustBoundary = true;
93158
}
94159

95160
public function getCacheKey()

0 commit comments

Comments
 (0)