source: trunk/tests/Auth_SQLTest.php @ 357

Last change on this file since 357 was 357, checked in by quinn, 15 years ago

updated tests to work. updated email validation regex to include quote marks around name part. changed logmsg tmp dir name to use script_filename.

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            ) VALUES (
48                'testuser',
49                '" . $this->Auth_SQL->encryptPassword('testpass') . "',
50                'John',
51                'Doe',
52                'root@localhost'
53            )
54        ");
55
56    }
57
58    function tearDown()
59    {
60        $db =& DB::getInstance();
61   
62        unset($this->Auth_SQL);
63        $db->query("DROP TABLE IF EXISTS test_user_tbl");
64        $db->query("DROP TABLE IF EXISTS test_login_tbl");
65    }
66
67    function test_set()
68    {
69        $this->Auth_SQL->set('testuserkey', 'testuserval');
70        $this->assertEquals('testuserval', $_SESSION['_auth_sql'][$this->Auth_SQL->_ns]['user_data']['testuserkey']);
71    }
72
73    function test_get()
74    {
75        $_SESSION['_auth_sql'][$this->Auth_SQL->_ns]['user_data']['testuserkey'] = 'testuserval';
76        $val = $this->Auth_SQL->get('testuserkey');
77        $this->assertEquals('testuserval', $val);
78    }
79
80    function test_setparam()
81    {
82        $this->Auth_SQL->setParam(array(
83            'login_url'         => 'testloginurl.php'
84        ));
85        $this->assertEquals('testloginurl.php', $this->Auth_SQL->_params['login_url']);
86    }
87
88    function test_getparam()
89    {
90        $this->Auth_SQL->_params['login_url'] = 'testloginurl.php';
91        $param = $this->Auth_SQL->getParam('login_url');
92        $this->assertEquals('testloginurl.php', $param);
93    }
94
95    function test_clear()
96    {
97        $login = $this->Auth_SQL->login('testuser', 'testpass');
98        $this->assertTrue($login, 'User login failed, but should have succeeded.');
99        $before_logged_in = $this->Auth_SQL->isloggedin();
100        $this->assertTrue($before_logged_in, 'User is not logged in, but should be.');
101        $this->Auth_SQL->clear();
102        $after_logged_in = $this->Auth_SQL->isloggedin();
103        $this->assertFalse($after_logged_in, 'User is still logged in but should not be.');
104    }
105
106    function test_authenticate()
107    {
108        $true = $this->Auth_SQL->authenticate('testuser', 'testpass');
109        $this->assertTrue($true, 'User login failed, but should have succeeded.');
110
111        // Testing wrong password.
112        $false = $this->Auth_SQL->authenticate('testuser', 'wrongpass');
113
114        $this->assertfalse($false, 'User login succeeded, but should have failed.');
115    }
116
117    function test_login_and_isLoggedIn()
118    {
119        $login = $this->Auth_SQL->login('testuser', 'testpass');
120        $this->assertTrue($login, '1. User login failed, but should have succeeded.');
121        $before_logged_in = $this->Auth_SQL->isloggedin();
122        $this->assertTrue($before_logged_in, '2. User is not logged in, but should be.');
123        $this->Auth_SQL->clear();
124        $after_logged_in = $this->Auth_SQL->isloggedin();
125        $this->assertFalse($after_logged_in, '3. User is still logged in but should not be.');
126       
127        // Testing wrong password.
128        $login2 = $this->Auth_SQL->login('testuser', 'wrongpass');
129        $this->assertFalse($login2, '4. User login succeeded, but should have failed.');
130        $before_logged_in2 = $this->Auth_SQL->isloggedin();
131        $this->assertFalse($before_logged_in2, '5. User is logged in, but should not be.');
132        $this->Auth_SQL->clear();
133        $after_logged_in2 = $this->Auth_SQL->isloggedin();
134        $this->assertFalse($after_logged_in2, '6. Wrong user is still logged in but should not be.');
135    }
136
137    function test_requirelogin()
138    {
139//         $this->Auth_SQL->requirelogin('Login is required!');
140    }
141
142    function test_blockaccount()
143    {
144        $db =& DB::getInstance();
145   
146        $this->Auth_SQL->login('testuser', 'testpass');
147        $this->Auth_SQL->blockaccount(null, 'blocktestuser');
148        $qid = $db->query("
149            SELECT blocked_reason
150            FROM test_user_tbl
151        ");
152        list($reason) = mysql_fetch_row($qid);
153        $this->assertEquals('blocktestuser', $reason, "Block not found in DB record.");
154    }
155
156    function test_unblockaccount()
157    {
158        $db =& DB::getInstance();
159   
160        $db->query("
161            UPDATE test_user_tbl SET blocked_reason = 'blocktestuser'
162        ");
163        $this->Auth_SQL->unblockaccount();
164
165        $qid = $db->query("
166            SELECT blocked_reason
167            FROM test_user_tbl
168        ");
169        list($reason) = mysql_fetch_row($qid);
170        $this->assertTrue('' == $reason, "Block not removed from DB record.");
171    }
172
173    function test_usernameexists()
174    {
175        $result = $this->Auth_SQL->usernameexists('testuser');
176        $this->assertTrue($result);
177    }
178
179    function test_getusername()
180    {
181        $result = $this->Auth_SQL->getusername(1);
182        $this->assertEquals('testuser', $result);
183    }
184
185    function test_generatepassword()
186    {
187        $result = $this->Auth_SQL->generatepassword('xCVcvd');
188        $this->assertRegExp('/[bcdfghjklmnprstvwxzBCDFGHJKLMNPRSTVWXZaeiouyAEIOUY0123456789][bcdfghjklmnprstvwxzBCDFGHJKLMNPRSTVWXZ][aeiouyAEIOUY][bcdfghjklmnprstvwxz][aeiouy][0123456789]/', $result, 'Generated password does not match intended pattern');
189    }
190
191    function test_encryptpassword()
192    {
193        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_MD5));
194        $result = $this->Auth_SQL->encryptpassword('123');
195        $this->assertEquals('202cb962ac59075b964b07152d234b70', $result);
196
197        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_MD5_HARDENED));
198        $result = $this->Auth_SQL->encryptpassword('123');
199        $this->assertEquals('c55e4ac608a8768ecd758fab971b0646', $result);
200
201        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_SHA1));
202        $result = $this->Auth_SQL->encryptpassword('123');
203        $this->assertEquals('40bd001563085fc35165329ea1ff5c5ecbdbbeef', $result);
204
205        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_SHA1_HARDENED));
206        $result = $this->Auth_SQL->encryptpassword('123');
207        $this->assertEquals('33d90af96a5928ac93cbd41fc436e8c55d2768c2', $result);
208
209        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_PLAINTEXT));
210        $result = $this->Auth_SQL->encryptpassword('123');
211        $this->assertEquals('123', $result);
212
213        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_CRYPT));
214        $result = $this->Auth_SQL->encryptpassword('123', 'saltstring');
215        $this->assertEquals('saEZ6MlWYV9nQ', $result);
216    }
217
218    function test_setpassword()
219    {
220        $db =& DB::getInstance();
221   
222        $this->Auth_SQL->setParam(array('encryption_type' => AUTH_ENCRYPT_SHA1_HARDENED));
223        $this->Auth_SQL->setpassword(null, '123');
224        $qid = $db->query("
225            SELECT userpass
226            FROM test_user_tbl
227        ");
228        list($pass) = mysql_fetch_row($qid);
229        $this->assertEquals('33d90af96a5928ac93cbd41fc436e8c55d2768c2', $pass);
230    }
231
232    function test_resetpassword()
233    {
234        $result = $this->Auth_SQL->resetpassword(1, 'Because this is a test.');
235        $this->assertType('array', $result);
236    }
237
238//     function test_inclearancezone()
239//     {
240//         $result = $this->Auth_SQL->inclearancezone(PARAM);
241//         $expected = EXPECTED_VAL;
242//         $this->assertEquals($expected, $result);
243//     }
244//
245//     function test_requireaccessclearance()
246//     {
247//         $result = $this->Auth_SQL->requireaccessclearance(PARAM);
248//         $expected = EXPECTED_VAL;
249//         $this->assertEquals($expected, $result);
250//     }
251
252}
253// Running the test.
254$suite = new PHPUnit_TestSuite('Auth_SQLTest');
255$result = PHPUnit::run($suite);
256echo $result->toString();
257?>
Note: See TracBrowser for help on using the repository browser.