wp-stateless
/
lib
/
Google
/
vendor
/
google
/
auth
/
tests
/
Credentials
/
AppIndentityCredentialsTest.php
AppIndentityCredentialsTest.php in WP-Stateless – Google Cloud Storage 2.1.4, at lib/Google/vendor/google/auth/tests/Credentials/AppIndentityCredentialsTest.php
| 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 | |
| 18 | namespace Google\Auth\Tests; |
| 19 | |
| 20 | use Google\Auth\Credentials\AppIdentityCredentials; |
| 21 | use GuzzleHttp\Psr7\Response; |
| 22 | |
| 23 | // included from tests\mocks\AppIdentityService.php |
| 24 | use google\appengine\api\app_identity\AppIdentityService; |
| 25 | |
| 26 | class AppIdentityCredentialsOnAppEngineTest extends \PHPUnit_Framework_TestCase |
| 27 | { |
| 28 | public function testIsFalseByDefault() |
| 29 | { |
| 30 | $this->assertFalse(AppIdentityCredentials::onAppEngine()); |
| 31 | } |
| 32 | |
| 33 | public function testIsTrueWhenServerSoftwareIsGoogleAppEngine() |
| 34 | { |
| 35 | $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; |
| 36 | $this->assertTrue(AppIdentityCredentials::onAppEngine()); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | class AppIdentityCredentialsGetCacheKeyTest extends \PHPUnit_Framework_TestCase |
| 41 | { |
| 42 | public function testShouldNotBeEmpty() |
| 43 | { |
| 44 | $g = new AppIdentityCredentials(); |
| 45 | $this->assertNotEmpty($g->getCacheKey()); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | class AppIdentityCredentialsFetchAuthTokenTest extends \PHPUnit_Framework_TestCase |
| 50 | { |
| 51 | public function testShouldBeEmptyIfNotOnAppEngine() |
| 52 | { |
| 53 | $g = new AppIdentityCredentials(); |
| 54 | $this->assertEquals(array(), $g->fetchAuthToken()); |
| 55 | } |
| 56 | |
| 57 | /* @expectedException */ |
| 58 | public function testThrowsExceptionIfClassDoesntExist() |
| 59 | { |
| 60 | $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; |
| 61 | $g = new AppIdentityCredentials(); |
| 62 | } |
| 63 | |
| 64 | public function testReturnsExpectedToken() |
| 65 | { |
| 66 | // include the mock AppIdentityService class |
| 67 | require_once __DIR__ . '/../mocks/AppIdentityService.php'; |
| 68 | |
| 69 | $wantedToken = [ |
| 70 | 'access_token' => '1/abdef1234567890', |
| 71 | 'expires_in' => '57', |
| 72 | 'token_type' => 'Bearer', |
| 73 | ]; |
| 74 | |
| 75 | AppIdentityService::$accessToken = $wantedToken; |
| 76 | |
| 77 | $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; |
| 78 | |
| 79 | $g = new AppIdentityCredentials(); |
| 80 | $this->assertEquals($wantedToken, $g->fetchAuthToken()); |
| 81 | } |
| 82 | |
| 83 | public function testScopeIsAlwaysArray() |
| 84 | { |
| 85 | // include the mock AppIdentityService class |
| 86 | require_once __DIR__ . '/../mocks/AppIdentityService.php'; |
| 87 | |
| 88 | $scope1 = ['scopeA', 'scopeB']; |
| 89 | $scope2 = 'scopeA scopeB'; |
| 90 | $scope3 = 'scopeA'; |
| 91 | |
| 92 | $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; |
| 93 | |
| 94 | $g = new AppIdentityCredentials($scope1); |
| 95 | $g->fetchAuthToken(); |
| 96 | $this->assertEquals($scope1, AppIdentityService::$scope); |
| 97 | |
| 98 | $g = new AppIdentityCredentials($scope2); |
| 99 | $g->fetchAuthToken(); |
| 100 | $this->assertEquals(explode(' ', $scope2), AppIdentityService::$scope); |
| 101 | |
| 102 | $g = new AppIdentityCredentials($scope3); |
| 103 | $g->fetchAuthToken(); |
| 104 | $this->assertEquals([$scope3], AppIdentityService::$scope); |
| 105 | } |
| 106 | } |
| 107 |