use Mojo::Base -strict; use Test::More; use Test::Mojo; use Test::Net::LDAP::Util qw/ldap_mockify/; my $t = Test::Mojo->new('MyApp'); my $basedn = 'dc=ldap,dc=perl,dc=org'; ldap_mockify { # mock the LDAP server and add entries my $ldap = Net::LDAP->new('ldap.example.com'); $ldap->add("uid=good,$basedn", attrs => [ userid => 'whitecamel']); $ldap->add("uid=evil,$basedn", attrs => [ userid => 'blackperl']); $ldap->mock_bind(sub { my $arg = shift; if ($arg->{dn}->dn() eq "uid=good,$basedn" && $arg->{password} eq 'LetMeIn' ) { return Net::LDAP::Constant::LDAP_SUCCESS; } else { return Net::LDAP::Constant::LDAP_INVALID_CREDENTIALS; } } ); # test the app $t->post_ok('/login' => {Accept => '*/*'} => form => {username => 'rubyred', password => 'nosuchuser'} ) ->status_is(403, 'User not found in LDAP'); $t->post_ok('/login' => {Accept => '*/*'} => form => {username => 'blackperl', password => 'Arrrgh'}) ->status_is(403, 'No access for incorrect password'); $t->post_ok('/login' => {Accept => '*/*'} => form => {username => 'whitecamel', password => 'wrongpassword'}) ->status_is(403, 'No access for incorrect password'); $t->get_ok('/secure/protected') ->status_is(401, 'Protected page is inaccessible without correct login'); $t->post_ok('/login' => {Accept => '*/*'} => form => {username => 'whitecamel', password => 'LetMeIn'} ) ->status_is(302, 'Redirected to Welcome page on successful login') ->content_like(qr/Welcome/); $t->get_ok('/secure/protected') ->status_is(200) ->content_like(qr/This is a protected page/, 'Protected page now accessible'); }; done_testing();