Bugzilla – Attachment 63069 Details for
Bug 11897
Stock Rotation for Koha
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 11897: REST API - add path defs, controller & tests
Bug-11897-REST-API---add-path-defs-controller--tes.patch (text/plain), 10.95 KB, created by
Alex Sassmannshausen
on 2017-05-04 09:38:53 UTC
(
hide
)
Description:
Bug 11897: REST API - add path defs, controller & tests
Filename:
MIME Type:
Creator:
Alex Sassmannshausen
Created:
2017-05-04 09:38:53 UTC
Size:
10.95 KB
patch
obsolete
>From 67ca21e8113951054434e8344e65dd1884fba343 Mon Sep 17 00:00:00 2001 >From: Martin Renvoize <martin.renvoize@ptfs-europe.com> >Date: Tue, 13 Dec 2016 16:41:40 +0000 >Subject: [PATCH] Bug 11897: REST API - add path defs, controller & tests > >* Koha/REST/V1/Stage.pm: New file >* api/v1/swagger/paths.json: Add route for stage manipulation >* api/v1/swagger/paths/rotas.json: New file >* t/db_dependent/api/v1/stockrotationstage.t: New file >--- > Koha/REST/V1/Stage.pm | 47 ++++++++ > api/v1/swagger/paths.json | 3 + > api/v1/swagger/paths/rotas.json | 60 ++++++++++ > t/db_dependent/api/v1/stockrotationstage.t | 172 +++++++++++++++++++++++++++++ > 4 files changed, 282 insertions(+) > create mode 100644 Koha/REST/V1/Stage.pm > create mode 100644 api/v1/swagger/paths/rotas.json > create mode 100644 t/db_dependent/api/v1/stockrotationstage.t > >diff --git a/Koha/REST/V1/Stage.pm b/Koha/REST/V1/Stage.pm >new file mode 100644 >index 0000000000..42d48101c9 >--- /dev/null >+++ b/Koha/REST/V1/Stage.pm >@@ -0,0 +1,47 @@ >+package Koha::REST::V1::Stage; >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it under the >+# terms of the GNU General Public License as published by the Free Software >+# Foundation; either version 3 of the License, or (at your option) any later >+# version. >+# >+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY >+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR >+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License along >+# with Koha; if not, write to the Free Software Foundation, Inc., >+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. >+ >+use Modern::Perl; >+ >+use Mojo::Base 'Mojolicious::Controller'; >+ >+use Koha::Stockrotationrotas; >+use Koha::Stockrotationstages; >+ >+sub move { >+ my ($c, $args, $cb) = @_; >+ >+ my $rota = Koha::Stockrotationrotas->find($args->{rota_id}); >+ my $stage = Koha::Stockrotationstages->find($args->{stage_id}); >+ >+ if ($stage && $rota) { >+ my $result = $stage->move_to($args->{position}); >+ return $c->$cb({}, 200) if $result; >+ return $c->$cb( >+ { error => "Bad request - new position invalid"}, >+ 400 >+ ); >+ } else { >+ return $c->$cb( >+ { error => "Not found - Invalid rota or stage ID"}, >+ 404 >+ ); >+ } >+ >+} >+ >+1; >diff --git a/api/v1/swagger/paths.json b/api/v1/swagger/paths.json >index b1fcf40f5e..6bf862d691 100644 >--- a/api/v1/swagger/paths.json >+++ b/api/v1/swagger/paths.json >@@ -16,5 +16,8 @@ > }, > "/patrons/{borrowernumber}": { > "$ref": "paths/patrons.json#/~1patrons~1{borrowernumber}" >+ }, >+ "/rotas/{rota_id}/stages/{stage_id}/position": { >+ "$ref": "paths/rotas.json#/~1rotas~1{rota_id}~1stages~1{stage_id}~1position" > } > } >diff --git a/api/v1/swagger/paths/rotas.json b/api/v1/swagger/paths/rotas.json >new file mode 100644 >index 0000000000..ff13ae1e16 >--- /dev/null >+++ b/api/v1/swagger/paths/rotas.json >@@ -0,0 +1,60 @@ >+{ >+ "/rotas/{rota_id}/stages/{stage_id}/position": { >+ "put": { >+ "operationId": "moveStage", >+ "tags": ["rotas"], >+ "parameters": [{ >+ "name": "rota_id", >+ "in": "path", >+ "required": true, >+ "description": "A rotas ID", >+ "type": "integer" >+ }, { >+ "name": "stage_id", >+ "in": "path", >+ "required": true, >+ "description": "A stages ID", >+ "type": "integer" >+ }, { >+ "name": "position", >+ "in": "body", >+ "required": true, >+ "description": "A stages position in the rota", >+ "schema": { >+ "type": "integer" >+ } >+ }], >+ "produces": [ >+ "application/json" >+ ], >+ "responses": { >+ "200": { >+ "description": "OK" >+ }, >+ "400": { >+ "description": "Bad request", >+ "schema": { >+ "$ref": "../definitions.json#/error" >+ } >+ }, >+ "403": { >+ "description": "Access forbidden", >+ "schema": { >+ "$ref": "../definitions.json#/error" >+ } >+ }, >+ "404": { >+ "description": "Position not found", >+ "schema": { >+ "$ref": "../definitions.json#/error" >+ } >+ } >+ }, >+ "x-koha-authorization": { >+ "permissions": { >+ "borrowers": "1" >+ } >+ } >+ } >+ } >+} >diff --git a/t/db_dependent/api/v1/stockrotationstage.t b/t/db_dependent/api/v1/stockrotationstage.t >new file mode 100644 >index 0000000000..2ec336563e >--- /dev/null >+++ b/t/db_dependent/api/v1/stockrotationstage.t >@@ -0,0 +1,172 @@ >+#!/usr/bin/env perl >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it under the >+# terms of the GNU General Public License as published by the Free Software >+# Foundation; either version 3 of the License, or (at your option) any later >+# version. >+# >+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY >+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR >+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License along >+# with Koha; if not, write to the Free Software Foundation, Inc., >+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. >+ >+use Modern::Perl; >+ >+use Test::More tests => 1; >+use Test::Mojo; >+use Test::Warn; >+ >+use t::lib::TestBuilder; >+use t::lib::Mocks; >+ >+use C4::Auth; >+use Koha::Stockrotationstages; >+ >+my $schema = Koha::Database->new->schema; >+my $builder = t::lib::TestBuilder->new; >+ >+# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling >+# this affects the other REST api tests >+t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); >+ >+my $remote_address = '127.0.0.1'; >+my $t = Test::Mojo->new('Koha::REST::V1'); >+ >+subtest 'move() tests' => sub { >+ >+ plan tests => 16; >+ >+ $schema->storage->txn_begin; >+ >+ my ( $unauthorized_borrowernumber, $unauthorized_session_id ) = >+ create_user_and_session( { authorized => 0 } ); >+ my ( $authorized_borrowernumber, $authorized_session_id ) = >+ create_user_and_session( { authorized => 1 } ); >+ >+ my $library1 = $builder->build({ source => 'Branch' }); >+ my $library2 = $builder->build({ source => 'Branch' }); >+ my $rota = $builder->build({ source => 'Stockrotationrota' }); >+ my $stage1 = $builder->build({ >+ source => 'Stockrotationstage', >+ value => { >+ branchcode_id => $library1->{branchcode}, >+ rota_id => $rota->{rota_id}, >+ } >+ }); >+ my $stage2 = $builder->build({ >+ source => 'Stockrotationstage', >+ value => { >+ branchcode_id => $library2->{branchcode}, >+ rota_id => $rota->{rota_id}, >+ } >+ }); >+ my $rota_id = $rota->{rota_id}; >+ my $stage1_id = $stage1->{stage_id}; >+ >+ # Unauthorized attempt to update >+ my $tx = $t->ua->build_tx( >+ PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" => >+ json => 2 >+ ); >+ $tx->req->cookies( >+ { name => 'CGISESSID', value => $unauthorized_session_id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(403); >+ >+ # Invalid attempt to move a stage on a non-existant rota >+ $tx = $t->ua->build_tx( >+ PUT => "/api/v1/rotas/99999999/stages/$stage1_id/position" => >+ json => 2 >+ ); >+ $tx->req->cookies( >+ { name => 'CGISESSID', value => $authorized_session_id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(404) >+ ->json_is( '/error' => "Not found - Invalid rota or stage ID" ); >+ >+ # Invalid attempt to move an non-existant stage >+ $tx = $t->ua->build_tx( >+ PUT => "/api/v1/rotas/$rota_id/stages/999999999/position" => >+ json => 2 >+ ); >+ $tx->req->cookies( >+ { name => 'CGISESSID', value => $authorized_session_id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(404) >+ ->json_is( '/error' => "Not found - Invalid rota or stage ID" ); >+ >+ # Invalid attempt to move stage to current position >+ my $curr_position = $stage1->{position}; >+ $tx = $t->ua->build_tx( >+ PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" => >+ json => $curr_position >+ ); >+ $tx->req->cookies( >+ { name => 'CGISESSID', value => $authorized_session_id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(400) >+ ->json_is( '/error' => "Bad request - new position invalid" ); >+ >+ # Invalid attempt to move stage to invalid position >+ $tx = $t->ua->build_tx( >+ PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" => >+ json => 99999999 >+ ); >+ $tx->req->cookies( >+ { name => 'CGISESSID', value => $authorized_session_id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(400) >+ ->json_is( '/error' => "Bad request - new position invalid" ); >+ >+ # Valid, authorised move >+ $tx = $t->ua->build_tx( >+ PUT => "/api/v1/rotas/$rota_id/stages/$stage1_id/position" => >+ json => 2 >+ ); >+ $tx->req->cookies( >+ { name => 'CGISESSID', value => $authorized_session_id } ); >+ $tx->req->env( { REMOTE_ADDR => $remote_address } ); >+ $t->request_ok($tx)->status_is(200); >+ >+ $schema->storage->txn_rollback; >+}; >+ >+sub create_user_and_session { >+ >+ my $args = shift; >+ my $flags = ( $args->{authorized} ) ? $args->{authorized} : 0; >+ my $dbh = C4::Context->dbh; >+ >+ my $user = $builder->build( >+ { >+ source => 'Borrower', >+ value => { >+ flags => $flags >+ } >+ } >+ ); >+ >+ # Create a session for the authorized user >+ my $session = C4::Auth::get_session(''); >+ $session->param( 'number', $user->{borrowernumber} ); >+ $session->param( 'id', $user->{userid} ); >+ $session->param( 'ip', '127.0.0.1' ); >+ $session->param( 'lasttime', time() ); >+ $session->flush; >+ >+ if ( $args->{authorized} ) { >+ $dbh->do( " >+ INSERT INTO user_permissions (borrowernumber,module_bit,code) >+ VALUES (?,3,'parameters_remaining_permissions')", undef, >+ $user->{borrowernumber} ); >+ } >+ >+ return ( $user->{borrowernumber}, $session->id ); >+} >+ >+1; >-- >2.12.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 11897
:
25887
|
58207
|
58208
|
58209
|
58210
|
58211
|
58212
|
58213
|
58214
|
58215
|
58216
|
58217
|
58218
|
58219
|
58280
|
58281
|
58687
|
58692
|
58693
|
58694
|
58695
|
58696
|
60175
|
60715
|
61825
|
61826
|
61827
|
61828
|
61829
|
61830
|
61831
|
61832
|
61833
|
61834
|
61835
|
61836
|
61837
|
61838
|
61839
|
61885
|
62730
|
62750
|
62751
|
62752
|
62753
|
62754
|
62755
|
62756
|
62757
|
62758
|
62759
|
62760
|
62761
|
62762
|
62763
|
62764
|
62765
|
62766
|
62767
|
63063
|
63064
|
63065
|
63066
|
63067
|
63068
|
63069
|
63070
|
63071
|
63072
|
63073
|
63074
|
63075
|
63076
|
63077
|
63078
|
63079
|
63080
|
63114
|
64850
|
64851
|
65715
|
65716
|
65717
|
65718
|
65719
|
65720
|
65721
|
65722
|
65723
|
65724
|
65725
|
65726
|
65727
|
65728
|
65729
|
65730
|
65731
|
65732
|
65733
|
65734
|
65735
|
65736
|
65737
|
65738
|
65739
|
65740
|
65741
|
65742
|
65743
|
65744
|
65745
|
65746
|
65747
|
65748
|
65749
|
65750
|
65751
|
65752
|
65753
|
65754
|
65755
|
65756
|
65757
|
65758
|
66381
|
70496
|
70497
|
70498
|
70499
|
70500
|
70501
|
70502
|
70503
|
70504
|
70505
|
70506
|
70507
|
70508
|
70509
|
70510
|
70511
|
70512
|
70513
|
70514
|
70515
|
70516
|
70517
|
70518
|
70519
|
70520
|
70521
|
70522
|
70523
|
70524
|
72124
|
72125
|
72126
|
72127
|
72128
|
74003
|
74004
|
74005
|
74006
|
74007
|
74008
|
74010
|
74011
|
74012
|
74013
|
74014
|
74015
|
74016
|
74017
|
74197
|
74198
|
74199
|
74200
|
74201
|
74202
|
74203
|
74204
|
74205
|
74206
|
74319
|
74320
|
74360
|
74459
|
74460
|
74461
|
74462
|
74463
|
74464
|
74465
|
74466
|
74467
|
74468
|
74469
|
74470
|
75409
|
75410
|
75411
|
75412
|
75413
|
75414
|
75415
|
75416
|
75417
|
75418
|
75419
|
75420
|
76121
|
76122
|
76123
|
76124
|
76125
|
76126
|
76127
|
76128
|
76129
|
76130
|
76131
|
76132
|
76133
|
76134
|
76135
|
76144
|
76706
|
76707
|
76708
|
76709
|
76710
|
77623
|
77624
|
77625
|
77626
|
78387
|
78388
|
78389
|
78390
|
78391
|
78392
|
78393
|
78394
|
78404
|
78405
|
78406
|
78407
|
78408
|
79040
|
79041
|
79042
|
79043
|
79044
|
79738
|
79739
|
79740
|
79741
|
79742
|
79746
|
79747
|
79748
|
79749
|
79750
|
79964
|
79965
|
79966
|
79967
|
79968
|
79969
|
79970
|
79971
|
79972
|
79973
|
79974
|
79975
|
80068
|
80087
|
80088
|
80089
|
80090
|
80091
|
80092
|
80093
|
80094
|
80095
|
80096
|
80097
|
80098
|
80099
|
80100
|
80118
|
80260
|
80261
|
80262
|
80263
|
80264
|
80265
|
80266
|
80267
|
80268
|
80269
|
80270
|
80271
|
80272
|
80273
|
80274
|
80275
|
80289
|
80298
|
80299
|
80300