Guest User

Untitled

a guest
Jan 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3.  
  4. use DBD::Mock;
  5. #use DBI
  6. use Test::Simple tests => 1;
  7. use Data::Dumper;
  8.  
  9. use Login;
  10. my $dbh = DBI->connect('dbi:Mock:', '', '');
  11.  
  12. $dbh->{mock_add_resultset} = {
  13. sql => "SELECT user_id FROM user_table users WHERE username = 'stvn' AND password = '1'",
  14. results => [[ 'user_id' ],
  15. [ 1 ]]
  16. };
  17.  
  18.  
  19.  
  20. sub login {
  21. my ($dbh, $u, $p) = @_;
  22. print "$u, $p $dbh";
  23. my $sth = $dbh->prepare("SELECT user_id FROM user_table users WHERE username = 'stvn' AND password = '1'");
  24. $sth->execute;
  25. my $user_id;
  26. while ( my $row = $sth->fetchrow_hashref ) {
  27. $user_id = $row->{user_id};
  28. }
  29. # look for the right username and password
  30. # my ($user_id) = $dbh->selectrow_array(
  31. # "SELECT user_id FROM users WHERE username = '$u' AND password = '$p'"
  32. # );
  33. print "id = $user_id\n";
  34. # if we find one, then ...
  35. if ($user_id) {
  36. # log the event and return success
  37. $dbh->do(
  38. "INSERT INTO event_log (event) VALUES('User $user_id logged in')"
  39. );
  40. return 'LOGIN SUCCESSFUL';
  41. }
  42. # if we don't find one then ...
  43. else {
  44. # see if the username exists ...
  45. my ($user_id, $login_failures) = $dbh->selectrow_array(
  46. "SELECT user_id, login_failures FROM users WHERE username = '$u'"
  47. );
  48. # if we do have a username, and the password doesnt match then
  49. if ($user_id) {
  50. # if we have not reached the max allowable login failures then
  51. if ($login_failures < $MAX_LOGIN_FAILURES) {
  52. # update the login failures
  53. $dbh->do(qq{
  54. UPDATE users
  55. SET login_failures = (login_failures + 1)
  56. WHERE user_id = $user_id
  57. });
  58. return 'BAD PASSWORD';
  59. }
  60. # otherwise ...
  61. else {
  62. # we must update the login failures, and lock the account
  63. $dbh->do(
  64. "UPDATE users SET login_failures = (login_failures + 1), " .
  65. "locked = 1 WHERE user_id = $user_id"
  66. );
  67. return 'USER ACCOUNT LOCKED';
  68. }
  69. }
  70. else {
  71. return 'USERNAME NOT FOUND';
  72. }
  73. }
  74. }
  75. print Dumper($got);
  76. $got = login($dbh, 'stvn', 1);
  77. ok($got eq 'LOGIN SUCCESSFUL', '... logged in successfully');
Add Comment
Please, Sign In to add comment