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",
"description": "The Pickles SOA Framework",
"description": "Pickles is a PHP framework for building kick-ass services",
"type": "library",
"keywords": ["framework", "api", "soa", "oauth"],
"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",
"This file is @generated automatically"
],
"hash": "16033f40d01b3ada4064b0b7488d4cb7",
"hash": "90c06945853fd0c6910bfd5e6f9c1e13",
"packages": [],
"packages-dev": [
{
@ -1068,12 +1068,12 @@
"source": {
"type": "git",
"url": "https://github.com/symfony/EventDispatcher.git",
"reference": "93d237c3da7565a9ad2b4b6a74af73f4e3c0c305"
"reference": "e133748fd9165e24f8e9498ef5862f8bd37004e5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/93d237c3da7565a9ad2b4b6a74af73f4e3c0c305",
"reference": "93d237c3da7565a9ad2b4b6a74af73f4e3c0c305",
"url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/e133748fd9165e24f8e9498ef5862f8bd37004e5",
"reference": "e133748fd9165e24f8e9498ef5862f8bd37004e5",
"shasum": ""
},
"require": {
@ -1117,7 +1117,7 @@
],
"description": "Symfony EventDispatcher Component",
"homepage": "http://symfony.com",
"time": "2014-10-01 05:53:11"
"time": "2014-10-04 06:08:58"
},
{
"name": "symfony/filesystem",

View file

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

View file

@ -1,7 +1,7 @@
<?php
/**
* Auth Abstraction
* Authentication
*
* Licensed under The MIT License
* Redistribution of these files must retain the above copyright notice.
@ -15,9 +15,9 @@
namespace Pickles;
/**
* Auth Abstract Class
* Auth Parent Class
*/
abstract class Auth extends Object
class Auth extends Object
{
/**
* Basic Auth
@ -29,12 +29,12 @@ abstract class Auth extends Object
*/
public function basic()
{
return false;
}
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
$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();
// @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'])
{
case 'basic':
@ -112,7 +127,6 @@ class Resource extends Object
throw new \Exception('Invalid authentication credentials.', 401);
}
break;
case 'oauth2':
$auth->oauth2();
break;
@ -121,6 +135,7 @@ class Resource extends Object
throw new \Exception('Invalid authentication scheme.', 401);
break;
}
*/
}
// Hack together some new globals

View file

@ -63,6 +63,7 @@ class Router extends Object
array_unshift($nouns, '', $this->config['pickles']['namespace'], 'Resources', $version);
$class = implode('\\', $nouns);
// @todo Make namespace mandatory
// Strips preceding slashs when there is no namespace
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
{
class ResourceTest extends PHPUnit_Framework_TestCase
@ -213,6 +235,49 @@ namespace
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()
{
Pickles\Object::$instances = [];