33namespace Google \Auth \Tests ;
44
55use Google \Auth \Cache \MemoryCacheItemPool ;
6+ use Google \Auth \GetUniverseDomainInterface ;
67use Google \Auth \HttpHandler \HttpHandlerFactory ;
78use Google \Auth \TrustBoundaryTrait ;
89use GuzzleHttp \Client ;
910use GuzzleHttp \Handler \MockHandler ;
1011use GuzzleHttp \Psr7 \Response ;
1112use PHPUnit \Framework \TestCase ;
13+ use Prophecy \Argument ;
14+ use Prophecy \PhpUnit \ProphecyTrait ;
15+ use Psr \Cache \CacheItemInterface ;
16+ use Psr \Cache \CacheItemPoolInterface ;
1217
1318class 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
76140class 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