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