| 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\GCECredentials; |
| 21 |
use Google\Auth\HttpHandler\Guzzle6HttpHandler; |
| 22 |
use GuzzleHttp\Client; |
| 23 |
use GuzzleHttp\Psr7; |
| 24 |
use GuzzleHttp\Psr7\Response; |
| 25 |
|
| 26 |
class GCECredentialsOnGCETest extends \PHPUnit_Framework_TestCase |
| 27 |
{ |
| 28 |
public function testIsFalseOnClientErrorStatus() |
| 29 |
{ |
| 30 |
$httpHandler = getHandler([ |
| 31 |
buildResponse(400) |
| 32 |
]); |
| 33 |
$this->assertFalse(GCECredentials::onGCE($httpHandler)); |
| 34 |
} |
| 35 |
|
| 36 |
public function testIsFalseOnServerErrorStatus() |
| 37 |
{ |
| 38 |
$httpHandler = getHandler([ |
| 39 |
buildResponse(500) |
| 40 |
]); |
| 41 |
$this->assertFalse(GCECredentials::onGCE($httpHandler)); |
| 42 |
} |
| 43 |
|
| 44 |
public function testIsFalseOnOkStatusWithoutExpectedHeader() |
| 45 |
{ |
| 46 |
$httpHandler = getHandler([ |
| 47 |
buildResponse(200) |
| 48 |
]); |
| 49 |
$this->assertFalse(GCECredentials::onGCE($httpHandler)); |
| 50 |
} |
| 51 |
|
| 52 |
public function testIsOkIfGoogleIsTheFlavor() |
| 53 |
{ |
| 54 |
$httpHandler = getHandler([ |
| 55 |
buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']) |
| 56 |
]); |
| 57 |
$this->assertTrue(GCECredentials::onGCE($httpHandler)); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
class GCECredentialsGetCacheKeyTest extends \PHPUnit_Framework_TestCase |
| 62 |
{ |
| 63 |
public function testShouldNotBeEmpty() |
| 64 |
{ |
| 65 |
$g = new GCECredentials(); |
| 66 |
$this->assertNotEmpty($g->getCacheKey()); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
class GCECredentialsFetchAuthTokenTest extends \PHPUnit_Framework_TestCase |
| 71 |
{ |
| 72 |
public function testShouldBeEmptyIfNotOnGCE() |
| 73 |
{ |
| 74 |
$httpHandler = getHandler([ |
| 75 |
buildResponse(500) |
| 76 |
]); |
| 77 |
$g = new GCECredentials(); |
| 78 |
$this->assertEquals(array(), $g->fetchAuthToken($httpHandler)); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @expectedException Exception |
| 83 |
* @expectedExceptionMessage Invalid JSON response |
| 84 |
*/ |
| 85 |
public function testShouldFailIfResponseIsNotJson() |
| 86 |
{ |
| 87 |
$notJson = '{"foo": , this is cannot be passed as json" "bar"}'; |
| 88 |
$httpHandler = getHandler([ |
| 89 |
buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']), |
| 90 |
buildResponse(200, [], $notJson), |
| 91 |
]); |
| 92 |
$g = new GCECredentials(); |
| 93 |
$g->fetchAuthToken($httpHandler); |
| 94 |
} |
| 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 |
} |
| 113 |
|