PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/vangtrangbac123/zinkaiuit.tk
PHP | 170 lines | 127 code | 27 blank | 16 comment | 0 complexity | 015d14f17725e0de1962692196dbb1d2 MD5 | raw file
  1<?php
  2/*
  3 * Copyright 2010 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\Credentials\AppIdentityCredentials;
 21use Google\Auth\Credentials\GCECredentials;
 22use Google\Auth\Credentials\ServiceAccountCredentials;
 23use Google\Auth\Credentials\ServiceAccountJwtAccessCredentials;
 24use Google\Auth\Credentials\UserRefreshCredentials;
 25use Google\Auth\CredentialsLoader;
 26use Google\Auth\FetchAuthTokenInterface;
 27use Google\Auth\OAuth2;
 28
 29class FetchAuthTokenTest extends BaseTest
 30{
 31    /** @dataProvider provideAuthTokenFetcher */
 32    public function testGetLastReceivedToken(FetchAuthTokenInterface $fetcher)
 33    {
 34        $accessToken = $fetcher->getLastReceivedToken();
 35
 36        $this->assertNotNull($accessToken);
 37        $this->assertArrayHasKey('access_token', $accessToken);
 38        $this->assertArrayHasKey('expires_at', $accessToken);
 39
 40        $this->assertEquals('xyz', $accessToken['access_token']);
 41        $this->assertEquals(strtotime('2001'), $accessToken['expires_at']);
 42    }
 43
 44    public function provideAuthTokenFetcher()
 45    {
 46        $scopes = ['https://www.googleapis.com/auth/drive.readonly'];
 47        $jsonPath = sprintf(
 48            '%s/fixtures/.config/%s',
 49            __DIR__,
 50            CredentialsLoader::WELL_KNOWN_PATH
 51        );
 52        $jsonPath2 = sprintf(
 53            '%s/fixtures2/.config/%s',
 54            __DIR__,
 55            CredentialsLoader::WELL_KNOWN_PATH
 56        );
 57
 58        return [
 59            [$this->getAppIdentityCredentials()],
 60            [$this->getGCECredentials()],
 61            [$this->getServiceAccountCredentials($scopes, $jsonPath)],
 62            [$this->getServiceAccountJwtAccessCredentials($jsonPath)],
 63            [$this->getUserRefreshCredentials($scopes, $jsonPath2)],
 64            [$this->getOAuth2()],
 65        ];
 66    }
 67
 68    private function getAppIdentityCredentials()
 69    {
 70        $class = new \ReflectionClass(
 71            'Google\Auth\Credentials\AppIdentityCredentials'
 72        );
 73        $property = $class->getProperty('lastReceivedToken');
 74        $property->setAccessible(true);
 75
 76        $credentials = new AppIdentityCredentials();
 77        $property->setValue($credentials, [
 78            'access_token' => 'xyz',
 79            'expiration_time' => strtotime('2001'),
 80        ]);
 81
 82        return $credentials;
 83    }
 84
 85    private function getGCECredentials()
 86    {
 87        $class = new \ReflectionClass(
 88            'Google\Auth\Credentials\GCECredentials'
 89        );
 90        $property = $class->getProperty('lastReceivedToken');
 91        $property->setAccessible(true);
 92
 93        $credentials = new GCECredentials();
 94        $property->setValue($credentials, [
 95            'access_token' => 'xyz',
 96            'expires_at' => strtotime('2001'),
 97        ]);
 98
 99        return $credentials;
100    }
101
102    private function getServiceAccountCredentials($scopes, $jsonPath)
103    {
104        $class = new \ReflectionClass(
105            'Google\Auth\Credentials\ServiceAccountCredentials'
106        );
107        $property = $class->getProperty('auth');
108        $property->setAccessible(true);
109
110        $credentials = new ServiceAccountCredentials($scopes, $jsonPath);
111        $property->setValue($credentials, $this->getOAuth2Mock());
112
113        return $credentials;
114    }
115
116    private function getServiceAccountJwtAccessCredentials($jsonPath)
117    {
118        $class = new \ReflectionClass(
119            'Google\Auth\Credentials\ServiceAccountJwtAccessCredentials'
120        );
121        $property = $class->getProperty('auth');
122        $property->setAccessible(true);
123
124        $credentials = new ServiceAccountJwtAccessCredentials($jsonPath);
125        $property->setValue($credentials, $this->getOAuth2Mock());
126
127        return $credentials;
128    }
129
130    private function getUserRefreshCredentials($scopes, $jsonPath)
131    {
132        $class = new \ReflectionClass(
133            'Google\Auth\Credentials\UserRefreshCredentials'
134        );
135        $property = $class->getProperty('auth');
136        $property->setAccessible(true);
137
138        $credentials = new UserRefreshCredentials($scopes, $jsonPath);
139        $property->setValue($credentials, $this->getOAuth2Mock());
140
141        return $credentials;
142    }
143
144    private function getOAuth2()
145    {
146        $oauth = new OAuth2([
147            'access_token' => 'xyz',
148            'expires_at' => strtotime('2001'),
149        ]);
150
151        return $oauth;
152    }
153
154    private function getOAuth2Mock()
155    {
156        $mock = $this->getMockBuilder('Google\Auth\OAuth2')
157            ->disableOriginalConstructor()
158            ->getMock();
159
160        $mock
161            ->expects($this->once())
162            ->method('getLastReceivedToken')
163            ->will($this->returnValue([
164                'access_token' => 'xyz',
165                'expires_at' => strtotime('2001'),
166            ]));
167
168        return $mock;
169    }
170}