Bug 35581: Koha::Illrequest::Config -> Koha::ILL::Request::Config
[koha.git] / t / db_dependent / api / v1 / ill_backends.t
1 #!/usr/bin/env perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 1;
21
22 use Test::MockModule;
23 use Test::MockObject;
24 use Test::Mojo;
25
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
28
29 use Koha::AuthorisedValueCategories;
30 use Koha::Illrequests;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37 my $t = Test::Mojo->new('Koha::REST::V1');
38
39 subtest 'list() tests' => sub {
40
41     plan tests => 16;
42
43     # Mock ILL::Request::Config (as module)
44     my $illconfig_module = Test::MockModule->new('Koha::ILL::Request::Config');
45
46     # Start with no backends installed
47     $illconfig_module->mock( 'available_backends', sub { () } );
48
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'; } );
54     $backend->mock(
55         'metadata',
56         sub {
57             my ( $self, $rq ) = @_;
58             return {
59                 ID    => $rq->illrequest_id,
60                 Title => $rq->patron->borrowernumber
61             };
62         }
63     );
64
65     #Add a backend-specific status
66     $backend->mock(
67         'status_graph',
68         sub {
69             return {
70                 READY => {
71                     prev_actions   => [ 'NEW', 'ERROR' ],
72                     id             => 'READY',
73                     name           => 'Request ready',
74                     ui_method_name => 'Mark request as ready',
75                     method         => 'ready',
76                     next_actions   => [],
77                     ui_method_icon => 'fa-check',
78                 }
79             };
80         }
81     );
82
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 }
87     );
88
89     $schema->storage->txn_begin;
90
91     Koha::Illrequests->search->delete;
92
93     # create an authorized user
94     my $librarian = $builder->build_object(
95         {
96             class => 'Koha::Patrons',
97             value => { flags => 2**22 }    # 22 => ill
98         }
99     );
100     my $password = 'thePassword123';
101     $librarian->set_password( { password => $password, skip_validation => 1 } );
102     my $userid = $librarian->userid;
103
104     # create an unauthorized user
105     my $patron = $builder->build_object(
106         {
107             class => 'Koha::Patrons',
108             value => { flags => 0 }
109         }
110     );
111
112     $patron->set_password( { password => $password, skip_validation => 1 } );
113     my $unauth_userid = $patron->userid;
114
115     # Make sure the ILL_STATUS_ALIAS authorised value category is defined
116     unless (
117         Koha::AuthorisedValueCategories->search(
118             { category_name => 'ILL_STATUS_ALIAS' }
119         )->count > 0
120       )
121     {
122         $builder->build_object(
123             {
124                 class => 'Koha::AuthorisedValueCategories',
125                 value => { category_name => 'ILL_STATUS_ALIAS' }
126             }
127         );
128     }
129
130     my $tag     = "Print copy";
131     my $av_code = "print_copy";
132     my $av      = $builder->build_object(
133         {
134             class => 'Koha::AuthorisedValues',
135             value => {
136                 category         => 'ILL_STATUS_ALIAS',
137                 authorised_value => $av_code,
138                 lib              => $tag,
139             }
140         }
141     );
142
143     # No backends, expect empty
144     $t->get_ok("//$userid:$password@/api/v1/ill/backends")->status_is(200)
145       ->json_is( [] );
146
147     # Mock one existing backend
148     $illconfig_module->mock( 'available_backends', sub { ["Mock"] } );
149
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' );
153
154     # Prepare status
155     my $backend_status = {
156         code => "READY",
157         str  => "Request ready"
158     };
159     my $core_status = {
160         code => "COMP",
161         str  => "Completed"
162     };
163
164     my $alias_status = {
165         code => $av_code,
166         str  => $tag,
167     };
168
169     # Create some ILL requests
170     my $backend_status_req = $builder->build_object(
171         {
172             class => 'Koha::Illrequests',
173             value =>
174               { status => $backend_status->{code}, backend => $backend->name }
175         }
176     );
177     my $core_status_req = $builder->build_object(
178         {
179             class => 'Koha::Illrequests',
180             value =>
181               { status => $core_status->{code}, backend => $backend->name }
182         }
183     );
184     my $alias_status_req = $builder->build_object(
185         {
186             class => 'Koha::Illrequests',
187             value => {
188                 status       => $core_status->{code},
189                 backend      => $backend->name,
190                 status_alias => $av->authorised_value
191             }
192         }
193     );
194
195     #Check for backend existing statuses
196     $t->get_ok("//$userid:$password@/api/v1/ill/backends/Mock" => {'x-koha-embed' => 'statuses+strings'} )
197       ->status_is(200)
198       ->json_has( '/statuses', [ $backend_status, $core_status, $alias_status ] );
199
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'} )
202       ->status_is(200)
203       ->json_hasnt( 'statuses' );
204
205     # Unauthorized attempt to list
206     $t->get_ok("//$unauth_userid:$password@/api/v1/ill/backends")
207       ->status_is(403);
208
209     # DELETE method not supported
210     $t->delete_ok("//$unauth_userid:$password@/api/v1/ill/backends")
211       ->status_is(404);
212
213     $schema->storage->txn_rollback;
214 };