1<?php
2require_once 'PHPUnit/Framework/TestCase.php';
3require_once 'Auth.php';
4
5class TestAuthContainer extends PHPUnit_Framework_TestCase
6{
7
8    // Abstract
9    function &getContainer() {}
10    function &getExtraOptions() {}
11
12    function setUp()
13    {
14        $this->container =& $this->getContainer();
15        $this->user = 'joe';
16        $this->pass = 'doe';
17        $this->opt = 'VeryCoolUser';
18        // Nedded since lazy loading of container was introduced
19        $this->container->_auth_obj =& new Auth(&$this);
20
21        $opt = $this->getExtraOptions();
22        // Add the default user to be used for some testing
23        $this->container->addUser($opt['username'], $opt['passwd']);
24    }
25
26    function tearDown()
27    {
28        $opt = $this->getExtraOptions();
29        // Remove default user
30        $this->container->removeUser($opt['username']);
31    }
32
33    function testListUsers()
34    {
35
36        $users = $this->container->listUsers();
37        if (AUTH_METHOD_NOT_SUPPORTED === $users) {
38            $this->markTestSkipped('This operation is not supported by '.get_class($this->container));
39        }
40
41        $opt = $this->getExtraOptions();
42        $this->assertTrue(is_array($users[0]), 'First array element from result was not an array');
43        $this->assertTrue($users[0]['username'] == $opt['username'], sprintf('First username was not equal to default username "%s" ', $opt['username']));
44    }
45
46    function testAddUser()
47    {
48        $cb = count($this->container->listUsers());
49        $res = $this->container->addUser($this->user, $this->pass, $this->opt);
50        if (AUTH_METHOD_NOT_SUPPORTED === $res) {
51            $this->markTestSkipped("This operation is not supported by ".get_class($this->container));
52        }
53
54        if (PEAR::isError($res)) {
55            $error = $res->getMessage().' ['.$res->getUserInfo().']';
56        } else {
57            $error = '';
58        }
59        $this->assertTrue(!PEAR::isError($res), 'error:'.$error);
60        $ca = count($this->container->listUsers());
61        $users = $this->container->listUsers();
62        $last_username = $users[$ca-1]['username'];
63        $this->assertTrue( ($cb === $ca-1) , sprintf('Count of users before (%s) and after (%s) does not differ by one', $cb, $ca));
64        $this->assertTrue( $this->container->fetchData($this->user, $this->pass) , sprintf('Could not verify with the newly created user %s',$this->user));
65
66        // Remove the user we just added, assumes removeUser works
67        $this->container->removeUser($this->user);
68    }
69
70    function testFetchData()
71    {
72        $opt = $this->getExtraOptions();
73        $fetch_res = $this->container->fetchData($opt['username'], $opt['passwd']);
74        if (AUTH_METHOD_NOT_SUPPORTED === $fetch_res) {
75            $this->markTestSkipped("This operation is not supported by ".get_class($this->container));
76        }
77
78        $this->assertTrue($fetch_res,sprintf('Could not verify with the default username (%s) and passwd (%s)', $opt['username'], $opt['passwd']));
79
80        // Test for fail fetchData
81        $opt = $this->getExtraOptions();
82        $this->assertFalse(
83            $this->container->fetchData(md5($opt['username']), $opt['passwd']),
84            "fetchData returned true with invalid username and pass"
85        );
86
87    }
88
89
90    /**
91     * Tjis test depends on add user & remove user to work
92     */
93    function testFetchDataSpaceInPassword()
94    {
95        $user = uniqid('user');
96        $pass = 'Some Pass ';
97
98        $res = $this->container->addUser($user, $pass, array());
99        if (AUTH_METHOD_NOT_SUPPORTED === $res) {
100            $this->markTestSkipped("This operation is not supported by ".get_class($this->container));
101        }
102
103        $fetch_res = $this->container->fetchData($user, $pass);
104        if (AUTH_METHOD_NOT_SUPPORTED === $fetch_res) {
105            $this->markTestSkipped("This operation is not supported by ".get_class($this->container));
106        }
107
108        $this->assertTrue($fetch_res, 'Could not verify user with space password');
109
110        $remove_res = $this->container->removeUser($user);
111    }
112
113
114
115
116    function testRemoveUser()
117    {
118        // Add a user to be removed when testing removeUuser method
119        // Assume add user works
120        $this->container->addUser('for_remove', 'for_remove');
121        $cb = count($this->container->listUsers());
122        $remove_res = $this->container->removeUser('for_remove');
123        if (AUTH_METHOD_NOT_SUPPORTED === $remove_res) {
124            $this->markTestSkipped("This operation is not supported by ".get_class($this->container));
125        }
126
127        $ca = count($this->container->listUsers());
128        $this->assertTrue($cb === $ca+1, sprintf('Could not remove user "%s", count before:%s count after:%s ', 'for_remove', $cb, $ca));
129    }
130
131}
132
133?>
134