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\FetchAuthTokenCache;
21
22class FetchAuthTokenCacheTest extends BaseTest
23{
24    protected function setUp()
25    {
26        $this->mockFetcher =
27            $this
28                ->getMockBuilder('Google\Auth\FetchAuthTokenInterface')
29                ->getMock();
30        $this->mockCacheItem =
31            $this
32                ->getMockBuilder('Psr\Cache\CacheItemInterface')
33                ->getMock();
34        $this->mockCache =
35            $this
36                ->getMockBuilder('Psr\Cache\CacheItemPoolInterface')
37                ->getMock();
38    }
39
40    public function testUsesCachedAuthToken()
41    {
42        $cacheKey = 'myKey';
43        $cachedValue = '2/abcdef1234567890';
44        $this->mockCacheItem
45            ->expects($this->once())
46            ->method('isHit')
47            ->will($this->returnValue(true));
48        $this->mockCacheItem
49            ->expects($this->once())
50            ->method('get')
51            ->will($this->returnValue($cachedValue));
52        $this->mockCache
53            ->expects($this->once())
54            ->method('getItem')
55            ->with($this->equalTo($cacheKey))
56            ->will($this->returnValue($this->mockCacheItem));
57        $this->mockFetcher
58            ->expects($this->never())
59            ->method('fetchAuthToken');
60        $this->mockFetcher
61            ->expects($this->any())
62            ->method('getCacheKey')
63            ->will($this->returnValue($cacheKey));
64
65        // Run the test.
66        $cachedFetcher = new FetchAuthTokenCache(
67            $this->mockFetcher,
68            null,
69            $this->mockCache
70        );
71        $accessToken = $cachedFetcher->fetchAuthToken();
72        $this->assertEquals($accessToken, ['access_token' => $cachedValue]);
73    }
74
75    public function testGetsCachedAuthTokenUsingCachePrefix()
76    {
77        $prefix = 'test_prefix_';
78        $cacheKey = 'myKey';
79        $cachedValue = '2/abcdef1234567890';
80        $this->mockCacheItem
81            ->expects($this->once())
82            ->method('isHit')
83            ->will($this->returnValue(true));
84        $this->mockCacheItem
85            ->expects($this->once())
86            ->method('get')
87            ->will($this->returnValue($cachedValue));
88        $this->mockCache
89            ->expects($this->once())
90            ->method('getItem')
91            ->with($this->equalTo($prefix . $cacheKey))
92            ->will($this->returnValue($this->mockCacheItem));
93        $this->mockFetcher
94            ->expects($this->never())
95            ->method('fetchAuthToken');
96        $this->mockFetcher
97            ->expects($this->any())
98            ->method('getCacheKey')
99            ->will($this->returnValue($cacheKey));
100
101        // Run the test
102        $cachedFetcher = new FetchAuthTokenCache(
103            $this->mockFetcher,
104            ['prefix' => $prefix],
105            $this->mockCache
106        );
107        $accessToken = $cachedFetcher->fetchAuthToken();
108        $this->assertEquals($accessToken, ['access_token' => $cachedValue]);
109    }
110
111    public function testShouldSaveValueInCacheWithCacheOptions()
112    {
113        $prefix = 'test_prefix_';
114        $lifetime = '70707';
115        $cacheKey = 'myKey';
116        $token = '1/abcdef1234567890';
117        $authResult = ['access_token' => $token];
118        $this->mockCacheItem
119            ->expects($this->any())
120            ->method('get')
121            ->will($this->returnValue(null));
122        $this->mockCacheItem
123            ->expects($this->once())
124            ->method('set')
125            ->with($this->equalTo($token))
126            ->will($this->returnValue(false));
127        $this->mockCacheItem
128            ->expects($this->once())
129            ->method('expiresAfter')
130            ->with($this->equalTo($lifetime));
131        $this->mockCache
132            ->expects($this->exactly(2))
133            ->method('getItem')
134            ->with($this->equalTo($prefix . $cacheKey))
135            ->will($this->returnValue($this->mockCacheItem));
136        $this->mockFetcher
137            ->expects($this->any())
138            ->method('getCacheKey')
139            ->will($this->returnValue($cacheKey));
140        $this->mockFetcher
141            ->expects($this->once())
142            ->method('fetchAuthToken')
143            ->will($this->returnValue($authResult));
144
145        // Run the test
146        $cachedFetcher = new FetchAuthTokenCache(
147            $this->mockFetcher,
148            ['prefix' => $prefix, 'lifetime' => $lifetime],
149            $this->mockCache
150        );
151        $accessToken = $cachedFetcher->fetchAuthToken();
152        $this->assertEquals($accessToken, ['access_token' => $token]);
153    }
154
155    public function testGetLastReceivedToken()
156    {
157        $token = 'foo';
158
159        $mockFetcher = $this->prophesize('Google\Auth\FetchAuthTokenInterface');
160        $mockFetcher->getLastReceivedToken()
161            ->shouldBeCalled()
162            ->willReturn([
163                'access_token' => $token
164            ]);
165
166        $fetcher = new FetchAuthTokenCache(
167            $mockFetcher->reveal(),
168            [],
169            $this->mockCache
170        );
171
172        $this->assertEquals($token, $fetcher->getLastReceivedToken()['access_token']);
173    }
174
175    public function testGetClientName()
176    {
177        $name = 'test@example.com';
178
179        $mockFetcher = $this->prophesize('Google\Auth\SignBlobInterface');
180        $mockFetcher->getClientName(null)
181            ->shouldBeCalled()
182            ->willReturn($name);
183
184        $fetcher = new FetchAuthTokenCache(
185            $mockFetcher->reveal(),
186            [],
187            $this->mockCache
188        );
189
190        $this->assertEquals($name, $fetcher->getClientName());
191    }
192
193    public function testSignBlob()
194    {
195        $stringToSign = 'foobar';
196        $signature = 'helloworld';
197
198        $mockFetcher = $this->prophesize('Google\Auth\SignBlobInterface');
199        $mockFetcher->willImplement('Google\Auth\FetchAuthTokenInterface');
200        $mockFetcher->signBlob($stringToSign, true)
201            ->shouldBeCalled()
202            ->willReturn($signature);
203
204        $fetcher = new FetchAuthTokenCache(
205            $mockFetcher->reveal(),
206            [],
207            $this->mockCache
208        );
209
210        $this->assertEquals($signature, $fetcher->signBlob($stringToSign, true));
211    }
212
213    /**
214     * @expectedException RuntimeException
215     */
216    public function testSignBlobInvalidFetcher()
217    {
218        $mockFetcher = $this->prophesize('Google\Auth\FetchAuthTokenInterface');
219        $mockFetcher->signBlob('test')
220            ->shouldNotbeCalled();
221
222        $fetcher = new FetchAuthTokenCache(
223            $mockFetcher->reveal(),
224            [],
225            $this->mockCache
226        );
227
228        $this->assertEquals($signature, $fetcher->signBlob('test'));
229    }
230}
231