1*a1a3b679SAndreas Boehler<?php 2*a1a3b679SAndreas Boehler 3*a1a3b679SAndreas Boehlernamespace Sabre\DAV\Auth; 4*a1a3b679SAndreas Boehler 5*a1a3b679SAndreas Boehleruse Sabre\HTTP; 6*a1a3b679SAndreas Boehleruse Sabre\DAV; 7*a1a3b679SAndreas Boehler 8*a1a3b679SAndreas Boehlerrequire_once 'Sabre/HTTP/ResponseMock.php'; 9*a1a3b679SAndreas Boehler 10*a1a3b679SAndreas Boehlerclass PluginTest extends \PHPUnit_Framework_TestCase { 11*a1a3b679SAndreas Boehler 12*a1a3b679SAndreas Boehler function testInit() { 13*a1a3b679SAndreas Boehler 14*a1a3b679SAndreas Boehler $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); 15*a1a3b679SAndreas Boehler $plugin = new Plugin(new Backend\Mock(),'realm'); 16*a1a3b679SAndreas Boehler $this->assertTrue($plugin instanceof Plugin); 17*a1a3b679SAndreas Boehler $fakeServer->addPlugin($plugin); 18*a1a3b679SAndreas Boehler $this->assertEquals($plugin, $fakeServer->getPlugin('auth')); 19*a1a3b679SAndreas Boehler $this->assertInternalType('array', $plugin->getPluginInfo()); 20*a1a3b679SAndreas Boehler 21*a1a3b679SAndreas Boehler } 22*a1a3b679SAndreas Boehler 23*a1a3b679SAndreas Boehler /** 24*a1a3b679SAndreas Boehler * @depends testInit 25*a1a3b679SAndreas Boehler */ 26*a1a3b679SAndreas Boehler function testAuthenticate() { 27*a1a3b679SAndreas Boehler 28*a1a3b679SAndreas Boehler $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); 29*a1a3b679SAndreas Boehler $plugin = new Plugin(new Backend\Mock()); 30*a1a3b679SAndreas Boehler $fakeServer->addPlugin($plugin); 31*a1a3b679SAndreas Boehler $this->assertTrue( 32*a1a3b679SAndreas Boehler $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]) 33*a1a3b679SAndreas Boehler ); 34*a1a3b679SAndreas Boehler 35*a1a3b679SAndreas Boehler } 36*a1a3b679SAndreas Boehler 37*a1a3b679SAndreas Boehler /** 38*a1a3b679SAndreas Boehler * @depends testInit 39*a1a3b679SAndreas Boehler * @expectedException Sabre\DAV\Exception\NotAuthenticated 40*a1a3b679SAndreas Boehler */ 41*a1a3b679SAndreas Boehler function testAuthenticateFail() { 42*a1a3b679SAndreas Boehler 43*a1a3b679SAndreas Boehler $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); 44*a1a3b679SAndreas Boehler $backend = new Backend\Mock(); 45*a1a3b679SAndreas Boehler $backend->fail = true; 46*a1a3b679SAndreas Boehler 47*a1a3b679SAndreas Boehler $plugin = new Plugin($backend); 48*a1a3b679SAndreas Boehler $fakeServer->addPlugin($plugin); 49*a1a3b679SAndreas Boehler $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]); 50*a1a3b679SAndreas Boehler 51*a1a3b679SAndreas Boehler } 52*a1a3b679SAndreas Boehler 53*a1a3b679SAndreas Boehler /** 54*a1a3b679SAndreas Boehler * @depends testAuthenticate 55*a1a3b679SAndreas Boehler */ 56*a1a3b679SAndreas Boehler function testMultipleBackend() { 57*a1a3b679SAndreas Boehler 58*a1a3b679SAndreas Boehler $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); 59*a1a3b679SAndreas Boehler $backend1 = new Backend\Mock(); 60*a1a3b679SAndreas Boehler $backend2 = new Backend\Mock(); 61*a1a3b679SAndreas Boehler $backend2->fail = true; 62*a1a3b679SAndreas Boehler 63*a1a3b679SAndreas Boehler $plugin = new Plugin(); 64*a1a3b679SAndreas Boehler $plugin->addBackend($backend1); 65*a1a3b679SAndreas Boehler $plugin->addBackend($backend2); 66*a1a3b679SAndreas Boehler 67*a1a3b679SAndreas Boehler $fakeServer->addPlugin($plugin); 68*a1a3b679SAndreas Boehler $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]); 69*a1a3b679SAndreas Boehler 70*a1a3b679SAndreas Boehler $this->assertEquals('principals/admin', $plugin->getCurrentPrincipal()); 71*a1a3b679SAndreas Boehler 72*a1a3b679SAndreas Boehler } 73*a1a3b679SAndreas Boehler 74*a1a3b679SAndreas Boehler /** 75*a1a3b679SAndreas Boehler * @depends testInit 76*a1a3b679SAndreas Boehler * @expectedException Sabre\DAV\Exception 77*a1a3b679SAndreas Boehler */ 78*a1a3b679SAndreas Boehler function testNoAuthBackend() { 79*a1a3b679SAndreas Boehler 80*a1a3b679SAndreas Boehler $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); 81*a1a3b679SAndreas Boehler 82*a1a3b679SAndreas Boehler $plugin = new Plugin(); 83*a1a3b679SAndreas Boehler $fakeServer->addPlugin($plugin); 84*a1a3b679SAndreas Boehler $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]); 85*a1a3b679SAndreas Boehler 86*a1a3b679SAndreas Boehler } 87*a1a3b679SAndreas Boehler /** 88*a1a3b679SAndreas Boehler * @depends testInit 89*a1a3b679SAndreas Boehler * @expectedException Sabre\DAV\Exception 90*a1a3b679SAndreas Boehler */ 91*a1a3b679SAndreas Boehler function testInvalidCheckResponse() { 92*a1a3b679SAndreas Boehler 93*a1a3b679SAndreas Boehler $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); 94*a1a3b679SAndreas Boehler $backend = new Backend\Mock(); 95*a1a3b679SAndreas Boehler $backend->invalidCheckResponse = true; 96*a1a3b679SAndreas Boehler 97*a1a3b679SAndreas Boehler $plugin = new Plugin($backend); 98*a1a3b679SAndreas Boehler $fakeServer->addPlugin($plugin); 99*a1a3b679SAndreas Boehler $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]); 100*a1a3b679SAndreas Boehler 101*a1a3b679SAndreas Boehler } 102*a1a3b679SAndreas Boehler 103*a1a3b679SAndreas Boehler /** 104*a1a3b679SAndreas Boehler * @depends testAuthenticate 105*a1a3b679SAndreas Boehler */ 106*a1a3b679SAndreas Boehler function testGetCurrentPrincipal() { 107*a1a3b679SAndreas Boehler 108*a1a3b679SAndreas Boehler $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); 109*a1a3b679SAndreas Boehler $plugin = new Plugin(new Backend\Mock()); 110*a1a3b679SAndreas Boehler $fakeServer->addPlugin($plugin); 111*a1a3b679SAndreas Boehler $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]); 112*a1a3b679SAndreas Boehler $this->assertEquals('principals/admin', $plugin->getCurrentPrincipal()); 113*a1a3b679SAndreas Boehler 114*a1a3b679SAndreas Boehler } 115*a1a3b679SAndreas Boehler 116*a1a3b679SAndreas Boehler /** 117*a1a3b679SAndreas Boehler * @depends testAuthenticate 118*a1a3b679SAndreas Boehler */ 119*a1a3b679SAndreas Boehler function testGetCurrentUser() { 120*a1a3b679SAndreas Boehler 121*a1a3b679SAndreas Boehler $fakeServer = new DAV\Server( new DAV\SimpleCollection('bla')); 122*a1a3b679SAndreas Boehler $plugin = new Plugin(new Backend\Mock()); 123*a1a3b679SAndreas Boehler $fakeServer->addPlugin($plugin); 124*a1a3b679SAndreas Boehler $fakeServer->emit('beforeMethod', [new HTTP\Request(), new HTTP\Response()]); 125*a1a3b679SAndreas Boehler $this->assertEquals('admin', $plugin->getCurrentUser()); 126*a1a3b679SAndreas Boehler 127*a1a3b679SAndreas Boehler } 128*a1a3b679SAndreas Boehler 129*a1a3b679SAndreas Boehler} 130*a1a3b679SAndreas Boehler 131