PageRenderTime 27ms CodeModel.GetById 6ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Google/vendor/google/auth/tests/ApplicationDefaultCredentialsTest.php

https://gitlab.com/vangtrangbac123/zinkaiuit.tk
PHP | 299 lines | 212 code | 44 blank | 43 comment | 6 complexity | 1f4496fcccd72b5a52b3aa171a0692c4 MD5 | raw file
  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
 18namespace Google\Auth\Tests;
 19
 20use Google\Auth\ApplicationDefaultCredentials;
 21use Google\Auth\Credentials\GCECredentials;
 22use Google\Auth\Credentials\ServiceAccountCredentials;
 23use GuzzleHttp\Psr7;
 24
 25class 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
104class 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     * @expectedException DomainException
146     */
147    public function testFailsIfNotOnGceAndNoDefaultFileFound()
148    {
149        putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
150
151        // simulate not being GCE by return 500
152        $httpHandler = getHandler([
153            buildResponse(500),
154        ]);
155
156        ApplicationDefaultCredentials::getMiddleware('a scope', $httpHandler);
157    }
158
159    public function testSuccedsIfNoDefaultFilesButIsOnGCE()
160    {
161        $wantedTokens = [
162            'access_token' => '1/abdef1234567890',
163            'expires_in' => '57',
164            'token_type' => 'Bearer',
165        ];
166        $jsonTokens = json_encode($wantedTokens);
167
168        // simulate the response from GCE.
169        $httpHandler = getHandler([
170            buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
171            buildResponse(200, [], Psr7\stream_for($jsonTokens)),
172        ]);
173
174        $this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope', $httpHandler));
175    }
176}
177
178class ADCGetCredentialsAppEngineTest extends BaseTest
179{
180    private $originalHome;
181    private $originalServiceAccount;
182
183    protected function setUp()
184    {
185        // set home to be somewhere else
186        $this->originalHome = getenv('HOME');
187        putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
188
189        // remove service account path
190        $this->originalServiceAccount = getenv(ServiceAccountCredentials::ENV_VAR);
191        putenv(ServiceAccountCredentials::ENV_VAR);
192    }
193
194    protected function tearDown()
195    {
196        // removes it if assigned
197        putenv('HOME=' . $this->originalHome);
198        putenv(ServiceAccountCredentials::ENV_VAR . '=' . $this->originalServiceAccount);
199    }
200
201    public function testAppEngineStandard()
202    {
203        $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
204        $this->assertInstanceOf(
205            'Google\Auth\Credentials\AppIdentityCredentials',
206            ApplicationDefaultCredentials::getCredentials()
207        );
208    }
209
210    public function testAppEngineFlexible()
211    {
212        $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
213        $_SERVER['GAE_VM'] = 'true';
214        $httpHandler = getHandler([
215            buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
216        ]);
217        $this->assertInstanceOf(
218            'Google\Auth\Credentials\GCECredentials',
219            ApplicationDefaultCredentials::getCredentials(null, $httpHandler)
220        );
221    }
222}
223
224// @todo consider a way to DRY this and above class up
225class ADCGetSubscriberTest extends BaseTest
226{
227    private $originalHome;
228
229    protected function setUp()
230    {
231        $this->onlyGuzzle5();
232
233        $this->originalHome = getenv('HOME');
234    }
235
236    protected function tearDown()
237    {
238        if ($this->originalHome != getenv('HOME')) {
239            putenv('HOME=' . $this->originalHome);
240        }
241        putenv(ServiceAccountCredentials::ENV_VAR);  // removes it if assigned
242    }
243
244    /**
245     * @expectedException DomainException
246     */
247    public function testIsFailsEnvSpecifiesNonExistentFile()
248    {
249        $keyFile = __DIR__ . '/fixtures' . '/does-not-exist-private.json';
250        putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
251        ApplicationDefaultCredentials::getSubscriber('a scope');
252    }
253
254    public function testLoadsOKIfEnvSpecifiedIsValid()
255    {
256        $keyFile = __DIR__ . '/fixtures' . '/private.json';
257        putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
258        $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope'));
259    }
260
261    public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
262    {
263        putenv('HOME=' . __DIR__ . '/fixtures');
264        $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope'));
265    }
266
267    /**
268     * @expectedException DomainException
269     */
270    public function testFailsIfNotOnGceAndNoDefaultFileFound()
271    {
272        putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
273
274        // simulate not being GCE by return 500
275        $httpHandler = getHandler([
276            buildResponse(500),
277        ]);
278
279        ApplicationDefaultCredentials::getSubscriber('a scope', $httpHandler);
280    }
281
282    public function testSuccedsIfNoDefaultFilesButIsOnGCE()
283    {
284        $wantedTokens = [
285            'access_token' => '1/abdef1234567890',
286            'expires_in' => '57',
287            'token_type' => 'Bearer',
288        ];
289        $jsonTokens = json_encode($wantedTokens);
290
291        // simulate the response from GCE.
292        $httpHandler = getHandler([
293            buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
294            buildResponse(200, [], Psr7\stream_for($jsonTokens)),
295        ]);
296
297        $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope', $httpHandler));
298    }
299}