3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 1;
26 use t::lib::TestBuilder;
29 use Koha::AuthorisedValueCategories;
30 use Koha::Illrequests;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37 my $t = Test::Mojo->new('Koha::REST::V1');
39 subtest 'list() tests' => sub {
43 # Mock ILL::Request::Config (as module)
44 my $illconfig_module = Test::MockModule->new('Koha::ILL::Request::Config');
46 # Start with no backends installed
47 $illconfig_module->mock( 'available_backends', sub { () } );
49 # Mock ILLBackend (as object)
50 my $backend = Test::MockObject->new;
51 $backend->set_isa('Koha::Illbackends::Mock');
52 $backend->set_always( 'name', 'Mock' );
53 $backend->set_always( 'capabilities', sub { return 'bar'; } );
57 my ( $self, $rq ) = @_;
59 ID => $rq->illrequest_id,
60 Title => $rq->patron->borrowernumber
65 #Add a backend-specific status
71 prev_actions => [ 'NEW', 'ERROR' ],
73 name => 'Request ready',
74 ui_method_name => 'Mark request as ready',
77 ui_method_icon => 'fa-check',
83 # Mock Koha::Illrequest::load_backend (to load Mocked Backend)
84 my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
85 $illreqmodule->mock( 'load_backend',
86 sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
89 $schema->storage->txn_begin;
91 Koha::Illrequests->search->delete;
93 # create an authorized user
94 my $librarian = $builder->build_object(
96 class => 'Koha::Patrons',
97 value => { flags => 2**22 } # 22 => ill
100 my $password = 'thePassword123';
101 $librarian->set_password( { password => $password, skip_validation => 1 } );
102 my $userid = $librarian->userid;
104 # create an unauthorized user
105 my $patron = $builder->build_object(
107 class => 'Koha::Patrons',
108 value => { flags => 0 }
112 $patron->set_password( { password => $password, skip_validation => 1 } );
113 my $unauth_userid = $patron->userid;
115 # Make sure the ILL_STATUS_ALIAS authorised value category is defined
117 Koha::AuthorisedValueCategories->search(
118 { category_name => 'ILL_STATUS_ALIAS' }
122 $builder->build_object(
124 class => 'Koha::AuthorisedValueCategories',
125 value => { category_name => 'ILL_STATUS_ALIAS' }
130 my $tag = "Print copy";
131 my $av_code = "print_copy";
132 my $av = $builder->build_object(
134 class => 'Koha::AuthorisedValues',
136 category => 'ILL_STATUS_ALIAS',
137 authorised_value => $av_code,
143 # No backends, expect empty
144 $t->get_ok("//$userid:$password@/api/v1/ill/backends")->status_is(200)
147 # Mock one existing backend
148 $illconfig_module->mock( 'available_backends', sub { ["Mock"] } );
150 #One backend exists, expect that
151 $t->get_ok("//$userid:$password@/api/v1/ill/backends")->status_is(200)
152 ->json_has( '/0/ill_backend_id', 'Mock' );
155 my $backend_status = {
157 str => "Request ready"
169 # Create some ILL requests
170 my $backend_status_req = $builder->build_object(
172 class => 'Koha::Illrequests',
174 { status => $backend_status->{code}, backend => $backend->name }
177 my $core_status_req = $builder->build_object(
179 class => 'Koha::Illrequests',
181 { status => $core_status->{code}, backend => $backend->name }
184 my $alias_status_req = $builder->build_object(
186 class => 'Koha::Illrequests',
188 status => $core_status->{code},
189 backend => $backend->name,
190 status_alias => $av->authorised_value
195 #Check for backend existing statuses
196 $t->get_ok("//$userid:$password@/api/v1/ill/backends/Mock" => {'x-koha-embed' => 'statuses+strings'} )
198 ->json_has( '/statuses', [ $backend_status, $core_status, $alias_status ] );
200 #Check for backend existing statuses of a backend that doesn't exist
201 $t->get_ok("//$userid:$password@/api/v1/ill/backends/GhostBackend" => {'x-koha-embed' => 'statuses+strings'} )
203 ->json_hasnt( 'statuses' );
205 # Unauthorized attempt to list
206 $t->get_ok("//$unauth_userid:$password@/api/v1/ill/backends")
209 # DELETE method not supported
210 $t->delete_ok("//$unauth_userid:$password@/api/v1/ill/backends")
213 $schema->storage->txn_rollback;