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 / Credentials / UserRefreshCredentialsTest.php

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

232 lines 5.8 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\ApplicationDefaultCredentials;
21 use Google\Auth\Credentials\UserRefreshCredentials;
22 use Google\Auth\HttpHandler\Guzzle6HttpHandler;
23 use Google\Auth\OAuth2;
24 use GuzzleHttp\Client;
25 use GuzzleHttp\Psr7;
26 use GuzzleHttp\Psr7\Response;
27
28 // Creates a standard JSON auth object for testing.
29 function createURCTestJson()
30 {
31 return [
32 'client_id' => 'client123',
33 'client_secret' => 'clientSecret123',
34 'refresh_token' => 'refreshToken123',
35 'type' => 'authorized_user'
36 ];
37 }
38
39 class URCGetCacheKeyTest extends \PHPUnit_Framework_TestCase
40 {
41 public function testShouldBeTheSameAsOAuth2WithTheSameScope()
42 {
43 $testJson = createURCTestJson();
44 $scope = ['scope/1', 'scope/2'];
45 $sa = new UserRefreshCredentials(
46 $scope,
47 $testJson);
48 $o = new OAuth2(['scope' => $scope]);
49 $this->assertSame(
50 $testJson['client_id'] . ':' . $o->getCacheKey(),
51 $sa->getCacheKey()
52 );
53 }
54 }
55
56 class URCConstructorTest extends \PHPUnit_Framework_TestCase
57 {
58 /**
59 * @expectedException InvalidArgumentException
60 */
61 public function testShouldFailIfScopeIsNotAValidType()
62 {
63 $testJson = createURCTestJson();
64 $notAnArrayOrString = new \stdClass();
65 $sa = new UserRefreshCredentials(
66 $notAnArrayOrString,
67 $testJson
68 );
69 }
70
71 /**
72 * @expectedException InvalidArgumentException
73 */
74 public function testShouldFailIfJsonDoesNotHaveClientSecret()
75 {
76 $testJson = createURCTestJson();
77 unset($testJson['client_secret']);
78 $scope = ['scope/1', 'scope/2'];
79 $sa = new UserRefreshCredentials(
80 $scope,
81 $testJson
82 );
83 }
84
85 /**
86 * @expectedException InvalidArgumentException
87 */
88 public function testShouldFailIfJsonDoesNotHaveRefreshToken()
89 {
90 $testJson = createURCTestJson();
91 unset($testJson['refresh_token']);
92 $scope = ['scope/1', 'scope/2'];
93 $sa = new UserRefreshCredentials(
94 $scope,
95 $testJson
96 );
97 }
98
99 /**
100 * @expectedException InvalidArgumentException
101 */
102 public function testFailsToInitalizeFromANonExistentFile()
103 {
104 $keyFile = __DIR__ . '/../fixtures' . '/does-not-exist-private.json';
105 new UserRefreshCredentials('scope/1', $keyFile);
106 }
107
108 public function testInitalizeFromAFile()
109 {
110 $keyFile = __DIR__ . '/../fixtures2' . '/private.json';
111 $this->assertNotNull(
112 new UserRefreshCredentials('scope/1', $keyFile)
113 );
114 }
115 }
116
117 class URCFromEnvTest extends \PHPUnit_Framework_TestCase
118 {
119 protected function tearDown()
120 {
121 putenv(UserRefreshCredentials::ENV_VAR); // removes it from
122 }
123
124 public function testIsNullIfEnvVarIsNotSet()
125 {
126 $this->assertNull(UserRefreshCredentials::fromEnv('a scope'));
127 }
128
129 /**
130 * @expectedException DomainException
131 */
132 public function testFailsIfEnvSpecifiesNonExistentFile()
133 {
134 $keyFile = __DIR__ . '/../fixtures' . '/does-not-exist-private.json';
135 putenv(UserRefreshCredentials::ENV_VAR . '=' . $keyFile);
136 UserRefreshCredentials::fromEnv('a scope');
137 }
138
139 public function testSucceedIfFileExists()
140 {
141 $keyFile = __DIR__ . '/../fixtures2' . '/private.json';
142 putenv(UserRefreshCredentials::ENV_VAR . '=' . $keyFile);
143 $this->assertNotNull(ApplicationDefaultCredentials::getCredentials('a scope'));
144 }
145 }
146
147 class URCFromWellKnownFileTest extends \PHPUnit_Framework_TestCase
148 {
149 private $originalHome;
150
151 protected function setUp()
152 {
153 $this->originalHome = getenv('HOME');
154 }
155
156 protected function tearDown()
157 {
158 if ($this->originalHome != getenv('HOME')) {
159 putenv('HOME=' . $this->originalHome);
160 }
161 }
162
163 public function testIsNullIfFileDoesNotExist()
164 {
165 putenv('HOME=' . __DIR__ . '/../not_exist_fixtures');
166 $this->assertNull(
167 UserRefreshCredentials::fromWellKnownFile('a scope')
168 );
169 }
170
171 public function testSucceedIfFileIsPresent()
172 {
173 putenv('HOME=' . __DIR__ . '/../fixtures2');
174 $this->assertNotNull(
175 ApplicationDefaultCredentials::getCredentials('a scope')
176 );
177 }
178 }
179
180 class URCFetchAuthTokenTest extends \PHPUnit_Framework_TestCase
181 {
182 /**
183 * @expectedException GuzzleHttp\Exception\ClientException
184 */
185 public function testFailsOnClientErrors()
186 {
187 $testJson = createURCTestJson();
188 $scope = ['scope/1', 'scope/2'];
189 $httpHandler = getHandler([
190 buildResponse(400)
191 ]);
192 $sa = new UserRefreshCredentials(
193 $scope,
194 $testJson
195 );
196 $sa->fetchAuthToken($httpHandler);
197 }
198
199 /**
200 * @expectedException GuzzleHttp\Exception\ServerException
201 */
202 public function testFailsOnServerErrors()
203 {
204 $testJson = createURCTestJson();
205 $scope = ['scope/1', 'scope/2'];
206 $httpHandler = getHandler([
207 buildResponse(500)
208 ]);
209 $sa = new UserRefreshCredentials(
210 $scope,
211 $testJson
212 );
213 $sa->fetchAuthToken($httpHandler);
214 }
215
216 public function testCanFetchCredsOK()
217 {
218 $testJson = createURCTestJson();
219 $testJsonText = json_encode($testJson);
220 $scope = ['scope/1', 'scope/2'];
221 $httpHandler = getHandler([
222 buildResponse(200, [], Psr7\stream_for($testJsonText))
223 ]);
224 $sa = new UserRefreshCredentials(
225 $scope,
226 $testJson
227 );
228 $tokens = $sa->fetchAuthToken($httpHandler);
229 $this->assertEquals($testJson, $tokens);
230 }
231 }
232