No Description

UserRefreshCredentialsTest.php 6.1KB

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