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

(-)a/Koha/AdvancedEditorMacro.pm (-8 / +4 lines)
Lines 33-53 Koha::AdvancedEditorMacro - Koha Advanced Editor Macro Object class Link Here
33
33
34
=head3 to_api_mapping
34
=head3 to_api_mapping
35
35
36
This method returns the mapping for representing a Koha::AdvancedEditorMacro object
37
on the API.
38
39
=cut
36
=cut
40
37
41
sub to_api_mapping {
38
sub to_api_mapping {
42
    return {
39
    return {
43
        id              => 'macro_id',
40
        id                  => 'macro_id',
44
        name            => 'name',
41
        macro               => 'macro_text',
45
        macro           => 'macro_text',
42
        borrowernumber      => 'patron_id',
46
        borrowernumber  => 'patron_id',
47
        public          => 'public',
48
    };
43
    };
49
}
44
}
50
45
46
51
=head2 Internal methods
47
=head2 Internal methods
52
48
53
=head3 _type
49
=head3 _type
(-)a/Koha/REST/V1/AdvancedEditorMacro.pm (-66 / +160 lines)
Lines 38-44 sub list { Link Here
38
    my $c = shift->openapi->valid_input or return;
38
    my $c = shift->openapi->valid_input or return;
39
    my $patron = $c->stash('koha.user');
39
    my $patron = $c->stash('koha.user');
40
    return try {
40
    return try {
41
        my $macros_set = Koha::AdvancedEditorMacros->search({ -or => { public => 1, borrowernumber => $patron->borrowernumber } });
41
        my $macros_set = Koha::AdvancedEditorMacros->search({ -or => { shared => 1, borrowernumber => $patron->borrowernumber } });
42
        my $macros = $c->objects->search( $macros_set, \&_to_model, \&_to_api );
42
        my $macros = $c->objects->search( $macros_set, \&_to_model, \&_to_api );
43
        return $c->render( status => 200, openapi => $macros );
43
        return $c->render( status => 200, openapi => $macros );
44
    }
44
    }
Lines 64-79 Controller function that handles retrieving a single Koha::AdvancedEditorMacro Link Here
64
sub get {
64
sub get {
65
    my $c = shift->openapi->valid_input or return;
65
    my $c = shift->openapi->valid_input or return;
66
    my $patron = $c->stash('koha.user');
66
    my $patron = $c->stash('koha.user');
67
    my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
67
    my $macro = Koha::AdvancedEditorMacros->find({
68
        id => $c->validation->param('advancededitormacro_id'),
69
    });
68
    unless ($macro) {
70
    unless ($macro) {
69
        return $c->render( status  => 404,
71
        return $c->render( status  => 404,
70
                           openapi => { error => "Macro not found" } );
72
                           openapi => { error => "Macro not found" } );
71
    }
73
    }
72
    unless ( $macro->public || $macro->borrowernumber == $patron->borrowernumber ){
74
    if( $macro->shared ){
73
        return $c->render( status  => 403,
75
        return $c->render( status => 403, openapi => {
74
                           openapi => { error => "You do not have permission to access this macro" } );
76
            error => "This macro is shared, you must access it via advancededitormacros/shared"
77
        });
75
    }
78
    }
79
    warn $macro->borrowernumber;
80
    warn $patron->borrowernumber;
81
    if( $macro->borrowernumber != $patron->borrowernumber ){
82
        return $c->render( status => 403, openapi => {
83
            error => "You do not have permission to access this macro"
84
        });
85
    }
86
87
    return $c->render( status => 200, openapi => $macro->to_api );
88
}
89
90
=head3 get_shared
91
92
Controller function that handles retrieving a single Koha::AdvancedEditorMacro
93
94
=cut
76
95
96
sub get_shared {
97
    my $c = shift->openapi->valid_input or return;
98
    my $patron = $c->stash('koha.user');
99
    my $macro = Koha::AdvancedEditorMacros->find({
100
        id => $c->validation->param('advancededitormacro_id'),
101
    });
102
    unless ($macro) {
103
        return $c->render( status  => 404,
104
                           openapi => { error => "Macro not found" } );
105
    }
106
    unless( $macro->shared ){
107
        return $c->render( status => 403, openapi => {
108
            error => "This macro is not shared, you must access it via advancededitormacros"
109
        });
110
    }
77
    return $c->render( status => 200, openapi => $macro->to_api );
111
    return $c->render( status => 200, openapi => $macro->to_api );
78
}
112
}
79
113
Lines 86-97 Controller function that handles adding a new Koha::AdvancedEditorMacro object Link Here
86
sub add {
120
sub add {
87
    my $c = shift->openapi->valid_input or return;
121
    my $c = shift->openapi->valid_input or return;
88
122
89
    if( defined $c->validation->param('body')->{public} && $c->validation->param('body')->{public} == 1 ){
123
    if( defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
90
        my $patron = $c->stash('koha.user');
124
        return $c->render( status  => 403,
91
        unless ( $patron->has_permission({ editcatalogue => 'create_public_macros' }) ){
125
                           openapi => { error => "To create shared macros you must use advancededitor/shared" } );
92
            return $c->render( status  => 403,
93
                               openapi => { error => "You do not have permission to create public macros" } );
94
        }
95
    }
126
    }
96
127
97
    return try {
128
    return try {
Lines 103-127 sub add { Link Here
103
            openapi => $macro->to_api
134
            openapi => $macro->to_api
104
        );
135
        );
105
    }
136
    }
106
    catch {
137
    catch { handle_error($_) };
107
        if ( $_->isa('DBIx::Class::Exception') ) {
138
}
108
            return $c->render(
139
109
                status  => 500,
140
=head3 add_shared
110
                openapi => { error => $_->{msg} }
141
111
            );
142
Controller function that handles adding a new shared Koha::AdvancedEditorMacro object
112
        }
143
113
        else {
144
=cut
114
            return $c->render(
145
115
                status  => 500,
146
sub add_shared {
116
                openapi => { error => "Something went wrong, check the logs." }
147
    my $c = shift->openapi->valid_input or return;
117
            );
148
118
        }
149
    unless( defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
119
    };
150
        return $c->render( status  => 403,
151
                           openapi => { error => "To create private macros you must use advancededitor" } );
152
    }
153
154
    return try {
155
        my $macro = Koha::AdvancedEditorMacro->new( _to_model( $c->validation->param('body') ) );
156
        $macro->store;
157
        $c->res->headers->location( $c->req->url->to_string . '/' . $macro->id );
158
        return $c->render(
159
            status  => 201,
160
            openapi => $macro->to_api
161
        );
162
    }
163
    catch { handle_error($_) };
120
}
164
}
121
165
122
=head3 update
166
=head3 update
123
167
124
Controller function that handles updating a Koha::Library object
168
Controller function that handles updating a Koha::AdvancedEditorMacro object
125
169
126
=cut
170
=cut
127
171
Lines 134-150 sub update { Link Here
134
        return $c->render( status  => 404,
178
        return $c->render( status  => 404,
135
                           openapi => { error => "Object not found" } );
179
                           openapi => { error => "Object not found" } );
136
    }
180
    }
137
138
    my $patron = $c->stash('koha.user');
181
    my $patron = $c->stash('koha.user');
139
    if( $macro->public == 1 || defined $c->validation->param('body')->{public} && $c->validation->param('body')->{public} == 1 && $macro->borrowernumber == $patron->borrowernumber ){
182
140
        unless ( $patron->has_permission({ editcatalogue => 'create_public_macros' }) ){
183
    if( $macro->shared == 1 || defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
141
            return $c->render( status  => 403,
184
        return $c->render( status  => 403,
142
                               openapi => { error => "You do not have permission to create/edit public macros" } );
185
                           openapi => { error => "To update a macro as shared you must use the advancededitormacros/shared endpoint" } );
143
        }
144
    } else {
186
    } else {
145
        unless ( $macro->borrowernumber == $patron->borrowernumber ){
187
        unless ( $macro->borrowernumber == $patron->borrowernumber ){
146
            return $c->render( status  => 403,
188
            return $c->render( status  => 403,
147
                               openapi => { error => "You can only edit macros you own or public macros (with permission) " } );
189
                               openapi => { error => "You can only edit macros you own" } );
148
        }
190
        }
149
    }
191
    }
150
192
Lines 154-174 sub update { Link Here
154
        $macro->store();
196
        $macro->store();
155
        return $c->render( status => 200, openapi => $macro->to_api );
197
        return $c->render( status => 200, openapi => $macro->to_api );
156
    }
198
    }
157
    catch {
199
    catch { handle_error($_) };
158
        if ( $_->isa('Koha::Exceptions::Object') ) {
200
}
159
            return $c->render( status  => 500,
201
160
                               openapi => { error => $_->message } );
202
=head3 update_shared
161
        }
203
162
        else {
204
Controller function that handles updating a shared Koha::AdvancedEditorMacro object
163
            return $c->render( status => 500,
205
164
                openapi => { error => "Something went wrong, check the logs."} );
206
=cut
165
        }
207
166
    };
208
sub update_shared {
209
    my $c = shift->openapi->valid_input or return;
210
211
    my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
212
213
    if ( not defined $macro ) {
214
        return $c->render( status  => 404,
215
                           openapi => { error => "Object not found" } );
216
    }
217
218
    unless( $macro->shared == 1 || defined $c->validation->param('body')->{shared} && $c->validation->param('body')->{shared} == 1 ){
219
        return $c->render( status  => 403,
220
                           openapi => { error => "You can only update shared macros using this endpoint" } );
221
    }
222
223
    return try {
224
        my $params = $c->req->json;
225
        $macro->set( _to_model($params) );
226
        $macro->store();
227
        return $c->render( status => 200, openapi => $macro->to_api );
228
    }
229
    catch { handle_error($_) };
167
}
230
}
168
231
169
=head3 delete
232
=head3 delete
170
233
171
Controller function that handles deleting a Koha::AdvancedEditoracro object
234
Controller function that handles deleting a Koha::AdvancedEditorMacro object
172
235
173
=cut
236
=cut
174
237
Lines 182-196 sub delete { Link Here
182
    }
245
    }
183
246
184
    my $patron = $c->stash('koha.user');
247
    my $patron = $c->stash('koha.user');
185
    if( $macro->public == 1 && $macro->borrowernumber != $patron->borrowernumber ){
248
    if( $macro->shared == 1 ){
186
        unless ( $patron->has_permission({ editcatalogue => 'delete_public_macros' }) ){
249
        return $c->render( status  => 403,
187
            return $c->render( status  => 403,
250
                           openapi => { error => "You cannot delete shared macros using this endpoint" } );
188
                               openapi => { error => "You do not have permission to create public macros" } );
189
        }
190
    } else {
251
    } else {
191
        unless ( $macro->borrowernumber == $patron->borrowernumber ){
252
        unless ( $macro->borrowernumber == $patron->borrowernumber ){
192
            return $c->render( status  => 403,
253
            return $c->render( status  => 403,
193
                               openapi => { error => "You can only delete macros you own or public macros (with permission) " } );
254
                               openapi => { error => "You can only delete macros you own" } );
194
        }
255
        }
195
    }
256
    }
196
257
Lines 198-215 sub delete { Link Here
198
        $macro->delete;
259
        $macro->delete;
199
        return $c->render( status => 200, openapi => "" );
260
        return $c->render( status => 200, openapi => "" );
200
    }
261
    }
201
    catch {
262
    catch { handle_error($_) };
202
        if ( $_->isa('DBIx::Class::Exception') ) {
203
            return $c->render( status  => 500,
204
                               openapi => { error => $_->{msg} } );
205
        }
206
        else {
207
            return $c->render( status => 500,
208
                openapi => { error => "Something went wrong, check the logs. line 159"} );
209
        }
210
    };
211
}
263
}
212
264
265
=head3 delete_shared
266
267
Controller function that handles deleting a shared Koha::AdvancedEditorMacro object
268
269
=cut
270
271
sub delete_shared {
272
    my $c = shift->openapi->valid_input or return;
273
274
    my $macro = Koha::AdvancedEditorMacros->find( $c->validation->param('advancededitormacro_id') );
275
    if ( not defined $macro ) {
276
        return $c->render( status  => 404,
277
                           openapi => { error => "Object not found" } );
278
    }
279
280
    unless( $macro->shared == 1 ){
281
        return $c->render( status  => 403,
282
                           openapi => { error => "You can only delete shared macros using this endpoint" } );
283
    }
284
285
    return try {
286
        $macro->delete;
287
        return $c->render( status => 200, openapi => "" );
288
    }
289
    catch { handle_error($_,$c) };
290
}
291
292
=head3 _handle_error
293
294
Helper function that passes exception or error
295
296
=cut
297
298
sub _handle_error {
299
    my ($err,$c) = @_;
300
    if ( $err->isa('DBIx::Class::Exception') ) {
301
        return $c->render( status  => 500,
302
                           openapi => { error => $err->{msg} } );
303
    }
304
    else {
305
        return $c->render( status => 500,
306
            openapi => { error => "Something went wrong, check the logs."} );
307
    }
308
};
309
310
213
=head3 _to_api
311
=head3 _to_api
214
312
215
Helper function that maps a hashref of Koha::AdvancedEditorMacro attributes into REST api
313
Helper function that maps a hashref of Koha::AdvancedEditorMacro attributes into REST api
Lines 266-273 sub _to_model { Link Here
266
        }
364
        }
267
    }
365
    }
268
366
269
    if ( exists $macro->{public} ) {
367
    if ( exists $macro->{shared} ) {
270
        $macro->{public} = ($macro->{public}) ? 1 : 0;
368
        $macro->{shared} = ($macro->{shared}) ? 1 : 0;
271
    }
369
    }
272
370
273
371
Lines 282-291 sub _to_model { Link Here
282
380
283
our $to_api_mapping = {
381
our $to_api_mapping = {
284
    id                  => 'macro_id',
382
    id                  => 'macro_id',
285
    name                => 'name',
286
    macro               => 'macro_text',
383
    macro               => 'macro_text',
287
    borrowernumber      => 'patron_id',
384
    borrowernumber      => 'patron_id',
288
    public              => 'public',
289
};
385
};
290
386
291
=head3 $to_model_mapping
387
=head3 $to_model_mapping
Lines 294-303 our $to_api_mapping = { Link Here
294
390
295
our $to_model_mapping = {
391
our $to_model_mapping = {
296
    macro_id         => 'id',
392
    macro_id         => 'id',
297
    name             => 'name',
298
    macro_text       => 'macro',
393
    macro_text       => 'macro',
299
    patron_id        => 'borrowernumber',
394
    patron_id        => 'borrowernumber',
300
    public           => 'public',
301
};
395
};
302
396
303
1;
397
1;
(-)a/api/v1/swagger/definitions/advancededitormacro.json (-3 / +3 lines)
Lines 16-26 Link Here
16
      "description": "borrower number",
16
      "description": "borrower number",
17
      "type": ["integer", "null"]
17
      "type": ["integer", "null"]
18
    },
18
    },
19
    "public": {
19
    "shared": {
20
        "description": "is macro public",
20
        "description": "is macro shared",
21
        "type": ["string", "null"]
21
        "type": ["string", "null"]
22
    }
22
    }
23
  },
23
  },
24
  "additionalProperties": false,
24
  "additionalProperties": false,
25
  "required": ["name", "macro_text", "patron_id", "public"]
25
  "required": ["name", "macro_text", "patron_id", "shared"]
26
}
26
}
(-)a/api/v1/swagger/paths.json (+6 lines)
Lines 74-79 Link Here
74
  "/advancededitormacros/{advancededitormacro_id}": {
74
  "/advancededitormacros/{advancededitormacro_id}": {
75
    "$ref": "paths/advancededitormacros.json#/~1advancededitormacros~1{advancededitormacro_id}"
75
    "$ref": "paths/advancededitormacros.json#/~1advancededitormacros~1{advancededitormacro_id}"
76
  },
76
  },
77
  "/advancededitormacros/shared": {
78
    "$ref": "paths/advancededitormacros.json#/~1advancededitormacros~1shared"
79
  },
80
  "/advancededitormacros/shared/{advancededitormacro_id}": {
81
    "$ref": "paths/advancededitormacros.json#/~1advancededitormacros~1shared~1{advancededitormacro_id}"
82
  },
77
  "/patrons": {
83
  "/patrons": {
78
    "$ref": "paths/patrons.json#/~1patrons"
84
    "$ref": "paths/patrons.json#/~1patrons"
79
  },
85
  },
(-)a/api/v1/swagger/paths/advancededitormacros.json (-2 / +227 lines)
Lines 30-38 Link Here
30
          "type": "string"
30
          "type": "string"
31
        },
31
        },
32
        {
32
        {
33
          "name": "public",
33
          "name": "shared",
34
          "in": "query",
34
          "in": "query",
35
          "description": "Search on public macros",
35
          "description": "Search on shared macros",
36
          "required": false,
36
          "required": false,
37
          "type": "string"
37
          "type": "string"
38
        }
38
        }
Lines 127-132 Link Here
127
      }
127
      }
128
    }
128
    }
129
  },
129
  },
130
  "/advancededitormacros/shared": {
131
    "post": {
132
      "x-mojo-to": "AdvancedEditorMacro#add_shared",
133
      "operationId": "addsharedAdvancedEditorMacro",
134
      "tags": ["advancededitormacro"],
135
      "parameters": [{
136
        "name": "body",
137
        "in": "body",
138
        "description": "A JSON object containing informations about the new macro",
139
        "required": true,
140
        "schema": {
141
          "$ref": "../definitions.json#/advancededitormacro"
142
        }
143
      }],
144
      "produces": [
145
        "application/json"
146
      ],
147
      "responses": {
148
        "201": {
149
          "description": "Macro added",
150
          "schema": {
151
            "$ref": "../definitions.json#/advancededitormacro"
152
          }
153
        },
154
        "401": {
155
          "description": "Authentication required",
156
          "schema": {
157
            "$ref": "../definitions.json#/error"
158
          }
159
        },
160
        "403": {
161
          "description": "Access forbidden",
162
          "schema": {
163
            "$ref": "../definitions.json#/error"
164
          }
165
        },
166
        "500": {
167
          "description": "Internal error",
168
          "schema": {
169
            "$ref": "../definitions.json#/error"
170
          }
171
        },
172
        "503": {
173
          "description": "Under maintenance",
174
          "schema": {
175
            "$ref": "../definitions.json#/error"
176
          }
177
        }
178
      },
179
      "x-koha-authorization": {
180
        "permissions": {
181
          "editcatalogue": "advanced_editor",
182
          "editcatalogue": "create_shared_macros"
183
        }
184
      }
185
    }
186
  },
130
  "/advancededitormacros/{advancededitormacro_id}": {
187
  "/advancededitormacros/{advancededitormacro_id}": {
131
    "get": {
188
    "get": {
132
      "x-mojo-to": "AdvancedEditorMacro#get",
189
      "x-mojo-to": "AdvancedEditorMacro#get",
Lines 292-296 Link Here
292
        }
349
        }
293
      }
350
      }
294
    }
351
    }
352
  },
353
  "/advancededitormacros/shared/{advancededitormacro_id}": {
354
    "get": {
355
      "x-mojo-to": "AdvancedEditorMacro#get_shared",
356
      "operationId": "getsharedAdvancedEditorMacro",
357
      "tags": ["advancededitormacros"],
358
      "parameters": [{
359
        "$ref": "../parameters.json#/advancededitormacro_id_pp"
360
      }],
361
      "produces": [
362
        "application/json"
363
      ],
364
      "responses": {
365
        "200": {
366
          "description": "A macro",
367
          "schema": {
368
            "$ref": "../definitions.json#/advancededitormacro"
369
          }
370
        },
371
        "403": {
372
          "description": "Access forbidden",
373
          "schema": {
374
            "$ref": "../definitions.json#/error"
375
          }
376
        },
377
        "404": {
378
          "description": "AdvancedEditorMacro not found",
379
          "schema": {
380
            "$ref": "../definitions.json#/error"
381
          }
382
        },
383
        "500": {
384
          "description": "Internal error",
385
          "schema": {
386
            "$ref": "../definitions.json#/error"
387
          }
388
        },
389
        "503": {
390
          "description": "Under maintenance",
391
          "schema": {
392
            "$ref": "../definitions.json#/error"
393
          }
394
        }
395
      },
396
      "x-koha-authorization": {
397
        "permissions": {
398
            "editcatalogue": "advanced_editor"
399
        }
400
      }
401
    },
402
    "put": {
403
      "x-mojo-to": "AdvancedEditorMacro#update_shared",
404
      "operationId": "updatesharedAdvancedEditorMacro",
405
      "tags": ["advancededitormacros"],
406
      "parameters": [{
407
        "$ref": "../parameters.json#/advancededitormacro_id_pp"
408
      }, {
409
        "name": "body",
410
        "in": "body",
411
        "description": "An advanced editor macro object",
412
        "required": true,
413
        "schema": {
414
          "$ref": "../definitions.json#/advancededitormacro"
415
        }
416
      }],
417
      "produces": [
418
        "application/json"
419
      ],
420
      "responses": {
421
        "200": {
422
          "description": "An advanced editor macro",
423
          "schema": {
424
            "$ref": "../definitions.json#/advancededitormacro"
425
          }
426
        },
427
        "401": {
428
          "description": "Authentication required",
429
          "schema": {
430
            "$ref": "../definitions.json#/error"
431
          }
432
        },
433
        "403": {
434
          "description": "Access forbidden",
435
          "schema": {
436
            "$ref": "../definitions.json#/error"
437
          }
438
        },
439
        "404": {
440
          "description": "Macro not found",
441
          "schema": {
442
            "$ref": "../definitions.json#/error"
443
          }
444
        },
445
        "500": {
446
          "description": "Internal error",
447
          "schema": {
448
            "$ref": "../definitions.json#/error"
449
          }
450
        },
451
        "503": {
452
          "description": "Under maintenance",
453
          "schema": {
454
            "$ref": "../definitions.json#/error"
455
          }
456
        }
457
      },
458
      "x-koha-authorization": {
459
        "permissions": {
460
          "editcatalogue": "advanced_editor",
461
          "editcatalogue": "create_shared_macros"
462
        }
463
      }
464
    },
465
    "delete": {
466
      "x-mojo-to": "AdvancedEditorMacro#delete_shared",
467
      "operationId": "deletesharedAdvancedEditorMacro",
468
      "tags": ["advancededitormacros"],
469
      "parameters": [{
470
        "$ref": "../parameters.json#/advancededitormacro_id_pp"
471
      }],
472
      "produces": [
473
        "application/json"
474
      ],
475
      "responses": {
476
        "200": {
477
          "description": "Advanced editor macro deleted",
478
          "schema": {
479
            "type": "string"
480
          }
481
        },
482
        "401": {
483
          "description": "Authentication required",
484
          "schema": {
485
            "$ref": "../definitions.json#/error"
486
          }
487
        },
488
        "403": {
489
          "description": "Access forbidden",
490
          "schema": {
491
            "$ref": "../definitions.json#/error"
492
          }
493
        },
494
        "404": {
495
          "description": "Macro not found",
496
          "schema": {
497
            "$ref": "../definitions.json#/error"
498
          }
499
        },
500
        "500": {
501
          "description": "Internal error",
502
          "schema": {
503
            "$ref": "../definitions.json#/error"
504
          }
505
        },
506
        "503": {
507
          "description": "Under maintenance",
508
          "schema": {
509
            "$ref": "../definitions.json#/error"
510
          }
511
        }
512
      },
513
      "x-koha-authorization": {
514
        "permissions": {
515
          "editcatalogue": "advanced_editor",
516
          "editcatalogue": "delete_shared_macros"
517
        }
518
      }
519
    }
295
  }
520
  }
296
}
521
}
(-)a/t/db_dependent/api/v1/advanced_editor_macros.t (-55 / +73 lines)
Lines 64-70 subtest 'list() tests' => sub { Link Here
64
            name => 'Test2',
64
            name => 'Test2',
65
            macro => 'delete 100',
65
            macro => 'delete 100',
66
            borrowernumber => $patron_1->borrowernumber,
66
            borrowernumber => $patron_1->borrowernumber,
67
            public => 1,
67
            shared=> 1,
68
        }
68
        }
69
    });
69
    });
70
    my $macro_3 = $builder->build_object({ class => 'Koha::AdvancedEditorMacros', value =>
70
    my $macro_3 = $builder->build_object({ class => 'Koha::AdvancedEditorMacros', value =>
Lines 79-89 subtest 'list() tests' => sub { Link Here
79
            name => 'Test4',
79
            name => 'Test4',
80
            macro => 'delete 100',
80
            macro => 'delete 100',
81
            borrowernumber => $patron_2->borrowernumber,
81
            borrowernumber => $patron_2->borrowernumber,
82
            public => 1,
82
            shared => 1,
83
        }
83
        }
84
    });
84
    });
85
85
86
    my $macros_index = Koha::AdvancedEditorMacros->search({ -or => { public => 1, borrowernumber => $patron_1->borrowernumber } })->count-1;
86
    my $macros_index = Koha::AdvancedEditorMacros->search({ -or => { shared => 1, borrowernumber => $patron_1->borrowernumber } })->count-1;
87
    ## Authorized user tests
87
    ## Authorized user tests
88
    # Make sure we are returned with the correct amount of macros
88
    # Make sure we are returned with the correct amount of macros
89
    $t->get_ok( "//$userid:$password@/api/v1/advancededitormacros" )
89
    $t->get_ok( "//$userid:$password@/api/v1/advancededitormacros" )
Lines 106-112 subtest 'list() tests' => sub { Link Here
106
        $t->get_ok("//$userid:$password@/api/v1/advancededitormacros?patron_id=" . $patron_1->borrowernumber)
106
        $t->get_ok("//$userid:$password@/api/v1/advancededitormacros?patron_id=" . $patron_1->borrowernumber)
107
          ->status_is(200)
107
          ->status_is(200)
108
          ->json_has( [ $macro_1, $macro_2 ] );
108
          ->json_has( [ $macro_1, $macro_2 ] );
109
        $t->get_ok("//$userid:$password@/api/v1/advancededitormacros?public=1")
109
        $t->get_ok("//$userid:$password@/api/v1/advancededitormacros?shared=1")
110
          ->status_is(200)
110
          ->status_is(200)
111
          ->json_has( [ $macro_2, $macro_4 ] );
111
          ->json_has( [ $macro_2, $macro_4 ] );
112
    };
112
    };
Lines 120-126 subtest 'list() tests' => sub { Link Here
120
120
121
subtest 'get() tests' => sub {
121
subtest 'get() tests' => sub {
122
122
123
    plan tests => 12;
123
    plan tests => 15;
124
124
125
    my $patron = $builder->build_object({
125
    my $patron = $builder->build_object({
126
        class => 'Koha::Patrons',
126
        class => 'Koha::Patrons',
Lines 131-159 subtest 'get() tests' => sub { Link Here
131
    my $userid = $patron->userid;
131
    my $userid = $patron->userid;
132
132
133
    my $macro_1 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => {
133
    my $macro_1 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => {
134
            public => 1,
134
            shared => 1,
135
        }
135
        }
136
    });
136
    });
137
    my $macro_2 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => {
137
    my $macro_2 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => {
138
            public => 0,
138
            shared => 0,
139
        }
139
        }
140
    });
140
    });
141
    my $macro_3 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => {
141
    my $macro_3 = $builder->build_object( { class => 'Koha::AdvancedEditorMacros', value => {
142
            borrowernumber => $patron->borrowernumber,
142
            borrowernumber => $patron->borrowernumber,
143
            shared => 0,
143
        }
144
        }
144
    });
145
    });
145
146
146
    $t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_1->id )
147
    $t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_1->id )
147
      ->status_is( 200, 'SWAGGER3.2.2' )
148
      ->status_is( 403, 'Cannot get a shared macro via regular endpoint' )
148
      ->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_1->TO_JSON ), 'SWAGGER3.3.2' );
149
      ->json_is( '/error' => 'This macro is shared, you must access it via advancededitormacros/shared' );
150
151
    $t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/shared/" . $macro_1->id )
152
      ->status_is( 200, 'Can get a shared macro via shared endpoint' )
153
      ->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_1->TO_JSON ), 'Macro correctly retrieved' );
149
154
150
    $t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_2->id )
155
    $t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_2->id )
151
      ->status_is( 403, 'SWAGGER3.2.2' )
156
      ->status_is( 403, 'Cannot access another users macro' )
152
      ->json_is( '/error' => 'You do not have permission to access this macro' );
157
      ->json_is( '/error' => 'You do not have permission to access this macro' );
153
158
154
    $t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_3->id )
159
    $t->get_ok( "//$userid:$password@/api/v1/advancededitormacros/" . $macro_3->id )
155
      ->status_is( 200, 'SWAGGER3.2.2' )
160
      ->status_is( 200, 'Can get your own private macro' )
156
      ->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_3->TO_JSON ), 'SWAGGER3.3.2' );
161
      ->json_is( '' => Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro_3->TO_JSON ), 'Macro correctly retrieved' );
157
162
158
    my $non_existent_code = $macro_1->id;
163
    my $non_existent_code = $macro_1->id;
159
    $macro_1->delete;
164
    $macro_1->delete;
Lines 166-172 subtest 'get() tests' => sub { Link Here
166
171
167
subtest 'add() tests' => sub {
172
subtest 'add() tests' => sub {
168
173
169
    plan tests => 20;
174
    plan tests => 24;
170
175
171
    my $authorized_patron = $builder->build_object({
176
    my $authorized_patron = $builder->build_object({
172
        class => 'Koha::Patrons',
177
        class => 'Koha::Patrons',
Lines 194-200 subtest 'add() tests' => sub { Link Here
194
199
195
    my $macro = $builder->build_object({
200
    my $macro = $builder->build_object({
196
        class => 'Koha::AdvancedEditorMacros',
201
        class => 'Koha::AdvancedEditorMacros',
197
        value => { public => 0 }
202
        value => { shared => 0 }
198
    });
203
    });
199
    my $macro_values     = Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro->TO_JSON );
204
    my $macro_values     = Koha::REST::V1::AdvancedEditorMacro::_to_api( $macro->TO_JSON );
200
    delete $macro_values->{macro_id};
205
    delete $macro_values->{macro_id};
Lines 222-233 subtest 'add() tests' => sub { Link Here
222
    # Authorized attempt to write
227
    # Authorized attempt to write
223
    $t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values )
228
    $t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values )
224
      ->status_is( 201, 'SWAGGER3.2.1' )
229
      ->status_is( 201, 'SWAGGER3.2.1' )
225
      ->json_has( '/macro_id', 'SWAGGER3.3.1' )
230
      ->json_has( '/macro_id', 'We generated a new id' )
226
      ->json_is( '/name' => $macro_values->{name}, 'SWAGGER3.3.1' )
231
      ->json_is( '/name' => $macro_values->{name}, 'The name matches what we supplied' )
227
      ->json_is( '/macro_text' => $macro_values->{macro_text}, 'SWAGGER3.3.1' )
232
      ->json_is( '/macro_text' => $macro_values->{macro_text}, 'The text matches what we supplied' )
228
      ->json_is( '/patron_id' => $macro_values->{patron_id}, 'SWAGGER3.3.1' )
233
      ->json_is( '/patron_id' => $macro_values->{patron_id}, 'The borrower matches the borrower who submitted' )
229
      ->json_is( '/public' => $macro_values->{public}, 'SWAGGER3.3.1' )
234
      ->json_is( '/shared' => 0, 'The macro is not shared' )
230
      ->header_like( Location => qr|^\/api\/v1\/advancededitormacros\/d*|, 'SWAGGER3.4.1' );
235
      ->header_like( Location => qr|^\/api\/v1\/advancededitormacros\/d*|, 'Correct location' );
231
236
232
    # save the library_id
237
    # save the library_id
233
    my $macro_id = 999;
238
    my $macro_id = 999;
Lines 245-274 subtest 'add() tests' => sub { Link Here
245
        ]
250
        ]
246
    );
251
    );
247
252
248
    $macro_values->{public} = 1;
253
    $macro_values->{shared} = 1;
249
    delete $macro_values->{macro_id};
254
    delete $macro_values->{macro_id};
250
255
251
    # Unauthorized attempt to write a public macro
256
    # Unauthorized attempt to write a shared macro on private endpoint
252
    $t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values )
257
    $t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values )
253
      ->status_is(403);
258
      ->status_is(403);
259
    # Unauthorized attempt to write a private macro on shared endpoint
260
    $t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared" => json => $macro_values )
261
      ->status_is(403);
254
262
255
    $builder->build({
263
    $builder->build({
256
        source => 'UserPermission',
264
        source => 'UserPermission',
257
        value  => {
265
        value  => {
258
            borrowernumber => $authorized_patron->borrowernumber,
266
            borrowernumber => $authorized_patron->borrowernumber,
259
            module_bit     => 9,
267
            module_bit     => 9,
260
            code           => 'create_public_macros',
268
            code           => 'create_shared_macros',
261
        },
269
        },
262
    });
270
    });
263
271
264
    # Authorized attempt to write a public macro
272
    # Authorized attempt to write a shared macro on private endpoint
265
    $t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values )
273
    $t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros" => json => $macro_values )
274
      ->status_is(403);
275
276
    # Authorized attempt to write a shared macro on shared endpoint
277
    $t->post_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared" => json => $macro_values )
266
      ->status_is(201);
278
      ->status_is(201);
267
279
268
};
280
};
269
281
270
subtest 'update() tests' => sub {
282
subtest 'update() tests' => sub {
271
    plan tests => 28;
283
    plan tests => 32;
272
284
273
    my $authorized_patron = $builder->build_object({
285
    my $authorized_patron = $builder->build_object({
274
        class => 'Koha::Patrons',
286
        class => 'Koha::Patrons',
Lines 296-306 subtest 'update() tests' => sub { Link Here
296
308
297
    my $macro = $builder->build_object({
309
    my $macro = $builder->build_object({
298
        class => 'Koha::AdvancedEditorMacros',
310
        class => 'Koha::AdvancedEditorMacros',
299
        value => { borrowernumber => $authorized_patron->borrowernumber, public => 0 }
311
        value => { borrowernumber => $authorized_patron->borrowernumber, shared => 0 }
300
    });
312
    });
301
    my $macro_2 = $builder->build_object({
313
    my $macro_2 = $builder->build_object({
302
        class => 'Koha::AdvancedEditorMacros',
314
        class => 'Koha::AdvancedEditorMacros',
303
        value => { borrowernumber => $unauthorized_patron->borrowernumber, public => 0 }
315
        value => { borrowernumber => $unauthorized_patron->borrowernumber, shared => 0 }
304
    });
316
    });
305
    my $macro_id = $macro->id;
317
    my $macro_id = $macro->id;
306
    my $macro_2_id = $macro_2->id;
318
    my $macro_2_id = $macro_2->id;
Lines 327-365 subtest 'update() tests' => sub { Link Here
327
        name => "Macro-update",
339
        name => "Macro-update",
328
        macro_text => "delete 100",
340
        macro_text => "delete 100",
329
        patron_id => $authorized_patron->borrowernumber,
341
        patron_id => $authorized_patron->borrowernumber,
330
        public => 0,
342
        shared => 0,
331
    };
343
    };
332
344
333
    my $test = $t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update )
345
    my $test = $t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update )
334
      ->status_is(200, 'SWAGGER3.2.1')
346
      ->status_is(200, 'Authorized user can update a macro')
335
      ->json_has( '/macro_id', 'SWAGGER3.3.1' )
347
      ->json_is( '/macro_id' => $macro_id, 'We get the id back' )
336
      ->json_is( '/name' => $macro_update->{name}, 'SWAGGER3.3.1' )
348
      ->json_is( '/name' => $macro_update->{name}, 'We get the name back' )
337
      ->json_is( '/macro_text' => $macro_update->{macro_text}, 'SWAGGER3.3.1' )
349
      ->json_is( '/macro_text' => $macro_update->{macro_text}, 'We get the text back' )
338
      ->json_is( '/patron_id' => $macro_update->{patron_id}, 'SWAGGER3.3.1' )
350
      ->json_is( '/patron_id' => $macro_update->{patron_id}, 'We get the patron_id back' )
339
      ->json_is( '/public' => $macro_update->{public}, 'SWAGGER3.3.1' );
351
      ->json_is( '/shared' => $macro_update->{shared}, 'It should still not be shared' );
340
352
341
    # Now try to make the macro public
353
    # Now try to make the macro shared
342
    $macro_update->{public} = 1;
354
    $macro_update->{shared} = 1;
343
355
344
    $t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update )
356
    $t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_id" => json => $macro_update )
345
      ->status_is(403, 'Cannot make your macro public without permission');
357
      ->status_is(403, 'Cannot make your macro shared on private endpoint');
358
    $t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_id" => json => $macro_update )
359
      ->status_is(403, 'Cannot make your macro shared without permission');
346
360
347
    $builder->build({
361
    $builder->build({
348
        source => 'UserPermission',
362
        source => 'UserPermission',
349
        value  => {
363
        value  => {
350
            borrowernumber => $authorized_patron->borrowernumber,
364
            borrowernumber => $authorized_patron->borrowernumber,
351
            module_bit     => 9,
365
            module_bit     => 9,
352
            code           => 'create_public_macros',
366
            code           => 'create_shared_macros',
353
        },
367
        },
354
    });
368
    });
355
369
356
    $t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update )
370
    $t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_id" => json => $macro_update )
357
      ->status_is(200, 'Can update macro to public with permission')
371
      ->status_is(403, 'Cannot make your macro shared on the private endpoint');
358
      ->json_has( '/macro_id', 'SWAGGER3.3.1' )
372
359
      ->json_is( '/name' => $macro_update->{name}, 'SWAGGER3.3.1' )
373
    $t->put_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_id" => json => $macro_update )
360
      ->json_is( '/macro_text' => $macro_update->{macro_text}, 'SWAGGER3.3.1' )
374
      ->status_is(200, 'Can update macro to shared with permission')
361
      ->json_is( '/patron_id' => $macro_update->{patron_id}, 'SWAGGER3.3.1' )
375
      ->json_is( '/macro_id' => $macro_id, 'We get back the id' )
362
      ->json_is( '/public' => $macro_update->{public}, 'SWAGGER3.3.1' );
376
      ->json_is( '/name' => $macro_update->{name}, 'We get back the name' )
377
      ->json_is( '/macro_text' => $macro_update->{macro_text}, 'We get back the text' )
378
      ->json_is( '/patron_id' => $macro_update->{patron_id}, 'We get back our patron id' )
379
      ->json_is( '/shared' => 1, 'It is shared' );
363
380
364
    # Authorized attempt to write invalid data
381
    # Authorized attempt to write invalid data
365
    my $macro_with_invalid_field = { %$macro_update };
382
    my $macro_with_invalid_field = { %$macro_update };
Lines 384-394 subtest 'update() tests' => sub { Link Here
384
      ->status_is(404);
401
      ->status_is(404);
385
402
386
    $t->put_ok("//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id" => json => $macro_update)
403
    $t->put_ok("//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id" => json => $macro_update)
387
      ->status_is(403, "Cannot update other borrowers non public macro");
404
      ->status_is(403, "Cannot update other borrowers private macro");
388
};
405
};
389
406
390
subtest 'delete() tests' => sub {
407
subtest 'delete() tests' => sub {
391
    plan tests => 10;
408
    plan tests => 12;
392
409
393
    my $authorized_patron = $builder->build_object({
410
    my $authorized_patron = $builder->build_object({
394
        class => 'Koha::Patrons',
411
        class => 'Koha::Patrons',
Lines 416-426 subtest 'delete() tests' => sub { Link Here
416
433
417
    my $macro = $builder->build_object({
434
    my $macro = $builder->build_object({
418
        class => 'Koha::AdvancedEditorMacros',
435
        class => 'Koha::AdvancedEditorMacros',
419
        value => { borrowernumber => $authorized_patron->borrowernumber, public => 0 }
436
        value => { borrowernumber => $authorized_patron->borrowernumber, shared => 0 }
420
    });
437
    });
421
    my $macro_2 = $builder->build_object({
438
    my $macro_2 = $builder->build_object({
422
        class => 'Koha::AdvancedEditorMacros',
439
        class => 'Koha::AdvancedEditorMacros',
423
        value => { borrowernumber => $unauthorized_patron->borrowernumber, public => 0 }
440
        value => { borrowernumber => $unauthorized_patron->borrowernumber, shared => 0 }
424
    });
441
    });
425
    my $macro_id = $macro->id;
442
    my $macro_id = $macro->id;
426
    my $macro_2_id = $macro_2->id;
443
    my $macro_2_id = $macro_2->id;
Lines 435-455 subtest 'delete() tests' => sub { Link Here
435
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id")
452
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id")
436
      ->status_is(403, 'Cannot delete other users macro with permission');
453
      ->status_is(403, 'Cannot delete other users macro with permission');
437
454
438
    $macro_2->public(1)->store();
455
    $macro_2->shared(1)->store();
439
456
440
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id")
457
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_2_id")
441
      ->status_is(403, 'Cannot delete other users public macro without permission');
458
      ->status_is(403, 'Cannot delete other users shared macro without permission');
442
459
443
    $builder->build({
460
    $builder->build({
444
        source => 'UserPermission',
461
        source => 'UserPermission',
445
        value  => {
462
        value  => {
446
            borrowernumber => $authorized_patron->borrowernumber,
463
            borrowernumber => $authorized_patron->borrowernumber,
447
            module_bit     => 9,
464
            module_bit     => 9,
448
            code           => 'delete_public_macros',
465
            code           => 'delete_shared_macros',
449
        },
466
        },
450
    });
467
    });
451
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id")
468
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/$macro_2_id")
452
      ->status_is(200, 'Can delete other users public macro with permission');
469
      ->status_is(403, 'Cannot delete other users shared macro with permission on private endpoint');
470
    $t->delete_ok( "//$auth_userid:$password@/api/v1/advancededitormacros/shared/$macro_2_id")
471
      ->status_is(200, 'Can delete other users shared macro with permission');
453
472
454
};
473
};
455
474
456
- 

Return to bug 17268