View | Details | Raw Unified | Return to bug 12026
Collapse All | Expand All

(-)a/t/Auth_with_shibboleth.t (-53 / +111 lines)
Lines 29-45 use C4::Context; Link Here
29
BEGIN {
29
BEGIN {
30
    if ( check_install( module => 'Test::DBIx::Class' ) ) {
30
    if ( check_install( module => 'Test::DBIx::Class' ) ) {
31
        plan tests => 11;
31
        plan tests => 11;
32
    } else {
32
    }
33
        plan skip_all => "Need Test::DBIx::Class"
33
    else {
34
        plan skip_all => "Need Test::DBIx::Class";
34
    }
35
    }
35
}
36
}
36
37
37
use Test::DBIx::Class { schema_class => 'Koha::Schema', connect_info => ['dbi:SQLite:dbname=:memory:','',''] };
38
use Test::DBIx::Class {
39
    schema_class => 'Koha::Schema',
40
    connect_info => [ 'dbi:SQLite:dbname=:memory:', '', '' ]
41
};
38
42
39
# Mock Variables
43
# Mock Variables
40
my $matchpoint = 'userid';
44
my $matchpoint = 'userid';
41
my %mapping = ( 'userid' => { 'is' => 'uid' }, );
45
my $autocreate = 0;
42
$ENV{'uid'} = "test1234";
46
my %mapping    = (
47
    'userid'       => { 'is' => 'uid' },
48
    'surname'      => { 'is' => 'sn' },
49
    'dateexpiry'   => { 'is' => 'exp' },
50
    'categorycode' => { 'is' => 'cat' },
51
    'address'      => { 'is' => 'add' },
52
    'city'         => { 'is' => 'city' },
53
);
54
$ENV{'uid'}  = "test1234";
55
$ENV{'sn'}   = undef;
56
$ENV{'exp'}  = undef;
57
$ENV{'cat'}  = undef;
58
$ENV{'add'}  = undef;
59
$ENV{'city'} = undef;
43
60
44
# Setup Mocks
61
# Setup Mocks
45
## Mock Context
62
## Mock Context
Lines 48-98 my $context = new Test::MockModule('C4::Context'); Link Here
48
### Mock ->config
65
### Mock ->config
49
$context->mock( 'config', \&mockedConfig );
66
$context->mock( 'config', \&mockedConfig );
50
67
51
sub mockedConfig {
52
    my $param = shift;
53
54
    my %shibboleth = (
55
        'matchpoint' => $matchpoint,
56
        'mapping'    => \%mapping
57
    );
58
59
    return \%shibboleth;
60
}
61
62
### Mock ->preference
68
### Mock ->preference
63
my $OPACBaseURL = "testopac.com";
69
my $OPACBaseURL = "testopac.com";
64
$context->mock( 'preference', \&mockedPref );
70
$context->mock( 'preference', \&mockedPref );
65
71
66
sub mockedPref {
67
    my $param = $_[1];
68
    my $return;
69
70
    if ( $param eq 'OPACBaseURL' ) {
71
        $return = $OPACBaseURL;
72
    }
73
74
    return $return;
75
}
76
77
## Mock Database
72
## Mock Database
78
my $database = new Test::MockModule('Koha::Database');
73
my $database = new Test::MockModule('Koha::Database');
79
74
80
### Mock ->schema
75
### Mock ->schema
81
$database->mock( 'schema', \&mockedSchema );
76
$database->mock( 'schema', \&mockedSchema );
82
77
83
sub mockedSchema {
84
    return Schema();
85
}
86
87
## Convenience method to reset config
88
sub reset_config {
89
    $matchpoint = 'userid';
90
    %mapping    = ( 'userid' => { 'is' => 'uid' }, );
91
    $ENV{'uid'} = "test1234";
92
93
    return 1;
94
}
95
96
# Tests
78
# Tests
97
##############################################################
79
##############################################################
98
80
Lines 175-181 subtest "get_login_shib tests" => sub { Link Here
175
157
176
## checkpw_shib
158
## checkpw_shib
177
subtest "checkpw_shib tests" => sub {
159
subtest "checkpw_shib tests" => sub {
178
    plan tests => 13;
160
    plan tests => 18;
179
161
180
    my $shib_login;
162
    my $shib_login;
181
    my ( $retval, $retcard, $retuserid );
163
    my ( $retval, $retcard, $retuserid );
Lines 186-191 subtest "checkpw_shib tests" => sub { Link Here
186
            [qw/cardnumber userid surname address city/],
168
            [qw/cardnumber userid surname address city/],
187
            [qw/testcardnumber test1234 renvoize myaddress johnston/],
169
            [qw/testcardnumber test1234 renvoize myaddress johnston/],
188
        ],
170
        ],
171
        'Category' => [ [qw/categorycode default_privacy/], [qw/S never/], ]
189
      ],
172
      ],
190
      'Installed some custom fixtures via the Populate fixture class';
173
      'Installed some custom fixtures via the Populate fixture class';
191
174
Lines 195-201 subtest "checkpw_shib tests" => sub { Link Here
195
    # good user
178
    # good user
196
    $shib_login = "test1234";
179
    $shib_login = "test1234";
197
    warnings_are {
180
    warnings_are {
198
        ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
181
        ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
199
    }
182
    }
200
    [], "good user with no debug";
183
    [], "good user with no debug";
201
    is( $retval,    "1",              "user authenticated" );
184
    is( $retval,    "1",              "user authenticated" );
Lines 205-226 subtest "checkpw_shib tests" => sub { Link Here
205
    # bad user
188
    # bad user
206
    $shib_login = 'martin';
189
    $shib_login = 'martin';
207
    warnings_are {
190
    warnings_are {
208
        ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
191
        ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
209
    }
192
    }
210
    [], "bad user with no debug";
193
    [], "bad user with no debug";
211
    is( $retval, "0", "user not authenticated" );
194
    is( $retval, "0", "user not authenticated" );
212
195
196
    # autocreate user
197
    $autocreate  = 1;
198
    $shib_login  = 'test4321';
199
    $ENV{'uid'}  = 'test4321';
200
    $ENV{'sn'}   = "pika";
201
    $ENV{'exp'}  = "2017";
202
    $ENV{'cat'}  = "S";
203
    $ENV{'add'}  = 'Address';
204
    $ENV{'city'} = 'City';
205
    warnings_are {
206
        ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
207
    }
208
    [], "new user added with no debug";
209
    is( $retval,    "1",        "user authenticated" );
210
    is( $retuserid, "test4321", "expected userid returned" );
211
    ok my $new_user = ResultSet('Borrower')
212
      ->search( { 'userid' => 'test4321' }, { rows => 1 } ), "new user found";
213
    is_fields [qw/surname dateexpiry address city/], $new_user->next,
214
      [qw/pika 2017 Address City/],
215
      'Found $new_users surname';
216
    $autocreate = 0;
217
213
    # debug on
218
    # debug on
214
    $C4::Auth_with_shibboleth::debug = '1';
219
    $C4::Auth_with_shibboleth::debug = '1';
215
220
216
    # good user
221
    # good user
217
    $shib_login = "test1234";
222
    $shib_login = "test1234";
218
    warnings_exist {
223
    warnings_exist {
219
        ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
224
        ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
220
    }
225
    }
221
    [ qr/checkpw_shib/, qr/koha borrower field to match: userid/,
226
    [
222
      qr/shibboleth attribute to match: uid/,
227
        qr/checkpw_shib/,
223
      qr/User Shibboleth-authenticated as:/ ],
228
        qr/koha borrower field to match: userid/,
229
        qr/shibboleth attribute to match: uid/,
230
        qr/User Shibboleth-authenticated as:/
231
    ],
224
      "good user with debug enabled";
232
      "good user with debug enabled";
225
    is( $retval,    "1",              "user authenticated" );
233
    is( $retval,    "1",              "user authenticated" );
226
    is( $retcard,   "testcardnumber", "expected cardnumber returned" );
234
    is( $retcard,   "testcardnumber", "expected cardnumber returned" );
Lines 229-235 subtest "checkpw_shib tests" => sub { Link Here
229
    # bad user
237
    # bad user
230
    $shib_login = "martin";
238
    $shib_login = "martin";
231
    warnings_exist {
239
    warnings_exist {
232
        ( $retval, $retcard, $retuserid ) = checkpw_shib( $shib_login );
240
        ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login);
233
    }
241
    }
234
    [
242
    [
235
        qr/checkpw_shib/,
243
        qr/checkpw_shib/,
Lines 251-258 is( C4::Auth_with_shibboleth::_get_uri(), Link Here
251
$OPACBaseURL = "http://testopac.com";
259
$OPACBaseURL = "http://testopac.com";
252
my $result;
260
my $result;
253
warning_like { $result = C4::Auth_with_shibboleth::_get_uri() }
261
warning_like { $result = C4::Auth_with_shibboleth::_get_uri() }
254
             [ qr/Shibboleth requires OPACBaseURL to use the https protocol!/ ],
262
[qr/Shibboleth requires OPACBaseURL to use the https protocol!/],
255
             "improper protocol - received expected warning";
263
  "improper protocol - received expected warning";
256
is( $result, "https://testopac.com", "https opac uri returned" );
264
is( $result, "https://testopac.com", "https opac uri returned" );
257
265
258
$OPACBaseURL = "https://testopac.com";
266
$OPACBaseURL = "https://testopac.com";
Lines 261-269 is( C4::Auth_with_shibboleth::_get_uri(), Link Here
261
269
262
$OPACBaseURL = undef;
270
$OPACBaseURL = undef;
263
warning_like { $result = C4::Auth_with_shibboleth::_get_uri() }
271
warning_like { $result = C4::Auth_with_shibboleth::_get_uri() }
264
             [ qr/OPACBaseURL not set!/ ],
272
[qr/OPACBaseURL not set!/],
265
             "undefined OPACBaseURL - received expected warning";
273
  "undefined OPACBaseURL - received expected warning";
266
is( $result, "https://", "https opac uri returned" );
274
is( $result, "https://", "https opac uri returned" );
267
275
268
## _get_shib_config
276
## _get_shib_config
269
# Internal helper function, covered in tests above
277
# Internal helper function, covered in tests above
270
- 
278
279
sub mockedConfig {
280
    my $param = shift;
281
282
    my %shibboleth = (
283
        'autocreate' => $autocreate,
284
        'matchpoint' => $matchpoint,
285
        'mapping'    => \%mapping
286
    );
287
288
    return \%shibboleth;
289
}
290
291
sub mockedPref {
292
    my $param = $_[1];
293
    my $return;
294
295
    if ( $param eq 'OPACBaseURL' ) {
296
        $return = $OPACBaseURL;
297
    }
298
299
    return $return;
300
}
301
302
sub mockedSchema {
303
    return Schema();
304
}
305
306
## Convenience method to reset config
307
sub reset_config {
308
    $matchpoint = 'userid';
309
    $autocreate = 0;
310
    %mapping    = (
311
        'userid'       => { 'is' => 'uid' },
312
        'surname'      => { 'is' => 'sn' },
313
        'dateexpiry'   => { 'is' => 'exp' },
314
        'categorycode' => { 'is' => 'cat' },
315
        'address'      => { 'is' => 'add' },
316
        'city'         => { 'is' => 'city' },
317
    );
318
    $ENV{'uid'}  = "test1234";
319
    $ENV{'sn'}   = undef;
320
    $ENV{'exp'}  = undef;
321
    $ENV{'cat'}  = undef;
322
    $ENV{'add'}  = undef;
323
    $ENV{'city'} = undef;
324
325
    return 1;
326
}
327
328
1;

Return to bug 12026