Trog-Auth-libravatar.t 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. use strict;
  2. use warnings;
  3. use Test::More;
  4. use Test::MockModule qw{strict};
  5. use FindBin;
  6. use lib "$FindBin::Bin/../lib";
  7. # Stub modules absent from the test environment before loading Trog::Auth
  8. BEGIN {
  9. # Logging stubs — Log::Dispatch family not installed in test env
  10. $INC{'Log/Dispatch.pm'} = 1;
  11. $INC{'Log/Dispatch/DBI.pm'} = 1;
  12. $INC{'Log/Dispatch/Screen.pm'} = 1;
  13. $INC{'Log/Dispatch/FileRotate.pm'} = 1;
  14. $INC{'Trog/Log/DBI.pm'} = 1;
  15. # Other missing optional deps
  16. $INC{'HTTP/Tiny/UNIX.pm'} = 1;
  17. $INC{'File/LibMagic.pm'} = 1;
  18. $INC{'Imager/QRCode.pm'} = 1;
  19. $INC{'Config/Simple.pm'} = 1;
  20. }
  21. require_ok('Trog::Auth') or BAIL_OUT("Can't load Trog::Auth");
  22. subtest 'users_with_emails — no rows returned' => sub {
  23. my $sqlite_mock = Test::MockModule->new('Trog::SQLite');
  24. $sqlite_mock->redefine( 'dbh', sub {
  25. my $fake_dbh = bless {}, 'FakeDBH_NoRows';
  26. no warnings 'once';
  27. *FakeDBH_NoRows::selectall_arrayref = sub { undef };
  28. return $fake_dbh;
  29. });
  30. my $result = Trog::Auth::users_with_emails();
  31. is( ref $result, 'ARRAY', 'returns arrayref even when DB returns undef' );
  32. is( scalar @$result, 0, 'empty arrayref when no rows' );
  33. };
  34. subtest 'users_with_emails — returns populated list' => sub {
  35. my $fake_rows = [
  36. { name => 'alice', contact_email => 'alice@example.com' },
  37. { name => 'bob', contact_email => 'bob@example.com' },
  38. ];
  39. my $sqlite_mock = Test::MockModule->new('Trog::SQLite');
  40. $sqlite_mock->redefine( 'dbh', sub {
  41. my $fake_dbh = bless {}, 'FakeDBH_WithRows';
  42. no warnings 'once';
  43. *FakeDBH_WithRows::selectall_arrayref = sub { $fake_rows };
  44. return $fake_dbh;
  45. });
  46. my $result = Trog::Auth::users_with_emails();
  47. is( ref $result, 'ARRAY', 'returns arrayref' );
  48. is( scalar @$result, 2, 'correct number of users' );
  49. is( $result->[0]{name}, 'alice', 'first user name' );
  50. is( $result->[0]{contact_email}, 'alice@example.com', 'first user email' );
  51. is( $result->[1]{name}, 'bob', 'second user name' );
  52. };
  53. subtest 'libravatar hash matching logic' => sub {
  54. use Digest::MD5 qw{md5_hex};
  55. use Digest::SHA qw{sha256_hex};
  56. my $email = 'Test@Example.COM';
  57. my $norm = lc($email);
  58. my $sha256 = sha256_hex($norm);
  59. my $md5 = md5_hex($norm);
  60. isnt( $sha256, $md5, 'SHA-256 and MD5 hashes differ' );
  61. is( length($sha256), 64, 'SHA-256 hash is 64 hex chars' );
  62. is( length($md5), 32, 'MD5 hash is 32 hex chars' );
  63. # Case-normalisation must be consistent with what the endpoint does
  64. is( sha256_hex( lc($email) ), $sha256, 'lc() gives stable SHA-256' );
  65. is( md5_hex( lc($email) ), $md5, 'lc() gives stable MD5' );
  66. # Simulate the lookup: given a hash, find the matching email
  67. my @users = (
  68. { name => 'alice', contact_email => 'alice@example.com' },
  69. { name => 'bob', contact_email => 'bob@example.com' },
  70. );
  71. my $target_hash = sha256_hex( lc('alice@example.com') );
  72. my $matched;
  73. for my $u (@users) {
  74. my $e = lc( $u->{contact_email} );
  75. if ( $target_hash eq sha256_hex($e) || $target_hash eq md5_hex($e) ) {
  76. $matched = $u->{name};
  77. last;
  78. }
  79. }
  80. is( $matched, 'alice', 'SHA-256 lookup finds the correct user' );
  81. my $target_md5 = md5_hex( lc('bob@example.com') );
  82. $matched = undef;
  83. for my $u (@users) {
  84. my $e = lc( $u->{contact_email} );
  85. if ( $target_md5 eq sha256_hex($e) || $target_md5 eq md5_hex($e) ) {
  86. $matched = $u->{name};
  87. last;
  88. }
  89. }
  90. is( $matched, 'bob', 'MD5 lookup finds the correct user' );
  91. # Unknown hash returns no match
  92. $matched = undef;
  93. for my $u (@users) {
  94. my $e = lc( $u->{contact_email} );
  95. if ( 'deadbeef' eq sha256_hex($e) || 'deadbeef' eq md5_hex($e) ) {
  96. $matched = $u->{name};
  97. last;
  98. }
  99. }
  100. is( $matched, undef, 'unknown hash returns no match' );
  101. };
  102. done_testing;