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

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

256 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\ApplicationDefaultCredentials;
21 use Google\Auth\Credentials\GCECredentials;
22 use Google\Auth\Credentials\ServiceAccountCredentials;
23 use GuzzleHttp\Psr7;
24
25 class ADCGetTest extends \PHPUnit_Framework_TestCase
26 {
27 private $originalHome;
28
29 protected function setUp()
30 {
31 $this->originalHome = getenv('HOME');
32 }
33
34 protected function tearDown()
35 {
36 if ($this->originalHome != getenv('HOME')) {
37 putenv('HOME=' . $this->originalHome);
38 }
39 putenv(ServiceAccountCredentials::ENV_VAR); // removes it from
40 }
41
42 /**
43 * @expectedException DomainException
44 */
45 public function testIsFailsEnvSpecifiesNonExistentFile()
46 {
47 $keyFile = __DIR__ . '/fixtures' . '/does-not-exist-private.json';
48 putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
49 ApplicationDefaultCredentials::getCredentials('a scope');
50 }
51
52 public function testLoadsOKIfEnvSpecifiedIsValid()
53 {
54 $keyFile = __DIR__ . '/fixtures' . '/private.json';
55 putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
56 $this->assertNotNull(
57 ApplicationDefaultCredentials::getCredentials('a scope')
58 );
59 }
60
61 public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
62 {
63 putenv('HOME=' . __DIR__ . '/fixtures');
64 $this->assertNotNull(
65 ApplicationDefaultCredentials::getCredentials('a scope')
66 );
67 }
68
69 /**
70 * @expectedException DomainException
71 */
72 public function testFailsIfNotOnGceAndNoDefaultFileFound()
73 {
74 putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
75 // simulate not being GCE by return 500
76 $httpHandler = getHandler([
77 buildResponse(500)
78 ]);
79
80 ApplicationDefaultCredentials::getCredentials('a scope', $httpHandler);
81 }
82
83 public function testSuccedsIfNoDefaultFilesButIsOnGCE()
84 {
85 $wantedTokens = [
86 'access_token' => '1/abdef1234567890',
87 'expires_in' => '57',
88 'token_type' => 'Bearer',
89 ];
90 $jsonTokens = json_encode($wantedTokens);
91
92 // simulate the response from GCE.
93 $httpHandler = getHandler([
94 buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
95 buildResponse(200, [], Psr7\stream_for($jsonTokens))
96 ]);
97
98 $this->assertNotNull(
99 ApplicationDefaultCredentials::getCredentials('a scope', $httpHandler)
100 );
101 }
102 }
103
104 class ADCGetMiddlewareTest extends \PHPUnit_Framework_TestCase
105 {
106 private $originalHome;
107
108 protected function setUp()
109 {
110 $this->originalHome = getenv('HOME');
111 }
112
113 protected function tearDown()
114 {
115 if ($this->originalHome != getenv('HOME')) {
116 putenv('HOME=' . $this->originalHome);
117 }
118 putenv(ServiceAccountCredentials::ENV_VAR); // removes it if assigned
119 }
120
121 /**
122 * @expectedException DomainException
123 */
124 public function testIsFailsEnvSpecifiesNonExistentFile()
125 {
126 $keyFile = __DIR__ . '/fixtures' . '/does-not-exist-private.json';
127 putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
128 ApplicationDefaultCredentials::getMiddleware('a scope');
129 }
130
131 public function testLoadsOKIfEnvSpecifiedIsValid()
132 {
133 $keyFile = __DIR__ . '/fixtures' . '/private.json';
134 putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
135 $this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope'));
136 }
137
138 public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
139 {
140 putenv('HOME=' . __DIR__ . '/fixtures');
141 $this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope'));
142
143 }
144
145 /**
146 * @expectedException DomainException
147 */
148 public function testFailsIfNotOnGceAndNoDefaultFileFound()
149 {
150 putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
151
152 // simulate not being GCE by return 500
153 $httpHandler = getHandler([
154 buildResponse(500)
155 ]);
156
157 ApplicationDefaultCredentials::getMiddleware('a scope', $httpHandler);
158 }
159
160 public function testSuccedsIfNoDefaultFilesButIsOnGCE()
161 {
162 $wantedTokens = [
163 'access_token' => '1/abdef1234567890',
164 'expires_in' => '57',
165 'token_type' => 'Bearer',
166 ];
167 $jsonTokens = json_encode($wantedTokens);
168
169 // simulate the response from GCE.
170 $httpHandler = getHandler([
171 buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
172 buildResponse(200, [], Psr7\stream_for($jsonTokens))
173 ]);
174
175 $this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope', $httpHandler));
176 }
177 }
178
179 // @todo consider a way to DRY this and above class up
180 class ADCGetSubscriberTest extends BaseTest
181 {
182 private $originalHome;
183
184 protected function setUp()
185 {
186 $this->onlyGuzzle5();
187
188 $this->originalHome = getenv('HOME');
189 }
190
191 protected function tearDown()
192 {
193 if ($this->originalHome != getenv('HOME')) {
194 putenv('HOME=' . $this->originalHome);
195 }
196 putenv(ServiceAccountCredentials::ENV_VAR); // removes it if assigned
197 }
198
199 /**
200 * @expectedException DomainException
201 */
202 public function testIsFailsEnvSpecifiesNonExistentFile()
203 {
204 $keyFile = __DIR__ . '/fixtures' . '/does-not-exist-private.json';
205 putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
206 ApplicationDefaultCredentials::getSubscriber('a scope');
207 }
208
209 public function testLoadsOKIfEnvSpecifiedIsValid()
210 {
211 $keyFile = __DIR__ . '/fixtures' . '/private.json';
212 putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
213 $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope'));
214 }
215
216 public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
217 {
218 putenv('HOME=' . __DIR__ . '/fixtures');
219 $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope'));
220
221 }
222
223 /**
224 * @expectedException DomainException
225 */
226 public function testFailsIfNotOnGceAndNoDefaultFileFound()
227 {
228 putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
229
230 // simulate not being GCE by return 500
231 $httpHandler = getHandler([
232 buildResponse(500)
233 ]);
234
235 ApplicationDefaultCredentials::getSubscriber('a scope', $httpHandler);
236 }
237
238 public function testSuccedsIfNoDefaultFilesButIsOnGCE()
239 {
240 $wantedTokens = [
241 'access_token' => '1/abdef1234567890',
242 'expires_in' => '57',
243 'token_type' => 'Bearer',
244 ];
245 $jsonTokens = json_encode($wantedTokens);
246
247 // simulate the response from GCE.
248 $httpHandler = getHandler([
249 buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
250 buildResponse(200, [], Psr7\stream_for($jsonTokens))
251 ]);
252
253 $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope', $httpHandler));
254 }
255 }
256