No Description

ScopedAccessTokenMiddlewareTest.php 8.5KB

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