2013-10-21 22:46:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Sabre\DAV\Auth;
|
|
|
|
|
|
|
|
use Sabre\HTTP;
|
|
|
|
use Sabre\DAV;
|
|
|
|
|
|
|
|
require_once 'Sabre/HTTP/ResponseMock.php';
|
|
|
|
|
|
|
|
class PluginTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
function testInit() {
|
|
|
|
|
|
|
|
$fakeServer = new DAV\Server( new DAV\SimpleCollection('bla'));
|
2016-05-11 00:26:44 +00:00
|
|
|
$plugin = new Plugin(new Backend\Mock());
|
2013-10-21 22:46:31 +00:00
|
|
|
$this->assertTrue($plugin instanceof Plugin);
|
|
|
|
$fakeServer->addPlugin($plugin);
|
|
|
|
$this->assertEquals($plugin, $fakeServer->getPlugin('auth'));
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->assertInternalType('array', $plugin->getPluginInfo());
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testInit
|
|
|
|
*/
|
|
|
|
function testAuthenticate() {
|
|
|
|
|
|
|
|
$fakeServer = new DAV\Server( new DAV\SimpleCollection('bla'));
|
2016-05-11 00:26:44 +00:00
|
|
|
$plugin = new Plugin(new Backend\Mock());
|
2013-10-21 22:46:31 +00:00
|
|
|
$fakeServer->addPlugin($plugin);
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->assertTrue(
|
|
|
|
$fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()])
|
|
|
|
);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testInit
|
|
|
|
* @expectedException Sabre\DAV\Exception\NotAuthenticated
|
|
|
|
*/
|
|
|
|
function testAuthenticateFail() {
|
|
|
|
|
|
|
|
$fakeServer = new DAV\Server( new DAV\SimpleCollection('bla'));
|
2016-05-11 00:26:44 +00:00
|
|
|
$backend = new Backend\Mock();
|
|
|
|
$backend->fail = true;
|
|
|
|
|
|
|
|
$plugin = new Plugin($backend);
|
2013-10-21 22:46:31 +00:00
|
|
|
$fakeServer->addPlugin($plugin);
|
2016-05-11 00:26:44 +00:00
|
|
|
$fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
/**
|
|
|
|
* @depends testAuthenticate
|
|
|
|
*/
|
|
|
|
function testMultipleBackend() {
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$fakeServer = new DAV\Server( new DAV\SimpleCollection('bla'));
|
|
|
|
$backend1 = new Backend\Mock();
|
|
|
|
$backend2 = new Backend\Mock();
|
|
|
|
$backend2->fail = true;
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$plugin = new Plugin();
|
|
|
|
$plugin->addBackend($backend1);
|
|
|
|
$plugin->addBackend($backend2);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$fakeServer->addPlugin($plugin);
|
|
|
|
$fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]);
|
2013-10-21 22:46:31 +00:00
|
|
|
|
2016-05-11 00:26:44 +00:00
|
|
|
$this->assertEquals('principals/admin', $plugin->getCurrentPrincipal());
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testInit
|
2016-05-11 00:26:44 +00:00
|
|
|
* @expectedException Sabre\DAV\Exception
|
|
|
|
*/
|
|
|
|
function testNoAuthBackend() {
|
|
|
|
|
|
|
|
$fakeServer = new DAV\Server( new DAV\SimpleCollection('bla'));
|
|
|
|
|
|
|
|
$plugin = new Plugin();
|
|
|
|
$fakeServer->addPlugin($plugin);
|
|
|
|
$fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]);
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @depends testInit
|
|
|
|
* @expectedException Sabre\DAV\Exception
|
|
|
|
*/
|
|
|
|
function testInvalidCheckResponse() {
|
|
|
|
|
|
|
|
$fakeServer = new DAV\Server( new DAV\SimpleCollection('bla'));
|
|
|
|
$backend = new Backend\Mock();
|
|
|
|
$backend->invalidCheckResponse = true;
|
|
|
|
|
|
|
|
$plugin = new Plugin($backend);
|
|
|
|
$fakeServer->addPlugin($plugin);
|
|
|
|
$fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testAuthenticate
|
|
|
|
*/
|
|
|
|
function testGetCurrentPrincipal() {
|
|
|
|
|
|
|
|
$fakeServer = new DAV\Server( new DAV\SimpleCollection('bla'));
|
|
|
|
$plugin = new Plugin(new Backend\Mock());
|
|
|
|
$fakeServer->addPlugin($plugin);
|
|
|
|
$fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]);
|
|
|
|
$this->assertEquals('principals/admin', $plugin->getCurrentPrincipal());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testAuthenticate
|
2013-10-21 22:46:31 +00:00
|
|
|
*/
|
2016-05-11 00:26:44 +00:00
|
|
|
function testGetCurrentUser() {
|
2013-10-21 22:46:31 +00:00
|
|
|
|
|
|
|
$fakeServer = new DAV\Server( new DAV\SimpleCollection('bla'));
|
2016-05-11 00:26:44 +00:00
|
|
|
$plugin = new Plugin(new Backend\Mock());
|
2013-10-21 22:46:31 +00:00
|
|
|
$fakeServer->addPlugin($plugin);
|
2016-05-11 00:26:44 +00:00
|
|
|
$fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]);
|
2013-10-21 22:46:31 +00:00
|
|
|
$this->assertEquals('admin', $plugin->getCurrentUser());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|