PluginProbe
WP-Stateless – Google Cloud Storage / 2.1.4
WP-Stateless – Google Cloud Storage v2.1.4
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / Google / vendor / google / auth / tests / Subscriber / ScopedAccessTokenSubscriberTest.php

ScopedAccessTokenSubscriberTest.php in WP-Stateless – Google Cloud Storage 2.1.4, at lib/Google/vendor/google/auth/tests/Subscriber/ScopedAccessTokenSubscriberTest.php

214 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\ScopedAccessTokenSubscriber;
21 use GuzzleHttp\Client;
22 use GuzzleHttp\Event\BeforeEvent;
23 use GuzzleHttp\Transaction;
24
25 class ScopedAccessTokenSubscriberTest extends BaseTest
26 {
27 const TEST_SCOPE = 'https://www.googleapis.com/auth/cloud-taskqueue';
28
29 protected function setUp()
30 {
31 $this->onlyGuzzle5();
32 }
33
34 /**
35 * @expectedException InvalidArgumentException
36 */
37 public function testRequiresScopeAsAStringOrArray()
38 {
39 $fakeAuthFunc = function ($unused_scopes) {
40 return '1/abcdef1234567890';
41 };
42 new ScopedAccessTokenSubscriber($fakeAuthFunc, new \stdClass(), array());
43 }
44
45 public function testSubscribesToEvents()
46 {
47 $fakeAuthFunc = function ($unused_scopes) {
48 return '1/abcdef1234567890';
49 };
50 $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array());
51 $this->assertArrayHasKey('before', $s->getEvents());
52 }
53
54 public function testAddsTheTokenAsAnAuthorizationHeader()
55 {
56 $fakeAuthFunc = function ($unused_scopes) {
57 return '1/abcdef1234567890';
58 };
59 $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array());
60 $client = new Client();
61 $request = $client->createRequest('GET', 'http://testing.org',
62 ['auth' => 'scoped']);
63 $before = new BeforeEvent(new Transaction($client, $request));
64 $s->onBefore($before);
65 $this->assertSame(
66 'Bearer 1/abcdef1234567890',
67 $request->getHeader('Authorization')
68 );
69 }
70
71 public function testUsesCachedAuthToken()
72 {
73 $cachedValue = '2/abcdef1234567890';
74 $fakeAuthFunc = function ($unused_scopes) {
75 return '';
76 };
77 $mockCache = $this
78 ->getMockBuilder('Google\Auth\CacheInterface')
79 ->getMock();
80 $mockCache
81 ->expects($this->once())
82 ->method('get')
83 ->will($this->returnValue($cachedValue));
84
85 // Run the test
86 $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array(),
87 $mockCache);
88 $client = new Client();
89 $request = $client->createRequest('GET', 'http://testing.org',
90 ['auth' => 'scoped']);
91 $before = new BeforeEvent(new Transaction($client, $request));
92 $s->onBefore($before);
93 $this->assertSame(
94 'Bearer 2/abcdef1234567890',
95 $request->getHeader('Authorization')
96 );
97 }
98
99 public function testGetsCachedAuthTokenUsingCacheOptions()
100 {
101 $prefix = 'test_prefix:';
102 $lifetime = '70707';
103 $cachedValue = '2/abcdef1234567890';
104 $fakeAuthFunc = function ($unused_scopes) {
105 return '';
106 };
107 $mockCache = $this
108 ->getMockBuilder('Google\Auth\CacheInterface')
109 ->getMock();
110 $mockCache
111 ->expects($this->once())
112 ->method('get')
113 ->with($this->equalTo($prefix . self::TEST_SCOPE),
114 $this->equalTo($lifetime))
115 ->will($this->returnValue($cachedValue));
116
117 // Run the test
118 $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE,
119 array('prefix' => $prefix,
120 'lifetime' => $lifetime),
121 $mockCache);
122 $client = new Client();
123 $request = $client->createRequest('GET', 'http://testing.org',
124 ['auth' => 'scoped']);
125 $before = new BeforeEvent(new Transaction($client, $request));
126 $s->onBefore($before);
127 $this->assertSame(
128 'Bearer 2/abcdef1234567890',
129 $request->getHeader('Authorization')
130 );
131 }
132
133 public function testShouldSaveValueInCache()
134 {
135 $token = '2/abcdef1234567890';
136 $fakeAuthFunc = function ($unused_scopes) {
137 return '2/abcdef1234567890';
138 };
139 $mockCache = $this
140 ->getMockBuilder('Google\Auth\CacheInterface')
141 ->getMock();
142 $mockCache
143 ->expects($this->once())
144 ->method('get')
145 ->will($this->returnValue(false));
146 $mockCache
147 ->expects($this->once())
148 ->method('set')
149 ->with($this->equalTo(self::TEST_SCOPE), $this->equalTo($token))
150 ->will($this->returnValue(false));
151 $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array(),
152 $mockCache);
153 $client = new Client();
154 $request = $client->createRequest('GET', 'http://testing.org',
155 ['auth' => 'scoped']);
156 $before = new BeforeEvent(new Transaction($client, $request));
157 $s->onBefore($before);
158 $this->assertSame(
159 'Bearer 2/abcdef1234567890',
160 $request->getHeader('Authorization')
161 );
162 }
163
164 public function testShouldSaveValueInCacheWithSpecifiedPrefix()
165 {
166 $token = '2/abcdef1234567890';
167 $prefix = 'test_prefix:';
168 $fakeAuthFunc = function ($unused_scopes) {
169 return '2/abcdef1234567890';
170 };
171 $mockCache = $this
172 ->getMockBuilder('Google\Auth\CacheInterface')
173 ->getMock();
174 $mockCache
175 ->expects($this->once())
176 ->method('get')
177 ->will($this->returnValue(false));
178 $mockCache
179 ->expects($this->once())
180 ->method('set')
181 ->with($this->equalTo($prefix . self::TEST_SCOPE),
182 $this->equalTo($token))
183 ->will($this->returnValue(false));
184
185 // Run the test
186 $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE,
187 array('prefix' => $prefix),
188 $mockCache);
189 $client = new Client();
190 $request = $client->createRequest('GET', 'http://testing.org',
191 ['auth' => 'scoped']);
192 $before = new BeforeEvent(new Transaction($client, $request));
193 $s->onBefore($before);
194 $this->assertSame(
195 'Bearer 2/abcdef1234567890',
196 $request->getHeader('Authorization')
197 );
198 }
199
200 public function testOnlyTouchesWhenAuthConfigScoped()
201 {
202 $fakeAuthFunc = function ($unused_scopes) {
203 return '1/abcdef1234567890';
204 };
205 $s = new ScopedAccessTokenSubscriber($fakeAuthFunc, self::TEST_SCOPE, array());
206 $client = new Client();
207 $request = $client->createRequest('GET', 'http://testing.org',
208 ['auth' => 'notscoped']);
209 $before = new BeforeEvent(new Transaction($client, $request));
210 $s->onBefore($before);
211 $this->assertSame('', $request->getHeader('Authorization'));
212 }
213 }
214