Skip to content

Commit

Permalink
Merge pull request #265 from foxworth42/improve-test-coverage
Browse files Browse the repository at this point in the history
Improve test coverage
  • Loading branch information
weaverryan authored Oct 4, 2020

Verified

This commit was signed with the committer’s verified signature.
sourabhxyz Sourabh
2 parents 9984ff5 + 3d9549f commit 822b1a4
Showing 12 changed files with 524 additions and 2 deletions.
90 changes: 90 additions & 0 deletions tests/Client/ClientRegistryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/*
* OAuth2 Client Bundle
* Copyright (c) KnpUniversity <http://knpuniversity.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KnpU\OAuth2ClientBundle\Tests\Client;

use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ClientRegistryTest extends TestCase
{
public function testShouldKnowWhatServicesAreConfigured()
{
$mockServiceMap = [
'facebook' => 'knpu.oauth2.client.facebook',
'google' => 'knpu.oauth2.client.google',
'okta' => 'knpu.oauth2.client.okta'
];
$mockContainer = $this->getMockBuilder(ContainerInterface::class)->disableOriginalConstructor()->getMock();

$testClientRegistry = new ClientRegistry($mockContainer, $mockServiceMap);

$results = $testClientRegistry->getEnabledClientKeys();
$this->assertEquals([
"facebook",
"google",
"okta"
], $results);
}

public function testShouldThrowExceptionIfClientDoesNotExist()
{
$mockServiceMap = [
'facebook' => 'knpu.oauth2.client.facebook',
'google' => 'knpu.oauth2.client.google',
'okta' => 'knpu.oauth2.client.okta'
];
$mockContainer = $this->getMockBuilder(ContainerInterface::class)->disableOriginalConstructor()->getMock();

$testClientRegistry = new ClientRegistry($mockContainer, $mockServiceMap);

$this->expectException(\InvalidArgumentException::class);

$testClientRegistry->getClient("unknownClient");
}

public function testShouldThrowExceptionIfClientExistsButNotOAuth2Client()
{
$mockServiceMap = [
'facebook' => 'knpu.oauth2.client.facebook',
'google' => 'knpu.oauth2.client.google',
'okta' => 'knpu.oauth2.client.okta',
'invalid' => 'knpu.oauth2.client.invalid'
];
$mockContainer = $this->getMockBuilder(ContainerInterface::class)->disableOriginalConstructor()->getMock();
$mockContainer->method("get")->willReturn(\stdClass::class);

$testClientRegistry = new ClientRegistry($mockContainer, $mockServiceMap);

$this->expectException(\InvalidArgumentException::class);

$testClientRegistry->getClient("invalid");
}

public function testShouldReturnValidClient()
{
$mockServiceMap = [
'facebook' => 'knpu.oauth2.client.facebook',
'google' => 'knpu.oauth2.client.google',
'okta' => 'knpu.oauth2.client.okta'
];
$mockClient = $this->getMockBuilder(OAuth2ClientInterface::class)->getMock();
$mockContainer = $this->getMockBuilder(ContainerInterface::class)->disableOriginalConstructor()->getMock();
$mockContainer->method("get")->willReturn($mockClient);

$testClientRegistry = new ClientRegistry($mockContainer, $mockServiceMap);

$result = $testClientRegistry->getClient("facebook");

$this->assertInstanceOf(OAuth2ClientInterface::class, $result);
}
}
52 changes: 52 additions & 0 deletions tests/Client/OAuth2ClientTest.php
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
use KnpU\OAuth2ClientBundle\Client\OAuth2Client;
use KnpU\OAuth2ClientBundle\Exception\InvalidStateException;
use KnpU\OAuth2ClientBundle\Exception\MissingAuthorizationCodeException;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\FacebookUser;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
@@ -233,4 +234,55 @@ public function testFetchUser()
$this->assertInstanceOf('League\OAuth2\Client\Provider\FacebookUser', $user);
$this->assertEquals('testUser', $user->getName());
}

public function testShouldReturnProviderObject()
{
$testClient = new OAuth2Client(
$this->provider->reveal(),
$this->requestStack
);

$result = $testClient->getOAuth2Provider();

$this->assertInstanceOf(AbstractProvider::class, $result);
}

public function testShouldThrowExceptionOnRedirectIfNoSessionAndNotRunningStateless()
{
$this->requestStack = new RequestStack();
$this->requestStack->push(new Request());

$testClient = new OAuth2Client(
$this->provider->reveal(),
$this->requestStack
);

$this->expectException(\LogicException::class);
$testClient->redirect();
}

public function testShouldThrowExceptionOnGetAccessTokenIfNoSessionAndNotRunningStateless()
{
$this->requestStack = new RequestStack();
$this->requestStack->push(new Request());

$testClient = new OAuth2Client(
$this->provider->reveal(),
$this->requestStack
);

$this->expectException(\LogicException::class);
$testClient->getAccessToken();
}

public function testShouldThrowExceptionIfThereIsNoRequestInTheStack()
{
$testClient = new OAuth2Client(
$this->provider->reveal(),
new RequestStack()
);

$this->expectException(\LogicException::class);
$testClient->getAccessToken();
}
}
66 changes: 66 additions & 0 deletions tests/Client/Provider/BatchProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* OAuth2 Client Bundle
* Copyright (c) KnpUniversity <http://knpuniversity.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KnpU\OAuth2ClientBundle\Tests\Client\Provider;

use KnpU\OAuth2ClientBundle\Client\OAuth2Client;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
use League\OAuth2\Client\Token\AccessToken;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class BatchProviderTest extends TestCase
{
public function testProviders()
{
// This is basically just validating that the clients are sane/implementing OAuth2Client

$mockAccessToken = $this->getMockBuilder(AccessToken::class)->disableOriginalConstructor()->getMock();
$mockProvider = $this->getMockProvider($mockAccessToken);
$mockRequestStack = $this->getMockRequestStack($this->getMockRequest());

$clients = scandir(__DIR__ . "/../../../src/Client/Provider");
foreach($clients as $client) {
if(substr($client, -4, 4) !== ".php") { continue; }

$client = sprintf("KnpU\OAuth2ClientBundle\Client\Provider\%s", explode(".", $client)[0]);
$testClient = new $client($mockProvider, $mockRequestStack);
$testClient->setAsStateless();
$this->assertTrue(is_subclass_of($testClient, OAuth2Client::class));

$this->assertInstanceOf(ResourceOwnerInterface::class, $testClient->fetchUserFromToken($mockAccessToken));
$this->assertInstanceOf(ResourceOwnerInterface::class, $testClient->fetchUser());
}
}

private function getMockProvider($mockAccessToken)
{
$mockProvider = $this->getMockBuilder(AbstractProvider::class)->getMock();
$mockProvider->method("getResourceOwner")->willReturn($this->getMockBuilder(ResourceOwnerInterface::class)->getMock());
$mockProvider->method("getAccessToken")->willReturn($mockAccessToken);
return $mockProvider;
}

private function getMockRequest()
{
$mockRequest = $this->getMockBuilder(Request::class)->getMock();
$mockRequest->method("get")->willReturn(true);
return $mockRequest;
}

private function getMockRequestStack($mockRequest)
{
$mockRequestStack = $this->getMockBuilder(RequestStack::class)->getMock();
$mockRequestStack->method("getCurrentRequest")->willReturn($mockRequest);
return $mockRequestStack;
}
}
62 changes: 62 additions & 0 deletions tests/DependencyInjection/ProviderFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* OAuth2 Client Bundle
* Copyright (c) KnpUniversity <http://knpuniversity.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KnpU\OAuth2ClientBundle\Tests\DependencyInjection;

use KnpU\OAuth2ClientBundle\DependencyInjection\ProviderFactory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ProviderFactoryTest extends TestCase
{
public function testShouldCreateProvider()
{
$redirectUri = "http://redirect.url";
$mockGenerator = $this->getMockGenerator($redirectUri);
$mockGenerator->expects($this->once())->method("generate");

$testProviderFactory = new ProviderFactory($mockGenerator);

$httpsRedirectUri = "symfony_route_name";
$result = $testProviderFactory->createProvider(MockProvider::class, [], $httpsRedirectUri);

$this->assertInstanceOf(MockProvider::class, $result);
$this->assertEquals(["redirectUri" => $redirectUri], $result->getOptions());
$this->assertEquals([], $result->getCollaborators());
}

private function getMockGenerator($generateReturn)
{
$mockGenerator = $this->getMockBuilder(UrlGeneratorInterface::class)->disableOriginalConstructor()->getMock();
$mockGenerator->method("generate")->willReturn($generateReturn);
return $mockGenerator;
}
}

class MockProvider
{
private $options;
private $collaborators;
public function __construct($options, $collaborators)
{
$this->options = $options;
$this->collaborators = $collaborators;
}

public function getOptions()
{
return $this->options;
}

public function getCollaborators()
{
return $this->collaborators;
}
}
26 changes: 26 additions & 0 deletions tests/KnpUOAuth2ClientBundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* OAuth2 Client Bundle
* Copyright (c) KnpUniversity <http://knpuniversity.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KnpU\OAuth2ClientBundle\Tests;

use KnpU\OAuth2ClientBundle\DependencyInjection\KnpUOAuth2ClientExtension;
use KnpU\OAuth2ClientBundle\KnpUOAuth2ClientBundle;
use PHPUnit\Framework\TestCase;

class KnpUOAuth2ClientBundleTest extends TestCase
{
public function testShouldReturnNewContainerExtension()
{
$testBundle = new KnpUOAuth2ClientBundle();

$result = $testBundle->getContainerExtension();
$this->assertInstanceOf(KnpUOAuth2ClientExtension::class, $result);
}
}
26 changes: 26 additions & 0 deletions tests/Security/Authenticator/SocialAuthenticatorTest.php
Original file line number Diff line number Diff line change
@@ -10,10 +10,14 @@

namespace KnpU\OAuth2ClientBundle\Tests\Security\Authenticator;

use KnpU\OAuth2ClientBundle\Exception\InvalidStateException;
use KnpU\OAuth2ClientBundle\Exception\MissingAuthorizationCodeException;
use KnpU\OAuth2ClientBundle\Security\Authenticator\SocialAuthenticator;
use KnpU\OAuth2ClientBundle\Client\OAuth2Client;
use KnpU\OAuth2ClientBundle\Security\Exception\IdentityProviderAuthenticationException;
use KnpU\OAuth2ClientBundle\Security\Exception\InvalidStateAuthenticationException;
use KnpU\OAuth2ClientBundle\Security\Exception\NoAuthCodeAuthenticationException;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -46,6 +50,28 @@ public function testFetchAccessTokenThrowsAuthenticationException()
$authenticator->doFetchAccessToken($client->reveal());
}

public function testFetchAccessTokenThrowsIdentityProviderException()
{
$this->expectException(IdentityProviderAuthenticationException::class);
$authenticator = new StubSocialAuthenticator();
$client = $this->prophesize('KnpU\OAuth2ClientBundle\Client\OAuth2Client');
$client->getAccessToken([])
->willThrow(new IdentityProviderException("message", 42, "response"));

$authenticator->doFetchAccessToken($client->reveal());
}

public function testFetchAccessTokenThrowsInvalidStateException()
{
$this->expectException(InvalidStateAuthenticationException::class);
$authenticator = new StubSocialAuthenticator();
$client = $this->prophesize('KnpU\OAuth2ClientBundle\Client\OAuth2Client');
$client->getAccessToken([])
->willThrow(new InvalidStateException());

$authenticator->doFetchAccessToken($client->reveal());
}

public function testCheckCredentials()
{
$authenticator = new StubSocialAuthenticator();
Original file line number Diff line number Diff line change
@@ -24,5 +24,6 @@ public function testException()
$e = new FinishRegistrationException($userInfo, '', 0);

$this->assertEquals($e->getUserInformation(), $userInfo);
$this->assertEquals($e->getMessageKey(), 'You need to finish registration to login.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* OAuth2 Client Bundle
* Copyright (c) KnpUniversity <http://knpuniversity.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KnpU\OAuth2ClientBundle\Tests\Security\Exception;

use KnpU\OAuth2ClientBundle\Security\Exception\IdentityProviderAuthenticationException;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use PHPUnit\Framework\TestCase;

class IdentityProviderAuthenticationExceptionTest extends TestCase
{
public function testException()
{
$mockProviderException = new IdentityProviderException("Message", 0, "Response");

$testException = new IdentityProviderAuthenticationException($mockProviderException);
$this->assertEquals('Error fetching OAuth credentials: "%error%".', $testException->getMessageKey());
$this->assertEquals(["%error%" => "Message"], $testException->getMessageData());
}
}
Loading

0 comments on commit 822b1a4

Please sign in to comment.