rename from ::Plugin and refactor tests
[catagits/Catalyst-Authentication-Store-LDAP.git] / t / sane.pl
1
2 use Test::More tests => 12;
3 use Carp;
4 use Net::LDAP;
5 use Net::LDAP::Server::Test;
6 use Net::LDAP::Entry;
7
8 #
9 # these tests pulled nearly verbatim from the Net::LDAP synopsis
10 #
11
12 my %opts = (
13     port  => '10636',
14     dnc   => 'ou=internal,dc=foo',
15     debug => $ENV{PERL_DEBUG} || 0,
16 );
17
18 my $host = 'ldap://localhost:' . $opts{port};
19
20 ok( my $server = Net::LDAP::Server::Test->new( $opts{port} ),
21     "spawn new server" );
22
23 ok( my $ldap = Net::LDAP->new( $host, %opts, ), "new LDAP connection" );
24
25 unless ($ldap) {
26     croak "Unable to connect to LDAP server $host: $@";
27 }
28
29 ok( my $rc = $ldap->bind(), "LDAP bind()" );
30
31 ok( my $mesg = $ldap->search(    # perform a search
32         base   => "c=US",
33         filter => "(&(sn=Barr) (o=Texas Instruments))"
34     ),
35     "LDAP search()"
36 );
37
38 $mesg->code && croak $mesg->error;
39
40 my $count = 0;
41 foreach my $entry ( $mesg->entries ) {
42
43     #$entry->dump;
44     $count++;
45 }
46
47 is( $count, 13, "$count entries found in search" );
48
49 ok( $mesg = $ldap->unbind, "LDAP unbind()" );
50
51 #warn "unbind done";
52
53 my @mydata;
54 my $entry = Net::LDAP::Entry->new;
55 $entry->dn('ou=foobar');
56 $entry->add(
57     dn => 'ou=foobar',
58     sn => 'value1',
59     cn => [qw(value1 value2)]
60 );
61 push @mydata, $entry;
62
63 ok( $server = Net::LDAP::Server::Test->new( $opts{port}, \@mydata ),
64     "spawn new server with our own data" );
65
66 ok( $ldap = Net::LDAP->new( $host, %opts, ), "new LDAP connection" );
67
68 unless ($ldap) {
69     croak "Unable to connect to LDAP server $host: $@";
70 }
71
72 ok( $rc = $ldap->bind(), "LDAP bind()" );
73
74 ok( $mesg = $ldap->search(    # perform a search
75         base   => "c=US",
76         filter => "(&(sn=Barr) (o=Texas Instruments))"
77     ),
78     "LDAP search()"
79 );
80
81 $mesg->code && croak $mesg->error;
82
83 $count = 0;
84 foreach my $entry ( $mesg->entries ) {
85
86     $entry->dump;
87     $count++;
88 }
89
90 is( $count, 1, "$count entries found in search" );
91
92 ok( $mesg = $ldap->unbind, "LDAP unbind()" );
93