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 |
{ |
34 |
sessionID => $sessionID, |
35 |
} |
36 |
); |
37 |
is( ref $user, 'Koha::Patron', 'User found' ); |
38 |
is( ref $user_session, 'CGI::Session', 'User session found' ); |
39 |
|
40 |
$session->delete; |
41 |
}; |
42 |
|
43 |
subtest 'Failed authentication' => sub { |
44 |
plan tests => 2; |
45 |
|
46 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
47 |
my $session = C4::Auth::get_session; |
48 |
$session->flush; |
49 |
$session->param( 'ip', '127.0.0.1' ); |
50 |
my $sessionID = $session->id; |
51 |
|
52 |
my ( $user, $user_session ) = Koha::Auth->authenticate( |
53 |
{ |
54 |
sessionID => $sessionID, |
55 |
} |
56 |
); |
57 |
is( $user, undef, 'User not found' ); |
58 |
is( $user_session, undef, 'User session not found' ); |
59 |
|
60 |
$session->delete; |
61 |
}; |
62 |
|
63 |
subtest 'Superlibrarian authorization' => sub { |
64 |
plan tests => 1; |
65 |
my $builder = t::lib::TestBuilder->new; |
66 |
my $borrower = $builder->build( { source => 'Borrower', value => { flags => 1, } } ); |
67 |
|
68 |
my $session = C4::Auth::get_session; |
69 |
$session->param( 'id', $borrower->{userid} ); |
70 |
$session->param( 'lasttime', time() ); |
71 |
$session->param( 'ip', '127.0.0.1' ); |
72 |
$session->flush; |
73 |
|
74 |
my $flags = Koha::Auth->authorize( |
75 |
{ |
76 |
session => $session, |
77 |
flagsrequired => { circulate => 1 }, |
78 |
} |
79 |
); |
80 |
is( $flags->{superlibrarian}, 1, 'Got superlibrarian authorization' ); |
81 |
}; |
82 |
|
83 |
subtest 'Circulation staff authorization' => sub { |
84 |
plan tests => 2; |
85 |
my $builder = t::lib::TestBuilder->new; |
86 |
my $borrower = $builder->build( { source => 'Borrower', value => { flags => 2, } } ); |
87 |
|
88 |
my $session = C4::Auth::get_session; |
89 |
$session->param( 'id', $borrower->{userid} ); |
90 |
$session->param( 'lasttime', time() ); |
91 |
$session->param( 'ip', '127.0.0.1' ); |
92 |
$session->flush; |
93 |
|
94 |
my $flags = Koha::Auth->authorize( |
95 |
{ |
96 |
session => $session, |
97 |
flagsrequired => { circulate => 1 }, |
98 |
} |
99 |
); |
100 |
is( $flags->{superlibrarian}, 0, 'Did not get superlibrarian authorization' ); |
101 |
is( $flags->{circulate}, 1, 'Did get circulate authorization' ); |
102 |
}; |
103 |
|
104 |
subtest 'Public user not authorized for circulate authorization requirement' => sub { |
105 |
plan tests => 1; |
106 |
my $builder = t::lib::TestBuilder->new; |
107 |
my $borrower = $builder->build( { source => 'Borrower', value => { flags => undef, } } ); |
108 |
|
109 |
my $session = C4::Auth::get_session; |
110 |
$session->param( 'id', $borrower->{userid} ); |
111 |
$session->param( 'lasttime', time() ); |
112 |
$session->param( 'ip', '127.0.0.1' ); |
113 |
$session->flush; |
114 |
|
115 |
my $flags = Koha::Auth->authorize( |
116 |
{ |
117 |
session => $session, |
118 |
flagsrequired => { circulate => 1 }, |
119 |
} |
120 |
); |
121 |
is( $flags, 0, 'Flags returned 0' ); |
122 |
}; |
123 |
|
124 |
subtest 'Staff user not authorized for circulate authorization requirement' => sub { |
125 |
plan tests => 1; |
126 |
my $builder = t::lib::TestBuilder->new; |
127 |
my $borrower = $builder->build( { source => 'Borrower', value => { flags => 4, } } ); |
128 |
|
129 |
my $session = C4::Auth::get_session; |
130 |
$session->param( 'id', $borrower->{userid} ); |
131 |
$session->param( 'lasttime', time() ); |
132 |
$session->param( 'ip', '127.0.0.1' ); |
133 |
$session->flush; |
134 |
|
135 |
my $flags = Koha::Auth->authorize( |
136 |
{ |
137 |
session => $session, |
138 |
flagsrequired => { circulate => 1 }, |
139 |
} |
140 |
); |
141 |
is( $flags, 0, 'Flags returned 0' ); |
142 |
}; |
143 |
|
144 |
subtest 'No authorization required' => sub { |
145 |
plan tests => 1; |
146 |
my $builder = t::lib::TestBuilder->new; |
147 |
my $borrower = $builder->build( { source => 'Borrower', value => { flags => undef, } } ); |
148 |
|
149 |
my $session = C4::Auth::get_session; |
150 |
$session->param( 'id', $borrower->{userid} ); |
151 |
$session->param( 'lasttime', time() ); |
152 |
$session->param( 'ip', '127.0.0.1' ); |
153 |
$session->flush; |
154 |
|
155 |
my $flags = Koha::Auth->authorize( |
156 |
{ |
157 |
session => $session, |
158 |
} |
159 |
); |
160 |
is( ref $flags, 'HASH', 'When no flags are required, a hashref is returned' ); |
161 |
}; |
162 |
|
163 |
$schema->storage->txn_rollback; |