Trog-Auth-change-request.t 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env perl
  2. use 5.040;
  3. use re '/aasx';
  4. use warnings FATAL => 'all';
  5. use FindBin::libs;
  6. use Test::More;
  7. use Cwd();
  8. use File::Copy();
  9. use File::Path();
  10. use File::Temp();
  11. # Auth keeps its database at config/auth.db under the cwd, so it gets a sandbox.
  12. my $repo = Cwd::getcwd();
  13. my $root = File::Temp::tempdir( CLEANUP => 1 );
  14. File::Path::make_path( "$root/config", "$root/schema" );
  15. File::Copy::copy( "$repo/schema/auth.schema", "$root/schema/auth.schema" ) or die $!;
  16. chdir($root) or die "chdir $root: $!";
  17. END { chdir($repo) if $repo }
  18. require_ok('Trog::Auth') or BAIL_OUT("Can't find SUT");
  19. my $dbh = Trog::SQLite::dbh( 'schema/auth.schema', 'config/auth.db' );
  20. sub contact_of ($user) { return $dbh->selectrow_hashref( 'SELECT display_name, contact_email FROM user WHERE name = ?', undef, $user ) }
  21. subtest 'process_change_request: each reset keeps its own user' => sub {
  22. Trog::Auth::useradd( qw{alice Alice old}, [], 'alice@test.test' );
  23. Trog::Auth::useradd( qw{bob Bob old}, [], 'bob@test.test' );
  24. Trog::Auth::add_change_request( user => $_, token => "t-$_", type => 'reset_pass', secret => 'new' ) for qw{alice bob};
  25. like( Trog::Auth::process_change_request('t-alice'), qr/for[ ]alice/, 'the first reset is done' );
  26. like( Trog::Auth::process_change_request('t-bob'), qr/for[ ]bob/, 'and the second' );
  27. is_deeply( contact_of('alice'), { display_name => 'Alice', contact_email => 'alice@test.test' }, 'the first user is as they were' );
  28. is_deeply( contact_of('bob'), { display_name => 'Bob', contact_email => 'bob@test.test' }, 'and the second keeps their own name and address, not the first one\'s' );
  29. };
  30. done_testing();