1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
use strict;
use Test::More tests => 59;
BEGIN {
use_ok('DBD::Mock');
}
use DBI;
{
package Login::Test;
my $MAX_LOGIN_FAILURES = 3;
sub login {
my ($dbh, $u, $p) = @_;
# look for the right username and password
my ($user_id) = $dbh->selectrow_array("SELECT user_id FROM users WHERE username = '$u' AND password = '$p'");
# if we find one, then ...
if ($user_id) {
# log the event and return true
$dbh->do("INSERT INTO event_log (event) VALUES('User $user_id logged in')");
return 'LOGIN SUCCESSFUL';
}
# if we don't find one then ...
else {
# see if the username exists ...
my ($user_id, $login_failures) = $dbh->selectrow_array("SELECT user_id, login_failures FROM users WHERE username = '$u'");
# if we do have a username, and the password doesnt match then ...
if ($user_id) {
# if we have not reached the max allowable login failures then ...
if ($login_failures < $MAX_LOGIN_FAILURES) {
# update the login failures
$dbh->do("UPDATE users SET login_failures = (login_failures + 1) WHERE user_id = $user_id");
return 'BAD PASSWORD';
}
# otherwise ...
else {
# we must update the login failures, and lock the account
$dbh->do("UPDATE users SET login_failures = (login_failures + 1), locked = 1 WHERE user_id = $user_id"); return 'USER ACCOUNT LOCKED';
}
}
else {
return 'USERNAME NOT FOUND';
}
}
}
}
{
my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
my $session = DBD::Mock::Session->new({});
isa_ok($session, 'DBD::Mock::Session');
is($session->name(), 'Session 1', '... got the first default session name');
$dbh->{mock_session} = $session;
my $fetched_session = $dbh->{mock_session};
is($fetched_session, $session, '... it is the same session we put in');
$dbh->{mock_session} = undef;
ok(!defined($dbh->{mock_session}), '... we no longer have a session in there');
my $session2 = DBD::Mock::Session->new({});
isa_ok($session2, 'DBD::Mock::Session');
is($session2->name(), 'Session 2', '... got the second default session name');
}
{
my $successful_login = DBD::Mock::Session->new('successful_login' => (
{
statement => "SELECT user_id FROM users WHERE username = 'user' AND password = '****'",
results => [[ 'user_id' ], [ 1 ]]
},
{
statement => "INSERT INTO event_log (event) VALUES('User 1 logged in')",
results => []
}
));
isa_ok($successful_login, 'DBD::Mock::Session');
is($successful_login->name(), 'successful_login', '... got the right name');
my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
$dbh->{mock_session} = $successful_login;
is(Login::Test::login($dbh, 'user', '****'), 'LOGIN SUCCESSFUL', '... logged in successfully');
# check the reusablity
# it is not reusable now
eval {
Login::Test::login($dbh, 'user', '****')
};
ok($@, '... got the exception');
like($@, qr/Session Error\: Session states exhausted/, '... got the exception we expected');
# reset the DBD::Mock::Session object
$successful_login->reset;
# and it is re-usable now
is(Login::Test::login($dbh, 'user', '****'), 'LOGIN SUCCESSFUL', '... logged in successfully');
}
{
my $bad_username = DBD::Mock::Session->new('bad_username' => (
{
statement => qr/SELECT user_id FROM users WHERE username = \'.*?\' AND password = \'.*?\'/, #'
results => [[ 'user_id' ], [ undef ]]
},
{
statement => qr/SELECT user_id, login_failures FROM users WHERE username = \'.*?\'/, #'
results => [[ 'user_id', 'login_failures' ], [ undef, undef ]]
}
));
isa_ok($bad_username, 'DBD::Mock::Session');
is($bad_username->name(), 'bad_username', '... got the right name');
my $dbh = DBI->connect('dbi:Mock:', '', '');
$dbh->{mock_session} = $bad_username;
is(Login::Test::login($dbh, 'user', '****'), 'USERNAME NOT FOUND', '... username is not found');
}
{
my $bad_password = DBD::Mock::Session->new('bad_password' => (
{
statement => sub { $_[0] eq "SELECT user_id FROM users WHERE username = 'user' AND password = '****'" },
results => [[ 'user_id' ], [ undef]]
},
{
statement => sub { $_[0] eq "SELECT user_id, login_failures FROM users WHERE username = 'user'" },
results => [[ 'user_id', 'login_failures' ], [ 1, 0 ]]
},
{
statement => sub { $_[0] eq "UPDATE users SET login_failures = (login_failures + 1) WHERE user_id = 1" },
results => []
}
));
isa_ok($bad_password, 'DBD::Mock::Session');
is($bad_password->name(), 'bad_password', '... got the right name');
my $dbh = DBI->connect('dbi:Mock:', '', '');
$dbh->{mock_session} = $bad_password;
is(Login::Test::login($dbh, 'user', '****'), 'BAD PASSWORD', '... username is found, but the password is wrong');
}
{
my $lock_user_account = DBD::Mock::Session->new('lock_user_account' => (
{
statement => "SELECT user_id FROM users WHERE username = 'user' AND password = '****'",
results => [[ 'user_id' ], [ undef]]
},
{
statement => qr/SELECT user_id, login_failures FROM users WHERE username = \'.*?\'/, #'
results => [[ 'user_id', 'login_failures' ], [ 1, 4 ]]
},
{
statement => sub { $_[0] eq "UPDATE users SET login_failures = (login_failures + 1), locked = 1 WHERE user_id = 1" },
results => []
}
));
isa_ok($lock_user_account, 'DBD::Mock::Session');
is($lock_user_account->name(), 'lock_user_account', '... got the right name');
my $dbh = DBI->connect('dbi:Mock:', '', '');
$dbh->{mock_session} = $lock_user_account;
is(Login::Test::login($dbh, 'user', '****'), 'USER ACCOUNT LOCKED', '... username is found, and the password is wrong, and the user account is now locked');
}
# now check some errors
{
my $not_enough_statements = DBD::Mock::Session->new((
{
statement => "SELECT user_id FROM users WHERE username = 'user' AND password = '****'",
results => [[ 'user_id' ], [ undef]]
},
{
statement => qr/SELECT user_id, login_failures FROM users WHERE username = \'.*?\'/, #'
results => [[ 'user_id', 'login_failures' ], [ 1, 4 ]]
},
# ... removed one statement here which DBI will be looking for
));
isa_ok($not_enough_statements, 'DBD::Mock::Session');
my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
$dbh->{mock_session} = $not_enough_statements;
eval {
Login::Test::login($dbh, 'user', '****');
};
ok(defined($@), '... got an error, as expected');
like($@, qr/Session Error\: Session states exhausted\, /, '... got the error we expected');
}
{
eval {
DBD::Mock::Session->new()
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^You must specify at least one session state/, '... got the error we expected');
eval {
DBD::Mock::Session->new([])
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^You must specify session states as HASH refs/, '... got the error we expected');
eval {
DBD::Mock::Session->new('session')
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^You must specify at least one session state/, '... got the error we expected');
eval {
DBD::Mock::Session->new('session', [])
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^You must specify session states as HASH refs/, '... got the error we expected');
eval {
DBD::Mock::Session->new('session', {}, [])
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^You must specify session states as HASH refs/, '... got the error we expected');
}
{
my $session = DBD::Mock::Session->new('session' => {});
isa_ok($session, 'DBD::Mock::Session');
eval {
$session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^Bad state \'0\' in DBD::Mock::Session \(session\)/, '... got the error we expected');
}
{
my $session = DBD::Mock::Session->new('session' => { statement => "" });
isa_ok($session, 'DBD::Mock::Session');
eval {
$session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^Bad state \'0\' in DBD::Mock::Session \(session\)/, '... got the error we expected');
}
{
my $session = DBD::Mock::Session->new('session' => { results => [] });
isa_ok($session, 'DBD::Mock::Session');
eval {
$session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^Bad state \'0\' in DBD::Mock::Session \(session\)/, '... got the error we expected');
}
{
my $session = DBD::Mock::Session->new('session' =>
{
statement => [],
results => []
}
);
isa_ok($session, 'DBD::Mock::Session');
eval {
$session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^Bad \'statement\' value \'ARRAY\(0x[a-f0-9]+\)\' in current state in DBD::Mock::Session \(session\)/, '... got the error we expected');
}
{
my $session = DBD::Mock::Session->new('session' =>
{
statement => "SELECT foo FROM baz",
results => []
}
);
isa_ok($session, 'DBD::Mock::Session');
eval {
$session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^Statement does not match current state in DBD::Mock::Session \(session\)/, '... got the error we expected');
}
{
my $session = DBD::Mock::Session->new('session' =>
{
statement => qr/SELECT foo FROM baz/,
results => []
}
);
isa_ok($session, 'DBD::Mock::Session');
eval {
$session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^Statement does not match current state \(with Regexp\) in DBD::Mock::Session \(session\)/, '... got the error we expected');
}
{
my $session = DBD::Mock::Session->new('session' =>
{
statement => sub { 0 },
results => []
}
);
isa_ok($session, 'DBD::Mock::Session');
eval {
$session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^Statement does not match current state \(with CODE ref\) in DBD::Mock::Session \(session\)/, '... got the error we expected');
}
{
my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
my $session = DBD::Mock::Session->new('session' =>
{
statement => 'Some SQL',
results => []
}
);
isa_ok($session, 'DBD::Mock::Session');
$dbh->{mock_session} = $session;
eval {
$dbh->disconnect;
};
ok(defined($@), '... got an error, as expected');
like($@, qr/^DBH->finish called when session still has states left/, '... got the error we expected');
# Shuts up warning when object is destroyed
undef $dbh->{mock_session};
}
|