No Description

GCECredentialsTest.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Credentials\GCECredentials;
  19. use GuzzleHttp\Psr7;
  20. use GuzzleHttp\Psr7\Response;
  21. use PHPUnit\Framework\TestCase;
  22. class GCECredentialsOnGCETest extends TestCase
  23. {
  24. public function testIsFalseOnClientErrorStatus()
  25. {
  26. $httpHandler = getHandler([
  27. buildResponse(400),
  28. ]);
  29. $this->assertFalse(GCECredentials::onGCE($httpHandler));
  30. }
  31. public function testIsFalseOnServerErrorStatus()
  32. {
  33. $httpHandler = getHandler([
  34. buildResponse(500),
  35. ]);
  36. $this->assertFalse(GCECredentials::onGCE($httpHandler));
  37. }
  38. public function testIsFalseOnOkStatusWithoutExpectedHeader()
  39. {
  40. $httpHandler = getHandler([
  41. buildResponse(200),
  42. ]);
  43. $this->assertFalse(GCECredentials::onGCE($httpHandler));
  44. }
  45. public function testIsOkIfGoogleIsTheFlavor()
  46. {
  47. $httpHandler = getHandler([
  48. buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
  49. ]);
  50. $this->assertTrue(GCECredentials::onGCE($httpHandler));
  51. }
  52. }
  53. class GCECredentialsOnAppEngineFlexibleTest extends TestCase
  54. {
  55. public function testIsFalseByDefault()
  56. {
  57. $this->assertFalse(GCECredentials::onAppEngineFlexible());
  58. }
  59. public function testIsTrueWhenGaeInstanceHasAefPrefix()
  60. {
  61. putenv('GAE_INSTANCE=aef-default-20180313t154438');
  62. $this->assertTrue(GCECredentials::onAppEngineFlexible());
  63. }
  64. protected function tearDown()
  65. {
  66. // removes it if assigned
  67. putenv('GAE_INSTANCE');
  68. }
  69. }
  70. class GCECredentialsGetCacheKeyTest extends TestCase
  71. {
  72. public function testShouldNotBeEmpty()
  73. {
  74. $g = new GCECredentials();
  75. $this->assertNotEmpty($g->getCacheKey());
  76. }
  77. }
  78. class GCECredentialsFetchAuthTokenTest extends TestCase
  79. {
  80. public function testShouldBeEmptyIfNotOnGCE()
  81. {
  82. $httpHandler = getHandler([
  83. buildResponse(500),
  84. ]);
  85. $g = new GCECredentials();
  86. $this->assertEquals(array(), $g->fetchAuthToken($httpHandler));
  87. }
  88. /**
  89. * @expectedException Exception
  90. * @expectedExceptionMessage Invalid JSON response
  91. */
  92. public function testShouldFailIfResponseIsNotJson()
  93. {
  94. $notJson = '{"foo": , this is cannot be passed as json" "bar"}';
  95. $httpHandler = getHandler([
  96. buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
  97. buildResponse(200, [], $notJson),
  98. ]);
  99. $g = new GCECredentials();
  100. $g->fetchAuthToken($httpHandler);
  101. }
  102. public function testShouldReturnTokenInfo()
  103. {
  104. $wantedTokens = [
  105. 'access_token' => '1/abdef1234567890',
  106. 'expires_in' => '57',
  107. 'token_type' => 'Bearer',
  108. ];
  109. $jsonTokens = json_encode($wantedTokens);
  110. $httpHandler = getHandler([
  111. buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
  112. buildResponse(200, [], Psr7\stream_for($jsonTokens)),
  113. ]);
  114. $g = new GCECredentials();
  115. $this->assertEquals($wantedTokens, $g->fetchAuthToken($httpHandler));
  116. $this->assertEquals(time() + 57, $g->getLastReceivedToken()['expires_at']);
  117. }
  118. }