BUG8446, QA Followup: Use DBIx::Class
[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 Test::DBIx::Class {schema_class => 'Koha::Schema', connect_info => ['dbi:SQLite:dbname=:memory:','','']};
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 # Setup Mocks
21 ## Mock Context
22 my $context = new Test::MockModule('C4::Context');
23
24 ### Mock ->config
25 $context->mock( 'config', \&mockedConfig );
26
27 sub mockedConfig {
28     my $param = shift;
29
30     my %shibboleth = (
31         'matchpoint' => $matchpoint,
32         'mapping'    => \%mapping
33     );
34
35     return \%shibboleth;
36 }
37
38 ### Mock ->preference
39 $context->mock( 'preference', \&mockedPref );
40
41 sub mockedPref {
42     my $param = $_[1];
43     my $return;
44
45     if ( $param eq 'OPACBaseURL' ) {
46         $return = "testopac.com";
47     }
48
49     return $return;
50 }
51
52 ## Mock Database
53 my $database = new Test::MockModule('Koha::Database');
54
55 ### Mock ->schema
56 $database->mock( 'schema', \&mockedSchema );
57
58 sub mockedSchema {
59     return Schema();
60 }
61
62 ## Convenience method to reset config
63 sub reset_config {
64     $matchpoint = 'userid';
65     %mapping    = ( 'userid' => { 'is' => 'uid' }, );
66     $ENV{'uid'} = "test1234";
67
68     return 1;
69 }
70
71 # Tests
72 ##############################################################
73
74 # Can module load
75 use_ok('C4::Auth_with_shibboleth');
76 $C4::Auth_with_shibboleth::debug = '0';
77
78 # Subroutine tests
79 ## shib_ok
80 subtest "shib_ok tests" => sub {
81     plan tests => 5;
82     my $result;
83
84     # correct config, no debug
85     is( shib_ok(), '1', "good config" );
86
87     # bad config, no debug
88     $matchpoint = undef;
89     warnings_are { $result = shib_ok() }
90     [ { carped => 'shibboleth matchpoint not defined' }, ],
91       "undefined matchpoint = fatal config, warning given";
92     is( $result, '0', "bad config" );
93
94     $matchpoint = 'email';
95     warnings_are { $result = shib_ok() }
96     [ { carped => 'shibboleth matchpoint not mapped' }, ],
97       "unmapped matchpoint = fatal config, warning given";
98     is( $result, '0', "bad config" );
99
100     # add test for undefined shibboleth block
101
102     reset_config();
103 };
104
105 ## logout_shib
106 #my $query = CGI->new();
107 #is(logout_shib($query),"https://".$opac."/Shibboleth.sso/Logout?return="."https://".$opac,"logout_shib");
108
109 ## login_shib_url
110 my $query_string = 'language=en-GB';
111 $ENV{QUERY_STRING} = $query_string;
112 $ENV{SCRIPT_NAME}  = '/cgi-bin/koha/opac-user.pl';
113 my $query = CGI->new($query_string);
114 is(
115     login_shib_url($query),
116     'https://testopac.com'
117       . '/Shibboleth.sso/Login?target='
118       . 'https://testopac.com/cgi-bin/koha/opac-user.pl' . '%3F'
119       . $query_string,
120     "login shib url"
121 );
122
123 ## get_login_shib
124 subtest "get_login_shib tests" => sub {
125     plan tests => 4;
126     my $login;
127
128     # good config
129     ## debug off
130     $C4::Auth_with_shibboleth::debug = '0';
131     warnings_are { $login = get_login_shib() }[],
132       "good config with debug off, no warnings recieved";
133     is( $login, "test1234",
134         "good config with debug off, attribute value returned" );
135
136     ## debug on
137     $C4::Auth_with_shibboleth::debug = '1';
138     warnings_are { $login = get_login_shib() }[
139         "koha borrower field to match: userid",
140         "shibboleth attribute to match: uid",
141         "uid value: test1234"
142     ],
143       "good config with debug enabled, correct warnings recieved";
144     is( $login, "test1234",
145         "good config with debug enabled, attribute value returned" );
146
147 # bad config - with shib_ok implimented, we should never reach this sub with a bad config
148 };
149
150 ## checkpw_shib
151 subtest "checkpw_shib tests" => sub {
152     plan tests => 13;
153
154     my $shib_login;
155     my ( $retval, $retcard, $retuserid );
156
157     # Setup Mock Database Data
158     fixtures_ok [
159         'Borrower' => [
160             [qw/cardnumber userid surname address city/],
161             [qw/testcardnumber test1234 renvoize myaddress johnston/],
162         ],
163       ],
164       'Installed some custom fixtures via the Populate fixture class';
165
166     # debug off
167     $C4::Auth_with_shibboleth::debug = '0';
168
169     # good user
170     $shib_login = "test1234";
171     warnings_are {
172         ( $retval, $retcard, $retuserid ) = checkpw_shib( $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     $shib_login = 'martin';
181     warnings_are {
182         ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
183     }
184     [], "bad user with no debug";
185     is( $retval, "0", "user not authenticated" );
186
187     # debug on
188     $C4::Auth_with_shibboleth::debug = '1';
189
190     # good user
191     $shib_login = "test1234";
192     warnings_exist {
193         ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
194     }
195     [ qr/checkpw_shib/, qr/User Shibboleth-authenticated as:/ ],
196       "good user with debug enabled";
197     is( $retval,    "1",              "user authenticated" );
198     is( $retcard,   "testcardnumber", "expected cardnumber returned" );
199     is( $retuserid, "test1234",       "expected userid returned" );
200
201     # bad user
202     $shib_login = "martin";
203     warnings_exist {
204         ( $retval, $retcard, $retuserid ) = checkpw_shib( $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
221 # Internal helper function, covered in tests above