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

(-)a/Koha/AuthorisedValue.pm (+18 lines)
Lines 47-52 sub opac_description { Link Here
47
    return $self->lib_opac() || $self->lib();
47
    return $self->lib_opac() || $self->lib();
48
}
48
}
49
49
50
=head3 to_api_mapping
51
52
This method returns the mapping for representing a Koha::AuthorisedValue object
53
on the API.
54
55
=cut
56
57
sub to_api_mapping {
58
    return {
59
        id               => 'authorised_value_id',
60
        category         => 'category',
61
        authorised_value => 'value',
62
        lib              => 'description',
63
        lib_opac         => 'opac_description',
64
        imageurl         => 'image_url',
65
    };
66
}
67
50
=head2 Internal methods
68
=head2 Internal methods
51
69
52
=head3 _type
70
=head3 _type
(-)a/Koha/REST/V1/AuthorisedValues.pm (+139 lines)
Line 0 Link Here
1
package Koha::REST::V1::AuthorisedValues;
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
22
use Koha::AuthorisedValues;
23
24
use Try::Tiny;
25
26
=head1 API
27
28
=head2 Methods
29
30
=head3 list
31
32
=cut
33
34
sub list {
35
    my $c = shift->openapi->valid_input or return;
36
37
    return try {
38
        my $av_set = Koha::AuthorisedValues->new;
39
        my $avs = $c->objects->search( $av_set );
40
        return $c->render( status => 200, openapi => $avs );
41
    }
42
    catch {
43
        $c->unhandled_exception($_);
44
    };
45
46
}
47
48
=head3 get
49
50
=cut
51
52
sub get {
53
    my $c = shift->openapi->valid_input or return;
54
55
    return try {
56
        my $av = Koha::AuthorisedValues->find( $c->validation->param('authorised_value_id') );
57
        unless ($av) {
58
            return $c->render( status  => 404,
59
                            openapi => { error => "Authorised value not found" } );
60
        }
61
62
        return $c->render( status => 200, openapi => $av->to_api );
63
    }
64
    catch {
65
        $c->unhandled_exception($_);
66
    }
67
}
68
69
=head3 add
70
71
=cut
72
73
sub add {
74
    my $c = shift->openapi->valid_input or return;
75
76
    return try {
77
        my $av = Koha::AuthorisedValue->new_from_api( $c->validation->param('body') );
78
        $av->store;
79
        $c->res->headers->location( $c->req->url->to_string . '/' . $av->id );
80
        return $c->render(
81
            status  => 201,
82
            openapi => $av->to_api
83
        );
84
    }
85
    catch {
86
        $c->unhandled_exception($_);
87
    };
88
}
89
90
=head3 update
91
92
=cut
93
94
sub update {
95
    my $c = shift->openapi->valid_input or return;
96
97
    my $av = Koha::AuthorisedValues->find( $c->validation->param('authorised_value_id') );
98
99
    if ( not defined $av ) {
100
        return $c->render( status  => 404,
101
                           openapi => { error => "Object not found" } );
102
    }
103
104
    return try {
105
        $av->set_from_api( $c->validation->param('body') );
106
        $av->store();
107
        return $c->render( status => 200, openapi => $av->to_api );
108
    }
109
    catch {
110
        $c->unhandled_exception($_);
111
    };
112
}
113
114
=head3 delete
115
116
=cut
117
118
sub delete {
119
    my $c = shift->openapi->valid_input or return;
120
121
    my $av = Koha::AuthorisedValues->find( $c->validation->param('authorised_value_id') );
122
    if ( not defined $av ) {
123
        return $c->render( status  => 404,
124
                           openapi => { error => "Object not found" } );
125
    }
126
127
    return try {
128
        $av->delete;
129
        return $c->render(
130
            status  => 204,
131
            openapi => q{}
132
        );
133
    }
134
    catch {
135
        $c->unhandled_exception($_);
136
    };
137
}
138
139
1;
(-)a/api/v1/swagger/definitions.json (+3 lines)
Lines 2-7 Link Here
2
  "account_line": {
2
  "account_line": {
3
    "$ref": "definitions/account_line.json"
3
    "$ref": "definitions/account_line.json"
4
  },
4
  },
5
  "authorised_value": {
6
    "$ref": "definitions/authorised_value.json"
7
  },
5
  "basket": {
8
  "basket": {
6
    "$ref": "definitions/basket.json"
9
    "$ref": "definitions/basket.json"
7
  },
10
  },
(-)a/api/v1/swagger/definitions/authorised_value.json (+30 lines)
Line 0 Link Here
1
{
2
  "type": "object",
3
  "properties": {
4
    "authorised_value_id": {
5
        "$ref": "../x-primitives.json#/authorised_value_id"
6
    },
7
    "category": {
8
      "description": "The category of this authorised value",
9
      "type": "string"
10
    },
11
    "value": {
12
      "description": "The code for this authorised value",
13
      "type": "string"
14
    },
15
    "description": {
16
      "description": "The staff interface description for this authorised value",
17
      "type": "string"
18
    },
19
    "opac_description": {
20
      "description": "The public interface description of this authorised value, if set",
21
      "type": ["string", "null"]
22
    },
23
    "image_url": {
24
      "description": "The url of the image associated with this authorised value, if any",
25
      "type": ["string", "null"]
26
    }
27
  },
28
  "additionalProperties": false,
29
  "required": ["category", "value", "description"]
30
}
(-)a/api/v1/swagger/parameters.json (+3 lines)
Lines 5-10 Link Here
5
  "advancededitormacro_id_pp": {
5
  "advancededitormacro_id_pp": {
6
    "$ref": "parameters/advancededitormacro.json#/advancededitormacro_id_pp"
6
    "$ref": "parameters/advancededitormacro.json#/advancededitormacro_id_pp"
7
  },
7
  },
8
  "authorised_value_id_pp": {
9
    "$ref": "parameters/authorised_value.json#/authorised_value_id_pp"
10
  },
8
  "patron_id_pp": {
11
  "patron_id_pp": {
9
    "$ref": "parameters/patron.json#/patron_id_pp"
12
    "$ref": "parameters/patron.json#/patron_id_pp"
10
  },
13
  },
(-)a/api/v1/swagger/parameters/authorised_value.json (+9 lines)
Line 0 Link Here
1
{
2
    "authorised_value_id_pp": {
3
      "name": "authorised_value_id",
4
      "in": "path",
5
      "description": "Authorised value internal identifier",
6
      "required": true,
7
      "type": "integer"
8
    }
9
}
(-)a/api/v1/swagger/paths.json (+6 lines)
Lines 17-22 Link Here
17
  "/acquisitions/funds": {
17
  "/acquisitions/funds": {
18
    "$ref": "paths/acquisitions_funds.json#/~1acquisitions~1funds"
18
    "$ref": "paths/acquisitions_funds.json#/~1acquisitions~1funds"
19
  },
19
  },
20
  "/authorised_values": {
21
    "$ref": "paths/authorised_values.json#/~1authorised_values"
22
  },
23
  "/authorised_values/{authorised_value_id}": {
24
    "$ref": "paths/authorised_values.json#/~1authorised_values~1{authorised_value_id}"
25
  },
20
  "/checkouts": {
26
  "/checkouts": {
21
    "$ref": "paths/checkouts.json#/~1checkouts"
27
    "$ref": "paths/checkouts.json#/~1checkouts"
22
  },
28
  },
(-)a/api/v1/swagger/paths/authorised_values.json (+334 lines)
Line 0 Link Here
1
{
2
  "/authorised_values": {
3
    "get": {
4
      "x-mojo-to": "AuthorisedValues#list",
5
      "operationId": "listAuthorisedValues",
6
      "tags": [
7
        "authorised", "values"
8
      ],
9
      "produces": [
10
        "application/json"
11
      ],
12
      "parameters": [
13
        {
14
          "name": "category",
15
          "in": "query",
16
          "description": "Search authorised values by category",
17
          "required": false,
18
          "type": "string"
19
        },
20
        {
21
          "name": "value",
22
          "in": "query",
23
          "description": "Search authorised values by value",
24
          "required": false,
25
          "type": "string"
26
        },
27
        {
28
          "name": "description",
29
          "in": "query",
30
          "description": "Search authorised values by description",
31
          "required": false,
32
          "type": "string"
33
        },
34
        {
35
          "name": "opac_description",
36
          "in": "query",
37
          "description": "Search authorised values by opac description",
38
          "required": false,
39
          "type": "string"
40
        },
41
        {
42
          "name": "image_url",
43
          "in": "query",
44
          "description": "Search authorised values by image url",
45
          "required": false,
46
          "type": "string"
47
        },
48
        {
49
          "$ref": "../parameters.json#/match"
50
        },
51
        {
52
          "$ref": "../parameters.json#/order_by"
53
        },
54
        {
55
          "$ref": "../parameters.json#/page"
56
        },
57
        {
58
          "$ref": "../parameters.json#/per_page"
59
        },
60
        {
61
          "$ref": "../parameters.json#/q_param"
62
        },
63
        {
64
          "$ref": "../parameters.json#/q_body"
65
        },
66
        {
67
          "$ref": "../parameters.json#/q_header"
68
        }
69
      ],
70
      "responses": {
71
        "200": {
72
          "description": "A list of authorised values",
73
          "schema": {
74
            "type": "array",
75
            "items": {
76
              "$ref": "../definitions.json#/authorised_value"
77
            }
78
          }
79
        },
80
        "403": {
81
          "description": "Access forbidden",
82
          "schema": {
83
            "$ref": "../definitions.json#/error"
84
          }
85
        },
86
        "500": {
87
          "description": "Internal error",
88
          "schema": {
89
            "$ref": "../definitions.json#/error"
90
          }
91
        },
92
        "503": {
93
          "description": "Under maintenance",
94
          "schema": {
95
            "$ref": "../definitions.json#/error"
96
          }
97
        }
98
      },
99
      "x-koha-authorization": {
100
        "permissions": {
101
          "catalogue": "1"
102
        }
103
      }
104
    },
105
    "post": {
106
      "x-mojo-to": "AuthorisedValues#add",
107
      "operationId": "addAuthorisedValue",
108
      "tags": [
109
        "authorised", "values"
110
      ],
111
      "parameters": [
112
        {
113
          "name": "body",
114
          "in": "body",
115
          "description": "A JSON object containing informations about the new authorised value",
116
          "required": true,
117
          "schema": {
118
            "$ref": "../definitions.json#/authorised_value"
119
          }
120
        }
121
      ],
122
      "produces": [
123
        "application/json"
124
      ],
125
      "responses": {
126
        "201": {
127
          "description": "Authorised value added",
128
          "schema": {
129
            "$ref": "../definitions.json#/authorised_value"
130
          }
131
        },
132
        "401": {
133
          "description": "Authentication required",
134
          "schema": {
135
            "$ref": "../definitions.json#/error"
136
          }
137
        },
138
        "403": {
139
          "description": "Access forbidden",
140
          "schema": {
141
            "$ref": "../definitions.json#/error"
142
          }
143
        },
144
        "500": {
145
          "description": "Internal error",
146
          "schema": {
147
            "$ref": "../definitions.json#/error"
148
          }
149
        },
150
        "503": {
151
          "description": "Under maintenance",
152
          "schema": {
153
            "$ref": "../definitions.json#/error"
154
          }
155
        }
156
      },
157
      "x-koha-authorization": {
158
        "permissions": {
159
          "parameters": "manage_auth_values"
160
        }
161
      }
162
    }
163
  },
164
  "/authorised_values/{authorised_value_id}": {
165
    "get": {
166
      "x-mojo-to": "AuthorisedValues#get",
167
      "operationId": "getAuthorisedValue",
168
      "tags": [
169
        "authorised", "values"
170
      ],
171
      "parameters": [
172
        {
173
          "$ref": "../parameters.json#/authorised_value_id_pp"
174
        }
175
      ],
176
      "produces": [
177
        "application/json"
178
      ],
179
      "responses": {
180
        "200": {
181
          "description": "An authorised value",
182
          "schema": {
183
            "$ref": "../definitions.json#/authorised_value"
184
          }
185
        },
186
        "404": {
187
          "description": "Authorised value not found",
188
          "schema": {
189
            "$ref": "../definitions.json#/error"
190
          }
191
        },
192
        "500": {
193
          "description": "Internal error",
194
          "schema": {
195
            "$ref": "../definitions.json#/error"
196
          }
197
        },
198
        "503": {
199
          "description": "Under maintenance",
200
          "schema": {
201
            "$ref": "../definitions.json#/error"
202
          }
203
        }
204
      },
205
      "x-koha-authorization": {
206
        "permissions": {
207
          "catalogue": "1"
208
        }
209
      }
210
    },
211
    "put": {
212
      "x-mojo-to": "AuthorisedValues#update",
213
      "operationId": "updateAuthorisedValue",
214
      "tags": [
215
        "authorised", "value"
216
      ],
217
      "parameters": [
218
        {
219
          "$ref": "../parameters.json#/authorised_value_id_pp"
220
        },
221
        {
222
          "name": "body",
223
          "in": "body",
224
          "description": "An authorised value object",
225
          "required": true,
226
          "schema": {
227
            "$ref": "../definitions.json#/authorised_value"
228
          }
229
        }
230
      ],
231
      "produces": [
232
        "application/json"
233
      ],
234
      "responses": {
235
        "200": {
236
          "description": "An authorised value",
237
          "schema": {
238
            "$ref": "../definitions.json#/authorised_value"
239
          }
240
        },
241
        "401": {
242
          "description": "Authentication required",
243
          "schema": {
244
            "$ref": "../definitions.json#/error"
245
          }
246
        },
247
        "403": {
248
          "description": "Access forbidden",
249
          "schema": {
250
            "$ref": "../definitions.json#/error"
251
          }
252
        },
253
        "404": {
254
          "description": "Authorised value not found",
255
          "schema": {
256
            "$ref": "../definitions.json#/error"
257
          }
258
        },
259
        "500": {
260
          "description": "Internal error",
261
          "schema": {
262
            "$ref": "../definitions.json#/error"
263
          }
264
        },
265
        "503": {
266
          "description": "Under maintenance",
267
          "schema": {
268
            "$ref": "../definitions.json#/error"
269
          }
270
        }
271
      },
272
      "x-koha-authorization": {
273
        "permissions": {
274
          "parameters": "manage_auth_values"
275
        }
276
      }
277
    },
278
    "delete": {
279
      "x-mojo-to": "AuthorisedValues#delete",
280
      "operationId": "deleteAuthorisedValue",
281
      "tags": [
282
        "authorised", "values"
283
      ],
284
      "parameters": [
285
        {
286
          "$ref": "../parameters.json#/authorised_value_id_pp"
287
        }
288
      ],
289
      "produces": [
290
        "application/json"
291
      ],
292
      "responses": {
293
        "204": {
294
          "description": "Authorised value deleted"
295
        },
296
        "401": {
297
          "description": "Authentication required",
298
          "schema": {
299
            "$ref": "../definitions.json#/error"
300
          }
301
        },
302
        "403": {
303
          "description": "Access forbidden",
304
          "schema": {
305
            "$ref": "../definitions.json#/error"
306
          }
307
        },
308
        "404": {
309
          "description": "Authorised value not found",
310
          "schema": {
311
            "$ref": "../definitions.json#/error"
312
          }
313
        },
314
        "500": {
315
          "description": "Internal error",
316
          "schema": {
317
            "$ref": "../definitions.json#/error"
318
          }
319
        },
320
        "503": {
321
          "description": "Under maintenance",
322
          "schema": {
323
            "$ref": "../definitions.json#/error"
324
          }
325
        }
326
      },
327
      "x-koha-authorization": {
328
        "permissions": {
329
          "parameters": "manage_auth_values"
330
        }
331
      }
332
    }
333
  }
334
}
(-)a/api/v1/swagger/x-primitives.json (+5 lines)
Lines 8-13 Link Here
8
    "description": "Internal advanced editor macro identifier",
8
    "description": "Internal advanced editor macro identifier",
9
    "readOnly": true
9
    "readOnly": true
10
  },
10
  },
11
  "authorised_value_id": {
12
    "type": "integer",
13
    "description": "internally assigned authorised value identifier",
14
    "readOnly": true
15
  },
11
  "patron_id": {
16
  "patron_id": {
12
    "type": "integer",
17
    "type": "integer",
13
    "description": "Internal patron identifier"
18
    "description": "Internal patron identifier"
(-)a/t/db_dependent/api/v1/authorised_values.t (-1 / +383 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 => 5;
21
use Test::Mojo;
22
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
25
26
use Koha::AuthorisedValues;
27
use Koha::Database;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
my $t = Test::Mojo->new('Koha::REST::V1');
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
35
subtest 'list() tests' => sub {
36
37
    plan tests => 20;
38
39
    $schema->storage->txn_begin;
40
41
    Koha::AuthorisedValues->search->delete;
42
43
    my $librarian = $builder->build_object(
44
        {
45
            class => 'Koha::Patrons',
46
            value => { flags => 2 ** 2 } # catalogue flag = 2
47
        }
48
    );
49
    my $password = 'thePassword123';
50
    $librarian->set_password( { password => $password, skip_validation => 1 } );
51
    my $userid = $librarian->userid;
52
53
    my $patron = $builder->build_object(
54
        {
55
            class => 'Koha::Patrons',
56
            value => { flags => 0 }
57
        }
58
    );
59
60
    $patron->set_password( { password => $password, skip_validation => 1 } );
61
    my $unauth_userid = $patron->userid;
62
63
    ## Authorized user tests
64
    # No AVs, so empty array should be returned
65
    $t->get_ok("//$userid:$password@/api/v1/authorised_values")
66
      ->status_is(200)
67
      ->json_is( [] );
68
69
    my $av = $builder->build_object({ class => 'Koha::AuthorisedValues' });
70
71
    # One av created, should get returned
72
    $t->get_ok("//$userid:$password@/api/v1/authorised_values")
73
      ->status_is(200)
74
      ->json_is( [$av->to_api] );
75
76
    my $another_av = $builder->build_object(
77
        { class => 'Koha::AuthorisedValues', value => { lib => $av->lib } } );
78
    my $av_with_another_lib = $builder->build_object({ class => 'Koha::AuthorisedValues' });
79
80
    # Two avs created, they should both be returned
81
    $t->get_ok("//$userid:$password@/api/v1/authorised_values")
82
      ->status_is(200)
83
      ->json_is([$av->to_api,
84
                 $another_av->to_api,
85
                 $av_with_another_lib->to_api
86
                 ] );
87
88
    # Filtering works, two avs sharing lib
89
    $t->get_ok("//$userid:$password@/api/v1/authorised_values?description=" . $av->lib )
90
      ->status_is(200)
91
      ->json_is([ $av->to_api,
92
                  $another_av->to_api
93
                  ]);
94
95
    $t->get_ok("//$userid:$password@/api/v1/authorised_values?value=" . $av->authorised_value )
96
      ->status_is(200)
97
      ->json_is( [$av->to_api] );
98
99
    # Warn on unsupported query parameter
100
    $t->get_ok("//$userid:$password@/api/v1/authorised_values?av_blah=blah" )
101
      ->status_is(400)
102
      ->json_is( [{ path => '/query/av_blah', message => 'Malformed query string'}] );
103
104
    # Unauthorized access
105
    $t->get_ok("//$unauth_userid:$password@/api/v1/authorised_values")
106
      ->status_is(403);
107
108
    $schema->storage->txn_rollback;
109
};
110
111
subtest 'get() tests' => sub {
112
113
    plan tests => 8;
114
115
    $schema->storage->txn_begin;
116
117
    my $av = $builder->build_object({ class => 'Koha::AuthorisedValues' });
118
    my $librarian = $builder->build_object(
119
        {
120
            class => 'Koha::Patrons',
121
            value => { flags => 2**2 }    # catalogue flag = 2
122
        }
123
    );
124
    my $password = 'thePassword123';
125
    $librarian->set_password( { password => $password, skip_validation => 1 } );
126
    my $userid = $librarian->userid;
127
128
    my $patron = $builder->build_object(
129
        {
130
            class => 'Koha::Patrons',
131
            value => { flags => 0 }
132
        }
133
    );
134
135
    $patron->set_password( { password => $password, skip_validation => 1 } );
136
    my $unauth_userid = $patron->userid;
137
138
    $t->get_ok( "//$userid:$password@/api/v1/authorised_values/" . $av->id )
139
      ->status_is(200)
140
      ->json_is($av->to_api);
141
142
    $t->get_ok( "//$unauth_userid:$password@/api/v1/authorised_values/" . $av->id )
143
      ->status_is(403);
144
145
    my $av_to_delete = $builder->build_object({ class => 'Koha::AuthorisedValues' });
146
    my $non_existent_id = $av_to_delete->id;
147
    $av_to_delete->delete;
148
149
    $t->get_ok( "//$userid:$password@/api/v1/authorised_values/$non_existent_id" )
150
      ->status_is(404)
151
      ->json_is( '/error' => 'Authorised value not found' );
152
153
    $schema->storage->txn_rollback;
154
};
155
156
subtest 'add() tests' => sub {
157
158
    plan tests => 18;
159
160
    $schema->storage->txn_begin;
161
162
    my $librarian = $builder->build_object(
163
        {
164
            class => 'Koha::Patrons',
165
            value => { flags => 2**3 }    # parameters flag = 2
166
        }
167
    );
168
    my $password = 'thePassword123';
169
    $librarian->set_password( { password => $password, skip_validation => 1 } );
170
    my $userid = $librarian->userid;
171
172
    my $patron = $builder->build_object(
173
        {
174
            class => 'Koha::Patrons',
175
            value => { flags => 0 }
176
        }
177
    );
178
179
    $patron->set_password( { password => $password, skip_validation => 1 } );
180
    my $unauth_userid = $patron->userid;
181
182
    my $av_cat = $builder->build_object({ class => 'Koha::AuthorisedValueCategories' });
183
    my $av = {
184
        category         => $av_cat->category_name,
185
        value            => "a value",
186
        description      => "a lib",
187
        opac_description => "opac lib"
188
    };
189
190
    # Unauthorized attempt to write
191
    $t->post_ok("//$unauth_userid:$password@/api/v1/authorised_values" => json => $av)
192
      ->status_is(403);
193
194
    # Authorized attempt to write invalid data
195
    my $av_with_invalid_field = {
196
        blah             => "Av Blah",
197
        category         => $av_cat->category_name,
198
        value            => "a value",
199
        description      => "a lib",
200
        opac_description => "opac lib"
201
    };
202
203
    $t->post_ok( "//$userid:$password@/api/v1/authorised_values" => json => $av_with_invalid_field )
204
      ->status_is(400)
205
      ->json_is(
206
        "/errors" => [
207
            {
208
                message => "Properties not allowed: blah.",
209
                path    => "/body"
210
            }
211
        ]
212
      );
213
214
    # Authorized attempt to write
215
    my $av_id =
216
      $t->post_ok( "//$userid:$password@/api/v1/authorised_values" => json => $av )
217
        ->status_is( 201, 'SWAGGER3.2.1' )
218
        ->header_like(
219
            Location => qr|^\/api\/v1\/authorised_values/\d*|,
220
            'SWAGGER3.4.1'
221
            )
222
        ->json_is( '/category'         => $av->{category} )
223
        ->json_is( '/value'            => $av->{value} )
224
        ->json_is( '/description'      => $av->{description} )
225
        ->json_is( '/opac_description' => $av->{opac_description} )
226
        ->tx->res->json->{authorised_value_id};
227
228
    # Authorized attempt to create with null id
229
    $av->{authorised_value_id} = undef;
230
    $t->post_ok( "//$userid:$password@/api/v1/authorised_values" => json => $av )
231
      ->status_is(400)
232
      ->json_has('/errors');
233
234
    # Authorized attempt to create with existing id
235
    $av->{authorised_value_id} = $av_id;
236
    $t->post_ok( "//$userid:$password@/api/v1/authorised_values" => json => $av )
237
      ->status_is(400)
238
      ->json_is(
239
        "/errors" => [
240
            {
241
                message => "Read-only.",
242
                path    => "/body/authorised_value_id"
243
            }
244
        ]
245
    );
246
247
    $schema->storage->txn_rollback;
248
};
249
250
subtest 'update() tests' => sub {
251
252
    plan tests => 15;
253
254
    $schema->storage->txn_begin;
255
256
    my $librarian = $builder->build_object(
257
        {
258
            class => 'Koha::Patrons',
259
            value => { flags => 2**3 }    # parameters flag = 2
260
        }
261
    );
262
    my $password = 'thePassword123';
263
    $librarian->set_password( { password => $password, skip_validation => 1 } );
264
    my $userid = $librarian->userid;
265
266
    my $patron = $builder->build_object(
267
        {
268
            class => 'Koha::Patrons',
269
            value => { flags => 0 }
270
        }
271
    );
272
273
    $patron->set_password( { password => $password, skip_validation => 1 } );
274
    my $unauth_userid = $patron->userid;
275
276
    my $av_id = $builder->build_object({ class => 'Koha::AuthorisedValues' } )->id;
277
278
    # Unauthorized attempt to update
279
    $t->put_ok( "//$unauth_userid:$password@/api/v1/authorised_values/$av_id" => json => { name => 'New unauthorized name change' } )
280
      ->status_is(403);
281
282
    my $av_cat = $builder->build_object({ class => 'Koha::AuthorisedValueCategories' });
283
284
    # Attempt partial update on a PUT
285
    my $av_with_missing_field = {
286
        category => $av_cat->category_name,
287
        value    => "a value",
288
    };
289
290
    $t->put_ok( "//$userid:$password@/api/v1/authorised_values/$av_id" => json => $av_with_missing_field )
291
      ->status_is(400)
292
      ->json_is( "/errors" =>
293
          [ { message => "Missing property.", path => "/body/description" } ]
294
      );
295
296
    # Full object update on PUT
297
    my $av_with_updated_field = {
298
        category    => $av_cat->category_name,
299
        value       => "a value",
300
        description => "a lib",
301
    };
302
303
    $t->put_ok( "//$userid:$password@/api/v1/authorised_values/$av_id" => json => $av_with_updated_field )
304
      ->status_is(200)
305
      ->json_is( '/description' => 'a lib' );
306
307
    # Authorized attempt to write invalid data
308
    my $av_with_invalid_field = {
309
        blah             => "Av Blah",
310
        category         => $av_cat->category_name,
311
        value            => "a value",
312
        description      => "a lib",
313
        opac_description => "opac lib"
314
    };
315
316
    $t->put_ok( "//$userid:$password@/api/v1/authorised_values/$av_id" => json => $av_with_invalid_field )
317
      ->status_is(400)
318
      ->json_is(
319
        "/errors" => [
320
            {
321
                message => "Properties not allowed: blah.",
322
                path    => "/body"
323
            }
324
        ]
325
    );
326
327
    my $av_to_delete = $builder->build_object({ class => 'Koha::AuthorisedValues' });
328
    my $non_existent_id = $av_to_delete->id;
329
    $av_to_delete->delete;
330
331
    $t->put_ok( "//$userid:$password@/api/v1/authorised_values/$non_existent_id" => json => $av_with_updated_field )
332
      ->status_is(404);
333
334
    # Wrong method (POST)
335
    $av_with_updated_field->{authorised_value_id} = 2;
336
337
    $t->post_ok( "//$userid:$password@/api/v1/authorised_values/$av_id" => json => $av_with_updated_field )
338
      ->status_is(404);
339
340
    $schema->storage->txn_rollback;
341
};
342
343
subtest 'delete() tests' => sub {
344
345
    plan tests => 7;
346
347
    $schema->storage->txn_begin;
348
349
    my $librarian = $builder->build_object(
350
        {
351
            class => 'Koha::Patrons',
352
            value => { flags => 2**3 }    # parameters flag = 2
353
        }
354
    );
355
    my $password = 'thePassword123';
356
    $librarian->set_password( { password => $password, skip_validation => 1 } );
357
    my $userid = $librarian->userid;
358
359
    my $patron = $builder->build_object(
360
        {
361
            class => 'Koha::Patrons',
362
            value => { flags => 0 }
363
        }
364
    );
365
366
    $patron->set_password( { password => $password, skip_validation => 1 } );
367
    my $unauth_userid = $patron->userid;
368
369
    my $av_id = $builder->build_object({ class => 'Koha::AuthorisedValues' })->id;
370
371
    # Unauthorized attempt to delete
372
    $t->delete_ok( "//$unauth_userid:$password@/api/v1/authorised_values/$av_id" )
373
      ->status_is(403);
374
375
    $t->delete_ok("//$userid:$password@/api/v1/authorised_values/$av_id")
376
      ->status_is(204, 'SWAGGER3.2.4')
377
      ->content_is('', 'SWAGGER3.3.4');
378
379
    $t->delete_ok("//$userid:$password@/api/v1/authorised_values/$av_id")
380
      ->status_is(404);
381
382
    $schema->storage->txn_rollback;
383
};

Return to bug 17390