source: trunk/tests/Auth_SQLTest.php @ 154

Last change on this file since 154 was 154, checked in by scdev, 18 years ago

${1}

File size: 8.7 KB
Line 
1<?php
2/**
3 * PHPUnit test case for Auth_SQL
4 *
5 * The method skeletons below need to be filled in with
6 * real data so that the tests will run correctly. Replace
7 * all EXPECTED_VAL and PARAM strings with real data.
8 *
9 * Created with PHPUnit_Skeleton on 2005-08-09
10 */
11require_once 'PHPUnit.php';
12class Auth_SQLTest extends PHPUnit_TestCase {
13
14    var $Auth_SQL;
15
16    function Auth_SQLTest($name)
17    {
18        $this->PHPUnit_TestCase($name);
19    }
20
21    function setUp()
22    {
23        require dirname(__FILE__) . '/_config.inc.php';
24        require_once '../lib/Auth_SQL.inc.php';
25        $this->Auth_SQL =& new Auth_SQL('testauth');
26        $this->Auth_SQL->setParam(array(
27            'db_table'          => 'test_user_tbl',
28            'db_primary_key'    => 'user_id',
29            'db_login_table'    => 'test_login_tbl',
30            'login_url'         => '/login.php',
31            'blocking'          => true,
32            'encryption_type' => AUTH_ENCRYPT_MD5_HARDENED,
33        ));
34
35        // Use fresh user table.
36        $this->Auth_SQL->initDB(true);
37
38        // Insert test data.
39        $db =& DB::getInstance();
40        $db->query("
41            INSERT INTO test_user_tbl (
42                username,
43                userpass,
44                first_name,
45                last_name,
46                email,
47                user_type
48            ) VALUES (
49                'testuser',
50                '" . $this->Auth_SQL->encryptPassword('testpass') . "',
51                'John',
52                'Doe',
53                'root@localhost',
54                'admin'
55            )
56        ");
57
58    }
59
60    function tearDown()
61    {
62        $db =& DB::getInstance();
63   
64        unset($this->Auth_SQL);
65        $db->query("DROP TABLE IF EXISTS test_user_tbl");
66        $db->query("DROP TABLE IF EXISTS test_login_tbl");
67    }
68
69    function test_set()
70    {
71        $this->Auth_SQL->set('testuserkey', 'testuserval');
72        $this->assertEquals('testuserval', $_SESSION['_auth_sql'][$this->Auth_SQL->_ns]['user_data']['testuserkey']);
73    }
74
75    function test_get()
76    {
77        $_SESSION['_auth_sql'][$this->Auth_SQL->_ns]['user_data']['testuserkey'] = 'testuserval';
78        $val = $this->Auth_SQL->get('testuserkey');
79        $this->assertEquals('testuserval', $val);
80    }
81
82    function test_setparam()
83    {
84        $this->Auth_SQL->setParam(array(
85            'login_url'         => 'testloginurl.php'
86        ));
87        $this->assertEquals('testloginurl.php', $this->Auth_SQL->_params['login_url']);
88    }
89
90    function test_getparam()
91    {
92        $this->Auth_SQL->_params['login_url'] = 'testloginurl.php';
93        $param = $this->Auth_SQL->getParam('login_url');
94        $this->assertEquals('testloginurl.php', $param);
95    }
96
97    function test_clear()
98    {
99        $login = $this->Auth_SQL->login('testuser', 'testpass');
100        $this->assertTrue($login, 'User login failed, but should have succeeded.');
101        $before_logged_in = $this->Auth_SQL->isloggedin();
102        $this->assertTrue($before_logged_in, 'User is not logged in, but should be.');
103        $this->Auth_SQL->clear();
104        $after_logged_in = $this->Auth_SQL->isloggedin();
105        $this->assertFalse($after_logged_in, 'User is still logged in but should not be.');
106    }
107
108    function test_authenticate()
109    {
110        $true = $this->Auth_SQL->authenticate('testuser', 'testpass');
111        $this->assertTrue($true, 'User login failed, but should have succeeded.');
112
113        // Testing wrong password.
114        $false = $this->Auth_SQL->authenticate('testuser', 'wrongpass');
115
116        $this->assertfalse($false, 'User login succeeded, but should have failed.');
117    }
118
119    function test_login_and_isLoggedIn()
120    {
121        $login = $this->Auth_SQL->login('testuser', 'testpass');
122        $this->assertTrue($login, '1. User login failed, but should have succeeded.');
123        $before_logged_in = $this->Auth_SQL->isloggedin();
124        $this->assertTrue($before_logged_in, '2. User is not logged in, but should be.');
125        $this->Auth_SQL->clear();
126        $after_logged_in = $this->Auth_SQL->isloggedin();
127        $this->assertFalse($after_logged_in, '3. User is still logged in but should not be.');
128       
129        // Testing wrong password.
130        $login2 = $this->Auth_SQL->login('testuser', 'wrongpass');
131        $this->assertFalse($login2, '4. User login succeeded, but should have failed.');
132        $before_logged_in2 = $this->Auth_SQL->isloggedin();
133        $this->assertFalse($before_logged_in2, '5. User is logged in, but should not be.');
134        $this->Auth_SQL->clear();
135        $after_logged_in2 = $this->Auth_SQL->isloggedin();
136        $this->assertFalse($after_logged_in2, '6. Wrong user is still logged in but should not be.');
137    }
138
139    function test_requirelogin()
140    {
141//         $this->Auth_SQL->requirelogin('Login is required!');
142    }
143
144    function test_blockaccount()
145    {
146        $db =& DB::getInstance();
147   
148        $this->Auth_SQL->login('testuser', 'testpass');
149        $this->Auth_SQL->blockaccount(null, 'blocktestuser');
150        $qid = $db->query("
151            SELECT blocked_reason
152            FROM test_user_tbl
153        ");
154        list($reason) = mysql_fetch_row($qid);
155        $this->assertEquals('blocktestuser', $reason, "Block not found in DB record.");
156    }
157
158    function test_unblockaccount()
159    {
160        $db =& DB::getInstance();
161   
162        $db->query("
163            UPDATE test_user_tbl SET blocked_reason = 'blocktestuser'
164        ");
165        $this->Auth_SQL->unblockaccount();
166
167        $qid = $db->query("
168            SELECT blocked_reason
169            FROM test_user_tbl
170        ");
171        list($reason) = mysql_fetch_row($qid);
172        $this->assertTrue('' == $reason, "Block not removed from DB record.");
173    }
174
175    function test_usernameexists()
176    {
177        $result = $this->Auth_SQL->usernameexists('testuser');
178        $this->assertTrue($result);
179    }
180
181    function test_getusername()
182    {
183        $result = $this->Auth_SQL->getusername(1);
184        $this->assertEquals('testuser', $result);
185    }
186
187    function test_generatepassword()
188    {
189        $result = $this->Auth_SQL->generatepassword('xCVcvd');
190        $this->assertRegExp('/[bcdfghjklmnprstvwxzBCDFGHJKLMNPRSTVWXZaeiouyAEIOUY0123456789][bcdfghjklmnprstvwxzBCDFGHJKLMNPRSTVWXZ][aeiouyAEIOUY][bcdfghjklmnprstvwxz][aeiouy][0123456789]/', $result, 'Generated password does not match intended pattern');
191    }
192
193    function test_encryptpassword()
194    {
195        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_MD5));
196        $result = $this->Auth_SQL->encryptpassword('123');
197        $this->assertEquals('202cb962ac59075b964b07152d234b70', $result);
198
199        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_MD5_HARDENED));
200        $result = $this->Auth_SQL->encryptpassword('123');
201        $this->assertEquals('c55e4ac608a8768ecd758fab971b0646', $result);
202
203        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_SHA1));
204        $result = $this->Auth_SQL->encryptpassword('123');
205        $this->assertEquals('40bd001563085fc35165329ea1ff5c5ecbdbbeef', $result);
206
207        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_SHA1_HARDENED));
208        $result = $this->Auth_SQL->encryptpassword('123');
209        $this->assertEquals('33d90af96a5928ac93cbd41fc436e8c55d2768c2', $result);
210
211        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_PLAINTEXT));
212        $result = $this->Auth_SQL->encryptpassword('123');
213        $this->assertEquals('123', $result);
214
215        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_CRYPT));
216        $result = $this->Auth_SQL->encryptpassword('123', 'saltstring');
217        $this->assertEquals('saEZ6MlWYV9nQ', $result);
218    }
219
220    function test_setpassword()
221    {
222        $db =& DB::getInstance();
223   
224        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_SHA1_HARDENED));
225        $this->Auth_SQL->setpassword(null, '123');
226        $qid = $db->query("
227            SELECT userpass
228            FROM test_user_tbl
229        ");
230        list($pass) = mysql_fetch_row($qid);
231        $this->assertEquals('33d90af96a5928ac93cbd41fc436e8c55d2768c2', $pass);
232    }
233
234    function test_resetpassword()
235    {
236        $result = $this->Auth_SQL->resetpassword(1, 'Because this is a test.');
237        $this->assertType('array', $result);
238    }
239
240//     function test_inclearancezone()
241//     {
242//         $result = $this->Auth_SQL->inclearancezone(PARAM);
243//         $expected = EXPECTED_VAL;
244//         $this->assertEquals($expected, $result);
245//     }
246//
247//     function test_requireaccessclearance()
248//     {
249//         $result = $this->Auth_SQL->requireaccessclearance(PARAM);
250//         $expected = EXPECTED_VAL;
251//         $this->assertEquals($expected, $result);
252//     }
253
254}
255// Running the test.
256$suite = new PHPUnit_TestSuite('Auth_SQLTest');
257$result = PHPUnit::run($suite);
258echo $result->toString();
259?>
Note: See TracBrowser for help on using the repository browser.