No Description

AuthTokenSubscriberTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /*
  3. * Copyright 2015 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Google\Auth\Tests;
  18. use Google\Auth\FetchAuthTokenCache;
  19. use Google\Auth\Subscriber\AuthTokenSubscriber;
  20. use GuzzleHttp\Client;
  21. use GuzzleHttp\Event\BeforeEvent;
  22. use GuzzleHttp\Transaction;
  23. class AuthTokenSubscriberTest extends BaseTest
  24. {
  25. private $mockFetcher;
  26. private $mockCacheItem;
  27. private $mockCache;
  28. protected function setUp()
  29. {
  30. $this->onlyGuzzle5();
  31. $this->mockFetcher =
  32. $this
  33. ->getMockBuilder('Google\Auth\FetchAuthTokenInterface')
  34. ->getMock();
  35. $this->mockCacheItem =
  36. $this
  37. ->getMockBuilder('Psr\Cache\CacheItemInterface')
  38. ->getMock();
  39. $this->mockCache =
  40. $this
  41. ->getMockBuilder('Psr\Cache\CacheItemPoolInterface')
  42. ->getMock();
  43. }
  44. public function testSubscribesToEvents()
  45. {
  46. $a = new AuthTokenSubscriber($this->mockFetcher);
  47. $this->assertArrayHasKey('before', $a->getEvents());
  48. }
  49. public function testOnlyTouchesWhenAuthConfigScoped()
  50. {
  51. $s = new AuthTokenSubscriber($this->mockFetcher);
  52. $client = new Client();
  53. $request = $client->createRequest('GET', 'http://testing.org',
  54. ['auth' => 'not_google_auth']);
  55. $before = new BeforeEvent(new Transaction($client, $request));
  56. $s->onBefore($before);
  57. $this->assertSame($request->getHeader('authorization'), '');
  58. }
  59. public function testAddsTheTokenAsAnAuthorizationHeader()
  60. {
  61. $authResult = ['access_token' => '1/abcdef1234567890'];
  62. $this->mockFetcher
  63. ->expects($this->once())
  64. ->method('fetchAuthToken')
  65. ->will($this->returnValue($authResult));
  66. // Run the test.
  67. $a = new AuthTokenSubscriber($this->mockFetcher);
  68. $client = new Client();
  69. $request = $client->createRequest('GET', 'http://testing.org',
  70. ['auth' => 'google_auth']);
  71. $before = new BeforeEvent(new Transaction($client, $request));
  72. $a->onBefore($before);
  73. $this->assertSame($request->getHeader('authorization'),
  74. 'Bearer 1/abcdef1234567890');
  75. }
  76. public function testDoesNotAddAnAuthorizationHeaderOnNoAccessToken()
  77. {
  78. $authResult = ['not_access_token' => '1/abcdef1234567890'];
  79. $this->mockFetcher
  80. ->expects($this->once())
  81. ->method('fetchAuthToken')
  82. ->will($this->returnValue($authResult));
  83. // Run the test.
  84. $a = new AuthTokenSubscriber($this->mockFetcher);
  85. $client = new Client();
  86. $request = $client->createRequest('GET', 'http://testing.org',
  87. ['auth' => 'google_auth']);
  88. $before = new BeforeEvent(new Transaction($client, $request));
  89. $a->onBefore($before);
  90. $this->assertSame($request->getHeader('authorization'), '');
  91. }
  92. public function testUsesCachedAuthToken()
  93. {
  94. $cacheKey = 'myKey';
  95. $cachedValue = '2/abcdef1234567890';
  96. $this->mockCacheItem
  97. ->expects($this->once())
  98. ->method('isHit')
  99. ->will($this->returnValue(true));
  100. $this->mockCacheItem
  101. ->expects($this->once())
  102. ->method('get')
  103. ->will($this->returnValue($cachedValue));
  104. $this->mockCache
  105. ->expects($this->once())
  106. ->method('getItem')
  107. ->with($this->equalTo($cacheKey))
  108. ->will($this->returnValue($this->mockCacheItem));
  109. $this->mockFetcher
  110. ->expects($this->never())
  111. ->method('fetchAuthToken');
  112. $this->mockFetcher
  113. ->expects($this->any())
  114. ->method('getCacheKey')
  115. ->will($this->returnValue($cacheKey));
  116. // Run the test.
  117. $cachedFetcher = new FetchAuthTokenCache(
  118. $this->mockFetcher,
  119. null,
  120. $this->mockCache
  121. );
  122. $a = new AuthTokenSubscriber($cachedFetcher);
  123. $client = new Client();
  124. $request = $client->createRequest('GET', 'http://testing.org',
  125. ['auth' => 'google_auth']);
  126. $before = new BeforeEvent(new Transaction($client, $request));
  127. $a->onBefore($before);
  128. $this->assertSame($request->getHeader('authorization'),
  129. 'Bearer 2/abcdef1234567890');
  130. }
  131. public function testGetsCachedAuthTokenUsingCachePrefix()
  132. {
  133. $prefix = 'test_prefix_';
  134. $cacheKey = 'myKey';
  135. $cachedValue = '2/abcdef1234567890';
  136. $this->mockCacheItem
  137. ->expects($this->once())
  138. ->method('isHit')
  139. ->will($this->returnValue(true));
  140. $this->mockCacheItem
  141. ->expects($this->once())
  142. ->method('get')
  143. ->will($this->returnValue($cachedValue));
  144. $this->mockCache
  145. ->expects($this->once())
  146. ->method('getItem')
  147. ->with($this->equalTo($prefix . $cacheKey))
  148. ->will($this->returnValue($this->mockCacheItem));
  149. $this->mockFetcher
  150. ->expects($this->never())
  151. ->method('fetchAuthToken');
  152. $this->mockFetcher
  153. ->expects($this->any())
  154. ->method('getCacheKey')
  155. ->will($this->returnValue($cacheKey));
  156. // Run the test
  157. $cachedFetcher = new FetchAuthTokenCache(
  158. $this->mockFetcher,
  159. ['prefix' => $prefix],
  160. $this->mockCache
  161. );
  162. $a = new AuthTokenSubscriber($cachedFetcher);
  163. $client = new Client();
  164. $request = $client->createRequest('GET', 'http://testing.org',
  165. ['auth' => 'google_auth']);
  166. $before = new BeforeEvent(new Transaction($client, $request));
  167. $a->onBefore($before);
  168. $this->assertSame($request->getHeader('authorization'),
  169. 'Bearer 2/abcdef1234567890');
  170. }
  171. public function testShouldSaveValueInCacheWithCacheOptions()
  172. {
  173. $prefix = 'test_prefix_';
  174. $lifetime = '70707';
  175. $cacheKey = 'myKey';
  176. $token = '1/abcdef1234567890';
  177. $authResult = ['access_token' => $token];
  178. $this->mockCacheItem
  179. ->expects($this->any())
  180. ->method('get')
  181. ->will($this->returnValue(null));
  182. $this->mockCacheItem
  183. ->expects($this->once())
  184. ->method('set')
  185. ->with($this->equalTo($token))
  186. ->will($this->returnValue(false));
  187. $this->mockCacheItem
  188. ->expects($this->once())
  189. ->method('expiresAfter')
  190. ->with($this->equalTo($lifetime));
  191. $this->mockCache
  192. ->expects($this->exactly(2))
  193. ->method('getItem')
  194. ->with($this->equalTo($prefix . $cacheKey))
  195. ->will($this->returnValue($this->mockCacheItem));
  196. $this->mockFetcher
  197. ->expects($this->any())
  198. ->method('getCacheKey')
  199. ->will($this->returnValue($cacheKey));
  200. $this->mockFetcher
  201. ->expects($this->once())
  202. ->method('fetchAuthToken')
  203. ->will($this->returnValue($authResult));
  204. // Run the test
  205. $cachedFetcher = new FetchAuthTokenCache(
  206. $this->mockFetcher,
  207. ['prefix' => $prefix, 'lifetime' => $lifetime],
  208. $this->mockCache
  209. );
  210. $a = new AuthTokenSubscriber($cachedFetcher);
  211. $client = new Client();
  212. $request = $client->createRequest('GET', 'http://testing.org',
  213. ['auth' => 'google_auth']);
  214. $before = new BeforeEvent(new Transaction($client, $request));
  215. $a->onBefore($before);
  216. $this->assertSame($request->getHeader('authorization'),
  217. 'Bearer 1/abcdef1234567890');
  218. }
  219. /** @dataProvider provideShouldNotifyTokenCallback */
  220. public function testShouldNotifyTokenCallback(callable $tokenCallback)
  221. {
  222. $prefix = 'test_prefix_';
  223. $cacheKey = 'myKey';
  224. $token = '1/abcdef1234567890';
  225. $authResult = ['access_token' => $token];
  226. $this->mockCacheItem
  227. ->expects($this->any())
  228. ->method('get')
  229. ->will($this->returnValue(null));
  230. $this->mockCache
  231. ->expects($this->any())
  232. ->method('getItem')
  233. ->with($this->equalTo($prefix . $cacheKey))
  234. ->will($this->returnValue($this->mockCacheItem));
  235. $this->mockFetcher
  236. ->expects($this->any())
  237. ->method('getCacheKey')
  238. ->will($this->returnValue($cacheKey));
  239. $this->mockFetcher
  240. ->expects($this->once())
  241. ->method('fetchAuthToken')
  242. ->will($this->returnValue($authResult));
  243. SubscriberCallback::$expectedKey = $this->getValidKeyName($prefix . $cacheKey);
  244. SubscriberCallback::$expectedValue = $token;
  245. SubscriberCallback::$called = false;
  246. // Run the test
  247. $cachedFetcher = new FetchAuthTokenCache(
  248. $this->mockFetcher,
  249. ['prefix' => $prefix],
  250. $this->mockCache
  251. );
  252. $a = new AuthTokenSubscriber(
  253. $cachedFetcher,
  254. null,
  255. $tokenCallback
  256. );
  257. $client = new Client();
  258. $request = $client->createRequest('GET', 'http://testing.org',
  259. ['auth' => 'google_auth']);
  260. $before = new BeforeEvent(new Transaction($client, $request));
  261. $a->onBefore($before);
  262. $this->assertTrue(SubscriberCallback::$called);
  263. }
  264. public function provideShouldNotifyTokenCallback()
  265. {
  266. SubscriberCallback::$phpunit = $this;
  267. $anonymousFunc = function ($key, $value) {
  268. SubscriberCallback::staticInvoke($key, $value);
  269. };
  270. return [
  271. ['Google\Auth\Tests\SubscriberCallbackFunction'],
  272. ['Google\Auth\Tests\SubscriberCallback::staticInvoke'],
  273. [['Google\Auth\Tests\SubscriberCallback', 'staticInvoke']],
  274. [$anonymousFunc],
  275. [[new SubscriberCallback, 'staticInvoke']],
  276. [[new SubscriberCallback, 'methodInvoke']],
  277. [new SubscriberCallback],
  278. ];
  279. }
  280. }
  281. class SubscriberCallback
  282. {
  283. public static $phpunit;
  284. public static $expectedKey;
  285. public static $expectedValue;
  286. public static $called = false;
  287. public function __invoke($key, $value)
  288. {
  289. self::$phpunit->assertEquals(self::$expectedKey, $key);
  290. self::$phpunit->assertEquals(self::$expectedValue, $value);
  291. self::$called = true;
  292. }
  293. public function methodInvoke($key, $value)
  294. {
  295. return $this($key, $value);
  296. }
  297. public static function staticInvoke($key, $value)
  298. {
  299. $instance = new self();
  300. return $instance($key, $value);
  301. }
  302. }
  303. function SubscriberCallbackFunction($key, $value)
  304. {
  305. return SubscriberCallback::staticInvoke($key, $value);
  306. }