Apache2-AuthCookieDBI

 view release on metacpan or  search on metacpan

t/utils.t  view on Meta::CPAN

        Data::Dumper::Dumper($mock_dbh) );

    Test::More::is_deeply( $r->log->error(), [],
        '_dbi_connect() - no unexpected errors.' );

    my $test_dsn = $mock_config->{"${auth_name}DBI_DSN"};

    my @expected_error_messages
        = ( qq{couldn't connect to $test_dsn for auth realm $auth_name}, );

    my @expected_info_messages = (
        q{_dbi_connect called in main::test__dbi_connect},
        qq{connect to test_DBI_DSN for auth realm $auth_name},
    );

    {
        no warnings qw(once);
        local $DBI::CONNECT_CACHED_FORCE_FAIL = 1;
        CLASS_UNDER_TEST->_dbi_connect($r);
    }

    my @got_info_messages  = @{ $r->log->info };
    my @got_error_messages = @{ $r->log->error };
    my $got_failures       = 0;

    for ( my $i = 0; $i <= $#expected_error_messages; $i++ ) {
        my $got            = $got_error_messages[$i];
        my $expected_regex = qr/$expected_error_messages[$i]/;
        Test::More::like( $got, $expected_regex,
            qq{_dbi_connect() logs info for "$expected_error_messages[$i]"} )
            || $got_failures++;
    }

    for ( my $i = 0; $i <= $#expected_info_messages; $i++ ) {
        my $got            = $got_info_messages[$i];
        my $expected_regex = qr/$expected_info_messages[$i]/;
        Test::More::like( $got, $expected_regex,
            qq{_dbi_connect() logs info for "$expected_info_messages[$i]"} )
            || $got_failures++;
    }

    if ($got_failures) {
        Test::More::diag( 'Mock request object contains: ',
            Data::Dumper::Dumper($r) );
    }
    return TRUE;
}

sub test_user_is_active {
    my $auth_name = 'test_user_is_active';
    my $r         = set_up($auth_name);
    my $user      = 'TestUser';

    # The default config has an empty string for DBI_UserActiveField
    my $is_active = CLASS_UNDER_TEST->user_is_active( $r, $user );
    Test::More::ok( $is_active, 'test_user_is_active() for default config' );

    # Now set DBI_UserActiveField so that user status is determined by
    # a call to the database (we'll intercept here using mocks.)
    $r->{'mock_config'}->{"${auth_name}DBI_UserActiveField"} = 'active';
    my $not_active;

    # Simulate a user the database says is not active.
    {
        no warnings qw(once redefine);
        local *DBI::Mock::sth::fetchrow_array = sub {
            return;    # simulates user is not active
        };
        $not_active = CLASS_UNDER_TEST->user_is_active( $r, $user );
    }
    Test::More::ok( !$not_active,
        'test_user_is_active() inactive user using DBI_UserActiveField' )
        || Test::More::diag("Expected a non-true value, got '$not_active'");

    # Now simulate an active user whose status is fetch from the database
    my $active_user;
    {
        no warnings qw(once redefine);
        local *DBI::Mock::sth::fetchrow_array = sub {
            return 'yes';    # simulates an active user
        };
        $active_user = CLASS_UNDER_TEST->user_is_active( $r, $user );
    }
    Test::More::ok( $active_user,
        'test_user_is_active() with active user using DBI_UserActiveField' );

    return TRUE;
}

sub test__get_new_session {
    my $auth_name      = 'test__get_new_session';
    my $r              = set_up($auth_name);
    my $user           = 'TestUser';
    my $session_module = 'Mock::Tieable';
    my $extra_data     = 'extra data';

    my $got_session = CLASS_UNDER_TEST->_get_new_session( $r, $user, $auth_name,
        $session_module, $extra_data );

    Test::More::is_deeply(
        $got_session,
        { user => $user, extra_data => $extra_data },
        q{_get_new_session() ties 'user' and 'extra_data' args.}
    );
    return TRUE;
}



( run in 1.341 second using v1.01-cache-2.11-cpan-39bf76dae61 )