Your mother was X.500 and your father smells of RFCs! Now go away or I shall mock you a second time!
- from the original draft of Monty Python and the Holy Grail.
I finally buckled down and starting to mock the LDAP server in my tests rather than trying to connect to a live server. Other than the documentation, there's not a lot of examples out there for Test::Net::LDAP::Mock or Test::Net::LDAP::Util, so here's the results from banging away at it for an afternoon along with what I think is going on. Please feel free to point out what I've done wrong. I've stopped where it started working for me.
I have a Mojolcious app that authenticates against LDAP, but the tests would fail when using dummy accounts or when I wasn't connected. Here's the test I wrote
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();
Well, what do you think? Does it get the job done?
Edit - while cleaning up tabs used for putting this post together, I found a relevant question on StackOverflow from 5 years ago, but it hasn't been answered so far.
Sometimes I can think of 6 impossible LDAP attributes before breakfast.
YAPC::Europe::2018 — Hmmm, need to talk to work about sending me there or to Mojoconf.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Mocking LDAP in your tests
by shmem (Chancellor) on Jun 01, 2018 at 00:13 UTC | |
by Ea (Chaplain) on Jun 01, 2018 at 10:03 UTC | |
by trantorvega (Initiate) on Sep 01, 2018 at 15:30 UTC | |
by haukex (Archbishop) on Sep 01, 2018 at 16:57 UTC |