Bug 13814 : Add Lines Alignment
[koha.git] / t / Auth_with_shibboleth.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Module::Load::Conditional qw/check_install/;
21 use Test::More;
22 use Test::MockModule;
23 use Test::Warn;
24
25 use CGI;
26 use C4::Context;
27
28 BEGIN {
29     if ( check_install( module => 'Test::DBIx::Class' ) ) {
30         plan tests => 6;
31     } else {
32         plan skip_all => "Need Test::DBIx::Class"
33     }
34 }
35
36 use Test::DBIx::Class { schema_class => 'Koha::Schema', connect_info => ['dbi:SQLite:dbname=:memory:','',''] };
37
38 # Mock Variables
39 my $matchpoint = 'userid';
40 my %mapping = ( 'userid' => { 'is' => 'uid' }, );
41 $ENV{'uid'} = "test1234";
42
43 # Setup Mocks
44 ## Mock Context
45 my $context = new Test::MockModule('C4::Context');
46
47 ### Mock ->config
48 $context->mock( 'config', \&mockedConfig );
49
50 sub mockedConfig {
51     my $param = shift;
52
53     my %shibboleth = (
54         'matchpoint' => $matchpoint,
55         'mapping'    => \%mapping
56     );
57
58     return \%shibboleth;
59 }
60
61 ### Mock ->preference
62 $context->mock( 'preference', \&mockedPref );
63
64 sub mockedPref {
65     my $param = $_[1];
66     my $return;
67
68     if ( $param eq 'OPACBaseURL' ) {
69         $return = "testopac.com";
70     }
71
72     return $return;
73 }
74
75 ## Mock Database
76 my $database = new Test::MockModule('Koha::Database');
77
78 ### Mock ->schema
79 $database->mock( 'schema', \&mockedSchema );
80
81 sub mockedSchema {
82     return Schema();
83 }
84
85 ## Convenience method to reset config
86 sub reset_config {
87     $matchpoint = 'userid';
88     %mapping    = ( 'userid' => { 'is' => 'uid' }, );
89     $ENV{'uid'} = "test1234";
90
91     return 1;
92 }
93
94 # Tests
95 ##############################################################
96
97 # Can module load
98 use_ok('C4::Auth_with_shibboleth');
99 $C4::Auth_with_shibboleth::debug = '0';
100
101 # Subroutine tests
102 ## shib_ok
103 subtest "shib_ok tests" => sub {
104     plan tests => 5;
105     my $result;
106
107     # correct config, no debug
108     is( shib_ok(), '1', "good config" );
109
110     # bad config, no debug
111     $matchpoint = undef;
112     warnings_are { $result = shib_ok() }
113     [ { carped => 'shibboleth matchpoint not defined' }, ],
114       "undefined matchpoint = fatal config, warning given";
115     is( $result, '0', "bad config" );
116
117     $matchpoint = 'email';
118     warnings_are { $result = shib_ok() }
119     [ { carped => 'shibboleth matchpoint not mapped' }, ],
120       "unmapped matchpoint = fatal config, warning given";
121     is( $result, '0', "bad config" );
122
123     # add test for undefined shibboleth block
124
125     reset_config();
126 };
127
128 ## logout_shib
129 #my $query = CGI->new();
130 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
131
132 ## login_shib_url
133 my $query_string = 'language=en-GB';
134 $ENV{QUERY_STRING} = $query_string;
135 $ENV{SCRIPT_NAME}  = '/cgi-bin/koha/opac-user.pl';
136 my $query = CGI->new($query_string);
137 is(
138     login_shib_url($query),
139     'https://testopac.com'
140       . '/Shibboleth.sso/Login?target='
141       . 'https://testopac.com/cgi-bin/koha/opac-user.pl' . '%3F'
142       . $query_string,
143     "login shib url"
144 );
145
146 ## get_login_shib
147 subtest "get_login_shib tests" => sub {
148     plan tests => 4;
149     my $login;
150
151     # good config
152     ## debug off
153     $C4::Auth_with_shibboleth::debug = '0';
154     warnings_are { $login = get_login_shib() }[],
155       "good config with debug off, no warnings recieved";
156     is( $login, "test1234",
157         "good config with debug off, attribute value returned" );
158
159     ## debug on
160     $C4::Auth_with_shibboleth::debug = '1';
161     warnings_are { $login = get_login_shib() }[
162         "koha borrower field to match: userid",
163         "shibboleth attribute to match: uid",
164         "uid value: test1234"
165     ],
166       "good config with debug enabled, correct warnings recieved";
167     is( $login, "test1234",
168         "good config with debug enabled, attribute value returned" );
169
170 # bad config - with shib_ok implimented, we should never reach this sub with a bad config
171 };
172
173 ## checkpw_shib
174 subtest "checkpw_shib tests" => sub {
175     plan tests => 13;
176
177     my $shib_login;
178     my ( $retval, $retcard, $retuserid );
179
180     # Setup Mock Database Data
181     fixtures_ok [
182         'Borrower' => [
183             [qw/cardnumber userid surname address city/],
184             [qw/testcardnumber test1234 renvoize myaddress johnston/],
185         ],
186       ],
187       'Installed some custom fixtures via the Populate fixture class';
188
189     # debug off
190     $C4::Auth_with_shibboleth::debug = '0';
191
192     # good user
193     $shib_login = "test1234";
194     warnings_are {
195         ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
196     }
197     [], "good user with no debug";
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     $shib_login = 'martin';
204     warnings_are {
205         ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
206     }
207     [], "bad user with no debug";
208     is( $retval, "0", "user not authenticated" );
209
210     # debug on
211     $C4::Auth_with_shibboleth::debug = '1';
212
213     # good user
214     $shib_login = "test1234";
215     warnings_exist {
216         ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
217     }
218     [ qr/checkpw_shib/, qr/User Shibboleth-authenticated as:/ ],
219       "good user with debug enabled";
220     is( $retval,    "1",              "user authenticated" );
221     is( $retcard,   "testcardnumber", "expected cardnumber returned" );
222     is( $retuserid, "test1234",       "expected userid returned" );
223
224     # bad user
225     $shib_login = "martin";
226     warnings_exist {
227         ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
228     }
229     [
230         qr/checkpw_shib/,
231         qr/User Shibboleth-authenticated as:/,
232         qr/not a valid Koha user/
233     ],
234       "bad user with debug enabled";
235     is( $retval, "0", "user not authenticated" );
236
237 };
238
239 ## _get_uri
240 is( C4::Auth_with_shibboleth::_get_uri(),
241     "https://testopac.com", "https opac uri returned" );
242
243 ## _get_shib_config
244 # Internal helper function, covered in tests above