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

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