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 (-1 / +50 lines)
Lines 152-155 Link Here
152
          $ref: "../swagger.yaml#/definitions/error"
152
          $ref: "../swagger.yaml#/definitions/error"
153
    x-koha-authorization:
153
    x-koha-authorization:
154
      permissions:
154
      permissions:
155
        reserveforothers: "1"
155
        reserveforothers: "1"
156
"/patrons/{patron_id}/hold_groups/{hold_group_id}":
157
  delete:
158
      x-mojo-to: Patrons::HoldGroups#delete
159
      operationId: deletePatronHoldGroups
160
      tags:
161
        - hold_groups
162
      summary: Delete hold group
163
      parameters:
164
        - $ref: "../swagger.yaml#/parameters/patron_id_pp"
165
      produces:
166
        - application/json
167
      responses:
168
        "204":
169
          description: Hold group deleted
170
        "400":
171
          description: Bad request
172
          schema:
173
            $ref: "../swagger.yaml#/definitions/error"
174
        "401":
175
          description: Authentication required
176
          schema:
177
            $ref: "../swagger.yaml#/definitions/error"
178
        "403":
179
          description: Access forbidden
180
          schema:
181
            $ref: "../swagger.yaml#/definitions/error"
182
        "404":
183
          description: Hold group not found
184
          schema:
185
            $ref: "../swagger.yaml#/definitions/error"
186
        "409":
187
          description: |
188
            Conflict. Possible `error_code` attribute values:
189
          schema:
190
            $ref: "../swagger.yaml#/definitions/error"
191
        "500":
192
          description: |
193
            Internal server error. Possible `error_code` attribute values:
194
195
            * `internal_server_error`
196
          schema:
197
            $ref: "../swagger.yaml#/definitions/error"
198
        "503":
199
          description: Under maintenance
200
          schema:
201
            $ref: "../swagger.yaml#/definitions/error"
202
      x-koha-authorization:
203
        permissions:
204
          reserveforothers: "1"
(-)a/api/v1/swagger/swagger.yaml (+2 lines)
Lines 485-490 paths: Link Here
485
    $ref: "./paths/patrons_holds.yaml#/~1patrons~1{patron_id}~1holds"
485
    $ref: "./paths/patrons_holds.yaml#/~1patrons~1{patron_id}~1holds"
486
  "/patrons/{patron_id}/hold_groups":
486
  "/patrons/{patron_id}/hold_groups":
487
    $ref: "./paths/patrons_hold_groups.yaml#/~1patrons~1{patron_id}~1hold_groups"
487
    $ref: "./paths/patrons_hold_groups.yaml#/~1patrons~1{patron_id}~1hold_groups"
488
  "/patrons/{patron_id}/hold_groups/{hold_group_id}":
489
    $ref: "./paths/patrons_hold_groups.yaml#/~1patrons~1{patron_id}~1hold_groups~1{hold_group_id}"
488
  "/patrons/{patron_id}/checkouts":
490
  "/patrons/{patron_id}/checkouts":
489
    $ref: "./paths/patrons_checkouts.yaml#/~1patrons~1{patron_id}~1checkouts"
491
    $ref: "./paths/patrons_checkouts.yaml#/~1patrons~1{patron_id}~1checkouts"
490
  "/patrons/{patron_id}/password":
492
  "/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