PageRenderTime 61ms CodeModel.GetById 18ms app.highlight 36ms RepoModel.GetById 2ms app.codeStats 0ms

/Tests/Security/Authentication/Provider/FacebookProviderTest.php

http://github.com/FriendsOfSymfony/FOSFacebookBundle
PHP | 342 lines | 238 code | 57 blank | 47 comment | 0 complexity | b5ab313c48c6bd2f3b437a93b8f33554 MD5 | raw file
  1<?php
  2
  3/*
  4 * This file is part of the FOSFacebookBundle package.
  5 *
  6 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  7 *
  8 * For the full copyright and license information, please view the LICENSE
  9 * file that was distributed with this source code.
 10 */
 11
 12namespace FOS\FacebookBundle\Tests\Security\Authentication\Provider;
 13
 14use FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken;
 15
 16use FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider;
 17use Symfony\Component\Security\Core\Exception\AuthenticationException;
 18
 19class FacebookProviderTest extends \PHPUnit_Framework_TestCase
 20{
 21    /**
 22     * @expectedException \InvalidArgumentException
 23     */
 24    public function testThatUserCheckerCannotBeNullWhenUserProviderIsNotNull()
 25    {
 26        $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
 27            ->disableOriginalConstructor()
 28            ->getMock();
 29        new FacebookProvider('main', $facebookMock, $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'));
 30    }
 31
 32    /**
 33     * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate
 34     */
 35    public function testThatCannotAuthenticateWhenTokenIsNotFacebookUserToken()
 36    {
 37        $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
 38            ->disableOriginalConstructor()
 39            ->getMock();
 40        $facebookProvider = new FacebookProvider('main', $facebookMock);
 41        $this->assertNull($facebookProvider->authenticate($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
 42    }
 43
 44    /**
 45     * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate
 46     * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::supports
 47     */
 48    public function testThatCannotAuthenticateWhenTokenFromOtherFirewall()
 49    {
 50        $providerKeyForProvider = 'main';
 51        $providerKeyForToken    = 'connect';
 52
 53        $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
 54            ->disableOriginalConstructor()
 55            ->getMock();
 56        $facebookProvider = new FacebookProvider($providerKeyForProvider, $facebookMock);
 57
 58        $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getProviderKey'), array($providerKeyForToken));
 59        $tokenMock->expects($this->any())
 60            ->method('getProviderKey')
 61            ->will($this->returnValue($providerKeyForToken));
 62
 63        $this->assertFalse($facebookProvider->supports($tokenMock));
 64        $this->assertNull($facebookProvider->authenticate($tokenMock));
 65    }
 66
 67    /**
 68     * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate
 69     * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::supports
 70     * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::createAuthenticatedToken
 71     */
 72    public function testThatCanAuthenticateUserWithoutUserProvider()
 73    {
 74        $providerKey = 'main';
 75
 76        $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
 77            ->disableOriginalConstructor()
 78            ->setMethods(array('getUser'))
 79            ->getMock();
 80        $facebookMock->expects($this->once())
 81            ->method('getUser')
 82            ->will($this->returnValue('123'));
 83
 84        $facebookProvider = new FacebookProvider($providerKey, $facebookMock);
 85
 86        $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getAttributes', 'getProviderKey'), array($providerKey));
 87        $tokenMock->expects($this->once())
 88            ->method('getAttributes')
 89            ->will($this->returnValue(array()));
 90        $tokenMock->expects($this->any())
 91            ->method('getProviderKey')
 92            ->will($this->returnValue($providerKey));
 93
 94        $this->assertTrue($facebookProvider->supports($tokenMock));
 95        $this->assertEquals('123', $facebookProvider->authenticate($tokenMock)->getUser());
 96    }
 97
 98    /**
 99     * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException
100     */
101    public function testThatCannotAuthenticateWhenUserProviderThrowsAuthenticationException()
102    {
103        $providerKey = 'main';
104
105        $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
106            ->disableOriginalConstructor()
107            ->setMethods(array('getUser'))
108            ->getMock();
109        $facebookMock->expects($this->once())
110            ->method('getUser')
111            ->will($this->returnValue('123'));
112
113        $userProviderMock = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
114        $userProviderMock->expects($this->once())
115            ->method('loadUserByUsername')
116            ->with('123')
117            ->will($this->throwException(new AuthenticationException('test')));
118
119        $userCheckerMock = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
120        $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getProviderKey'), array($providerKey));
121        $tokenMock->expects($this->any())
122            ->method('getProviderKey')
123            ->will($this->returnValue($providerKey));
124
125        $facebookProvider = new FacebookProvider($providerKey, $facebookMock, $userProviderMock, $userCheckerMock);
126        $facebookProvider->authenticate($tokenMock);
127    }
128
129    /**
130     * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException
131     */
132    public function testThatCannotAuthenticateWhenUserProviderDoesNotReturnUserInterface()
133    {
134        $providerKey = 'main';
135
136        $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
137            ->disableOriginalConstructor()
138            ->setMethods(array('getUser'))
139            ->getMock();
140        $facebookMock->expects($this->once())
141            ->method('getUser')
142            ->will($this->returnValue('123'));
143
144        $userProviderMock = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
145        $userProviderMock->expects($this->once())
146            ->method('loadUserByUsername')
147            ->with('123')
148            ->will($this->returnValue('234'));
149
150        $userCheckerMock = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
151        $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getProviderKey'), array($providerKey));
152        $tokenMock->expects($this->any())
153            ->method('getProviderKey')
154            ->will($this->returnValue($providerKey));
155
156        $facebookProvider = new FacebookProvider($providerKey, $facebookMock, $userProviderMock, $userCheckerMock);
157        $facebookProvider->authenticate($tokenMock);
158    }
159
160    /**
161     * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException
162     */
163    public function testThatCannotAuthenticateWhenCannotRetrieveFacebookUserFromSession()
164    {
165        $providerKey = 'main';
166
167        $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
168            ->disableOriginalConstructor()
169            ->setMethods(array('getUser'))
170            ->getMock();
171        $facebookMock->expects($this->once())
172            ->method('getUser')
173            ->will($this->returnValue(false));
174
175        $userProviderMock = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
176        $userCheckerMock = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
177
178        $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getProviderKey'), array($providerKey));
179        $tokenMock->expects($this->any())
180            ->method('getProviderKey')
181            ->will($this->returnValue($providerKey));
182
183        $facebookProvider = new FacebookProvider($providerKey, $facebookMock, $userProviderMock, $userCheckerMock);
184        $facebookProvider->authenticate($tokenMock);
185    }
186
187    /**
188     * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate
189     * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::createAuthenticatedToken
190     */
191    public function testThatCanAutenticateUsingUserProvider()
192    {
193        $providerKey = 'main';
194
195        $userMock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
196        $userMock->expects($this->once())
197            ->method('getUsername')
198            ->will($this->returnValue('l3l0'));
199        $userMock->expects($this->once())
200            ->method('getRoles')
201            ->will($this->returnValue(array()));
202
203        $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
204            ->disableOriginalConstructor()
205            ->setMethods(array('getUser'))
206            ->getMock();
207        $facebookMock->expects($this->once())
208            ->method('getUser')
209            ->will($this->returnValue('123'));
210
211        $userProviderMock = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
212        $userProviderMock->expects($this->once())
213            ->method('loadUserByUsername')
214            ->with('123')
215            ->will($this->returnValue($userMock));
216
217        $userCheckerMock = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
218        $userCheckerMock->expects($this->once())
219            ->method('checkPostAuth');
220
221        $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getAttributes', 'getProviderKey'), array($providerKey));
222        $tokenMock->expects($this->once())
223            ->method('getAttributes')
224            ->will($this->returnValue(array()));
225        $tokenMock->expects($this->any())
226            ->method('getProviderKey')
227            ->will($this->returnValue($providerKey));
228
229        $facebookProvider = new FacebookProvider($providerKey, $facebookMock, $userProviderMock, $userCheckerMock);
230        $this->assertEquals('l3l0', $facebookProvider->authenticate($tokenMock)->getUsername());
231    }
232
233    /**
234     * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException
235     */
236    public function testThatAccessTokenIsSetToFacebookSessionPersistenceWithAccessTokenFromFacebookUserToken()
237    {
238        $providerKey = 'main';
239        $accessToken = 'AbCd';
240
241        $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
242            ->disableOriginalConstructor()
243            ->setMethods(array('setAccessToken','getUser'))
244            ->getMock();
245        $facebookMock->expects($this->once())
246          ->method('setAccessToken')
247          ->with($accessToken);
248        $facebookMock->expects($this->once())
249            ->method('getUser');
250
251        $userProviderMock = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
252
253        $userCheckerMock = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
254
255        $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getProviderKey','getAccessToken'), array($providerKey,'',array(),$accessToken));
256        $tokenMock->expects($this->any())
257            ->method('getProviderKey')
258            ->will($this->returnValue($providerKey));
259        $tokenMock->expects($this->any())
260             ->method('getAccessToken')
261             ->will($this->returnValue($accessToken));
262
263        $facebookProvider = new FacebookProvider($providerKey, $facebookMock, $userProviderMock, $userCheckerMock);
264        $facebookProvider->authenticate($tokenMock);
265    }
266
267    /**
268     * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate
269     * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::createAuthenticatedToken
270     */
271    public function testThatAccessTokenIsSetToNewFacebookUserTokenWhenAuthenticateWithUserProvider()
272    {
273        $providerKey = 'main';
274        $accessToken = 'AbCd';
275
276        $userMock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
277        $userMock->expects($this->once())
278            ->method('getRoles')
279            ->will($this->returnValue(array()));
280
281        $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
282            ->disableOriginalConstructor()
283            ->setMethods(array('getUser'))
284            ->getMock();
285
286        $facebookMock->expects($this->once())
287            ->method('getUser')
288            ->will($this->returnValue('123'));
289
290        $userProviderMock = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
291        $userProviderMock->expects($this->once())
292            ->method('loadUserByUsername')
293            ->with('123')
294            ->will($this->returnValue($userMock));
295
296        $userCheckerMock = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
297        $userCheckerMock->expects($this->once())
298            ->method('checkPostAuth');
299
300        $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getAttributes', 'getProviderKey'), array($providerKey,'',array(),$accessToken));
301        $tokenMock->expects($this->once())
302            ->method('getAttributes')
303            ->will($this->returnValue(array()));
304        $tokenMock->expects($this->any())
305            ->method('getProviderKey')
306            ->will($this->returnValue($providerKey));
307
308        $facebookProvider = new FacebookProvider($providerKey, $facebookMock, $userProviderMock, $userCheckerMock);
309        $this->assertEquals($accessToken, $facebookProvider->authenticate($tokenMock)->getAccessToken());
310    }
311
312    /**
313     * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate
314     * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::createAuthenticatedToken
315     */
316    public function testThatAccessTokenIsSetToNewFacebookUserTokenWhenAuthenticateWithoutUserProvider()
317    {
318        $providerKey = 'main';
319        $accessToken = 'AbCd';
320
321        $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
322            ->disableOriginalConstructor()
323            ->setMethods(array('getUser'))
324            ->getMock();
325        $facebookMock->expects($this->once())
326            ->method('getUser')
327            ->will($this->returnValue('123'));
328
329        $facebookProvider = new FacebookProvider($providerKey, $facebookMock);
330
331        $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getAttributes', 'getProviderKey'), array($providerKey,'',array(),$accessToken));
332        $tokenMock->expects($this->once())
333            ->method('getAttributes')
334            ->will($this->returnValue(array()));
335        $tokenMock->expects($this->any())
336            ->method('getProviderKey')
337            ->will($this->returnValue($providerKey));
338
339        $this->assertEquals($accessToken, $facebookProvider->authenticate($tokenMock)->getAccessToken());
340
341    }
342}