DBD-Mock

 view release on metacpan or  search on metacpan

t/021_DBD_Mock_Session.t  view on Meta::CPAN

            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';
            }
        }
    }
}

{

t/021_DBD_Mock_Session.t  view on Meta::CPAN

    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]]
        },



( run in 0.831 second using v1.01-cache-2.11-cpan-49f99fa48dc )