PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Japang-Jawara/jawara-penilaian
PHP | 253 lines | 175 code | 38 blank | 40 comment | 6 complexity | 9946af64be9f62eb99c5debdef66869d 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. // @todo consider a way to DRY this and above class up
  153. class ADCGetSubscriberTest extends BaseTest
  154. {
  155. private $originalHome;
  156. protected function setUp()
  157. {
  158. $this->onlyGuzzle5();
  159. $this->originalHome = getenv('HOME');
  160. }
  161. protected function tearDown()
  162. {
  163. if ($this->originalHome != getenv('HOME')) {
  164. putenv('HOME=' . $this->originalHome);
  165. }
  166. putenv(ServiceAccountCredentials::ENV_VAR); // removes it if assigned
  167. }
  168. /**
  169. * @expectedException DomainException
  170. */
  171. public function testIsFailsEnvSpecifiesNonExistentFile()
  172. {
  173. $keyFile = __DIR__ . '/fixtures' . '/does-not-exist-private.json';
  174. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  175. ApplicationDefaultCredentials::getSubscriber('a scope');
  176. }
  177. public function testLoadsOKIfEnvSpecifiedIsValid()
  178. {
  179. $keyFile = __DIR__ . '/fixtures' . '/private.json';
  180. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  181. $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope'));
  182. }
  183. public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
  184. {
  185. putenv('HOME=' . __DIR__ . '/fixtures');
  186. $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope'));
  187. }
  188. /**
  189. * @expectedException DomainException
  190. */
  191. public function testFailsIfNotOnGceAndNoDefaultFileFound()
  192. {
  193. putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
  194. // simulate not being GCE by return 500
  195. $httpHandler = getHandler([
  196. buildResponse(500),
  197. ]);
  198. ApplicationDefaultCredentials::getSubscriber('a scope', $httpHandler);
  199. }
  200. public function testSuccedsIfNoDefaultFilesButIsOnGCE()
  201. {
  202. $wantedTokens = [
  203. 'access_token' => '1/abdef1234567890',
  204. 'expires_in' => '57',
  205. 'token_type' => 'Bearer',
  206. ];
  207. $jsonTokens = json_encode($wantedTokens);
  208. // simulate the response from GCE.
  209. $httpHandler = getHandler([
  210. buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
  211. buildResponse(200, [], Psr7\stream_for($jsonTokens)),
  212. ]);
  213. $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope', $httpHandler));
  214. }
  215. }