Dancer2-Plugin-Auth-Extensible

 view release on metacpan or  search on metacpan

lib/Dancer2/Plugin/Auth/Extensible/Test/App.pm  view on Meta::CPAN

package Dancer2::Plugin::Auth::Extensible::Test::App;

=head1 NAME

Dancer2::Plugin::Auth::Extensible::Test::App - Dancer2 app for testing providers

=cut

our $VERSION = '0.714';

use strict;
use warnings;
use Test::More;
use Test::Deep qw(bag cmp_deeply);
use Test::Fatal;
use Dancer2 appname => 'TestApp';
use Dancer2::Plugin::Auth::Extensible;
use Scalar::Util qw(blessed);
use YAML ();

set session => 'simple';
set logger => 'capture';
set log => 'debug';
set show_errors => 1;

# nasty shared global makes it easy to pass data between app and test script
our $data = {};

config->{plugins}->{"Auth::Extensible"}->{password_reset_send_email} =
  __PACKAGE__ . "::email_send";
config->{plugins}->{"Auth::Extensible"}->{welcome_send} =
  __PACKAGE__ . "::email_send";

sub email_send {
    my ( $plugin, %args ) = @_;
    $data = { %args, called => 1 };
}

# we need the plugin object and a provider for provider tests
my $plugin = app->with_plugin('Auth::Extensible');
my $provider = $plugin->auth_provider('config1');

my @provider_can = ();

push @provider_can, 'record_lastlogin' if $plugin->config->{record_lastlogin};

config->{plugins}->{"Auth::Extensible"}->{reset_password_handler} = 1
  if $provider->can('get_user_by_code');

#
# IMPORTANT NOTE
#
# We use "isnt exception {...}, undef, ..." a lot which is REALLY BAD
# practice. This should only ever be done in provider tests since we cannot
# be sure what exception message a provider returns and we do NOT mandate
# specific messages so we can only test if something died.
#
# When writing new provider tests please always test first with a "like /qr/"
# instead and then once the tests are all working against one provider switch
# them to the bad "isnt undef" style so they are portable.
#

subtest 'Provider authenticate_user tests' => sub {
    my $ret;
    push @provider_can, 'authenticate_user';

    isnt exception { $ret = $provider->authenticate_user(); },
      undef,
      "authenticate_user with no args dies.";

    isnt exception { $ret = $provider->authenticate_user(''); },
      undef,
      "authenticate_user with empty username and no password dies.";

    isnt exception { $ret = $provider->authenticate_user(undef, ''); },



( run in 0.953 second using v1.01-cache-2.11-cpan-54e63673c56 )