/library/Google/vendor/google/auth/tests/Credentials/UserRefreshCredentialsTest.php

https://gitlab.com/vangtrangbac123/zinkaiuit.tk · PHP · 228 lines · 170 code · 21 blank · 37 comment · 2 complexity · b54a56acaf8d4872a6e72966ec84e66f 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\UserRefreshCredentials;
  20. use Google\Auth\OAuth2;
  21. use GuzzleHttp\Psr7;
  22. // Creates a standard JSON auth object for testing.
  23. function createURCTestJson()
  24. {
  25. return [
  26. 'client_id' => 'client123',
  27. 'client_secret' => 'clientSecret123',
  28. 'refresh_token' => 'refreshToken123',
  29. 'type' => 'authorized_user',
  30. ];
  31. }
  32. class URCGetCacheKeyTest extends \PHPUnit_Framework_TestCase
  33. {
  34. public function testShouldBeTheSameAsOAuth2WithTheSameScope()
  35. {
  36. $testJson = createURCTestJson();
  37. $scope = ['scope/1', 'scope/2'];
  38. $sa = new UserRefreshCredentials(
  39. $scope,
  40. $testJson);
  41. $o = new OAuth2(['scope' => $scope]);
  42. $this->assertSame(
  43. $testJson['client_id'] . ':' . $o->getCacheKey(),
  44. $sa->getCacheKey()
  45. );
  46. }
  47. }
  48. class URCConstructorTest extends \PHPUnit_Framework_TestCase
  49. {
  50. /**
  51. * @expectedException InvalidArgumentException
  52. */
  53. public function testShouldFailIfScopeIsNotAValidType()
  54. {
  55. $testJson = createURCTestJson();
  56. $notAnArrayOrString = new \stdClass();
  57. $sa = new UserRefreshCredentials(
  58. $notAnArrayOrString,
  59. $testJson
  60. );
  61. }
  62. /**
  63. * @expectedException InvalidArgumentException
  64. */
  65. public function testShouldFailIfJsonDoesNotHaveClientSecret()
  66. {
  67. $testJson = createURCTestJson();
  68. unset($testJson['client_secret']);
  69. $scope = ['scope/1', 'scope/2'];
  70. $sa = new UserRefreshCredentials(
  71. $scope,
  72. $testJson
  73. );
  74. }
  75. /**
  76. * @expectedException InvalidArgumentException
  77. */
  78. public function testShouldFailIfJsonDoesNotHaveRefreshToken()
  79. {
  80. $testJson = createURCTestJson();
  81. unset($testJson['refresh_token']);
  82. $scope = ['scope/1', 'scope/2'];
  83. $sa = new UserRefreshCredentials(
  84. $scope,
  85. $testJson
  86. );
  87. }
  88. /**
  89. * @expectedException InvalidArgumentException
  90. */
  91. public function testFailsToInitalizeFromANonExistentFile()
  92. {
  93. $keyFile = __DIR__ . '/../fixtures' . '/does-not-exist-private.json';
  94. new UserRefreshCredentials('scope/1', $keyFile);
  95. }
  96. public function testInitalizeFromAFile()
  97. {
  98. $keyFile = __DIR__ . '/../fixtures2' . '/private.json';
  99. $this->assertNotNull(
  100. new UserRefreshCredentials('scope/1', $keyFile)
  101. );
  102. }
  103. }
  104. class URCFromEnvTest extends \PHPUnit_Framework_TestCase
  105. {
  106. protected function tearDown()
  107. {
  108. putenv(UserRefreshCredentials::ENV_VAR); // removes it from
  109. }
  110. public function testIsNullIfEnvVarIsNotSet()
  111. {
  112. $this->assertNull(UserRefreshCredentials::fromEnv('a scope'));
  113. }
  114. /**
  115. * @expectedException DomainException
  116. */
  117. public function testFailsIfEnvSpecifiesNonExistentFile()
  118. {
  119. $keyFile = __DIR__ . '/../fixtures' . '/does-not-exist-private.json';
  120. putenv(UserRefreshCredentials::ENV_VAR . '=' . $keyFile);
  121. UserRefreshCredentials::fromEnv('a scope');
  122. }
  123. public function testSucceedIfFileExists()
  124. {
  125. $keyFile = __DIR__ . '/../fixtures2' . '/private.json';
  126. putenv(UserRefreshCredentials::ENV_VAR . '=' . $keyFile);
  127. $this->assertNotNull(ApplicationDefaultCredentials::getCredentials('a scope'));
  128. }
  129. }
  130. class URCFromWellKnownFileTest extends \PHPUnit_Framework_TestCase
  131. {
  132. private $originalHome;
  133. protected function setUp()
  134. {
  135. $this->originalHome = getenv('HOME');
  136. }
  137. protected function tearDown()
  138. {
  139. if ($this->originalHome != getenv('HOME')) {
  140. putenv('HOME=' . $this->originalHome);
  141. }
  142. }
  143. public function testIsNullIfFileDoesNotExist()
  144. {
  145. putenv('HOME=' . __DIR__ . '/../not_exist_fixtures');
  146. $this->assertNull(
  147. UserRefreshCredentials::fromWellKnownFile('a scope')
  148. );
  149. }
  150. public function testSucceedIfFileIsPresent()
  151. {
  152. putenv('HOME=' . __DIR__ . '/../fixtures2');
  153. $this->assertNotNull(
  154. ApplicationDefaultCredentials::getCredentials('a scope')
  155. );
  156. }
  157. }
  158. class URCFetchAuthTokenTest extends \PHPUnit_Framework_TestCase
  159. {
  160. /**
  161. * @expectedException GuzzleHttp\Exception\ClientException
  162. */
  163. public function testFailsOnClientErrors()
  164. {
  165. $testJson = createURCTestJson();
  166. $scope = ['scope/1', 'scope/2'];
  167. $httpHandler = getHandler([
  168. buildResponse(400),
  169. ]);
  170. $sa = new UserRefreshCredentials(
  171. $scope,
  172. $testJson
  173. );
  174. $sa->fetchAuthToken($httpHandler);
  175. }
  176. /**
  177. * @expectedException GuzzleHttp\Exception\ServerException
  178. */
  179. public function testFailsOnServerErrors()
  180. {
  181. $testJson = createURCTestJson();
  182. $scope = ['scope/1', 'scope/2'];
  183. $httpHandler = getHandler([
  184. buildResponse(500),
  185. ]);
  186. $sa = new UserRefreshCredentials(
  187. $scope,
  188. $testJson
  189. );
  190. $sa->fetchAuthToken($httpHandler);
  191. }
  192. public function testCanFetchCredsOK()
  193. {
  194. $testJson = createURCTestJson();
  195. $testJsonText = json_encode($testJson);
  196. $scope = ['scope/1', 'scope/2'];
  197. $httpHandler = getHandler([
  198. buildResponse(200, [], Psr7\stream_for($testJsonText)),
  199. ]);
  200. $sa = new UserRefreshCredentials(
  201. $scope,
  202. $testJson
  203. );
  204. $tokens = $sa->fetchAuthToken($httpHandler);
  205. $this->assertEquals($testJson, $tokens);
  206. }
  207. }