No Description

AppIndentityCredentialsTest.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\appengine\api\app_identity\AppIdentityService;
  19. // included from tests\mocks\AppIdentityService.php
  20. use Google\Auth\Credentials\AppIdentityCredentials;
  21. use PHPUnit\Framework\TestCase;
  22. class AppIdentityCredentialsOnAppEngineTest extends TestCase
  23. {
  24. public function testIsFalseByDefault()
  25. {
  26. $this->assertFalse(AppIdentityCredentials::onAppEngine());
  27. }
  28. public function testIsTrueWhenServerSoftwareIsGoogleAppEngine()
  29. {
  30. $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
  31. $this->assertTrue(AppIdentityCredentials::onAppEngine());
  32. }
  33. public function testIsTrueWhenAppEngineRuntimeIsPhp()
  34. {
  35. $_SERVER['APPENGINE_RUNTIME'] = 'php';
  36. $this->assertTrue(AppIdentityCredentials::onAppEngine());
  37. }
  38. }
  39. class AppIdentityCredentialsGetCacheKeyTest extends TestCase
  40. {
  41. public function testShouldBeEmpty()
  42. {
  43. $g = new AppIdentityCredentials();
  44. $this->assertEmpty($g->getCacheKey());
  45. }
  46. }
  47. class AppIdentityCredentialsFetchAuthTokenTest extends TestCase
  48. {
  49. public function testShouldBeEmptyIfNotOnAppEngine()
  50. {
  51. $g = new AppIdentityCredentials();
  52. $this->assertEquals(array(), $g->fetchAuthToken());
  53. }
  54. /* @expectedException */
  55. public function testThrowsExceptionIfClassDoesntExist()
  56. {
  57. $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
  58. $g = new AppIdentityCredentials();
  59. }
  60. public function testReturnsExpectedToken()
  61. {
  62. // include the mock AppIdentityService class
  63. require_once __DIR__ . '/../mocks/AppIdentityService.php';
  64. $wantedToken = [
  65. 'access_token' => '1/abdef1234567890',
  66. 'expires_in' => '57',
  67. 'token_type' => 'Bearer',
  68. ];
  69. AppIdentityService::$accessToken = $wantedToken;
  70. $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
  71. $g = new AppIdentityCredentials();
  72. $this->assertEquals($wantedToken, $g->fetchAuthToken());
  73. }
  74. public function testScopeIsAlwaysArray()
  75. {
  76. // include the mock AppIdentityService class
  77. require_once __DIR__ . '/../mocks/AppIdentityService.php';
  78. $scope1 = ['scopeA', 'scopeB'];
  79. $scope2 = 'scopeA scopeB';
  80. $scope3 = 'scopeA';
  81. $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
  82. $g = new AppIdentityCredentials($scope1);
  83. $g->fetchAuthToken();
  84. $this->assertEquals($scope1, AppIdentityService::$scope);
  85. $g = new AppIdentityCredentials($scope2);
  86. $g->fetchAuthToken();
  87. $this->assertEquals(explode(' ', $scope2), AppIdentityService::$scope);
  88. $g = new AppIdentityCredentials($scope3);
  89. $g->fetchAuthToken();
  90. $this->assertEquals([$scope3], AppIdentityService::$scope);
  91. }
  92. }