| 1 |
<?php |
| 2 |
|
| 3 |
namespace Google\Auth\Tests; |
| 4 |
|
| 5 |
use Google\Auth\Credentials\AppIdentityCredentials; |
| 6 |
use Google\Auth\Credentials\GCECredentials; |
| 7 |
use Google\Auth\Credentials\ServiceAccountCredentials; |
| 8 |
use Google\Auth\Credentials\ServiceAccountJwtAccessCredentials; |
| 9 |
use Google\Auth\Credentials\UserRefreshCredentials; |
| 10 |
use Google\Auth\CredentialsLoader; |
| 11 |
use Google\Auth\FetchAuthTokenInterface; |
| 12 |
use Google\Auth\OAuth2; |
| 13 |
|
| 14 |
class FetchAuthTokenTest extends BaseTest |
| 15 |
{ |
| 16 |
/** @dataProvider provideAuthTokenFetcher */ |
| 17 |
public function testGetLastReceivedToken(FetchAuthTokenInterface $fetcher) |
| 18 |
{ |
| 19 |
$accessToken = $fetcher->getLastReceivedToken(); |
| 20 |
|
| 21 |
$this->assertNotNull($accessToken); |
| 22 |
$this->assertArrayHasKey('access_token', $accessToken); |
| 23 |
$this->assertArrayHasKey('expires_at', $accessToken); |
| 24 |
|
| 25 |
$this->assertEquals('xyz', $accessToken['access_token']); |
| 26 |
$this->assertEquals(strtotime('2001'), $accessToken['expires_at']); |
| 27 |
} |
| 28 |
|
| 29 |
public function provideAuthTokenFetcher() |
| 30 |
{ |
| 31 |
$scopes = ['https://www.googleapis.com/auth/drive.readonly']; |
| 32 |
$jsonPath = sprintf( |
| 33 |
'%s/fixtures/.config/%s', |
| 34 |
__DIR__, |
| 35 |
CredentialsLoader::WELL_KNOWN_PATH |
| 36 |
); |
| 37 |
$jsonPath2 = sprintf( |
| 38 |
'%s/fixtures2/.config/%s', |
| 39 |
__DIR__, |
| 40 |
CredentialsLoader::WELL_KNOWN_PATH |
| 41 |
); |
| 42 |
|
| 43 |
return [ |
| 44 |
[ $this->getAppIdentityCredentials() ], |
| 45 |
[ $this->getGCECredentials() ], |
| 46 |
[ $this->getServiceAccountCredentials($scopes, $jsonPath) ], |
| 47 |
[ $this->getServiceAccountJwtAccessCredentials($jsonPath) ], |
| 48 |
[ $this->getUserRefreshCredentials($scopes, $jsonPath2) ], |
| 49 |
[ $this->getOAuth2() ], |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
private function getAppIdentityCredentials() |
| 54 |
{ |
| 55 |
$class = new \ReflectionClass( |
| 56 |
'Google\Auth\Credentials\AppIdentityCredentials' |
| 57 |
); |
| 58 |
$property = $class->getProperty('lastReceivedToken'); |
| 59 |
$property->setAccessible(true); |
| 60 |
|
| 61 |
$credentials = new AppIdentityCredentials(); |
| 62 |
$property->setValue($credentials, [ |
| 63 |
'access_token' => 'xyz', |
| 64 |
'expiration_time' => strtotime('2001'), |
| 65 |
]); |
| 66 |
|
| 67 |
return $credentials; |
| 68 |
} |
| 69 |
|
| 70 |
private function getGCECredentials() |
| 71 |
{ |
| 72 |
$class = new \ReflectionClass( |
| 73 |
'Google\Auth\Credentials\GCECredentials' |
| 74 |
); |
| 75 |
$property = $class->getProperty('lastReceivedToken'); |
| 76 |
$property->setAccessible(true); |
| 77 |
|
| 78 |
$credentials = new GCECredentials(); |
| 79 |
$property->setValue($credentials, [ |
| 80 |
'access_token' => 'xyz', |
| 81 |
'expires_at' => strtotime('2001'), |
| 82 |
]); |
| 83 |
|
| 84 |
return $credentials; |
| 85 |
} |
| 86 |
|
| 87 |
private function getServiceAccountCredentials($scopes, $jsonPath) |
| 88 |
{ |
| 89 |
$class = new \ReflectionClass( |
| 90 |
'Google\Auth\Credentials\ServiceAccountCredentials' |
| 91 |
); |
| 92 |
$property = $class->getProperty('auth'); |
| 93 |
$property->setAccessible(true); |
| 94 |
|
| 95 |
$credentials = new ServiceAccountCredentials($scopes, $jsonPath); |
| 96 |
$property->setValue($credentials, $this->getOAuth2Mock()); |
| 97 |
|
| 98 |
return $credentials; |
| 99 |
} |
| 100 |
|
| 101 |
private function getServiceAccountJwtAccessCredentials($jsonPath) |
| 102 |
{ |
| 103 |
$class = new \ReflectionClass( |
| 104 |
'Google\Auth\Credentials\ServiceAccountJwtAccessCredentials' |
| 105 |
); |
| 106 |
$property = $class->getProperty('auth'); |
| 107 |
$property->setAccessible(true); |
| 108 |
|
| 109 |
$credentials = new ServiceAccountJwtAccessCredentials($jsonPath); |
| 110 |
$property->setValue($credentials, $this->getOAuth2Mock()); |
| 111 |
|
| 112 |
return $credentials; |
| 113 |
} |
| 114 |
|
| 115 |
private function getUserRefreshCredentials($scopes, $jsonPath) |
| 116 |
{ |
| 117 |
$class = new \ReflectionClass( |
| 118 |
'Google\Auth\Credentials\UserRefreshCredentials' |
| 119 |
); |
| 120 |
$property = $class->getProperty('auth'); |
| 121 |
$property->setAccessible(true); |
| 122 |
|
| 123 |
$credentials = new UserRefreshCredentials($scopes, $jsonPath); |
| 124 |
$property->setValue($credentials, $this->getOAuth2Mock()); |
| 125 |
|
| 126 |
return $credentials; |
| 127 |
} |
| 128 |
|
| 129 |
private function getOAuth2() |
| 130 |
{ |
| 131 |
$oauth = new OAuth2([ |
| 132 |
'access_token' => 'xyz', |
| 133 |
'expires_at' => strtotime('2001'), |
| 134 |
]); |
| 135 |
|
| 136 |
return $oauth; |
| 137 |
} |
| 138 |
|
| 139 |
private function getOAuth2Mock() |
| 140 |
{ |
| 141 |
$mock = $this->getMockBuilder('Google\Auth\OAuth2') |
| 142 |
->disableOriginalConstructor() |
| 143 |
->getMock(); |
| 144 |
|
| 145 |
$mock |
| 146 |
->expects($this->once()) |
| 147 |
->method('getLastReceivedToken') |
| 148 |
->will($this->returnValue([ |
| 149 |
'access_token' => 'xyz', |
| 150 |
'expires_at' => strtotime('2001'), |
| 151 |
])); |
| 152 |
|
| 153 |
return $mock; |
| 154 |
} |
| 155 |
} |
| 156 |
|