Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
use Modern::Perl; |
4 |
use C4::Auth qw( get_session ); |
5 |
use Test::More tests => 8; |
6 |
use t::lib::TestBuilder; |
7 |
use t::lib::Mocks; |
8 |
|
9 |
use Data::Dumper; |
10 |
use Koha::Database; |
11 |
|
12 |
use_ok('Koha::Auth'); |
13 |
|
14 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
15 |
|
16 |
my $schema = Koha::Database->new->schema; |
17 |
$schema->storage->txn_begin; |
18 |
|
19 |
subtest 'Successful authentication' => sub { |
20 |
plan tests => 2; |
21 |
|
22 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
23 |
my $builder = t::lib::TestBuilder->new; |
24 |
my $borrower = $builder->build({ source => 'Borrower' }); |
25 |
my $session = C4::Auth::get_session; |
26 |
$session->param( 'id', $borrower->{userid} ); |
27 |
$session->param( 'lasttime', time() ); |
28 |
$session->param( 'ip', '127.0.0.1' ); |
29 |
$session->flush; |
30 |
my $sessionID = $session->id; |
31 |
|
32 |
my ($user,$user_session) = Koha::Auth->authenticate({ |
33 |
sessionID => $sessionID, |
34 |
}); |
35 |
is(ref $user, 'Koha::Patron', 'User found'); |
36 |
is(ref $user_session, 'CGI::Session', 'User session found'); |
37 |
|
38 |
$session->delete; |
39 |
}; |
40 |
|
41 |
subtest 'Failed authentication' => sub { |
42 |
plan tests => 2; |
43 |
|
44 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
45 |
my $session = C4::Auth::get_session; |
46 |
$session->flush; |
47 |
$session->param( 'ip', '127.0.0.1' ); |
48 |
my $sessionID = $session->id; |
49 |
|
50 |
my ($user,$user_session) = Koha::Auth->authenticate({ |
51 |
sessionID => $sessionID, |
52 |
}); |
53 |
is($user, undef, 'User not found'); |
54 |
is($user_session, undef, 'User session not found'); |
55 |
|
56 |
$session->delete; |
57 |
}; |
58 |
|
59 |
subtest 'Superlibrarian authorization' => sub { |
60 |
plan tests => 1; |
61 |
my $builder = t::lib::TestBuilder->new; |
62 |
my $borrower = $builder->build({ source => 'Borrower', value => {flags => 1,} }); |
63 |
|
64 |
my $session = C4::Auth::get_session; |
65 |
$session->param( 'id', $borrower->{userid} ); |
66 |
$session->param( 'lasttime', time() ); |
67 |
$session->param( 'ip', '127.0.0.1' ); |
68 |
$session->flush; |
69 |
|
70 |
my $flags = Koha::Auth->authorize({ |
71 |
session => $session, |
72 |
flagsrequired => { circulate => 1 }, |
73 |
}); |
74 |
is($flags->{superlibrarian},1,'Got superlibrarian authorization'); |
75 |
}; |
76 |
|
77 |
subtest 'Circulation staff authorization' => sub { |
78 |
plan tests => 2; |
79 |
my $builder = t::lib::TestBuilder->new; |
80 |
my $borrower = $builder->build({ source => 'Borrower', value => {flags => 2,} }); |
81 |
|
82 |
my $session = C4::Auth::get_session; |
83 |
$session->param( 'id', $borrower->{userid} ); |
84 |
$session->param( 'lasttime', time() ); |
85 |
$session->param( 'ip', '127.0.0.1' ); |
86 |
$session->flush; |
87 |
|
88 |
my $flags = Koha::Auth->authorize({ |
89 |
session => $session, |
90 |
flagsrequired => { circulate => 1 }, |
91 |
}); |
92 |
is($flags->{superlibrarian},0,'Did not get superlibrarian authorization'); |
93 |
is($flags->{circulate},1,'Did get circulate authorization'); |
94 |
}; |
95 |
|
96 |
subtest 'Public user not authorized for circulate authorization requirement' => sub { |
97 |
plan tests => 1; |
98 |
my $builder = t::lib::TestBuilder->new; |
99 |
my $borrower = $builder->build({ source => 'Borrower', value => {flags => undef,} }); |
100 |
|
101 |
my $session = C4::Auth::get_session; |
102 |
$session->param( 'id', $borrower->{userid} ); |
103 |
$session->param( 'lasttime', time() ); |
104 |
$session->param( 'ip', '127.0.0.1' ); |
105 |
$session->flush; |
106 |
|
107 |
my $flags = Koha::Auth->authorize({ |
108 |
session => $session, |
109 |
flagsrequired => { circulate => 1 }, |
110 |
}); |
111 |
is($flags,0,'Flags returned 0'); |
112 |
}; |
113 |
|
114 |
subtest 'Staff user not authorized for circulate authorization requirement' => sub { |
115 |
plan tests => 1; |
116 |
my $builder = t::lib::TestBuilder->new; |
117 |
my $borrower = $builder->build({ source => 'Borrower', value => {flags => 4,} }); |
118 |
|
119 |
my $session = C4::Auth::get_session; |
120 |
$session->param( 'id', $borrower->{userid} ); |
121 |
$session->param( 'lasttime', time() ); |
122 |
$session->param( 'ip', '127.0.0.1' ); |
123 |
$session->flush; |
124 |
|
125 |
my $flags = Koha::Auth->authorize({ |
126 |
session => $session, |
127 |
flagsrequired => { circulate => 1 }, |
128 |
}); |
129 |
is($flags,0,'Flags returned 0'); |
130 |
}; |
131 |
|
132 |
subtest 'No authorization required' => sub { |
133 |
plan tests => 1; |
134 |
my $builder = t::lib::TestBuilder->new; |
135 |
my $borrower = $builder->build({ source => 'Borrower', value => {flags => undef,} }); |
136 |
|
137 |
my $session = C4::Auth::get_session; |
138 |
$session->param( 'id', $borrower->{userid} ); |
139 |
$session->param( 'lasttime', time() ); |
140 |
$session->param( 'ip', '127.0.0.1' ); |
141 |
$session->flush; |
142 |
|
143 |
my $flags = Koha::Auth->authorize({ |
144 |
session => $session, |
145 |
}); |
146 |
is(ref $flags,'HASH','When no flags are required, a hashref is returned'); |
147 |
}; |
148 |
|
149 |
$schema->storage->txn_rollback; |