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