123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- /*
- * Copyright 2010 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- namespace Google\Auth\tests;
-
- use Google\Auth\Credentials\AppIdentityCredentials;
- use Google\Auth\Credentials\GCECredentials;
- use Google\Auth\Credentials\ServiceAccountCredentials;
- use Google\Auth\Credentials\ServiceAccountJwtAccessCredentials;
- use Google\Auth\Credentials\UserRefreshCredentials;
- use Google\Auth\CredentialsLoader;
- use Google\Auth\FetchAuthTokenInterface;
- use Google\Auth\OAuth2;
-
- class FetchAuthTokenTest extends BaseTest
- {
- private $scopes = ['https://www.googleapis.com/auth/drive.readonly'];
-
- /** @dataProvider provideMakeHttpClient */
- public function testMakeHttpClient($fetcherClass)
- {
- $mockFetcher = $this->getMockBuilder($fetcherClass)
- ->disableOriginalConstructor()
- ->getMock();
-
- $mockFetcher
- ->expects($this->once())
- ->method('fetchAuthToken')
- ->will($this->returnCallback(function ($httpHandler) {
- return $httpHandler();
- }));
-
- $httpHandlerCalled = false;
- $httpHandler = function () use (&$httpHandlerCalled) {
- $httpHandlerCalled = true;
- return ['access_token' => 'xyz'];
- };
-
- $tokenCallbackCalled = false;
- $tokenCallback = function ($cacheKey, $accessToken) use (&$tokenCallbackCalled) {
- $tokenCallbackCalled = true;
- $this->assertEquals('xyz', $accessToken);
- };
-
- $client = CredentialsLoader::makeHttpClient(
- $mockFetcher,
- [
- 'base_url' => 'https://www.googleapis.com/books/v1/',
- 'base_uri' => 'https://www.googleapis.com/books/v1/',
- 'exceptions' => false,
- 'defaults' => ['exceptions' => false]
- ],
- $httpHandler,
- $tokenCallback
- );
-
- $response = $client->get(
- 'volumes?q=Henry+David+Thoreau&country=US'
- );
-
- $this->assertEquals(401, $response->getStatusCode());
- $this->assertTrue($httpHandlerCalled);
- $this->assertTrue($tokenCallbackCalled);
- }
-
- public function provideMakeHttpClient()
- {
- return [
- ['Google\Auth\Credentials\AppIdentityCredentials'],
- ['Google\Auth\Credentials\GCECredentials'],
- ['Google\Auth\Credentials\ServiceAccountCredentials'],
- ['Google\Auth\Credentials\ServiceAccountJwtAccessCredentials'],
- ['Google\Auth\Credentials\UserRefreshCredentials'],
- ['Google\Auth\OAuth2'],
- ];
- }
-
- public function testAppIdentityCredentialsGetLastReceivedToken()
- {
- $class = new \ReflectionClass(
- 'Google\Auth\Credentials\AppIdentityCredentials'
- );
- $property = $class->getProperty('lastReceivedToken');
- $property->setAccessible(true);
-
- $credentials = new AppIdentityCredentials();
- $property->setValue($credentials, [
- 'access_token' => 'xyz',
- 'expiration_time' => strtotime('2001'),
- ]);
-
- $this->assertGetLastReceivedToken($credentials);
- }
-
- public function testGCECredentialsGetLastReceivedToken()
- {
- $class = new \ReflectionClass(
- 'Google\Auth\Credentials\GCECredentials'
- );
- $property = $class->getProperty('lastReceivedToken');
- $property->setAccessible(true);
-
- $credentials = new GCECredentials();
- $property->setValue($credentials, [
- 'access_token' => 'xyz',
- 'expires_at' => strtotime('2001'),
- ]);
-
- $this->assertGetLastReceivedToken($credentials);
- }
-
- public function testServiceAccountCredentialsGetLastReceivedToken()
- {
- $jsonPath = sprintf(
- '%s/fixtures/.config/%s',
- __DIR__,
- CredentialsLoader::WELL_KNOWN_PATH
- );
-
- $class = new \ReflectionClass(
- 'Google\Auth\Credentials\ServiceAccountCredentials'
- );
- $property = $class->getProperty('auth');
- $property->setAccessible(true);
-
- $credentials = new ServiceAccountCredentials($this->scopes, $jsonPath);
- $property->setValue($credentials, $this->getOAuth2Mock());
-
- $this->assertGetLastReceivedToken($credentials);
- }
-
- public function testServiceAccountJwtAccessCredentialsGetLastReceivedToken()
- {
- $jsonPath = sprintf(
- '%s/fixtures/.config/%s',
- __DIR__,
- CredentialsLoader::WELL_KNOWN_PATH
- );
-
- $class = new \ReflectionClass(
- 'Google\Auth\Credentials\ServiceAccountJwtAccessCredentials'
- );
- $property = $class->getProperty('auth');
- $property->setAccessible(true);
-
- $credentials = new ServiceAccountJwtAccessCredentials($jsonPath);
- $property->setValue($credentials, $this->getOAuth2Mock());
-
- $this->assertGetLastReceivedToken($credentials);
- }
-
- public function testUserRefreshCredentialsGetLastReceivedToken()
- {
- $jsonPath = sprintf(
- '%s/fixtures2/.config/%s',
- __DIR__,
- CredentialsLoader::WELL_KNOWN_PATH
- );
-
- $class = new \ReflectionClass(
- 'Google\Auth\Credentials\UserRefreshCredentials'
- );
- $property = $class->getProperty('auth');
- $property->setAccessible(true);
-
- $credentials = new UserRefreshCredentials($this->scopes, $jsonPath);
- $property->setValue($credentials, $this->getOAuth2Mock());
-
- $this->assertGetLastReceivedToken($credentials);
- }
-
- private function getOAuth2()
- {
- $oauth = new OAuth2([
- 'access_token' => 'xyz',
- 'expires_at' => strtotime('2001'),
- ]);
-
- $this->assertGetLastReceivedToken($oauth);
- }
-
- private function getOAuth2Mock()
- {
- $mock = $this->getMockBuilder('Google\Auth\OAuth2')
- ->disableOriginalConstructor()
- ->getMock();
-
- $mock
- ->expects($this->once())
- ->method('getLastReceivedToken')
- ->will($this->returnValue([
- 'access_token' => 'xyz',
- 'expires_at' => strtotime('2001'),
- ]));
-
- return $mock;
- }
-
- private function assertGetLastReceivedToken(FetchAuthTokenInterface $fetcher)
- {
- $accessToken = $fetcher->getLastReceivedToken();
-
- $this->assertNotNull($accessToken);
- $this->assertArrayHasKey('access_token', $accessToken);
- $this->assertArrayHasKey('expires_at', $accessToken);
-
- $this->assertEquals('xyz', $accessToken['access_token']);
- $this->assertEquals(strtotime('2001'), $accessToken['expires_at']);
- }
- }
|