Bug 27947: (QA follow-up) Clarify permissions
[koha.git] / t / db_dependent / api / v1 / article_requests.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 => 2;
21 use Test::Mojo;
22
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use Koha::Database;
27
28 my $schema  = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
30
31 my $t = Test::Mojo->new('Koha::REST::V1');
32 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
33
34 subtest 'cancel() tests' => sub {
35
36     plan tests => 8;
37
38     $schema->storage->txn_begin;
39
40     my $authorized_patron = $builder->build_object(
41         {
42             class => 'Koha::Patrons',
43             value => { flags => 2 ** 1 } # circulate flag = 1
44         }
45     );
46     my $password = 'thePassword123';
47     $authorized_patron->set_password(
48         { password => $password, skip_validation => 1 } );
49     my $userid = $authorized_patron->userid;
50
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;
55
56     $t->delete_ok(
57 "//$userid:$password@/api/v1/article_requests/$deleted_article_request_id"
58     )->status_is(404)->json_is( { error => "Article request not found" } );
59
60     my $article_request =
61       $builder->build_object( { class => 'Koha::ArticleRequests' } );
62
63     my $reason = 'A reason';
64     my $notes  = 'Some notes';
65
66     $t->delete_ok( "//$userid:$password@/api/v1/article_requests/"
67           . $article_request->id
68           . "?cancellation_reason=$reason&notes=$notes" )
69       ->status_is( 204, 'SWAGGER3.2.4' )->content_is( q{}, 'SWAGGER3.2.4' );
70
71     # refresh object
72     $article_request->discard_changes;
73
74     is( $article_request->cancellation_reason,
75         $reason, 'Reason stored correctly' );
76     is( $article_request->notes, $notes, 'Notes stored correctly' );
77
78     $schema->storage->txn_rollback;
79 };
80
81 subtest 'patron_cancel() tests' => sub {
82
83     plan tests => 14;
84
85     t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 );
86     t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
87
88     $schema->storage->txn_begin;
89
90     my $patron = $builder->build_object(
91         {
92             class => 'Koha::Patrons',
93             value => { flags => 0 }
94         }
95     );
96     my $password = 'thePassword123';
97     $patron->set_password( { password => $password, skip_validation => 1 } );
98     my $userid    = $patron->userid;
99     my $patron_id = $patron->borrowernumber;
100
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;
104
105     # delete non existent article request
106     $t->delete_ok("//$userid:$password@/api/v1/public/patrons/$patron_id/article_requests/$deleted_article_request_id")
107       ->status_is(404)
108       ->json_is( { error => "Article request not found" } );
109
110     my $another_patron = $builder->build_object({ class => 'Koha::Patrons' });
111     my $another_patron_id = $another_patron->id;
112
113     my $article_request_2 = $builder->build_object({ class => 'Koha::ArticleRequests', value => { borrowernumber => $another_patron_id } });
114
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)
117       ->status_is(403)
118       ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
119
120     my $another_article_request = $builder->build_object(
121         {
122             class => 'Koha::ArticleRequests',
123             value => { borrowernumber => $another_patron->id }
124         }
125     );
126
127     $t->delete_ok("//$userid:$password@/api/v1/public/patrons/$patron_id/article_requests/" . $another_article_request->id)
128       ->status_is(404)
129       ->json_is( { error => 'Article request not found' } );
130
131     my $article_request = $builder->build_object(
132         {
133             class => 'Koha::ArticleRequests',
134             value => { borrowernumber => $patron->id }
135         }
136     );
137
138     my $reason = 'A reason';
139     my $notes  = 'Some notes';
140
141     $t->delete_ok(
142         "//$userid:$password@/api/v1/public/patrons/$patron_id/article_requests/"
143           . $article_request->id
144           . "?cancellation_reason=$reason&notes=$notes" )
145       ->status_is( 204, 'SWAGGER3.2.4' )
146       ->content_is( q{}, 'SWAGGER3.2.4' );
147
148     # refresh object
149     $article_request->discard_changes;
150
151     is( $article_request->cancellation_reason, $reason, 'Reason stored correctly' );
152     is( $article_request->notes, $notes, 'Notes stored correctly' );
153
154     $schema->storage->txn_rollback;
155 };