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 / Middleware / ScopedAccessTokenMiddlewareTest.php

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

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