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

(-)a/t/db_dependent/Koha/Auth.t (-1 / +92 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use C4::Auth qw( get_session );
5
use Test::More tests => 5;
6
use t::lib::TestBuilder;
7
8
use Koha::Database;
9
10
use_ok('Koha::Auth');
11
12
my $schema = Koha::Database->new->schema;
13
$schema->storage->txn_begin;
14
15
subtest 'Successful authentication' => sub {
16
    plan tests => 2;
17
18
    $ENV{REMOTE_ADDR} = '127.0.0.1';
19
    my $builder = t::lib::TestBuilder->new;
20
    my $borrower = $builder->build({ source => 'Borrower' });
21
    my $session = C4::Auth::get_session;
22
    $session->param( 'id',           $borrower->{userid} );
23
    $session->param( 'lasttime', time() );
24
    $session->param( 'ip', '127.0.0.1' );
25
    $session->flush;
26
    my $sessionID = $session->id;
27
28
    my ($user,$user_session) = Koha::Auth->authenticate({
29
        sessionID => $sessionID,
30
    });
31
    is(ref $user, 'Koha::Patron', 'User found');
32
    is(ref $user_session, 'CGI::Session', 'User session found');
33
34
    $session->delete;
35
};
36
37
subtest 'Failed authentication' => sub {
38
    plan tests => 2;
39
40
    $ENV{REMOTE_ADDR} = '127.0.0.1';
41
    my $session = C4::Auth::get_session;
42
    $session->flush;
43
    $session->param( 'ip', '127.0.0.1' );
44
    my $sessionID = $session->id;
45
46
    my ($user,$user_session) = Koha::Auth->authenticate({
47
        sessionID => $sessionID,
48
    });
49
    is($user, undef, 'User not found');
50
    is($user_session, undef, 'User session not found');
51
52
    $session->delete;
53
};
54
55
subtest 'Superlibrarian authorization' => sub {
56
    plan tests => 1;
57
    my $builder = t::lib::TestBuilder->new;
58
    my $borrower = $builder->build({ source => 'Borrower', value => {flags => 1,} });
59
60
    my $session = C4::Auth::get_session;
61
    $session->param( 'id',           $borrower->{userid} );
62
    $session->param( 'lasttime', time() );
63
    $session->param( 'ip', '127.0.0.1' );
64
    $session->flush;
65
66
    my $flags = Koha::Auth->authorize({
67
        session => $session,
68
        flagsrequired => { circulate => 1 },
69
    });
70
    is($flags->{superlibrarian},1,'Got superlibrarian authorization');
71
};
72
73
subtest 'Circulation staff authorization' => sub {
74
    plan tests => 2;
75
    my $builder = t::lib::TestBuilder->new;
76
    my $borrower = $builder->build({ source => 'Borrower', value => {flags => 2,} });
77
78
    my $session = C4::Auth::get_session;
79
    $session->param( 'id',           $borrower->{userid} );
80
    $session->param( 'lasttime', time() );
81
    $session->param( 'ip', '127.0.0.1' );
82
    $session->flush;
83
84
    my $flags = Koha::Auth->authorize({
85
        session => $session,
86
        flagsrequired => { circulate => 1 },
87
    });
88
    is($flags->{superlibrarian},0,'Did not get superlibrarian authorization');
89
    is($flags->{circulate},1,'Did get circulate authorization');
90
};
91
92
$schema->storage->txn_rollback;

Return to bug 31380