BUG8446, QA Followup: Minor Code Tidies
[koha.git] / t / Auth_with_shibboleth.t
1 #!/usr/bin/perl
2 #
3 # Add more tests here!!!
4
5 use Modern::Perl;
6
7 use Test::More tests => 6;
8 use Test::MockModule;
9 use Test::Warn;
10 use DBD::Mock;
11
12 use CGI;
13 use C4::Context;
14
15 # Mock Variables
16 my $matchpoint = 'userid';
17 my %mapping = ( 'userid' => { 'is' => 'uid' }, );
18 $ENV{'uid'} = "test1234";
19
20 #my %shibboleth = (
21 #    'matchpoint' => $matchpoint,
22 #    'mapping'    => \%mapping
23 #);
24
25 # Setup Mocks
26 ## Mock Context
27 my $context = new Test::MockModule('C4::Context');
28
29 ### Mock ->dbh
30 $context->mock(
31     '_new_dbh',
32     sub {
33         my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
34           || die "Cannot create handle: $DBI::errstr\n";
35         return $dbh;
36     }
37 );
38
39 ### Mock ->config
40 $context->mock( 'config', \&mockedConfig );
41
42 sub mockedConfig {
43     my $param = shift;
44
45     my %shibboleth = (
46         'matchpoint' => $matchpoint,
47         'mapping'    => \%mapping
48     );
49
50     return \%shibboleth;
51 }
52
53 ### Mock ->preference
54 $context->mock( 'preference', \&mockedPref );
55
56 sub mockedPref {
57     my $param = $_[1];
58     my $return;
59
60     if ( $param eq 'OPACBaseURL' ) {
61         $return = "testopac.com";
62     }
63
64     return $return;
65 }
66
67 # Convenience methods
68 ## Reset Context
69 sub reset_config {
70     $matchpoint = 'userid';
71     %mapping    = ( 'userid' => { 'is' => 'uid' }, );
72     $ENV{'uid'} = "test1234";
73
74     return 1;
75 }
76
77 # Tests
78 my $dbh = C4::Context->dbh();
79
80 # Can module load
81 use_ok('C4::Auth_with_shibboleth');
82 $C4::Auth_with_shibboleth::debug = '0';
83
84 # Subroutine tests
85 ## shib_ok
86 subtest "shib_ok tests" => sub {
87     plan tests => 5;
88     my $result;
89
90     # correct config, no debug
91     is( shib_ok(), '1', "good config" );
92
93     # bad config, no debug
94     $matchpoint = undef;
95     warnings_are { $result = shib_ok() }
96     [ { carped => 'shibboleth matchpoint not defined' }, ],
97       "undefined matchpoint = fatal config, warning given";
98     is( $result, '0', "bad config" );
99
100     $matchpoint = 'email';
101     warnings_are { $result = shib_ok() }
102     [ { carped => 'shibboleth matchpoint not mapped' }, ],
103       "unmapped matchpoint = fatal config, warning given";
104     is( $result, '0', "bad config" );
105
106     # add test for undefined shibboleth block
107
108     reset_config();
109 };
110
111 ## logout_shib
112 #my $query = CGI->new();
113 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
114
115 ## login_shib_url
116 my $query_string = 'language=en-GB';
117 $ENV{QUERY_STRING} = $query_string;
118 $ENV{SCRIPT_NAME}  = '/cgi-bin/koha/opac-user.pl';
119 my $query = CGI->new($query_string);
120 is(
121     login_shib_url($query),
122     'https://testopac.com'
123       . '/Shibboleth.sso/Login?target='
124       . 'https://testopac.com/cgi-bin/koha/opac-user.pl' . '%3F'
125       . $query_string,
126     "login shib url"
127 );
128
129 ## get_login_shib
130 subtest "get_login_shib tests" => sub {
131     plan tests => 4;
132     my $login;
133
134     # good config
135     ## debug off
136     $C4::Auth_with_shibboleth::debug = '0';
137     warnings_are { $login = get_login_shib() }[],
138       "good config with debug off, no warnings recieved";
139     is( $login, "test1234",
140         "good config with debug off, attribute value returned" );
141
142     ## debug on
143     $C4::Auth_with_shibboleth::debug = '1';
144     warnings_are { $login = get_login_shib() }[
145         "koha borrower field to match: userid",
146         "shibboleth attribute to match: uid",
147         "uid value: test1234"
148     ],
149       "good config with debug enabled, correct warnings recieved";
150     is( $login, "test1234",
151         "good config with debug enabled, attribute value returned" );
152
153 # bad config - with shib_ok implimented, we should never reach this sub with a bad config
154 };
155
156 ## checkpw_shib
157 subtest "checkpw_shib tests" => sub {
158     plan tests => 12;
159
160     my $shib_login = 'test1234';
161     my @borrower_results =
162       ( [ 'cardnumber', 'userid' ], [ 'testcardnumber', 'test1234' ], );
163     $dbh->{mock_add_resultset} = \@borrower_results;
164
165     my ( $retval, $retcard, $retuserid );
166
167     # debug off
168     $C4::Auth_with_shibboleth::debug = '0';
169
170     # good user
171     warnings_are {
172         ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
173     }
174     [], "good user with no debug";
175     is( $retval,    "1",              "user authenticated" );
176     is( $retcard,   "testcardnumber", "expected cardnumber returned" );
177     is( $retuserid, "test1234",       "expected userid returned" );
178
179     # bad user
180     warnings_are {
181         ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
182     }
183     [], "bad user with no debug";
184     is( $retval, "0", "user not authenticated" );
185
186     # reset db mock
187     $dbh->{mock_add_resultset} = \@borrower_results;
188
189     # debug on
190     $C4::Auth_with_shibboleth::debug = '1';
191
192     # good user
193     warnings_exist {
194         ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
195     }
196     [ qr/checkpw_shib/, qr/User Shibboleth-authenticated as:/ ],
197       "good user with debug enabled";
198     is( $retval,    "1",              "user authenticated" );
199     is( $retcard,   "testcardnumber", "expected cardnumber returned" );
200     is( $retuserid, "test1234",       "expected userid returned" );
201
202     # bad user
203     warnings_exist {
204         ( $retval, $retcard, $retuserid ) = checkpw_shib( $dbh, $shib_login );
205     }
206     [
207         qr/checkpw_shib/,
208         qr/User Shibboleth-authenticated as:/,
209         qr/not a valid Koha user/
210     ],
211       "bad user with debug enabled";
212     is( $retval, "0", "user not authenticated" );
213
214 };
215
216 ## _get_uri
217 is( C4::Auth_with_shibboleth::_get_uri(),
218     "https://testopac.com", "https opac uri returned" );
219
220 ## _get_shib_config