Added auth test, cleaned up directory

This commit is contained in:
Josh Sherman 2014-10-04 07:24:57 -04:00
parent a866a1a61b
commit e45e1251e1
25 changed files with 134 additions and 15 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "picklesphp/pickles", "name": "picklesphp/pickles",
"description": "The Pickles SOA Framework", "description": "Pickles is a PHP framework for building kick-ass services",
"type": "library", "type": "library",
"keywords": ["framework", "api", "soa", "oauth"], "keywords": ["framework", "api", "soa", "oauth"],
"homepage": "http://picklesphp.com", "homepage": "http://picklesphp.com",

10
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "16033f40d01b3ada4064b0b7488d4cb7", "hash": "90c06945853fd0c6910bfd5e6f9c1e13",
"packages": [], "packages": [],
"packages-dev": [ "packages-dev": [
{ {
@ -1068,12 +1068,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/EventDispatcher.git", "url": "https://github.com/symfony/EventDispatcher.git",
"reference": "93d237c3da7565a9ad2b4b6a74af73f4e3c0c305" "reference": "e133748fd9165e24f8e9498ef5862f8bd37004e5"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/93d237c3da7565a9ad2b4b6a74af73f4e3c0c305", "url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/e133748fd9165e24f8e9498ef5862f8bd37004e5",
"reference": "93d237c3da7565a9ad2b4b6a74af73f4e3c0c305", "reference": "e133748fd9165e24f8e9498ef5862f8bd37004e5",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -1117,7 +1117,7 @@
], ],
"description": "Symfony EventDispatcher Component", "description": "Symfony EventDispatcher Component",
"homepage": "http://symfony.com", "homepage": "http://symfony.com",
"time": "2014-10-01 05:53:11" "time": "2014-10-04 06:08:58"
}, },
{ {
"name": "symfony/filesystem", "name": "symfony/filesystem",

View file

@ -2,13 +2,13 @@
<phpunit <phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.2/phpunit.xsd" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.2/phpunit.xsd"
bootstrap="tests/Bootstrap.php" bootstrap="tests/bootstrap.php"
colors="true" colors="true"
stderr="true" stderr="true"
> >
<testsuites> <testsuites>
<testsuite name="Pickles Test Suite"> <testsuite name="Pickles Test Harness">
<directory>./tests/</directory> <directory>./tests/Pickles</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
</phpunit> </phpunit>

View file

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Auth Abstraction * Authentication
* *
* Licensed under The MIT License * Licensed under The MIT License
* Redistribution of these files must retain the above copyright notice. * Redistribution of these files must retain the above copyright notice.
@ -15,9 +15,9 @@
namespace Pickles; namespace Pickles;
/** /**
* Auth Abstract Class * Auth Parent Class
*/ */
abstract class Auth extends Object class Auth extends Object
{ {
/** /**
* Basic Auth * Basic Auth
@ -29,12 +29,12 @@ abstract class Auth extends Object
*/ */
public function basic() public function basic()
{ {
return false;
} }
public function oauth2() public function oauth2()
{ {
return false;
} }
} }

View file

@ -102,8 +102,23 @@ class Resource extends Object
// This class should be in the classes directory of the service // This class should be in the classes directory of the service
$auth = '\\' . $this->config['pickles']['namespace'] . '\\Classes\\Auth'; $auth = '\\' . $this->config['pickles']['namespace'] . '\\Classes\\Auth';
// Strips preceding slashs when there is no namespace
if (strpos($auth, '\\\\') === 0)
{
$auth = substr($auth, 2);
}
$auth = new $auth(); $auth = new $auth();
// @todo Remove when switch is implemented
if (!$auth->basic())
{
throw new \Exception('Invalid authentication credentials.', 401);
}
// @todo Not yet implemented
/*
switch ($this->config['pickles']['auth']) switch ($this->config['pickles']['auth'])
{ {
case 'basic': case 'basic':
@ -112,7 +127,6 @@ class Resource extends Object
throw new \Exception('Invalid authentication credentials.', 401); throw new \Exception('Invalid authentication credentials.', 401);
} }
break; break;
case 'oauth2': case 'oauth2':
$auth->oauth2(); $auth->oauth2();
break; break;
@ -121,6 +135,7 @@ class Resource extends Object
throw new \Exception('Invalid authentication scheme.', 401); throw new \Exception('Invalid authentication scheme.', 401);
break; break;
} }
*/
} }
// Hack together some new globals // Hack together some new globals

View file

@ -63,6 +63,7 @@ class Router extends Object
array_unshift($nouns, '', $this->config['pickles']['namespace'], 'Resources', $version); array_unshift($nouns, '', $this->config['pickles']['namespace'], 'Resources', $version);
$class = implode('\\', $nouns); $class = implode('\\', $nouns);
// @todo Make namespace mandatory
// Strips preceding slashs when there is no namespace // Strips preceding slashs when there is no namespace
if (strpos($class, '\\\\') === 0) if (strpos($class, '\\\\') === 0)
{ {

View file

@ -0,0 +1,38 @@
<?php
class AuthTest extends PHPUnit_Framework_TestCase
{
private $auth;
public function setUp()
{
Pickles\Object::$instances = [];
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['SERVER_NAME'] = '127.0.0.1';
file_put_contents('/tmp/pickles.php', '<?php
$config = [
"environments" => [
"local" => "127.0.0.1",
"production" => "123.456.789.0",
],
];
');
Pickles\Config::getInstance('/tmp/pickles.php');
$this->auth = new Pickles\Auth();
}
public function testBasic()
{
$this->assertFalse($this->auth->basic());
}
public function testOAuth2()
{
$this->assertFalse($this->auth->oauth2());
}
}

View file

@ -63,6 +63,28 @@ namespace Resources\v1
} }
} }
namespace Classes
{
class Auth extends \Pickles\Auth
{
private static $count = 0;
public function basic()
{
self::$count++;
if (self::$count % 2)
{
return false;
}
else
{
return true;
}
}
}
}
namespace namespace
{ {
class ResourceTest extends PHPUnit_Framework_TestCase class ResourceTest extends PHPUnit_Framework_TestCase
@ -213,6 +235,49 @@ namespace
new Pickles\Router(); new Pickles\Router();
} }
public function testBasicAuthBadCredentials()
{
Pickles\Object::$instances = [];
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['SERVER_NAME'] = '127.0.0.1';
file_put_contents('/tmp/pickles.php', '<?php
$config = [
"environments" => [
"local" => "127.0.0.1",
"production" => "123.456.789.0",
],
"pickles" => [
"namespace" => "",
"datasource" => "mysql",
"auth" => "basic",
],
"datasources" => [
"mysql" => [
"driver" => "pdo_mysql",
],
],
];
');
Pickles\Config::getInstance('/tmp/pickles.php');
$response = json_encode([
'meta' => [
'status' => 401,
'message' => 'Invalid authentication credentials.',
],
]);
$this->expectOutputString($response);
$_SERVER['REQUEST_METHOD'] = 'DELETE';
$_REQUEST['request'] = 'v1/resource/1';
new Pickles\Router();
}
public function testBasicAuth() public function testBasicAuth()
{ {
Pickles\Object::$instances = []; Pickles\Object::$instances = [];