Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 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
 
18
namespace Google\Auth\tests;
19
 
20
use Google\Auth\Credentials\AppIdentityCredentials;
21
use Google\Auth\Credentials\GCECredentials;
22
use Google\Auth\Credentials\ServiceAccountCredentials;
23
use Google\Auth\Credentials\ServiceAccountJwtAccessCredentials;
24
use Google\Auth\Credentials\UserRefreshCredentials;
25
use Google\Auth\CredentialsLoader;
26
use Google\Auth\FetchAuthTokenInterface;
27
use Google\Auth\OAuth2;
28
 
29
class 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
}