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 / AuthTokenMiddlewareTest.php

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

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