No Description

ScopedAccessTokenSubscriberTest.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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\Subscriber\ScopedAccessTokenSubscriber;
  19. use GuzzleHttp\Client;
  20. use GuzzleHttp\Event\BeforeEvent;
  21. use GuzzleHttp\Transaction;
  22. class ScopedAccessTokenSubscriberTest extends BaseTest
  23. {
  24. const TEST_SCOPE = 'https://www.googleapis.com/auth/cloud-taskqueue';
  25. private $mockCacheItem;
  26. private $mockCache;
  27. private $mockRequest;
  28. protected function setUp()
  29. {
  30. $this->onlyGuzzle5();
  31. $this->mockCacheItem =
  32. $this
  33. ->getMockBuilder('Psr\Cache\CacheItemInterface')
  34. ->getMock();
  35. $this->mockCache =
  36. $this
  37. ->getMockBuilder('Psr\Cache\CacheItemPoolInterface')
  38. ->getMock();
  39. $this->mockRequest =
  40. $this
  41. ->getMockBuilder('GuzzleHttp\Psr7\Request')
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. }
  45. /**
  46. * @expectedException InvalidArgumentException
  47. */
  48. public function testRequiresScopeAsAStringOrArray()
  49. {
  50. $fakeAuthFunc = function ($unused_scopes) {
  51. return '1/abcdef1234567890';
  52. };
  53. new ScopedAccessTokenSubscriber($fakeAuthFunc, new \stdClass(), array());
  54. }
  55. public function testSubscribesToEvents()
  56. {
  57. $fakeAuthFunc = function ($unused_scopes) {
  58. return '1/abcdef1234567890';
  59. };
  60. $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array());
  61. $this->assertArrayHasKey('before', $s->getEvents());
  62. }
  63. public function testAddsTheTokenAsAnAuthorizationHeader()
  64. {
  65. $fakeAuthFunc = function ($unused_scopes) {
  66. return '1/abcdef1234567890';
  67. };
  68. $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array());
  69. $client = new Client();
  70. $request = $client->createRequest('GET', 'http://testing.org',
  71. ['auth' => 'scoped']);
  72. $before = new BeforeEvent(new Transaction($client, $request));
  73. $s->onBefore($before);
  74. $this->assertSame(
  75. 'Bearer 1/abcdef1234567890',
  76. $request->getHeader('authorization')
  77. );
  78. }
  79. public function testUsesCachedAuthToken()
  80. {
  81. $cachedValue = '2/abcdef1234567890';
  82. $fakeAuthFunc = function ($unused_scopes) {
  83. return '';
  84. };
  85. $this->mockCacheItem
  86. ->expects($this->once())
  87. ->method('isHit')
  88. ->will($this->returnValue(true));
  89. $this->mockCacheItem
  90. ->expects($this->once())
  91. ->method('get')
  92. ->will($this->returnValue($cachedValue));
  93. $this->mockCache
  94. ->expects($this->once())
  95. ->method('getItem')
  96. ->with($this->getValidKeyName(self::TEST_SCOPE))
  97. ->will($this->returnValue($this->mockCacheItem));
  98. // Run the test
  99. $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array(),
  100. $this->mockCache);
  101. $client = new Client();
  102. $request = $client->createRequest('GET', 'http://testing.org',
  103. ['auth' => 'scoped']);
  104. $before = new BeforeEvent(new Transaction($client, $request));
  105. $s->onBefore($before);
  106. $this->assertSame(
  107. 'Bearer 2/abcdef1234567890',
  108. $request->getHeader('authorization')
  109. );
  110. }
  111. public function testGetsCachedAuthTokenUsingCachePrefix()
  112. {
  113. $prefix = 'test_prefix_';
  114. $cachedValue = '2/abcdef1234567890';
  115. $fakeAuthFunc = function ($unused_scopes) {
  116. return '';
  117. };
  118. $this->mockCacheItem
  119. ->expects($this->once())
  120. ->method('isHit')
  121. ->will($this->returnValue(true));
  122. $this->mockCacheItem
  123. ->expects($this->once())
  124. ->method('get')
  125. ->will($this->returnValue($cachedValue));
  126. $this->mockCache
  127. ->expects($this->once())
  128. ->method('getItem')
  129. ->with($prefix . $this->getValidKeyName(self::TEST_SCOPE))
  130. ->will($this->returnValue($this->mockCacheItem));
  131. // Run the test
  132. $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE,
  133. ['prefix' => $prefix],
  134. $this->mockCache);
  135. $client = new Client();
  136. $request = $client->createRequest('GET', 'http://testing.org',
  137. ['auth' => 'scoped']);
  138. $before = new BeforeEvent(new Transaction($client, $request));
  139. $s->onBefore($before);
  140. $this->assertSame(
  141. 'Bearer 2/abcdef1234567890',
  142. $request->getHeader('authorization')
  143. );
  144. }
  145. public function testShouldSaveValueInCache()
  146. {
  147. $token = '2/abcdef1234567890';
  148. $fakeAuthFunc = function ($unused_scopes) {
  149. return '2/abcdef1234567890';
  150. };
  151. $this->mockCacheItem
  152. ->expects($this->once())
  153. ->method('isHit')
  154. ->will($this->returnValue(false));
  155. $this->mockCacheItem
  156. ->expects($this->once())
  157. ->method('set')
  158. ->with($this->equalTo($token))
  159. ->will($this->returnValue(false));
  160. $this->mockCache
  161. ->expects($this->exactly(2))
  162. ->method('getItem')
  163. ->with($this->getValidKeyName(self::TEST_SCOPE))
  164. ->will($this->returnValue($this->mockCacheItem));
  165. $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array(),
  166. $this->mockCache);
  167. $client = new Client();
  168. $request = $client->createRequest('GET', 'http://testing.org',
  169. ['auth' => 'scoped']);
  170. $before = new BeforeEvent(new Transaction($client, $request));
  171. $s->onBefore($before);
  172. $this->assertSame(
  173. 'Bearer 2/abcdef1234567890',
  174. $request->getHeader('authorization')
  175. );
  176. }
  177. public function testShouldSaveValueInCacheWithCacheOptions()
  178. {
  179. $token = '2/abcdef1234567890';
  180. $prefix = 'test_prefix_';
  181. $lifetime = '70707';
  182. $fakeAuthFunc = function ($unused_scopes) {
  183. return '2/abcdef1234567890';
  184. };
  185. $this->mockCacheItem
  186. ->expects($this->once())
  187. ->method('isHit')
  188. ->will($this->returnValue(false));
  189. $this->mockCacheItem
  190. ->expects($this->once())
  191. ->method('set')
  192. ->with($this->equalTo($token));
  193. $this->mockCacheItem
  194. ->expects($this->once())
  195. ->method('expiresAfter')
  196. ->with($this->equalTo($lifetime));
  197. $this->mockCache
  198. ->expects($this->exactly(2))
  199. ->method('getItem')
  200. ->with($prefix . $this->getValidKeyName(self::TEST_SCOPE))
  201. ->will($this->returnValue($this->mockCacheItem));
  202. // Run the test
  203. $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE,
  204. ['prefix' => $prefix, 'lifetime' => $lifetime],
  205. $this->mockCache);
  206. $client = new Client();
  207. $request = $client->createRequest('GET', 'http://testing.org',
  208. ['auth' => 'scoped']);
  209. $before = new BeforeEvent(new Transaction($client, $request));
  210. $s->onBefore($before);
  211. $this->assertSame(
  212. 'Bearer 2/abcdef1234567890',
  213. $request->getHeader('authorization')
  214. );
  215. }
  216. public function testOnlyTouchesWhenAuthConfigScoped()
  217. {
  218. $fakeAuthFunc = function ($unused_scopes) {
  219. return '1/abcdef1234567890';
  220. };
  221. $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array());
  222. $client = new Client();
  223. $request = $client->createRequest('GET', 'http://testing.org',
  224. ['auth' => 'notscoped']);
  225. $before = new BeforeEvent(new Transaction($client, $request));
  226. $s->onBefore($before);
  227. $this->assertSame('', $request->getHeader('authorization'));
  228. }
  229. }