PageRenderTime 64ms CodeModel.GetById 52ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Google/vendor/google/auth/tests/Credentials/UserRefreshCredentialsTest.php

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