/library/Google/vendor/google/auth/tests/ApplicationDefaultCredentialsTest.php

https://gitlab.com/vangtrangbac123/zinkaiuit.tk · PHP · 299 lines · 212 code · 44 blank · 43 comment · 6 complexity · 1f4496fcccd72b5a52b3aa171a0692c4 MD5 · raw file

  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\ApplicationDefaultCredentials;
  19. use Google\Auth\Credentials\GCECredentials;
  20. use Google\Auth\Credentials\ServiceAccountCredentials;
  21. use GuzzleHttp\Psr7;
  22. class ADCGetTest extends \PHPUnit_Framework_TestCase
  23. {
  24. private $originalHome;
  25. protected function setUp()
  26. {
  27. $this->originalHome = getenv('HOME');
  28. }
  29. protected function tearDown()
  30. {
  31. if ($this->originalHome != getenv('HOME')) {
  32. putenv('HOME=' . $this->originalHome);
  33. }
  34. putenv(ServiceAccountCredentials::ENV_VAR); // removes it from
  35. }
  36. /**
  37. * @expectedException DomainException
  38. */
  39. public function testIsFailsEnvSpecifiesNonExistentFile()
  40. {
  41. $keyFile = __DIR__ . '/fixtures' . '/does-not-exist-private.json';
  42. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  43. ApplicationDefaultCredentials::getCredentials('a scope');
  44. }
  45. public function testLoadsOKIfEnvSpecifiedIsValid()
  46. {
  47. $keyFile = __DIR__ . '/fixtures' . '/private.json';
  48. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  49. $this->assertNotNull(
  50. ApplicationDefaultCredentials::getCredentials('a scope')
  51. );
  52. }
  53. public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
  54. {
  55. putenv('HOME=' . __DIR__ . '/fixtures');
  56. $this->assertNotNull(
  57. ApplicationDefaultCredentials::getCredentials('a scope')
  58. );
  59. }
  60. /**
  61. * @expectedException DomainException
  62. */
  63. public function testFailsIfNotOnGceAndNoDefaultFileFound()
  64. {
  65. putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
  66. // simulate not being GCE by return 500
  67. $httpHandler = getHandler([
  68. buildResponse(500),
  69. ]);
  70. ApplicationDefaultCredentials::getCredentials('a scope', $httpHandler);
  71. }
  72. public function testSuccedsIfNoDefaultFilesButIsOnGCE()
  73. {
  74. $wantedTokens = [
  75. 'access_token' => '1/abdef1234567890',
  76. 'expires_in' => '57',
  77. 'token_type' => 'Bearer',
  78. ];
  79. $jsonTokens = json_encode($wantedTokens);
  80. // simulate the response from GCE.
  81. $httpHandler = getHandler([
  82. buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
  83. buildResponse(200, [], Psr7\stream_for($jsonTokens)),
  84. ]);
  85. $this->assertNotNull(
  86. ApplicationDefaultCredentials::getCredentials('a scope', $httpHandler)
  87. );
  88. }
  89. }
  90. class ADCGetMiddlewareTest extends \PHPUnit_Framework_TestCase
  91. {
  92. private $originalHome;
  93. protected function setUp()
  94. {
  95. $this->originalHome = getenv('HOME');
  96. }
  97. protected function tearDown()
  98. {
  99. if ($this->originalHome != getenv('HOME')) {
  100. putenv('HOME=' . $this->originalHome);
  101. }
  102. putenv(ServiceAccountCredentials::ENV_VAR); // removes it if assigned
  103. }
  104. /**
  105. * @expectedException DomainException
  106. */
  107. public function testIsFailsEnvSpecifiesNonExistentFile()
  108. {
  109. $keyFile = __DIR__ . '/fixtures' . '/does-not-exist-private.json';
  110. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  111. ApplicationDefaultCredentials::getMiddleware('a scope');
  112. }
  113. public function testLoadsOKIfEnvSpecifiedIsValid()
  114. {
  115. $keyFile = __DIR__ . '/fixtures' . '/private.json';
  116. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  117. $this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope'));
  118. }
  119. public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
  120. {
  121. putenv('HOME=' . __DIR__ . '/fixtures');
  122. $this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope'));
  123. }
  124. /**
  125. * @expectedException DomainException
  126. */
  127. public function testFailsIfNotOnGceAndNoDefaultFileFound()
  128. {
  129. putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
  130. // simulate not being GCE by return 500
  131. $httpHandler = getHandler([
  132. buildResponse(500),
  133. ]);
  134. ApplicationDefaultCredentials::getMiddleware('a scope', $httpHandler);
  135. }
  136. public function testSuccedsIfNoDefaultFilesButIsOnGCE()
  137. {
  138. $wantedTokens = [
  139. 'access_token' => '1/abdef1234567890',
  140. 'expires_in' => '57',
  141. 'token_type' => 'Bearer',
  142. ];
  143. $jsonTokens = json_encode($wantedTokens);
  144. // simulate the response from GCE.
  145. $httpHandler = getHandler([
  146. buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
  147. buildResponse(200, [], Psr7\stream_for($jsonTokens)),
  148. ]);
  149. $this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope', $httpHandler));
  150. }
  151. }
  152. class ADCGetCredentialsAppEngineTest extends BaseTest
  153. {
  154. private $originalHome;
  155. private $originalServiceAccount;
  156. protected function setUp()
  157. {
  158. // set home to be somewhere else
  159. $this->originalHome = getenv('HOME');
  160. putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
  161. // remove service account path
  162. $this->originalServiceAccount = getenv(ServiceAccountCredentials::ENV_VAR);
  163. putenv(ServiceAccountCredentials::ENV_VAR);
  164. }
  165. protected function tearDown()
  166. {
  167. // removes it if assigned
  168. putenv('HOME=' . $this->originalHome);
  169. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $this->originalServiceAccount);
  170. }
  171. public function testAppEngineStandard()
  172. {
  173. $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
  174. $this->assertInstanceOf(
  175. 'Google\Auth\Credentials\AppIdentityCredentials',
  176. ApplicationDefaultCredentials::getCredentials()
  177. );
  178. }
  179. public function testAppEngineFlexible()
  180. {
  181. $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
  182. $_SERVER['GAE_VM'] = 'true';
  183. $httpHandler = getHandler([
  184. buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
  185. ]);
  186. $this->assertInstanceOf(
  187. 'Google\Auth\Credentials\GCECredentials',
  188. ApplicationDefaultCredentials::getCredentials(null, $httpHandler)
  189. );
  190. }
  191. }
  192. // @todo consider a way to DRY this and above class up
  193. class ADCGetSubscriberTest extends BaseTest
  194. {
  195. private $originalHome;
  196. protected function setUp()
  197. {
  198. $this->onlyGuzzle5();
  199. $this->originalHome = getenv('HOME');
  200. }
  201. protected function tearDown()
  202. {
  203. if ($this->originalHome != getenv('HOME')) {
  204. putenv('HOME=' . $this->originalHome);
  205. }
  206. putenv(ServiceAccountCredentials::ENV_VAR); // removes it if assigned
  207. }
  208. /**
  209. * @expectedException DomainException
  210. */
  211. public function testIsFailsEnvSpecifiesNonExistentFile()
  212. {
  213. $keyFile = __DIR__ . '/fixtures' . '/does-not-exist-private.json';
  214. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  215. ApplicationDefaultCredentials::getSubscriber('a scope');
  216. }
  217. public function testLoadsOKIfEnvSpecifiedIsValid()
  218. {
  219. $keyFile = __DIR__ . '/fixtures' . '/private.json';
  220. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  221. $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope'));
  222. }
  223. public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
  224. {
  225. putenv('HOME=' . __DIR__ . '/fixtures');
  226. $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope'));
  227. }
  228. /**
  229. * @expectedException DomainException
  230. */
  231. public function testFailsIfNotOnGceAndNoDefaultFileFound()
  232. {
  233. putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
  234. // simulate not being GCE by return 500
  235. $httpHandler = getHandler([
  236. buildResponse(500),
  237. ]);
  238. ApplicationDefaultCredentials::getSubscriber('a scope', $httpHandler);
  239. }
  240. public function testSuccedsIfNoDefaultFilesButIsOnGCE()
  241. {
  242. $wantedTokens = [
  243. 'access_token' => '1/abdef1234567890',
  244. 'expires_in' => '57',
  245. 'token_type' => 'Bearer',
  246. ];
  247. $jsonTokens = json_encode($wantedTokens);
  248. // simulate the response from GCE.
  249. $httpHandler = getHandler([
  250. buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
  251. buildResponse(200, [], Psr7\stream_for($jsonTokens)),
  252. ]);
  253. $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope', $httpHandler));
  254. }
  255. }