| Line 0
          
      
      
        Link Here | 
          
            
              | 0 | -  | 1 | #!/usr/bin/env perl | 
            
              |  |  | 2 |  | 
            
              | 3 | # Copyright 2023 PTFS Europe | 
            
              | 4 |  | 
            
              | 5 | # This file is part of Koha. | 
            
              | 6 | # | 
            
              | 7 | # Koha is free software; you can redistribute it and/or modify it | 
            
              | 8 | # under the terms of the GNU General Public License as published by | 
            
              | 9 | # the Free Software Foundation; either version 3 of the License, or | 
            
              | 10 | # (at your option) any later version. | 
            
              | 11 | # | 
            
              | 12 | # Koha is distributed in the hope that it will be useful, but | 
            
              | 13 | # WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
              | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
              | 15 | # GNU General Public License for more details. | 
            
              | 16 | # | 
            
              | 17 | # You should have received a copy of the GNU General Public License | 
            
              | 18 | # along with Koha; if not, see <http://www.gnu.org/licenses>. | 
            
              | 19 |  | 
            
              | 20 | use Modern::Perl; | 
            
              | 21 |  | 
            
              | 22 | use Test::More tests => 3; | 
            
              | 23 | use Test::Mojo; | 
            
              | 24 |  | 
            
              | 25 | use t::lib::TestBuilder; | 
            
              | 26 | use t::lib::Mocks; | 
            
              | 27 |  | 
            
              | 28 | use Koha::ERM::EUsage::CounterLogs; | 
            
              | 29 | use Koha::Database; | 
            
              | 30 |  | 
            
              | 31 | my $schema  = Koha::Database->new->schema; | 
            
              | 32 | my $builder = t::lib::TestBuilder->new; | 
            
              | 33 |  | 
            
              | 34 | my $t = Test::Mojo->new('Koha::REST::V1'); | 
            
              | 35 | t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); | 
            
              | 36 |  | 
            
              | 37 | subtest 'list() tests' => sub { | 
            
              | 38 |  | 
            
              | 39 |     plan tests => 17; | 
            
              | 40 |  | 
            
              | 41 |     $schema->storage->txn_begin; | 
            
              | 42 |  | 
            
              | 43 |     Koha::ERM::EUsage::CounterLogs->search->delete; | 
            
              | 44 |  | 
            
              | 45 |     my $librarian = $builder->build_object( | 
            
              | 46 |         { | 
            
              | 47 |             class => 'Koha::Patrons', | 
            
              | 48 |             value => { flags => 2**28 } | 
            
              | 49 |         } | 
            
              | 50 |     ); | 
            
              | 51 |     my $password = 'thePassword123'; | 
            
              | 52 |     $librarian->set_password( { password => $password, skip_validation => 1 } ); | 
            
              | 53 |     my $userid = $librarian->userid; | 
            
              | 54 |  | 
            
              | 55 |     my $patron = $builder->build_object( | 
            
              | 56 |         { | 
            
              | 57 |             class => 'Koha::Patrons', | 
            
              | 58 |             value => { flags => 0 } | 
            
              | 59 |         } | 
            
              | 60 |     ); | 
            
              | 61 |  | 
            
              | 62 |     $patron->set_password( { password => $password, skip_validation => 1 } ); | 
            
              | 63 |     my $unauth_userid = $patron->userid; | 
            
              | 64 |  | 
            
              | 65 |     ## Authorized user tests | 
            
              | 66 |     # No counter logs, so empty array should be returned | 
            
              | 67 |     $t->get_ok("//$userid:$password@/api/v1/erm/counter_logs")->status_is(200)->json_is( [] ); | 
            
              | 68 |  | 
            
              | 69 |     my $counter_log = $builder->build_object( { class => 'Koha::ERM::EUsage::CounterLogs' } ); | 
            
              | 70 |  | 
            
              | 71 |     # One counter_log created, should get returned | 
            
              | 72 |     $t->get_ok("//$userid:$password@/api/v1/erm/counter_logs")->status_is(200)->json_is( [ $counter_log->to_api ] ); | 
            
              | 73 |  | 
            
              | 74 |     my $another_counter_log = $builder->build_object( | 
            
              | 75 |         { | 
            
              | 76 |             class => 'Koha::ERM::EUsage::CounterLogs', | 
            
              | 77 |         } | 
            
              | 78 |     ); | 
            
              | 79 |  | 
            
              | 80 |     # Two counter_logs created, they should both be returned | 
            
              | 81 |     $t->get_ok("//$userid:$password@/api/v1/erm/counter_logs")->status_is(200) | 
            
              | 82 |         ->json_is( [ $counter_log->to_api, $another_counter_log->to_api, ] ); | 
            
              | 83 |  | 
            
              | 84 |     # Return 2 counter logs with patron embeded | 
            
              | 85 |     $t->get_ok( "//$userid:$password@/api/v1/erm/counter_logs/" => { 'x-koha-embed' => 'patron' } )->status_is(200) | 
            
              | 86 |         ->json_is( | 
            
              | 87 |         [ | 
            
              | 88 |             { %{ $counter_log->to_api }, patron => $counter_log->patron->to_api( { user => $librarian } ) }, | 
            
              | 89 |             { | 
            
              | 90 |                 %{ $another_counter_log->to_api }, | 
            
              | 91 |                 patron => $another_counter_log->patron->to_api( { user => $librarian } ) | 
            
              | 92 |             } | 
            
              | 93 |         ] | 
            
              | 94 |         ); | 
            
              | 95 |  | 
            
              | 96 |     # Warn on unsupported query parameter | 
            
              | 97 |     $t->get_ok("//$userid:$password@/api/v1/erm/counter_logs?blah=blah")->status_is(400) | 
            
              | 98 |         ->json_is( [ { path => '/query/blah', message => 'Malformed query string' } ] ); | 
            
              | 99 |  | 
            
              | 100 |     # Unauthorized access | 
            
              | 101 |     $t->get_ok("//$unauth_userid:$password@/api/v1/erm/counter_logs")->status_is(403); | 
            
              | 102 |  | 
            
              | 103 |     $schema->storage->txn_rollback; | 
            
              | 104 | }; | 
            
              | 105 |  | 
            
              | 106 | subtest 'get() tests' => sub { | 
            
              | 107 |  | 
            
              | 108 |     plan tests => 2; | 
            
              | 109 |  | 
            
              | 110 |     $schema->storage->txn_begin; | 
            
              | 111 |  | 
            
              | 112 |     my $counter_log = $builder->build_object( { class => 'Koha::ERM::EUsage::CounterLogs' } ); | 
            
              | 113 |     my $librarian   = $builder->build_object( | 
            
              | 114 |         { | 
            
              | 115 |             class => 'Koha::Patrons', | 
            
              | 116 |             value => { flags => 2**28 } | 
            
              | 117 |         } | 
            
              | 118 |     ); | 
            
              | 119 |     my $password = 'thePassword123'; | 
            
              | 120 |     $librarian->set_password( { password => $password, skip_validation => 1 } ); | 
            
              | 121 |     my $userid = $librarian->userid; | 
            
              | 122 |  | 
            
              | 123 |     my $patron = $builder->build_object( | 
            
              | 124 |         { | 
            
              | 125 |             class => 'Koha::Patrons', | 
            
              | 126 |             value => { flags => 0 } | 
            
              | 127 |         } | 
            
              | 128 |     ); | 
            
              | 129 |  | 
            
              | 130 |     $patron->set_password( { password => $password, skip_validation => 1 } ); | 
            
              | 131 |     my $unauth_userid = $patron->userid; | 
            
              | 132 |  | 
            
              | 133 |     # This get method is not implemented should return 404 | 
            
              | 134 |     $t->get_ok( "//$userid:$password@/api/v1/erm/counter_logs/" . $counter_log->erm_counter_log_id )->status_is(404); | 
            
              | 135 |  | 
            
              | 136 |     $schema->storage->txn_rollback; | 
            
              | 137 | }; | 
            
              | 138 |  | 
            
              | 139 | subtest 'delete() tests' => sub { | 
            
              | 140 |  | 
            
              | 141 |     plan tests => 4; | 
            
              | 142 |  | 
            
              | 143 |     $schema->storage->txn_begin; | 
            
              | 144 |  | 
            
              | 145 |     my $librarian = $builder->build_object( | 
            
              | 146 |         { | 
            
              | 147 |             class => 'Koha::Patrons', | 
            
              | 148 |             value => { flags => 2**28 } | 
            
              | 149 |         } | 
            
              | 150 |     ); | 
            
              | 151 |     my $password = 'thePassword123'; | 
            
              | 152 |     $librarian->set_password( { password => $password, skip_validation => 1 } ); | 
            
              | 153 |     my $userid = $librarian->userid; | 
            
              | 154 |  | 
            
              | 155 |     my $patron = $builder->build_object( | 
            
              | 156 |         { | 
            
              | 157 |             class => 'Koha::Patrons', | 
            
              | 158 |             value => { flags => 0 } | 
            
              | 159 |         } | 
            
              | 160 |     ); | 
            
              | 161 |  | 
            
              | 162 |     $patron->set_password( { password => $password, skip_validation => 1 } ); | 
            
              | 163 |     my $unauth_userid = $patron->userid; | 
            
              | 164 |  | 
            
              | 165 |     my $counter_log_id = $builder->build_object( { class => 'Koha::ERM::EUsage::CounterLogs' } )->erm_counter_log_id; | 
            
              | 166 |  | 
            
              | 167 |     # Attempt to delete fails (route does not exist) | 
            
              | 168 |     $t->delete_ok("//$unauth_userid:$password@/api/v1/erm/counter_logs/$counter_log_id")->status_is(404); | 
            
              | 169 |  | 
            
              | 170 |     # Attempt to delete non-existent counter_log | 
            
              | 171 |     $t->delete_ok("//$userid:$password@/api/v1/erm/counter_logs/$counter_log_id")->status_is(404); | 
            
              | 172 |  | 
            
              | 173 |     $schema->storage->txn_rollback; | 
            
              | 174 | }; |