Skip to content

Commit

Permalink
Add ResourceTypeHandlerFactoryTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
hswong3i committed Jul 12, 2014
1 parent 6659aae commit ec32b06
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Tests/ResourceType/BarResourceTypeHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* This file is part of the authbucket/oauth2-bundle package.
*
* (c) Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace AuthBucket\Bundle\OAuth2Bundle\Tests\ResourceType;

use AuthBucket\OAuth2\Model\ModelManagerFactoryInterface;
use AuthBucket\OAuth2\ResourceType\ResourceTypeHandlerInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class BarResourceTypeHandler implements ResourceTypeHandlerInterface
{
public function handle(
HttpKernelInterface $httpKernel,
ModelManagerFactoryInterface $modelManagerFactory,
$accessToken,
array $options = array()
)
{
}
}
16 changes: 16 additions & 0 deletions Tests/ResourceType/FooResourceTypeHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* This file is part of the authbucket/oauth2-bundle package.
*
* (c) Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace AuthBucket\Bundle\OAuth2Bundle\Tests\ResourceType;

class FooResourceTypeHandler
{
}
58 changes: 58 additions & 0 deletions Tests/ResourceType/ResourceTypeHandlerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* This file is part of the authbucket/oauth2-bundle package.
*
* (c) Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace AuthBucket\Bundle\OAuth2Bundle\Tests\ResourceType;

use AuthBucket\OAuth2\ResourceType\ResourceTypeHandlerFactory;

class ResourceTypeHandlerFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \AuthBucket\OAuth2\Exception\ServerErrorException
*/
public function testNonExistsResourceTypeHandler()
{
$resourceTypeHandlerFactory = new ResourceTypeHandlerFactory(array(
'foo' => 'AuthBucket\\Bundle\\OAuth2Bundle\\Tests\\ResourceType\\NonExistsResourceTypeHandler',
));
$resourceTypeHandlerFactory->addResourceTypeHandler('foo', $responseTypeHandler);
}

/**
* @expectedException \AuthBucket\OAuth2\Exception\ServerErrorException
*/
public function testBadAddResourceTypeHandler()
{
$resourceTypeHandlerFactory = new ResourceTypeHandlerFactory(array(
'foo' => 'AuthBucket\\Bundle\\OAuth2Bundle\\Tests\\ResourceType\\FooResourceTypeHandler',
));
$resourceTypeHandlerFactory->addResourceTypeHandler('foo', $responseTypeHandler);
}

/**
* @expectedException \AuthBucket\OAuth2\Exception\ServerErrorException
*/
public function testBadGetResourceTypeHandler()
{
$resourceTypeHandlerFactory = new ResourceTypeHandlerFactory(array(
'bar' => 'AuthBucket\\Bundle\\OAuth2Bundle\\Tests\\ResourceType\\BarResourceTypeHandler',
));
$resourceTypeHandlerFactory->getResourceTypeHandler('foo');
}

public function testGoodGetResourceTypeHandler()
{
$resourceTypeHandlerFactory = new ResourceTypeHandlerFactory(array(
'bar' => 'AuthBucket\\Bundle\\OAuth2Bundle\\Tests\\ResourceType\\BarResourceTypeHandler',
));
$resourceTypeHandlerFactory->getResourceTypeHandler('bar');
}
}
60 changes: 60 additions & 0 deletions Tests/Security/Authentication/Provider/ResourceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* This file is part of the authbucket/oauth2-bundle package.
*
* (c) Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace AuthBucket\Bundle\OAuth2Bundle\Tests\Security\Authentication\Provider;

use AuthBucket\Bundle\OAuth2Bundle\Tests\WebTestCase;
use Symfony\Component\HttpFoundation\Request;

class ResourceProviderTest extends WebTestCase
{
public function testNonCompatibileScope()
{
$parameters = array(
'debug_token' => 'bcc105b66698a64ed23c87b967885289',
);
$server = array(
'HTTP_Authorization' => implode(' ', array('Bearer', 'bcc105b66698a64ed23c87b967885289')),
);
$client = $this->createClient();
$crawler = $client->request('GET', '/resource/debug/model', $parameters, array(), $server);
$resourceResponse = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals('invalid_scope', $resourceResponse['error']);
}

public function testEnoughScope()
{
$parameters = array(
'debug_token' => 'eeb5aa92bbb4b56373b9e0d00bc02d93',
);
$server = array(
'HTTP_Authorization' => implode(' ', array('Bearer', 'eeb5aa92bbb4b56373b9e0d00bc02d93')),
);
$client = $this->createClient();
$crawler = $client->request('GET', '/resource/debug/model', $parameters, array(), $server);
$resourceResponse = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals('demousername1', $resourceResponse['username']);
}

public function testMoreScope()
{
$parameters = array(
'debug_token' => 'ba2e8d1f54ed3e3d96935796576f1a06',
);
$server = array(
'HTTP_Authorization' => implode(' ', array('Bearer', 'ba2e8d1f54ed3e3d96935796576f1a06')),
);
$client = $this->createClient();
$crawler = $client->request('GET', '/resource/debug/model', $parameters, array(), $server);
$resourceResponse = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals('demousername1', $resourceResponse['username']);
}
}
23 changes: 23 additions & 0 deletions Tests/TestBundle/DataFixtures/ORM/AccessTokenFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ public function load(ObjectManager $manager)
));
$manager->persist($model);

$model = new AccessToken();
$model->setAccessToken('ba2e8d1f54ed3e3d96935796576f1a06')
->setTokenType('bearer')
->setClientId('http://democlient1.com/')
->setUsername('demousername1')
->setExpires(new \DateTime('+1 hours'))
->setScope(array(
'demoscope1',
'demoscope2',
));
$manager->persist($model);

$model = new AccessToken();
$model->setAccessToken('bcc105b66698a64ed23c87b967885289')
->setTokenType('bearer')
->setClientId('http://democlient1.com/')
->setUsername('demousername1')
->setExpires(new \DateTime('+1 hours'))
->setScope(array(
'demoscope3',
));
$manager->persist($model);

$manager->flush();
}
}

0 comments on commit ec32b06

Please sign in to comment.