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

(-)a/Koha/Exceptions/TransferLimit.pm (+17 lines)
Line 0 Link Here
1
package Koha::Exceptions::TransferLimit;
2
3
use Modern::Perl;
4
5
use Exception::Class (
6
7
    'Koha::Exceptions::TransferLimit::Exception' => {
8
        description => 'Something went wrong!',
9
    },
10
11
    'Koha::Exceptions::TransferLimit::Duplicate' => {
12
        isa => 'Koha::Exceptions::TransferLimit::Exception',
13
        description => 'A transfer limit with the given parameters already exists!',
14
    },
15
);
16
17
1;
(-)a/Koha/Item/Transfer/Limit.pm (+17 lines)
Lines 34-39 Koha::Item::Transfer::Limit - Koha Item Transfer Limit Object class Link Here
34
34
35
=cut
35
=cut
36
36
37
=head3 to_api_mapping
38
39
This method returns the mapping for representing a Koha::Item object
40
on the API.
41
42
=cut
43
44
sub to_api_mapping {
45
    return {
46
        limitId    => 'limit_id',
47
        toBranch   => 'to_library_id',
48
        fromBranch => 'from_library_id',
49
        itemtype   => 'item_type',
50
        ccode      => 'collection_code',
51
    };
52
}
53
37
=head3 type
54
=head3 type
38
55
39
=cut
56
=cut
(-)a/Koha/REST/V1/TransferLimits.pm (+191 lines)
Line 0 Link Here
1
package Koha::REST::V1::TransferLimits;
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 Mojo::Base 'Mojolicious::Controller';
21
use Koha::Item::Transfer::Limits;
22
use Koha::Libraries;
23
24
use Koha::Exceptions::TransferLimit;
25
26
use Scalar::Util qw( blessed );
27
28
use Try::Tiny;
29
30
=head1 NAME
31
32
Koha::REST::V1::TransferLimits - Koha REST API for handling libraries (V1)
33
34
=head1 API
35
36
=head2 Methods
37
38
=cut
39
40
=head3 list
41
42
Controller function that handles listing Koha::Item::Transfer::Limits objects
43
44
=cut
45
46
sub list {
47
    my $c = shift->openapi->valid_input or return;
48
49
    return try {
50
        my $limits_set = Koha::Item::Transfer::Limits->new;
51
        my $limits = $c->objects->search( $limits_set );
52
        return $c->render( status => 200, openapi => $limits );
53
    }
54
    catch {
55
        $c->unhandled_exception( $_ );
56
    };
57
}
58
59
=head3 add
60
61
Controller function that handles adding a new transfer limit
62
63
=cut
64
65
sub add {
66
    my $c = shift->openapi->valid_input or return;
67
68
    return try {
69
        my $params = $c->validation->param( 'body' );
70
        my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $params );
71
72
        if ( Koha::Item::Transfer::Limits->search( $transfer_limit->attributes_from_api($params) )->count == 0 ) {
73
            $transfer_limit->store;
74
        } else {
75
            Koha::Exceptions::TransferLimit::Duplicate->throw()
76
        }
77
78
        return $c->render(
79
            status  => 201,
80
            openapi => $transfer_limit->to_api
81
        );
82
    }
83
    catch {
84
        if ( blessed $_ && $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
85
            return $c->render(
86
                status  => 409,
87
                openapi => { error => $_->error, conflict => $_->duplicate_id }
88
            );
89
        }
90
91
        $c->unhandled_exception($_);
92
    };
93
}
94
95
=head3 delete
96
97
Controller function that handles deleting a transfer limit
98
99
=cut
100
101
sub delete {
102
103
    my $c = shift->openapi->valid_input or return;
104
105
    my $transfer_limit = Koha::Item::Transfer::Limits->find( $c->validation->param( 'limit_id' ) );
106
107
    if ( not defined $transfer_limit ) {
108
        return $c->render( status => 404, openapi => { error => "Transfer limit not found" } );
109
    }
110
111
    return try {
112
        $transfer_limit->delete;
113
        return $c->render( status => 204, openapi => '');
114
    }
115
    catch {
116
        $c->unhandled_exception($_);
117
    };
118
}
119
120
=head3 batch_add
121
122
Controller function that handles adding a new transfer limit
123
124
=cut
125
126
sub batch_add {
127
    my $c = shift->openapi->valid_input or return;
128
129
    return try {
130
        my $params = $c->validation->param( 'body' );
131
132
        my @libraries = Koha::Libraries->search->as_list;
133
134
        my @from_branches = $params->{from_library_id} ? $params->{from_library_id} : map { $_->id } @libraries;
135
        my @to_branches = $params->{to_library_id} ? $params->{to_library_id} : map { $_->id } @libraries;
136
137
        my @results;
138
        foreach my $from ( @from_branches ) {
139
            foreach my $to ( @to_branches ) {
140
                my $limit_params = { %$params };
141
142
                $limit_params->{from_library_id} = $from;
143
                $limit_params->{to_library_id} = $to;
144
145
                next if $to eq $from;
146
147
                my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $limit_params );
148
                my $exists = Koha::Item::Transfer::Limits->search( $transfer_limit->unblessed )->count;
149
                unless ( $exists ) {
150
                    $transfer_limit->store;
151
                    push( @results, $transfer_limit->to_api());
152
                }
153
            }
154
        }
155
        my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $params );
156
157
        return $c->render(
158
            status  => 201,
159
            openapi => \@results
160
        );
161
    }
162
    catch {
163
        $c->unhandled_exception($_);
164
    };
165
}
166
167
=head3 batch_delete
168
169
Controller function that handles batch deleting transfer limits
170
171
=cut
172
173
sub batch_delete {
174
175
    my $c = shift->openapi->valid_input or return;
176
177
    return try {
178
        my $params = $c->validation->param( 'body' );
179
        my $transfer_limit = Koha::Item::Transfer::Limit->new_from_api( $params );
180
        my $search_params = $transfer_limit->unblessed;
181
182
        Koha::Item::Transfer::Limits->search($search_params)->delete;
183
184
        return $c->render( status => 204, openapi => '');
185
    }
186
    catch {
187
        $c->unhandled_exception($_);
188
    };
189
}
190
191
1;
(-)a/api/v1/swagger/definitions.json (+3 lines)
Lines 41-46 Link Here
41
  "library": {
41
  "library": {
42
    "$ref": "definitions/library.json"
42
    "$ref": "definitions/library.json"
43
  },
43
  },
44
  "transfer_limit": {
45
    "$ref": "definitions/transfer_limit.json"
46
  },
44
  "item": {
47
  "item": {
45
    "$ref": "definitions/item.json"
48
    "$ref": "definitions/item.json"
46
  },
49
  },
(-)a/api/v1/swagger/definitions/transfer_limit.json (+27 lines)
Line 0 Link Here
1
{
2
  "type": "object",
3
  "properties": {
4
    "limit_id": {
5
      "type": "integer",
6
      "description": "Internal transfer limit identifier"
7
    },
8
    "to_library_id": {
9
      "type": "string",
10
      "description": "Internal library id for which library the item is going to"
11
    },
12
    "from_library_id": {
13
      "type": "string",
14
      "description": "Internal library id for which library the item is coming from"
15
    },
16
    "item_type": {
17
      "type": ["string", "null"],
18
      "description": "Itemtype defining the type for this limi"
19
    },
20
    "collection_code": {
21
      "type": ["string", "null"],
22
      "description": "Authorized value for the collection code associated with this limit"
23
    }
24
  },
25
  "additionalProperties": false,
26
  "required": ["to_library_id", "from_library_id"]
27
}
(-)a/api/v1/swagger/parameters.json (+3 lines)
Lines 8-13 Link Here
8
  "patron_id_pp": {
8
  "patron_id_pp": {
9
    "$ref": "parameters/patron.json#/patron_id_pp"
9
    "$ref": "parameters/patron.json#/patron_id_pp"
10
  },
10
  },
11
  "transfer_limit_id_pp": {
12
    "$ref": "parameters/transfer_limit.json#/transfer_limit_id_pp"
13
  },
11
  "patron_id_qp": {
14
  "patron_id_qp": {
12
    "$ref": "parameters/patron.json#/patron_id_qp"
15
    "$ref": "parameters/patron.json#/patron_id_qp"
13
  },
16
  },
(-)a/api/v1/swagger/parameters/transfer_limit.json (+9 lines)
Line 0 Link Here
1
{
2
  "transfer_limit_id_pp": {
3
    "name": "limit_id",
4
    "in": "path",
5
    "description": "Internal transfer limit identifier",
6
    "required": true,
7
    "type": "string"
8
  }
9
}
(-)a/api/v1/swagger/paths.json (+9 lines)
Lines 68-73 Link Here
68
  "/libraries": {
68
  "/libraries": {
69
    "$ref": "paths/libraries.json#/~1libraries"
69
    "$ref": "paths/libraries.json#/~1libraries"
70
  },
70
  },
71
  "/transfer_limits": {
72
    "$ref": "paths/transfer_limits.json#/~1transfer_limits"
73
  },
74
  "/transfer_limits/{limit_id}": {
75
    "$ref": "paths/transfer_limits.json#/~1transfer_limits~1{limit_id}"
76
  },
77
  "/transfer_limits/batch": {
78
    "$ref": "paths/transfer_limits.json#/~1transfer_limits~1batch"
79
  },
71
  "/libraries/{library_id}": {
80
  "/libraries/{library_id}": {
72
    "$ref": "paths/libraries.json#/~1libraries~1{library_id}"
81
    "$ref": "paths/libraries.json#/~1libraries~1{library_id}"
73
  },
82
  },
(-)a/api/v1/swagger/paths/transfer_limits.json (+376 lines)
Line 0 Link Here
1
{
2
  "/transfer_limits": {
3
    "get": {
4
      "x-mojo-to": "TransferLimits#list",
5
      "operationId": "listTransferLimits",
6
      "tags": [
7
        "transfer"
8
      ],
9
      "parameters": [
10
        {
11
          "name": "to_library_id",
12
          "in": "query",
13
          "description": "Search on to_library_id",
14
          "required": false,
15
          "type": "string"
16
        },
17
        {
18
          "name": "from_library_id",
19
          "in": "query",
20
          "description": "Search on from_library_id",
21
          "required": false,
22
          "type": "string"
23
        },
24
        {
25
          "name": "item_type",
26
          "in": "query",
27
          "description": "Search on item_type",
28
          "required": false,
29
          "type": "string"
30
        },
31
        {
32
          "name": "collection_code",
33
          "in": "query",
34
          "description": "Search on collection_code",
35
          "required": false,
36
          "type": "string"
37
        },
38
        {
39
          "$ref": "../parameters.json#/match"
40
        },
41
        {
42
          "$ref": "../parameters.json#/order_by"
43
        },
44
        {
45
          "$ref": "../parameters.json#/page"
46
        },
47
        {
48
          "$ref": "../parameters.json#/per_page"
49
        },
50
        {
51
          "$ref": "../parameters.json#/q_param"
52
        },
53
        {
54
          "$ref": "../parameters.json#/q_body"
55
        },
56
        {
57
          "$ref": "../parameters.json#/q_header"
58
        }
59
      ],
60
      "produces": [
61
        "application/json"
62
      ],
63
      "responses": {
64
        "200": {
65
          "description": "A list of transfer limits",
66
          "schema": {
67
            "type": "array",
68
            "items": {
69
              "$ref": "../definitions.json#/transfer_limit"
70
            }
71
          }
72
        },
73
        "500": {
74
          "description": "Internal error",
75
          "schema": {
76
            "$ref": "../definitions.json#/error"
77
          }
78
        },
79
        "503": {
80
          "description": "Under maintenance",
81
          "schema": {
82
            "$ref": "../definitions.json#/error"
83
          }
84
        }
85
      },
86
      "x-koha-authorization": {
87
        "permissions": {
88
          "parameters": "manage_transfers"
89
        }
90
      }
91
    },
92
    "post": {
93
      "x-mojo-to": "TransferLimits#add",
94
      "operationId": "addTransferLimit",
95
      "tags": [
96
        "transfer"
97
      ],
98
      "parameters": [
99
        {
100
          "name": "body",
101
          "in": "body",
102
          "description": "A JSON object containing information about a new transfer limit",
103
          "required": true,
104
          "schema": {
105
            "$ref": "../definitions.json#/transfer_limit"
106
          }
107
        }
108
      ],
109
      "produces": [
110
        "application/json"
111
      ],
112
      "responses": {
113
        "201": {
114
          "description": "Transfer limit added",
115
          "schema": {
116
            "$ref": "../definitions.json#/transfer_limit"
117
          }
118
        },
119
        "400": {
120
          "description": "Bad request",
121
          "schema": {
122
            "$ref": "../definitions.json#/error"
123
          }
124
        },
125
        "401": {
126
          "description": "Authentication required",
127
          "schema": {
128
            "$ref": "../definitions.json#/error"
129
          }
130
        },
131
        "403": {
132
          "description": "Access forbidden",
133
          "schema": {
134
            "$ref": "../definitions.json#/error"
135
          }
136
        },
137
        "409": {
138
          "description": "Conflict in creating resource",
139
          "schema": {
140
            "$ref": "../definitions.json#/error"
141
          }
142
        },
143
        "500": {
144
          "description": "Internal error",
145
          "schema": {
146
            "$ref": "../definitions.json#/error"
147
          }
148
        },
149
        "503": {
150
          "description": "Under maintenance",
151
          "schema": {
152
            "$ref": "../definitions.json#/error"
153
          }
154
        }
155
      },
156
      "x-koha-authorization": {
157
        "permissions": {
158
          "parameters": "manage_transfers"
159
        }
160
      }
161
    }
162
  },
163
  "/transfer_limits/{limit_id}": {
164
    "delete": {
165
      "x-mojo-to": "TransferLimits#delete",
166
      "operationId": "deleteTransferLimit",
167
      "tags": [
168
        "transfer"
169
      ],
170
      "parameters": [
171
        {
172
          "$ref": "../parameters.json#/transfer_limit_id_pp"
173
        }
174
      ],
175
      "produces": [
176
        "application/json"
177
      ],
178
      "responses": {
179
        "204": {
180
          "description": "Transfer limit deleted",
181
          "schema": {
182
            "type": "string"
183
          }
184
        },
185
        "401": {
186
          "description": "Authentication required",
187
          "schema": {
188
            "$ref": "../definitions.json#/error"
189
          }
190
        },
191
        "403": {
192
          "description": "Access forbidden",
193
          "schema": {
194
            "$ref": "../definitions.json#/error"
195
          }
196
        },
197
        "404": {
198
          "description": "Library not found",
199
          "schema": {
200
            "$ref": "../definitions.json#/error"
201
          }
202
        },
203
        "500": {
204
          "description": "Internal error",
205
          "schema": {
206
            "$ref": "../definitions.json#/error"
207
          }
208
        },
209
        "503": {
210
          "description": "Under maintenance",
211
          "schema": {
212
            "$ref": "../definitions.json#/error"
213
          }
214
        }
215
      },
216
      "x-koha-authorization": {
217
        "permissions": {
218
          "parameters": "manage_transfers"
219
        }
220
      }
221
    }
222
  },
223
  "/transfer_limits/batch": {
224
    "post": {
225
      "x-mojo-to": "TransferLimits#batch_add",
226
      "operationId": "batchAddTransferLimits",
227
      "tags": [
228
        "transfer"
229
      ],
230
      "parameters": [
231
        {
232
          "name": "body",
233
          "in": "body",
234
          "description": "A JSON object containing information about new transfer limits.",
235
          "required": true,
236
          "schema": {
237
          "type": "object",
238
          "properties": {
239
              "to_library_id": {
240
                "type": "string",
241
                "description": "Internal library id for which library the item is going to"
242
              },
243
              "from_library_id": {
244
                "type": "string",
245
                "description": "Internal library id for which library the item is coming from"
246
              },
247
              "item_type": {
248
                "type": ["string", "null"],
249
                "description": "Itemtype defining the type for this limi"
250
              },
251
              "collection_code": {
252
                "type": ["string", "null"],
253
                "description": "Authorized value for the collection code associated with this limit"
254
              }
255
            },
256
            "additionalProperties": false
257
          }
258
        }
259
      ],
260
      "produces": [
261
        "application/json"
262
      ],
263
      "responses": {
264
        "201": {
265
          "description": "A list of transfer limits",
266
          "schema": {
267
            "type": "array",
268
            "items": {
269
              "$ref": "../definitions.json#/transfer_limit"
270
            }
271
          }
272
        },
273
        "500": {
274
          "description": "Internal error",
275
          "schema": {
276
            "$ref": "../definitions.json#/error"
277
          }
278
        },
279
        "503": {
280
          "description": "Under maintenance",
281
          "schema": {
282
            "$ref": "../definitions.json#/error"
283
          }
284
        }
285
      },
286
      "x-koha-authorization": {
287
        "permissions": {
288
          "parameters": "manage_transfers"
289
        }
290
      }
291
    },
292
    "delete": {
293
      "x-mojo-to": "TransferLimits#batch_delete",
294
      "operationId": "batchDeleteTransferLimits",
295
      "tags": [
296
        "transfer"
297
      ],
298
      "parameters": [
299
        {
300
          "name": "body",
301
          "in": "body",
302
          "description": "A JSON object containing information about new transfer limits.",
303
          "required": true,
304
          "schema": {
305
            "type": "object",
306
            "properties": {
307
              "to_library_id": {
308
                "type": "string",
309
                "description": "Internal library id for which library the item is going to"
310
              },
311
              "from_library_id": {
312
                "type": "string",
313
                "description": "Internal library id for which library the item is coming from"
314
              },
315
              "item_type": {
316
                "type": ["string", "null"],
317
                "description": "Itemtype defining the type for this limi"
318
              },
319
              "collection_code": {
320
                "type": ["string", "null"],
321
                "description": "Authorized value for the collection code associated with this limit"
322
              }
323
            },
324
            "additionalProperties": false
325
          }
326
        }
327
      ],
328
      "produces": [
329
        "application/json"
330
      ],
331
      "responses": {
332
        "204": {
333
          "description": "Transfer limits deleted",
334
          "schema": {
335
            "type": "string"
336
          }
337
        },
338
        "401": {
339
          "description": "Authentication required",
340
          "schema": {
341
            "$ref": "../definitions.json#/error"
342
          }
343
        },
344
        "403": {
345
          "description": "Access forbidden",
346
          "schema": {
347
            "$ref": "../definitions.json#/error"
348
          }
349
        },
350
        "404": {
351
          "description": "Library not found",
352
          "schema": {
353
            "$ref": "../definitions.json#/error"
354
          }
355
        },
356
        "500": {
357
          "description": "Internal error",
358
          "schema": {
359
            "$ref": "../definitions.json#/error"
360
          }
361
        },
362
        "503": {
363
          "description": "Under maintenance",
364
          "schema": {
365
            "$ref": "../definitions.json#/error"
366
          }
367
        }
368
      },
369
      "x-koha-authorization": {
370
        "permissions": {
371
          "parameters": "manage_transfers"
372
        }
373
      }
374
    }
375
  }
376
}
(-)a/api/v1/swagger/x-primitives.json (+4 lines)
Lines 18-23 Link Here
18
    "maxLength": 10,
18
    "maxLength": 10,
19
    "minLength": 1
19
    "minLength": 1
20
  },
20
  },
21
  "limit_id": {
22
    "type": "integer",
23
    "description": "Internal transfer limit identifier"
24
  },
21
  "cardnumber": {
25
  "cardnumber": {
22
    "type": ["string", "null"],
26
    "type": ["string", "null"],
23
    "description": "library assigned user identifier"
27
    "description": "library assigned user identifier"
(-)a/t/db_dependent/api/v1/transfer_limits.t (-1 / +261 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 => 4;
21
use Test::Mojo;
22
use Test::Warn;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use List::Util qw(min);
28
29
use Koha::Item::Transfer::Limits;
30
use Koha::Database;
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37
my $t = Test::Mojo->new('Koha::REST::V1');
38
39
subtest 'list() tests' => sub {
40
    plan tests => 3;
41
42
    $schema->storage->txn_begin;
43
44
    Koha::Item::Transfer::Limits->delete;
45
46
    my $patron = $builder->build_object({
47
        class => 'Koha::Patrons',
48
        value => { flags => 1 }
49
    });
50
    my $password = 'thePassword123';
51
    $patron->set_password({ password => $password, skip_validation => 1 });
52
    my $userid = $patron->userid;
53
54
    my $limit = $builder->build_object({ class => 'Koha::Item::Transfer::Limits' });
55
56
    $t->get_ok( "//$userid:$password@/api/v1/transfer_limits" )
57
      ->status_is( 200, 'SWAGGER3.2.2' )
58
      ->json_is( [$limit->to_api] );
59
60
    $schema->storage->txn_rollback;
61
};
62
63
subtest 'add() tests' => sub {
64
65
    plan tests => 8;
66
67
    $schema->storage->txn_begin;
68
69
    my $authorized_patron = $builder->build_object({
70
        class => 'Koha::Patrons',
71
        value => { flags => 1 }
72
    });
73
    my $password = 'thePassword123';
74
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
75
    my $auth_userid = $authorized_patron->userid;
76
77
    my $unauthorized_patron = $builder->build_object({
78
        class => 'Koha::Patrons',
79
        value => { flags => 4 }
80
    });
81
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
82
    my $unauth_userid = $unauthorized_patron->userid;
83
84
    my $limit = $builder->build_object({ class => 'Koha::Item::Transfer::Limits' });
85
    my $limit_hashref = $limit->to_api;
86
    delete $limit_hashref->{limit_id};
87
    $limit->delete;
88
89
    # Unauthorized attempt to write
90
    $t->post_ok( "//$unauth_userid:$password@/api/v1/transfer_limits" => json => $limit_hashref )
91
      ->status_is(403);
92
93
    # Authorized attempt to write invalid data
94
    my $limit_with_invalid_field = {'invalid' => 'invalid'};
95
96
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits" => json => $limit_with_invalid_field )
97
      ->status_is(400)
98
      ->json_is(
99
        "/errors" => [
100
            {
101
                message => "Properties not allowed: invalid.",
102
                path    => "/body"
103
            }
104
        ]
105
    );
106
107
    # Authorized attempt to write
108
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits" => json => $limit_hashref )
109
      ->status_is( 201, 'SWAGGER3.2.1' )
110
      ->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
111
112
    $schema->storage->txn_rollback;
113
};
114
115
subtest 'delete() tests' => sub {
116
    plan tests => 7;
117
118
    $schema->storage->txn_begin;
119
120
    my $authorized_patron = $builder->build_object({
121
        class => 'Koha::Patrons',
122
        value => { flags => 1 }
123
    });
124
    my $password = 'thePassword123';
125
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
126
    my $auth_userid = $authorized_patron->userid;
127
128
    my $unauthorized_patron = $builder->build_object({
129
        class => 'Koha::Patrons',
130
        value => { flags => 4 }
131
    });
132
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
133
    my $unauth_userid = $unauthorized_patron->userid;
134
135
    my $limit = $builder->build_object({ class => 'Koha::Item::Transfer::Limits' });
136
    my $limit_id = $limit->id;
137
138
    # Unauthorized attempt to delete
139
    $t->delete_ok( "//$unauth_userid:$password@/api/v1/transfer_limits/$limit_id" )
140
      ->status_is(403);
141
142
    $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/$limit_id" )
143
      ->status_is(204, 'SWAGGER3.2.4')
144
      ->content_is('', 'SWAGGER3.3.4');
145
146
    $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/$limit_id" )
147
      ->status_is(404);
148
149
    $schema->storage->txn_rollback;
150
};
151
152
subtest 'batch_add() and batch_delete() tests' => sub {
153
    plan tests => 26;
154
155
    $schema->storage->txn_begin;
156
157
    Koha::Item::Transfer::Limits->delete;
158
159
    #my $library = $builder->build_object({ class => 'Koha::Libraries' });
160
161
    my $library = Koha::Libraries->search->next;
162
    my $itemtype = Koha::ItemTypes->search->next;
163
164
    my $authorized_patron = $builder->build_object({
165
        class => 'Koha::Patrons',
166
        value => { flags => 1 }
167
    });
168
    my $password = 'thePassword123';
169
    $authorized_patron->set_password({ password => $password, skip_validation => 1 });
170
    my $auth_userid = $authorized_patron->userid;
171
172
    my $unauthorized_patron = $builder->build_object({
173
        class => 'Koha::Patrons',
174
        value => { flags => 4 }
175
    });
176
    $unauthorized_patron->set_password({ password => $password, skip_validation => 1 });
177
    my $unauth_userid = $unauthorized_patron->userid;
178
179
    my $limit_hashref = {
180
        item_type => $itemtype->id
181
    };
182
183
    # Unauthorized attempt to write
184
    $t->post_ok( "//$unauth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
185
        ->status_is(403);
186
187
    # Authorized attempt to write invalid data
188
    my $limit_with_invalid_field = {'invalid' => 'invalid'};
189
190
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_with_invalid_field )
191
        ->status_is(400)
192
        ->json_is(
193
        "/errors" => [
194
            {
195
                message => "Properties not allowed: invalid.",
196
                path    => "/body"
197
            }
198
        ]
199
    );
200
201
    # Create all combinations of to/from libraries
202
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
203
        ->status_is( 201, 'SWAGGER3.2.1' )
204
        ->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
205
206
    my $limits = Koha::Item::Transfer::Limits->search;
207
208
    my $libraries_count = Koha::Libraries->search->count;
209
    is( $limits->count, $libraries_count * ($libraries_count - 1 ), "Created the correct number of limits" );
210
211
    # Delete all combinations of to/from libraries
212
    $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
213
        ->status_is( 204, 'SWAGGER3.2.1' );
214
215
    $limits = Koha::Item::Transfer::Limits->search;
216
217
    is( $limits->count, 0, "Deleted the correct number of limits" );
218
219
    # Create all combinations of 'to' libraries
220
    $limit_hashref->{to_library_id} = $library->id;
221
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
222
        ->status_is( 201, 'SWAGGER3.2.1' )
223
        ->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
224
225
    $limits = Koha::Item::Transfer::Limits->search;
226
227
    is( $limits->count, $libraries_count - 1 , "Created the correct number of limits" );
228
229
    # Delete all combinations of 'to' libraries
230
    $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
231
        ->status_is( 204, 'SWAGGER3.2.1' );
232
233
    $limits = Koha::Item::Transfer::Limits->search;
234
235
    is( $limits->count, 0, "Deleted the correct number of limits" );
236
237
    # Create all combinations of 'from' libraries
238
    Koha::Item::Transfer::Limits->search->delete;
239
240
    delete $limit_hashref->{to_library_id};
241
    $limit_hashref->{from_library_id} = $library->id;
242
    $t->post_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
243
        ->status_is( 201, 'SWAGGER3.2.1' )
244
        ->json_has( '' => $limit_hashref, 'SWAGGER3.3.1' );
245
246
    $limits = Koha::Item::Transfer::Limits->search;
247
248
    $libraries_count = Koha::Libraries->search->count;
249
    is( $limits->count, $libraries_count - 1 , "Created the correct number of limits" );
250
251
    # Delete all combinations of 'from' libraries
252
    $t->delete_ok( "//$auth_userid:$password@/api/v1/transfer_limits/batch" => json => $limit_hashref )
253
        ->status_is( 204, 'SWAGGER3.2.1' );
254
255
    $limits = Koha::Item::Transfer::Limits->search;
256
257
    $libraries_count = Koha::Libraries->search->count;
258
    is( $limits->count, 0, "Deleted the correct number of limits" );
259
260
    $schema->storage->txn_rollback;
261
};

Return to bug 26633