3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Test
::More tests
=> 3;
22 use C4
::Auth qw
/checkpw/;
27 use t
::lib
::Mocks qw
/mock_preference/; # to mock CronjobLog
28 use t
::lib
::TestBuilder
;
30 # Make sure we can rollback.
31 our $schema = Koha
::Database
->new->schema;
32 $schema->storage->txn_begin;
33 our $dbh = C4
::Context
->dbh;
35 subtest
'Existing tests' => sub {
40 # FIXME: are we sure there is an member number 1?
41 logaction
("MEMBERS","MODIFY",1,"test operation");
47 ok
($success, "logaction seemed to work");
49 # We want numbers to be the same between runs.
50 $dbh->do("DELETE FROM action_logs;");
52 t
::lib
::Mocks
::mock_preference
('CronjobLog',0);
54 my $cronJobCount = $dbh->selectrow_array("SELECT COUNT(*) FROM action_logs WHERE module='CRONJOBS';",{});
55 is
($cronJobCount,0,"Cronjob not logged as expected.");
57 t
::lib
::Mocks
::mock_preference
('CronjobLog',1);
59 $cronJobCount = $dbh->selectrow_array("SELECT COUNT(*) FROM action_logs WHERE module='CRONJOBS';",{});
60 is
($cronJobCount,1,"Cronjob logged as expected.");
63 subtest
'logaction(): interface is correctly logged' => sub {
67 # No interface passed, using C4::Context->interface
68 $dbh->do("DELETE FROM action_logs;");
69 C4
::Context
->interface( 'commandline' );
70 logaction
( "MEMBERS", "MODIFY", 1, "test operation");
71 my $log = Koha
::ActionLogs
->search->next;
72 is
( $log->interface, 'commandline', 'Interface correctly deduced (commandline)');
74 # No interface passed, using C4::Context->interface
75 $dbh->do("DELETE FROM action_logs;");
76 C4
::Context
->interface( 'opac' );
77 logaction
( "MEMBERS", "MODIFY", 1, "test operation");
78 $log = Koha
::ActionLogs
->search->next;
79 is
( $log->interface, 'opac', 'Interface correctly deduced (opac)');
82 $dbh->do("DELETE FROM action_logs;");
83 C4
::Context
->interface( 'intranet' );
84 logaction
( "MEMBERS", "MODIFY", 1, 'test info', 'intranet');
85 $log = Koha
::ActionLogs
->search->next;
86 is
( $log->interface, 'intranet', 'Passed interface is respected (intranet)');
89 $dbh->do("DELETE FROM action_logs;");
90 C4
::Context
->interface( 'sip' );
91 logaction
( "MEMBERS", "MODIFY", 1, 'test info', 'sip');
92 $log = Koha
::ActionLogs
->search->next;
93 is
( $log->interface, 'sip', 'Passed interface is respected (sip)');
96 subtest
'GDPR logging' => sub {
99 my $builder = t
::lib
::TestBuilder
->new;
100 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
102 t
::lib
::Mocks
::mock_userenv
({ patron
=> $patron });
103 logaction
( 'AUTH', 'FAILURE', $patron->id, '', 'opac' );
104 my $logs = Koha
::ActionLogs
->search(
109 object
=> $patron->id,
112 is
( $logs->count, 1, 'We should find one auth failure' );
114 t
::lib
::Mocks
::mock_preference
('AuthFailureLog', 1);
115 my $strong_password = 'N0tStr0ngAnyM0reN0w:)';
116 $patron->set_password({ password
=> $strong_password });
117 my @ret = checkpw
( $dbh, $patron->userid, 'WrongPassword', undef, undef, 1);
118 is
( $ret[0], 0, 'Authentication failed' );
119 # Look for auth failure but NOT on patron id, pass userid in info parameter
120 $logs = Koha
::ActionLogs
->search(
124 info
=> { -like
=> '%'.$patron->userid.'%' },
127 is
( $logs->count, 1, 'We should find one auth failure with this userid' );
128 t
::lib
::Mocks
::mock_preference
('AuthFailureLog', 0);
129 @ret = checkpw
( $dbh, $patron->userid, 'WrongPassword', undef, undef, 1);
130 $logs = Koha
::ActionLogs
->search(
134 info
=> { -like
=> '%'.$patron->userid.'%' },
137 is
( $logs->count, 1, 'Still only one failure with this userid' );
138 t
::lib
::Mocks
::mock_preference
('AuthSuccessLog', 1);
139 @ret = checkpw
( $dbh, $patron->userid, $strong_password, undef, undef, 1);
140 is
( $ret[0], 1, 'Authentication succeeded' );
141 # Now we can look for patron id
142 $logs = Koha
::ActionLogs
->search(
147 object
=> $patron->id,
151 is
( $logs->count, 1, 'We expect only one auth success line for this patron' );
154 $schema->storage->txn_rollback;