wp-stateless
/
lib
/
Google
/
vendor
/
google
/
auth
/
tests
/
Subscriber
/
AuthTokenSubscriberTest.php
AuthTokenSubscriberTest.php in WP-Stateless – Google Cloud Storage 2.1.4, at lib/Google/vendor/google/auth/tests/Subscriber/AuthTokenSubscriberTest.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\Subscriber\AuthTokenSubscriber; |
| 21 | use GuzzleHttp\Client; |
| 22 | use GuzzleHttp\ClientInterface; |
| 23 | use GuzzleHttp\Event\BeforeEvent; |
| 24 | use GuzzleHttp\Transaction; |
| 25 | |
| 26 | class AuthTokenSubscriberTest extends BaseTest |
| 27 | { |
| 28 | private $mockFetcher; |
| 29 | private $mockCache; |
| 30 | |
| 31 | protected function setUp() |
| 32 | { |
| 33 | $this->onlyGuzzle5(); |
| 34 | |
| 35 | $this->mockFetcher = |
| 36 | $this |
| 37 | ->getMockBuilder('Google\Auth\FetchAuthTokenInterface') |
| 38 | ->getMock(); |
| 39 | $this->mockCache = |
| 40 | $this |
| 41 | ->getMockBuilder('Google\Auth\CacheInterface') |
| 42 | ->getMock(); |
| 43 | } |
| 44 | |
| 45 | public function testSubscribesToEvents() |
| 46 | { |
| 47 | $a = new AuthTokenSubscriber($this->mockFetcher, array()); |
| 48 | $this->assertArrayHasKey('before', $a->getEvents()); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | public function testOnlyTouchesWhenAuthConfigScoped() |
| 53 | { |
| 54 | $s = new AuthTokenSubscriber($this->mockFetcher, array()); |
| 55 | $client = new Client(); |
| 56 | $request = $client->createRequest('GET', 'http://testing.org', |
| 57 | ['auth' => 'not_google_auth']); |
| 58 | $before = new BeforeEvent(new Transaction($client, $request)); |
| 59 | $s->onBefore($before); |
| 60 | $this->assertSame($request->getHeader('Authorization'), ''); |
| 61 | } |
| 62 | |
| 63 | public function testAddsTheTokenAsAnAuthorizationHeader() |
| 64 | { |
| 65 | $authResult = ['access_token' => '1/abcdef1234567890']; |
| 66 | $this->mockFetcher |
| 67 | ->expects($this->once()) |
| 68 | ->method('fetchAuthToken') |
| 69 | ->will($this->returnValue($authResult)); |
| 70 | |
| 71 | // Run the test. |
| 72 | $a = new AuthTokenSubscriber($this->mockFetcher, array()); |
| 73 | $client = new Client(); |
| 74 | $request = $client->createRequest('GET', 'http://testing.org', |
| 75 | ['auth' => 'google_auth']); |
| 76 | $before = new BeforeEvent(new Transaction($client, $request)); |
| 77 | $a->onBefore($before); |
| 78 | $this->assertSame($request->getHeader('Authorization'), |
| 79 | 'Bearer 1/abcdef1234567890'); |
| 80 | } |
| 81 | |
| 82 | public function testDoesNotAddAnAuthorizationHeaderOnNoAccessToken() |
| 83 | { |
| 84 | $authResult = ['not_access_token' => '1/abcdef1234567890']; |
| 85 | $this->mockFetcher |
| 86 | ->expects($this->once()) |
| 87 | ->method('fetchAuthToken') |
| 88 | ->will($this->returnValue($authResult)); |
| 89 | |
| 90 | // Run the test. |
| 91 | $a = new AuthTokenSubscriber($this->mockFetcher, array()); |
| 92 | $client = new Client(); |
| 93 | $request = $client->createRequest('GET', 'http://testing.org', |
| 94 | ['auth' => 'google_auth']); |
| 95 | $before = new BeforeEvent(new Transaction($client, $request)); |
| 96 | $a->onBefore($before); |
| 97 | $this->assertSame($request->getHeader('Authorization'), ''); |
| 98 | } |
| 99 | |
| 100 | public function testUsesCachedAuthToken() |
| 101 | { |
| 102 | $cacheKey = 'myKey'; |
| 103 | $cachedValue = '2/abcdef1234567890'; |
| 104 | $this->mockCache |
| 105 | ->expects($this->once()) |
| 106 | ->method('get') |
| 107 | ->with($this->equalTo($cacheKey), |
| 108 | $this->equalTo(AuthTokenSubscriber::DEFAULT_CACHE_LIFETIME)) |
| 109 | ->will($this->returnValue($cachedValue)); |
| 110 | $this->mockFetcher |
| 111 | ->expects($this->never()) |
| 112 | ->method('fetchAuthToken'); |
| 113 | $this->mockFetcher |
| 114 | ->expects($this->any()) |
| 115 | ->method('getCacheKey') |
| 116 | ->will($this->returnValue($cacheKey)); |
| 117 | |
| 118 | // Run the test. |
| 119 | $a = new AuthTokenSubscriber($this->mockFetcher, array(), $this->mockCache); |
| 120 | $client = new Client(); |
| 121 | $request = $client->createRequest('GET', 'http://testing.org', |
| 122 | ['auth' => 'google_auth']); |
| 123 | $before = new BeforeEvent(new Transaction($client, $request)); |
| 124 | $a->onBefore($before); |
| 125 | $this->assertSame($request->getHeader('Authorization'), |
| 126 | 'Bearer 2/abcdef1234567890'); |
| 127 | } |
| 128 | |
| 129 | public function testGetsCachedAuthTokenUsingCacheOptions() |
| 130 | { |
| 131 | $prefix = 'test_prefix:'; |
| 132 | $lifetime = '70707'; |
| 133 | $cacheKey = 'myKey'; |
| 134 | $cachedValue = '2/abcdef1234567890'; |
| 135 | $this->mockCache |
| 136 | ->expects($this->once()) |
| 137 | ->method('get') |
| 138 | ->with($this->equalTo($prefix . $cacheKey), |
| 139 | $this->equalTo($lifetime)) |
| 140 | ->will($this->returnValue($cachedValue)); |
| 141 | $this->mockFetcher |
| 142 | ->expects($this->never()) |
| 143 | ->method('fetchAuthToken'); |
| 144 | $this->mockFetcher |
| 145 | ->expects($this->any()) |
| 146 | ->method('getCacheKey') |
| 147 | ->will($this->returnValue($cacheKey)); |
| 148 | |
| 149 | // Run the test |
| 150 | $a = new AuthTokenSubscriber($this->mockFetcher, |
| 151 | array('prefix' => $prefix, |
| 152 | 'lifetime' => $lifetime), |
| 153 | $this->mockCache); |
| 154 | $client = new Client(); |
| 155 | $request = $client->createRequest('GET', 'http://testing.org', |
| 156 | ['auth' => 'google_auth']); |
| 157 | $before = new BeforeEvent(new Transaction($client, $request)); |
| 158 | $a->onBefore($before); |
| 159 | $this->assertSame($request->getHeader('Authorization'), |
| 160 | 'Bearer 2/abcdef1234567890'); |
| 161 | } |
| 162 | |
| 163 | public function testShouldSaveValueInCacheWithSpecifiedPrefix() |
| 164 | { |
| 165 | $token = '1/abcdef1234567890'; |
| 166 | $authResult = ['access_token' => $token]; |
| 167 | $cacheKey = 'myKey'; |
| 168 | $prefix = 'test_prefix:'; |
| 169 | $this->mockCache |
| 170 | ->expects($this->any()) |
| 171 | ->method('get') |
| 172 | ->will($this->returnValue(null)); |
| 173 | $this->mockCache |
| 174 | ->expects($this->once()) |
| 175 | ->method('set') |
| 176 | ->with($this->equalTo($prefix . $cacheKey), |
| 177 | $this->equalTo($token)) |
| 178 | ->will($this->returnValue(false)); |
| 179 | $this->mockFetcher |
| 180 | ->expects($this->any()) |
| 181 | ->method('getCacheKey') |
| 182 | ->will($this->returnValue($cacheKey)); |
| 183 | $this->mockFetcher |
| 184 | ->expects($this->once()) |
| 185 | ->method('fetchAuthToken') |
| 186 | ->will($this->returnValue($authResult)); |
| 187 | |
| 188 | // Run the test |
| 189 | $a = new AuthTokenSubscriber($this->mockFetcher, |
| 190 | array('prefix' => $prefix), |
| 191 | $this->mockCache); |
| 192 | |
| 193 | $client = new Client(); |
| 194 | $request = $client->createRequest('GET', 'http://testing.org', |
| 195 | ['auth' => 'google_auth']); |
| 196 | $before = new BeforeEvent(new Transaction($client, $request)); |
| 197 | $a->onBefore($before); |
| 198 | $this->assertSame($request->getHeader('Authorization'), |
| 199 | 'Bearer 1/abcdef1234567890'); |
| 200 | } |
| 201 | } |
| 202 |