Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase code coverage #34

Merged
merged 2 commits into from Jul 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/Common.php
Expand Up @@ -79,6 +79,10 @@ public function define(Container $di)
'expire_ttl' => 14400,
);

$di->params['Aura\Auth\Session\Session'] = array(
'cookie' => $_COOKIE
);

/**
* Aura\Auth\Verifier\HashVerifier
*/
Expand Down
17 changes: 11 additions & 6 deletions src/Adapter/LdapAdapter.php
Expand Up @@ -11,6 +11,7 @@
namespace Aura\Auth\Adapter;

use Aura\Auth\Exception;
use Aura\Auth\ExtensionProxy;

/**
*
Expand Down Expand Up @@ -42,7 +43,10 @@ class LdapAdapter extends AbstractAdapter
protected $filter = '\w';
protected $options = array();

protected $proxy;

public function __construct(
ExtensionProxy $proxy,
$uri,
$format,
$filter = '\w',
Expand All @@ -52,6 +56,7 @@ public function __construct(
$this->format = $format;
$this->filter = $filter;
$this->options = $options;
$this->proxy = $proxy;
}

/**
Expand All @@ -70,13 +75,13 @@ public function login(array $cred)
$username = $cred['username'];
$password = $cred['password'];

$conn = ldap_connect($this->uri);
$conn = $this->proxy->connect($this->uri);
if (! $conn) {
throw new Exception\ConnectionFailed($this->uri);
}

foreach ($this->options as $opt => $val) {
ldap_set_option($conn, $opt, $val);
$this->proxy->set_option($conn, $opt, $val);
}

// filter the username to prevent LDAP injection
Expand All @@ -85,18 +90,18 @@ public function login(array $cred)

// bind to the server
$rdn = sprintf($this->format, $username);
$bind = ldap_bind($conn, $rdn, $password);
$bind = $this->proxy->bind($conn, $rdn, $password);

// did the bind succeed?
if ($bind) {
ldap_close($conn);
$this->proxy->close($conn);
return array('username' => $username);
}

$e = new Exception\ConnectionFailed(
ldap_errno($conn) . ': ' . ldap_error($conn)
$this->proxy->errno($conn) . ': ' . $this->proxy->error($conn)
);
ldap_close($conn);
$this->proxy->close($conn);
throw $e;
}
}
58 changes: 58 additions & 0 deletions src/ExtensionProxy.php
@@ -0,0 +1,58 @@
<?php
/**
*
* This file is part of the Aura project for PHP.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\Auth;

/**
*
* Extension proxy for the ease of testing PHP functions
*
* Thank you Mike Naberezny
*
* http://mikenaberezny.com/2007/08/01/wrapping-php-functions-for-testability/
*
* @package Aura.Auth
*
*/

class ExtensionProxy
{
/**
* ext
*
* @var mixed
*
*/
protected $ext;

/**
* __construct
*
* @param mixed $ext
*
*/
public function __construct($ext)
{
$this->ext = $ext;
}

/**
* __call
*
* @param string $method
*
* @param bool $arguments
*
* @param mixed $args
*
*/
public function __call($method, $args)
{
return call_user_func_array("{$this->ext}_{$method}", $args);
}
}
90 changes: 89 additions & 1 deletion tests/src/Adapter/LdapAdapterTest.php
@@ -1,13 +1,22 @@
<?php
namespace Aura\Auth\Adapter;

use Aura\Auth\ExtensionProxy;

class LdapAdapterTest extends \PHPUnit_Framework_TestCase
{
protected $adapter;

protected $proxy;

protected function setUp()
{
$this->adapter = new LdapAdapter('ldap.example.com', '');
$this->proxy = $this->getMock(
'Aura\Auth\ExtensionProxy',
array('connect', 'bind', 'set_option', 'close', 'errno', 'error'),
array('ldap')
);
$this->adapter = new LdapAdapter($this->proxy, 'ldap.example.com', '');
}

public function testInstance()
Expand All @@ -17,4 +26,83 @@ public function testInstance()
$this->adapter
);
}

/**
*
* @expectedException Aura\Auth\Exception\ConnectionFailed
*
*/
public function testLoginConnectionFailed()
{
$cred = array(
'username' => 'someusername',
'password' => 'secretpassword'
);
$this->proxy->expects($this->once())
->method('connect')
->with('ldap.example.com')
->will($this->returnValue(false));
$this->adapter->login($cred);
}

public function testLoginSuccess()
{
$this->proxy->expects($this->once())
->method('connect')
->with('ldap.example.com')
->will($this->returnValue(true));

$this->proxy->expects($this->any())
->method('set_option')
->will($this->returnValue(true));

$this->proxy->expects($this->once())
->method('bind')
->will($this->returnValue(true));

$cred = array(
'username' => 'someusername',
'password' => 'secretpassword'
);
$this->assertEquals(array('username' => $cred['username']), $this->adapter->login($cred));
}

/**
*
* @expectedException Aura\Auth\Exception\ConnectionFailed
*
*/
public function testUsernamePasswordFailure()
{
$this->proxy->expects($this->once())
->method('connect')
->with('ldap.example.com')
->will($this->returnValue(true));

$this->proxy->expects($this->any())
->method('set_option')
->will($this->returnValue(true));

$this->proxy->expects($this->once())
->method('bind')
->will($this->returnValue(false));

$this->proxy->expects($this->once())
->method('error')
->will($this->returnValue(400));

$this->proxy->expects($this->once())
->method('errno')
->will($this->returnValue(500));

$this->proxy->expects($this->once())
->method('close')
->will($this->returnValue(true));

$cred = array(
'username' => 'someusername',
'password' => 'secretpassword'
);
$this->adapter->login($cred);
}
}
20 changes: 20 additions & 0 deletions tests/src/ExtensionProxyTest.php
@@ -0,0 +1,20 @@
<?php
namespace Aura\Auth;

class ExtensionProxyTest extends \PHPUnit_Framework_TestCase
{
protected $proxy;

protected function setUp()
{
$this->proxy = new ExtensionProxy('str');
}

public function testInstance()
{
$this->assertEquals(
str_replace('Hello', 'Hi', 'Hello Aura'),
$this->proxy->replace('Hello', 'Hi', 'Hello Aura')
);
}
}