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

(-)a/Koha/REST/V1/Patrons/HoldGroups.pm (+28 lines)
Lines 126-129 sub add { Link Here
126
    };
126
    };
127
}
127
}
128
128
129
=head3 delete
130
131
Controller method that handles removing a hold group.
132
133
=cut
134
135
sub delete {
136
    my $c = shift->openapi->valid_input or return;
137
138
    my $patron = Koha::Patrons->find( $c->param('patron_id') );
139
140
    return $c->render_resource_not_found("Patron")
141
        unless $patron;
142
143
    return try {
144
145
        my $hold_group = $patron->hold_groups->find( $c->param('hold_group_id') );
146
147
        return $c->render_resource_not_found("Hold group")
148
            unless $hold_group;
149
150
        $hold_group->delete;
151
        return $c->render_resource_deleted;
152
    } catch {
153
        $c->unhandled_exception($_);
154
    };
155
}
156
129
1;
157
1;
(-)a/api/v1/swagger/paths/patrons_hold_groups.yaml (+49 lines)
Lines 154-156 Link Here
154
    x-koha-authorization:
154
    x-koha-authorization:
155
      permissions:
155
      permissions:
156
        reserveforothers: "1"
156
        reserveforothers: "1"
157
"/patrons/{patron_id}/hold_groups/{hold_group_id}":
158
  delete:
159
      x-mojo-to: Patrons::HoldGroups#delete
160
      operationId: deletePatronHoldGroups
161
      tags:
162
        - hold_groups
163
      summary: Delete hold group
164
      parameters:
165
        - $ref: "../swagger.yaml#/parameters/patron_id_pp"
166
      produces:
167
        - application/json
168
      responses:
169
        "204":
170
          description: Hold group deleted
171
        "400":
172
          description: Bad request
173
          schema:
174
            $ref: "../swagger.yaml#/definitions/error"
175
        "401":
176
          description: Authentication required
177
          schema:
178
            $ref: "../swagger.yaml#/definitions/error"
179
        "403":
180
          description: Access forbidden
181
          schema:
182
            $ref: "../swagger.yaml#/definitions/error"
183
        "404":
184
          description: Hold group not found
185
          schema:
186
            $ref: "../swagger.yaml#/definitions/error"
187
        "409":
188
          description: |
189
            Conflict. Possible `error_code` attribute values:
190
          schema:
191
            $ref: "../swagger.yaml#/definitions/error"
192
        "500":
193
          description: |
194
            Internal server error. Possible `error_code` attribute values:
195
196
            * `internal_server_error`
197
          schema:
198
            $ref: "../swagger.yaml#/definitions/error"
199
        "503":
200
          description: Under maintenance
201
          schema:
202
            $ref: "../swagger.yaml#/definitions/error"
203
      x-koha-authorization:
204
        permissions:
205
          reserveforothers: "1"
(-)a/api/v1/swagger/swagger.yaml (+2 lines)
Lines 501-506 paths: Link Here
501
    $ref: "./paths/patrons_holds.yaml#/~1patrons~1{patron_id}~1holds"
501
    $ref: "./paths/patrons_holds.yaml#/~1patrons~1{patron_id}~1holds"
502
  "/patrons/{patron_id}/hold_groups":
502
  "/patrons/{patron_id}/hold_groups":
503
    $ref: "./paths/patrons_hold_groups.yaml#/~1patrons~1{patron_id}~1hold_groups"
503
    $ref: "./paths/patrons_hold_groups.yaml#/~1patrons~1{patron_id}~1hold_groups"
504
  "/patrons/{patron_id}/hold_groups/{hold_group_id}":
505
    $ref: "./paths/patrons_hold_groups.yaml#/~1patrons~1{patron_id}~1hold_groups~1{hold_group_id}"
504
  "/patrons/{patron_id}/checkouts":
506
  "/patrons/{patron_id}/checkouts":
505
    $ref: "./paths/patrons_checkouts.yaml#/~1patrons~1{patron_id}~1checkouts"
507
    $ref: "./paths/patrons_checkouts.yaml#/~1patrons~1{patron_id}~1checkouts"
506
  "/patrons/{patron_id}/password":
508
  "/patrons/{patron_id}/password":
(-)a/t/db_dependent/api/v1/patrons_hold_groups.t (-2 / +78 lines)
Lines 18-24 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::NoWarnings;
20
use Test::NoWarnings;
21
use Test::More tests => 3;
21
use Test::More tests => 4;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Mojo;
23
use Test::Mojo;
24
24
Lines 171-173 subtest 'add() tests' => sub { Link Here
171
171
172
    $schema->storage->txn_rollback;
172
    $schema->storage->txn_rollback;
173
};
173
};
174
- 
174
175
subtest 'delete() tests' => sub {
176
    plan tests => 8;
177
178
    $schema->storage->txn_begin;
179
180
    my $authorized_patron = $builder->build_object(
181
        {
182
            class => 'Koha::Patrons',
183
            value => { flags => 1 }
184
        }
185
    );
186
    my $password = 'thePassword123';
187
    $authorized_patron->set_password( { password => $password, skip_validation => 1 } );
188
    my $auth_userid = $authorized_patron->userid;
189
190
    my $unauthorized_patron = $builder->build_object(
191
        {
192
            class => 'Koha::Patrons',
193
            value => { flags => 4 }
194
        }
195
    );
196
    $unauthorized_patron->set_password( { password => $password, skip_validation => 1 } );
197
    my $unauth_userid = $unauthorized_patron->userid;
198
199
    my $hold_1 = $builder->build_object(
200
        {
201
            class => 'Koha::Holds',
202
            value => { borrowernumber => $authorized_patron->id, hold_group_id => undef, found => undef }
203
        }
204
    );
205
    my $hold_2 = $builder->build_object(
206
        {
207
            class => 'Koha::Holds',
208
            value => { borrowernumber => $authorized_patron->id, hold_group_id => undef, found => undef }
209
        }
210
    );
211
212
    my $hold_3 = $builder->build_object(
213
        {
214
            class => 'Koha::Holds',
215
            value => { borrowernumber => $unauthorized_patron->borrowernumber, hold_group_id => undef, found => undef }
216
        }
217
    );
218
    my $hold_4 = $builder->build_object(
219
        {
220
            class => 'Koha::Holds',
221
            value => { borrowernumber => $unauthorized_patron->borrowernumber, hold_group_id => undef, found => undef }
222
        }
223
    );
224
225
    my $unauth_hold_group = $unauthorized_patron->create_hold_group( [ $hold_3->reserve_id, $hold_4->reserve_id ] );
226
    my $hold_group        = $authorized_patron->create_hold_group( [ $hold_1->reserve_id, $hold_2->reserve_id ] );
227
228
    # Unauthorized attempt to delete
229
    $t->delete_ok( "//$unauth_userid:$password@/api/v1/patrons/"
230
            . $unauthorized_patron->borrowernumber
231
            . "/hold_groups/"
232
            . $unauth_hold_group->hold_group_id )->status_is(403);
233
234
    # Attempt to delete a hold group of another patron
235
    $t->delete_ok( "//$auth_userid:$password@/api/v1/patrons/"
236
            . $authorized_patron->borrowernumber
237
            . "/hold_groups/"
238
            . $unauth_hold_group->hold_group_id )->status_is(404);
239
240
    # Successful deletion
241
    $t->delete_ok( "//$auth_userid:$password@/api/v1/patrons/"
242
            . $authorized_patron->borrowernumber
243
            . "/hold_groups/"
244
            . $hold_group->hold_group_id )->status_is( 204, 'REST3.2.4' )->content_is( '', 'REST3.3.4' );
245
246
    my $nonexistent_hold = Koha::HoldGroups->find( $hold_group->hold_group_id );
247
    is( $nonexistent_hold, undef, 'The hold group does not exist after deletion' );
248
249
    $schema->storage->txn_rollback;
250
};

Return to bug 40613