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 => 2;
24 use t::lib::TestBuilder;
28 my $schema = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
31 my $t = Test::Mojo->new('Koha::REST::V1');
32 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34 subtest 'cancel() tests' => sub {
38 $schema->storage->txn_begin;
40 my $authorized_patron = $builder->build_object(
42 class => 'Koha::Patrons',
43 value => { flags => 2 ** 1 } # circulate flag = 1
46 my $password = 'thePassword123';
47 $authorized_patron->set_password(
48 { password => $password, skip_validation => 1 } );
49 my $userid = $authorized_patron->userid;
51 my $deleted_article_request =
52 $builder->build_object( { class => 'Koha::ArticleRequests' } );
53 my $deleted_article_request_id = $deleted_article_request->id;
54 $deleted_article_request->delete;
57 "//$userid:$password@/api/v1/article_requests/$deleted_article_request_id"
58 )->status_is(404)->json_is( { error => "Article request not found" } );
61 $builder->build_object( { class => 'Koha::ArticleRequests' } );
63 my $reason = 'A reason';
64 my $notes = 'Some notes';
66 $t->delete_ok( "//$userid:$password@/api/v1/article_requests/"
67 . $article_request->id
68 . "?cancellation_reason=$reason¬es=$notes" )
69 ->status_is( 204, 'SWAGGER3.2.4' )->content_is( q{}, 'SWAGGER3.2.4' );
72 $article_request->discard_changes;
74 is( $article_request->cancellation_reason,
75 $reason, 'Reason stored correctly' );
76 is( $article_request->notes, $notes, 'Notes stored correctly' );
78 $schema->storage->txn_rollback;
81 subtest 'patron_cancel() tests' => sub {
85 t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 );
86 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
88 $schema->storage->txn_begin;
90 my $patron = $builder->build_object(
92 class => 'Koha::Patrons',
93 value => { flags => 0 }
96 my $password = 'thePassword123';
97 $patron->set_password( { password => $password, skip_validation => 1 } );
98 my $userid = $patron->userid;
99 my $patron_id = $patron->borrowernumber;
101 my $deleted_article_request = $builder->build_object( { class => 'Koha::ArticleRequests', value => { borrowernumber => $patron_id } } );
102 my $deleted_article_request_id = $deleted_article_request->id;
103 $deleted_article_request->delete;
105 # delete non existent article request
106 $t->delete_ok("//$userid:$password@/api/v1/public/patrons/$patron_id/article_requests/$deleted_article_request_id")
108 ->json_is( { error => "Article request not found" } );
110 my $another_patron = $builder->build_object({ class => 'Koha::Patrons' });
111 my $another_patron_id = $another_patron->id;
113 my $article_request_2 = $builder->build_object({ class => 'Koha::ArticleRequests', value => { borrowernumber => $another_patron_id } });
115 # delete another patron's request
116 $t->delete_ok("//$userid:$password@/api/v1/public/patrons/$another_patron_id/article_requests/" . $article_request_2->id)
118 ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
120 my $another_article_request = $builder->build_object(
122 class => 'Koha::ArticleRequests',
123 value => { borrowernumber => $another_patron->id }
127 $t->delete_ok("//$userid:$password@/api/v1/public/patrons/$patron_id/article_requests/" . $another_article_request->id)
129 ->json_is( { error => 'Article request not found' } );
131 my $article_request = $builder->build_object(
133 class => 'Koha::ArticleRequests',
134 value => { borrowernumber => $patron->id }
138 my $reason = 'A reason';
139 my $notes = 'Some notes';
142 "//$userid:$password@/api/v1/public/patrons/$patron_id/article_requests/"
143 . $article_request->id
144 . "?cancellation_reason=$reason¬es=$notes" )
145 ->status_is( 204, 'SWAGGER3.2.4' )
146 ->content_is( q{}, 'SWAGGER3.2.4' );
149 $article_request->discard_changes;
151 is( $article_request->cancellation_reason, $reason, 'Reason stored correctly' );
152 is( $article_request->notes, $notes, 'Notes stored correctly' );
154 $schema->storage->txn_rollback;