Trog-Auth-libravatar.t 4.2 KB

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