123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- /*
- * Copyright 2015 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\Subscriber\ScopedAccessTokenSubscriber;
- use GuzzleHttp\Client;
- use GuzzleHttp\Event\BeforeEvent;
- use GuzzleHttp\Transaction;
-
- class ScopedAccessTokenSubscriberTest extends BaseTest
- {
- const TEST_SCOPE = 'https://www.googleapis.com/auth/cloud-taskqueue';
-
- private $mockCacheItem;
- private $mockCache;
- private $mockRequest;
-
- protected function setUp()
- {
- $this->onlyGuzzle5();
-
- $this->mockCacheItem =
- $this
- ->getMockBuilder('Psr\Cache\CacheItemInterface')
- ->getMock();
- $this->mockCache =
- $this
- ->getMockBuilder('Psr\Cache\CacheItemPoolInterface')
- ->getMock();
- $this->mockRequest =
- $this
- ->getMockBuilder('GuzzleHttp\Psr7\Request')
- ->disableOriginalConstructor()
- ->getMock();
- }
-
- /**
- * @expectedException InvalidArgumentException
- */
- public function testRequiresScopeAsAStringOrArray()
- {
- $fakeAuthFunc = function ($unused_scopes) {
- return '1/abcdef1234567890';
- };
- new ScopedAccessTokenSubscriber($fakeAuthFunc, new \stdClass(), array());
- }
-
- public function testSubscribesToEvents()
- {
- $fakeAuthFunc = function ($unused_scopes) {
- return '1/abcdef1234567890';
- };
- $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array());
- $this->assertArrayHasKey('before', $s->getEvents());
- }
-
- public function testAddsTheTokenAsAnAuthorizationHeader()
- {
- $fakeAuthFunc = function ($unused_scopes) {
- return '1/abcdef1234567890';
- };
- $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array());
- $client = new Client();
- $request = $client->createRequest('GET', 'http://testing.org',
- ['auth' => 'scoped']);
- $before = new BeforeEvent(new Transaction($client, $request));
- $s->onBefore($before);
- $this->assertSame(
- 'Bearer 1/abcdef1234567890',
- $request->getHeader('authorization')
- );
- }
-
- public function testUsesCachedAuthToken()
- {
- $cachedValue = '2/abcdef1234567890';
- $fakeAuthFunc = function ($unused_scopes) {
- return '';
- };
- $this->mockCacheItem
- ->expects($this->once())
- ->method('isHit')
- ->will($this->returnValue(true));
- $this->mockCacheItem
- ->expects($this->once())
- ->method('get')
- ->will($this->returnValue($cachedValue));
- $this->mockCache
- ->expects($this->once())
- ->method('getItem')
- ->with($this->getValidKeyName(self::TEST_SCOPE))
- ->will($this->returnValue($this->mockCacheItem));
-
- // Run the test
- $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array(),
- $this->mockCache);
- $client = new Client();
- $request = $client->createRequest('GET', 'http://testing.org',
- ['auth' => 'scoped']);
- $before = new BeforeEvent(new Transaction($client, $request));
- $s->onBefore($before);
- $this->assertSame(
- 'Bearer 2/abcdef1234567890',
- $request->getHeader('authorization')
- );
- }
-
- public function testGetsCachedAuthTokenUsingCachePrefix()
- {
- $prefix = 'test_prefix_';
- $cachedValue = '2/abcdef1234567890';
- $fakeAuthFunc = function ($unused_scopes) {
- return '';
- };
- $this->mockCacheItem
- ->expects($this->once())
- ->method('isHit')
- ->will($this->returnValue(true));
- $this->mockCacheItem
- ->expects($this->once())
- ->method('get')
- ->will($this->returnValue($cachedValue));
- $this->mockCache
- ->expects($this->once())
- ->method('getItem')
- ->with($prefix . $this->getValidKeyName(self::TEST_SCOPE))
- ->will($this->returnValue($this->mockCacheItem));
-
- // Run the test
- $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE,
- ['prefix' => $prefix],
- $this->mockCache);
- $client = new Client();
- $request = $client->createRequest('GET', 'http://testing.org',
- ['auth' => 'scoped']);
- $before = new BeforeEvent(new Transaction($client, $request));
- $s->onBefore($before);
- $this->assertSame(
- 'Bearer 2/abcdef1234567890',
- $request->getHeader('authorization')
- );
- }
-
- public function testShouldSaveValueInCache()
- {
- $token = '2/abcdef1234567890';
- $fakeAuthFunc = function ($unused_scopes) {
- return '2/abcdef1234567890';
- };
- $this->mockCacheItem
- ->expects($this->once())
- ->method('isHit')
- ->will($this->returnValue(false));
- $this->mockCacheItem
- ->expects($this->once())
- ->method('set')
- ->with($this->equalTo($token))
- ->will($this->returnValue(false));
- $this->mockCache
- ->expects($this->exactly(2))
- ->method('getItem')
- ->with($this->getValidKeyName(self::TEST_SCOPE))
- ->will($this->returnValue($this->mockCacheItem));
- $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array(),
- $this->mockCache);
- $client = new Client();
- $request = $client->createRequest('GET', 'http://testing.org',
- ['auth' => 'scoped']);
- $before = new BeforeEvent(new Transaction($client, $request));
- $s->onBefore($before);
- $this->assertSame(
- 'Bearer 2/abcdef1234567890',
- $request->getHeader('authorization')
- );
- }
-
- public function testShouldSaveValueInCacheWithCacheOptions()
- {
- $token = '2/abcdef1234567890';
- $prefix = 'test_prefix_';
- $lifetime = '70707';
- $fakeAuthFunc = function ($unused_scopes) {
- return '2/abcdef1234567890';
- };
- $this->mockCacheItem
- ->expects($this->once())
- ->method('isHit')
- ->will($this->returnValue(false));
- $this->mockCacheItem
- ->expects($this->once())
- ->method('set')
- ->with($this->equalTo($token));
- $this->mockCacheItem
- ->expects($this->once())
- ->method('expiresAfter')
- ->with($this->equalTo($lifetime));
- $this->mockCache
- ->expects($this->exactly(2))
- ->method('getItem')
- ->with($prefix . $this->getValidKeyName(self::TEST_SCOPE))
- ->will($this->returnValue($this->mockCacheItem));
-
- // Run the test
- $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE,
- ['prefix' => $prefix, 'lifetime' => $lifetime],
- $this->mockCache);
- $client = new Client();
- $request = $client->createRequest('GET', 'http://testing.org',
- ['auth' => 'scoped']);
- $before = new BeforeEvent(new Transaction($client, $request));
- $s->onBefore($before);
- $this->assertSame(
- 'Bearer 2/abcdef1234567890',
- $request->getHeader('authorization')
- );
- }
-
- public function testOnlyTouchesWhenAuthConfigScoped()
- {
- $fakeAuthFunc = function ($unused_scopes) {
- return '1/abcdef1234567890';
- };
- $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array());
- $client = new Client();
- $request = $client->createRequest('GET', 'http://testing.org',
- ['auth' => 'notscoped']);
- $before = new BeforeEvent(new Transaction($client, $request));
- $s->onBefore($before);
- $this->assertSame('', $request->getHeader('authorization'));
- }
- }
|