View | Details | Raw Unified | Return to bug 27947
Collapse All | Expand All

(-)a/t/db_dependent/api/v1/article_requests.t (-1 / +139 lines)
Line 0 Link Here
0
- 
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 => 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_requet =
52
      $builder->build_object( { class => 'Koha::ArticleRequests' } );
53
    my $deleted_article_requet_id = $deleted_article_requet->id;
54
    $deleted_article_requet->delete;
55
56
    $t->delete_ok(
57
"//$userid:$password@/api/v1/article_requests/$deleted_article_requet_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 => 10;
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 => { privacy_guarantor_checkouts => 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_requet = $builder->build_object( { class => 'Koha::ArticleRequests' } );
102
    my $deleted_article_requet_id = $deleted_article_requet->id;
103
    $deleted_article_requet->delete;
104
105
    my $another_patron = $builder->build_object({ class => 'Koha::Patrons' });
106
    my $another_patron_id = $another_patron->id;
107
108
    $t->delete_ok("//$userid:$password@/api/v1/public/patrons/$another_patron_id/article_requests/$deleted_article_requet_id")
109
      ->status_is(403);
110
111
    $t->delete_ok("//$userid:$password@/api/v1/public/patrons/$patron_id/article_requests/$deleted_article_requet_id")
112
      ->status_is(404)
113
      ->json_is( { error => "Article request not found" } );
114
115
    my $article_request = $builder->build_object(
116
        {
117
            class => 'Koha::ArticleRequests',
118
            value => { borrowernumber => $patron->id }
119
        }
120
    );
121
122
    my $reason = 'A reason';
123
    my $notes  = 'Some notes';
124
125
    $t->delete_ok(
126
        "//$userid:$password@/api/v1/public/patrons/$patron_id/article_requests/"
127
          . $article_request->id
128
          . "?cancellation_reason=$reason&notes=$notes" )
129
      ->status_is( 204, 'SWAGGER3.2.4' )
130
      ->content_is( q{}, 'SWAGGER3.2.4' );
131
132
    # refresh object
133
    $article_request->discard_changes;
134
135
    is( $article_request->cancellation_reason, $reason, 'Reason stored correctly' );
136
    is( $article_request->notes, $notes, 'Notes stored correctly' );
137
138
    $schema->storage->txn_rollback;
139
};

Return to bug 27947