Skip to content

Commit

Permalink
Revoke all unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
hswong3i committed Aug 12, 2014
1 parent 3f65b21 commit ca7065c
Show file tree
Hide file tree
Showing 26 changed files with 535 additions and 173 deletions.
43 changes: 43 additions & 0 deletions Controller/ModelController.php
@@ -0,0 +1,43 @@
<?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\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class ModelController extends Controller
{
public function createModelAction(Request $request, $type)
{
return $this->get('authbucket_oauth2.model_controller')->createModelAction($request, $type);
}

public function readModelAction(Request $request, $type, $id)
{
return $this->get('authbucket_oauth2.model_controller')->readModelAction($request, $type, $id);
}

public function updateModelAction(Request $request, $type, $id)
{
return $this->get('authbucket_oauth2.model_controller')->updateModelAction($request, $type, $id);
}

public function deleteModelAction(Request $request, $type, $id)
{
return $this->get('authbucket_oauth2.model_controller')->deleteModelAction($request, $type, $id);
}

public function listModelAction(Request $request, $type)
{
return $this->get('authbucket_oauth2.model_controller')->listModelAction($request, $type);
}
}
6 changes: 3 additions & 3 deletions Resources/config/routing.yml
@@ -1,11 +1,11 @@
oauth2_authorize:
pattern: /authorize
pattern: /oauth2/authorize
defaults: { _controller: AuthBucketOAuth2Bundle:Authorize:authorize }

oauth2_token:
pattern: /token
pattern: /oauth2/token
defaults: { _controller: AuthBucketOAuth2Bundle:Token:token }

oauth2_debug:
pattern: /debug
pattern: /oauth2/debug
defaults: { _controller: AuthBucketOAuth2Bundle:Debug:debug }
7 changes: 7 additions & 0 deletions Resources/config/services.yml
Expand Up @@ -77,6 +77,13 @@ services:
- "@authbucket_oauth2.model_manager.factory"
- "@authbucket_oauth2.token_handler.factory"

authbucket_oauth2.model_controller:
class: AuthBucket\OAuth2\Controller\ModelController
arguments:
- "@validator"
- "@serializer"
- "@authbucket_oauth2.model_manager.factory"

security.authentication.provider.token:
class: AuthBucket\OAuth2\Security\Authentication\Provider\TokenProvider
arguments:
Expand Down
159 changes: 159 additions & 0 deletions Tests/Controller/ModelControllerTest.php
@@ -0,0 +1,159 @@
<?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\Controller;

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

class ModelControllerTest extends WebTestCase
{
public function testCreateModelJson()
{
$content = $this->get('serializer')->encode(array('scope' => 'demoscopeJson'), 'json');
$client = $this->createClient();
$crawler = $client->request('POST', '/oauth2/model/scope.json', array(), array(), array(), $content);
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'json');
$this->assertEquals('demoscopeJson', $response['scope']);
}

public function testCreateModelXml()
{
$content = $this->get('serializer')->encode(array('scope' => 'demoscopeXml'), 'xml');
$client = $this->createClient();
$crawler = $client->request('POST', '/oauth2/model/scope.xml', array(), array(), array(), $content);
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'xml');
$this->assertEquals('demoscopeXml', $response['scope']);
}

public function testReadModelJson()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/oauth2/model/scope/1.json');
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'json');
$this->assertEquals('debug', $response['scope']);
}

public function testReadModelXml()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/oauth2/model/scope/1.xml');
$response = simplexml_load_string($client->getResponse()->getContent());
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'xml');
$this->assertEquals('debug', $response['scope']);
}

public function testUpdateModelJson()
{
$scope = substr(md5(uniqid(null, true)), 0, 8);
$content = $this->get('serializer')->encode(array('scope' => $scope), 'json');
$client = $this->createClient();
$crawler = $client->request('POST', '/oauth2/model/scope.json', array(), array(), array(), $content);
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'json');
$this->assertEquals($scope, $response['scope']);

$id = $response['id'];
$scopeUpdated = substr(md5(uniqid(null, true)), 0, 8);
$content = $this->get('serializer')->encode(array('scope' => $scopeUpdated), 'json');
$client = $this->createClient();
$crawler = $client->request('PUT', "/oauth2/model/scope/${id}.json", array(), array(), array(), $content);
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'json');
$this->assertEquals($scopeUpdated, $response['scope']);

$client = $this->createClient();
$crawler = $client->request('GET', "/oauth2/model/scope/${id}.json");
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'json');
$this->assertEquals($scopeUpdated, $response['scope']);
}

public function testUpdateModelXml()
{
$scope = substr(md5(uniqid(null, true)), 0, 8);
$content = $this->get('serializer')->encode(array('scope' => $scope), 'xml');
$client = $this->createClient();
$crawler = $client->request('POST', '/oauth2/model/scope.xml', array(), array(), array(), $content);
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'xml');
$this->assertEquals($scope, $response['scope']);

$id = $response['id'];
$scopeUpdated = substr(md5(uniqid(null, true)), 0, 8);
$content = $this->get('serializer')->encode(array('scope' => $scopeUpdated), 'xml');
$client = $this->createClient();
$crawler = $client->request('PUT', "/oauth2/model/scope/${id}.xml", array(), array(), array(), $content);
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'xml');
$this->assertEquals($scopeUpdated, $response['scope']);

$client = $this->createClient();
$crawler = $client->request('GET', "/oauth2/model/scope/${id}.xml");
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'xml');
$this->assertEquals($scopeUpdated, $response['scope']);
}

public function testDeleteModelJson()
{
$scope = substr(md5(uniqid(null, true)), 0, 8);
$content = $this->get('serializer')->encode(array('scope' => $scope), 'json');
$client = $this->createClient();
$crawler = $client->request('POST', '/oauth2/model/scope.json', array(), array(), array(), $content);
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'json');
$this->assertEquals($scope, $response['scope']);

$id = $response['id'];
$client = $this->createClient();
$crawler = $client->request('DELETE', "/oauth2/model/scope/${id}.json");
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'json');
$this->assertEquals(null, $response['id']);
$this->assertEquals($scope, $response['scope']);

$client = $this->createClient();
$crawler = $client->request('GET', "/oauth2/model/scope/${id}.json");
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'json');
$this->assertEquals(null, $response);
}

public function testDeleteModelXml()
{
$scope = substr(md5(uniqid(null, true)), 0, 8);
$content = $this->get('serializer')->encode(array('scope' => $scope), 'xml');
$client = $this->createClient();
$crawler = $client->request('POST', '/oauth2/model/scope.xml', array(), array(), array(), $content);
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'xml');
$this->assertEquals($scope, $response['scope']);

$id = $response['id'];
$client = $this->createClient();
$crawler = $client->request('DELETE', "/oauth2/model/scope/${id}.xml");
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'xml');
$this->assertEquals(null, $response['id']);
$this->assertEquals($scope, $response['scope']);

$client = $this->createClient();
$crawler = $client->request('GET', "/oauth2/model/scope/${id}.xml");
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'xml');
$this->assertEquals(null, $response);
}

public function testListModelJson()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/oauth2/model/scope.json');
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'json');
$this->assertEquals('debug', $response[0]['scope']);
}

public function testListModelXml()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/oauth2/model/scope.xml');
$response = $this->get('serializer')->decode($client->getResponse()->getContent(), 'xml');
$this->assertEquals('debug', $response[0]['scope']);
}
}
59 changes: 19 additions & 40 deletions Tests/GrantType/AuthorizationCodeGrantTypeHandlerTest.php
Expand Up @@ -50,52 +50,33 @@ public function testExceptionAuthCodeBadRedirectUri()
$this->assertEquals(400, $client->getResponse()->getStatusCode());
$this->assertNotNull(json_decode($client->getResponse()->getContent()));
$tokenResponse = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals('invalid_request', $tokenResponse['error']);
}

public function testErrorAuthCodeNoCode()
{
$request = new Request();
$parameters = array(
'grant_type' => 'authorization_code',
'redirect_uri' => 'http://democlient1.com/redirect_uri',
);
$server = array(
'PHP_AUTH_USER' => 'http://democlient1.com/',
'PHP_AUTH_PW' => 'demosecret1',
);
$client = $this->createClient();
$crawler = $client->request('POST', '/oauth2/token', $parameters, array(), $server);
$this->assertEquals(400, $client->getResponse()->getStatusCode());
$this->assertNotNull(json_decode($client->getResponse()->getContent()));
$tokenResponse = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals('invalid_request', $tokenResponse['error']);
$this->assertEquals('invalid_grant', $tokenResponse['error']);
}

public function testExceptionWrongClientIdAuthCode()
public function testExceptionAuthCodeBadRedirectUriFormat()
{
$parameters = array(
'grant_type' => 'authorization_code',
'code' => 'f0c68d250bcc729eb780a235371a9a55',
'redirect_uri' => 'http://democlient2.com/redirect_uri',
'redirect_uri' => "aaa\x22bbb\x5Cccc\x7Fddd",
);
$server = array(
'PHP_AUTH_USER' => 'http://democlient3.com/',
'PHP_AUTH_PW' => 'demosecret3',
'PHP_AUTH_USER' => 'http://democlient2.com/',
'PHP_AUTH_PW' => 'demosecret2',
);
$client = $this->createClient();
$crawler = $client->request('POST', '/oauth2/token', $parameters, array(), $server);
$this->assertEquals(400, $client->getResponse()->getStatusCode());
$this->assertNotNull(json_decode($client->getResponse()->getContent()));
$tokenResponse = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals('invalid_grant', $tokenResponse['error']);
$this->assertEquals('invalid_request', $tokenResponse['error']);
}

public function testExceptionExpiredAuthCode()
public function testErrorAuthCodeNoCode()
{
$request = new Request();
$parameters = array(
'grant_type' => 'authorization_code',
'code' => '1e5aa97ddaf4b0228dfb4223010d4417',
'redirect_uri' => 'http://democlient1.com/redirect_uri',
);
$server = array(
Expand All @@ -107,47 +88,45 @@ public function testExceptionExpiredAuthCode()
$this->assertEquals(400, $client->getResponse()->getStatusCode());
$this->assertNotNull(json_decode($client->getResponse()->getContent()));
$tokenResponse = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals('invalid_grant', $tokenResponse['error']);
$this->assertEquals('invalid_request', $tokenResponse['error']);
}

public function testExceptionBadStateFormat()
public function testExceptionWrongClientIdAuthCode()
{
$parameters = array(
'grant_type' => 'authorization_code',
'code' => 'f0c68d250bcc729eb780a235371a9a55',
'redirect_uri' => 'http://democlient2.com/redirect_uri',
'state' => "aaa\x19bbb\x7Fccc",
);
$server = array(
'PHP_AUTH_USER' => 'http://democlient2.com/',
'PHP_AUTH_PW' => 'demosecret2',
'PHP_AUTH_USER' => 'http://democlient3.com/',
'PHP_AUTH_PW' => 'demosecret3',
);
$client = $this->createClient();
$crawler = $client->request('POST', '/oauth2/token', $parameters, array(), $server);
$this->assertEquals(400, $client->getResponse()->getStatusCode());
$this->assertNotNull(json_decode($client->getResponse()->getContent()));
$tokenResponse = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals('invalid_request', $tokenResponse['error']);
$this->assertEquals('invalid_grant', $tokenResponse['error']);
}

public function testExceptionWrongState()
public function testExceptionExpiredAuthCode()
{
$parameters = array(
'grant_type' => 'authorization_code',
'code' => 'f0c68d250bcc729eb780a235371a9a55',
'redirect_uri' => 'http://democlient2.com/redirect_uri',
'state' => 'wrongstate',
'code' => '1e5aa97ddaf4b0228dfb4223010d4417',
'redirect_uri' => 'http://democlient1.com/redirect_uri',
);
$server = array(
'PHP_AUTH_USER' => 'http://democlient2.com/',
'PHP_AUTH_PW' => 'demosecret2',
'PHP_AUTH_USER' => 'http://democlient1.com/',
'PHP_AUTH_PW' => 'demosecret1',
);
$client = $this->createClient();
$crawler = $client->request('POST', '/oauth2/token', $parameters, array(), $server);
$this->assertEquals(400, $client->getResponse()->getStatusCode());
$this->assertNotNull(json_decode($client->getResponse()->getContent()));
$tokenResponse = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals('invalid_request', $tokenResponse['error']);
$this->assertEquals('invalid_grant', $tokenResponse['error']);
}

public function testGoodAuthCode()
Expand Down
16 changes: 1 addition & 15 deletions Tests/GrantType/BarGrantTypeHandler.php
Expand Up @@ -12,25 +12,11 @@
namespace AuthBucket\Bundle\OAuth2Bundle\Tests\GrantType;

use AuthBucket\OAuth2\GrantType\GrantTypeHandlerInterface;
use AuthBucket\OAuth2\Model\ModelManagerFactoryInterface;
use AuthBucket\OAuth2\TokenType\TokenTypeHandlerFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

class BarGrantTypeHandler implements GrantTypeHandlerInterface
{
public function handle(
SecurityContextInterface $securityContext,
UserCheckerInterface $userChecker,
EncoderFactoryInterface $encoderFactory,
Request $request,
ModelManagerFactoryInterface $modelManagerFactory,
TokenTypeHandlerFactoryInterface $tokenTypeHandlerFactory,
UserProviderInterface $userProvider = null
)
public function handle(Request $request)
{
}
}
15 changes: 15 additions & 0 deletions Tests/GrantType/ClientCredentialsGrantTypeHandlerTest.php
Expand Up @@ -67,4 +67,19 @@ public function testGoodClientCred()
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertNotNull(json_decode($client->getResponse()->getContent()));
}

public function testGoodClientCredNoScope()
{
$parameters = array(
'grant_type' => 'client_credentials',
);
$server = array(
'PHP_AUTH_USER' => 'http://democlient1.com/',
'PHP_AUTH_PW' => 'demosecret1',
);
$client = $this->createClient();
$crawler = $client->request('POST', '/oauth2/token', $parameters, array(), $server);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertNotNull(json_decode($client->getResponse()->getContent()));
}
}

0 comments on commit ca7065c

Please sign in to comment.