From 9954e8b721ae4107fac97017471d25cad62690fd Mon Sep 17 00:00:00 2001 From: Jiri Kozlovsky Date: Tue, 27 Jun 2017 18:27:48 +0000 Subject: [PATCH] Bug 17371 - REST API: add CRUD for biblios QA followup Note: Rebased on master --- api/v1/swagger/definitions/biblio.json | 4 +- t/db_dependent/api/v1/biblios.t | 85 ++++++++++++++++------------- t/db_dependent/api/v1/swagger/definitions.t | 7 ++- 3 files changed, 55 insertions(+), 41 deletions(-) diff --git a/api/v1/swagger/definitions/biblio.json b/api/v1/swagger/definitions/biblio.json index a46a55d..ecd4aab 100644 --- a/api/v1/swagger/definitions/biblio.json +++ b/api/v1/swagger/definitions/biblio.json @@ -33,11 +33,11 @@ "description": "publication or copyright date from the MARC record" }, "timestamp": { - "type": "string", + "type": ["string", "null"], "description": "date and time this record was last touched" }, "datecreated": { - "type": "string", + "type": ["string", "null"], "description": "the date this record was added to Koha" }, "abstract": { diff --git a/t/db_dependent/api/v1/biblios.t b/t/db_dependent/api/v1/biblios.t index 5f54a18..3aab9dc 100644 --- a/t/db_dependent/api/v1/biblios.t +++ b/t/db_dependent/api/v1/biblios.t @@ -17,41 +17,47 @@ use Modern::Perl; -use t::lib::TestBuilder; - use Test::More tests => 3; use Test::Mojo; +use Test::Warn; + +use t::lib::TestBuilder; +use t::lib::Mocks; + use Data::Dumper; use C4::Auth; use C4::Context; + use Koha::Database; -use MARC::File::XML ( BinaryEncoding => 'utf8', RecordFormat => 'UNIMARC' ); +use MARC::File::XML ( BinaryEncoding => 'utf8', RecordFormat => 'MARC21' ); + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); + +my $remote_address = '127.0.0.1'; +my $t = Test::Mojo->new('Koha::REST::V1'); BEGIN { use_ok('Koha::Biblios'); } -my $schema = Koha::Database->schema; my $dbh = C4::Context->dbh; -my $builder = t::lib::TestBuilder->new; - -$ENV{REMOTE_ADDR} = '127.0.0.1'; -my $t = Test::Mojo->new('Koha::REST::V1'); - -$schema->storage->txn_begin; my $file = MARC::File::XML->in( 't/db_dependent/Record/testrecords/marcxml_utf8.xml' ); my $record = $file->next(); -my ( $biblionumber, $itemnumber ); + +$schema->storage->txn_begin; my $librarian = $builder->build({ - source => "Borrower", - value => { - categorycode => 'S', - branchcode => 'NPL', - flags => 1, # editcatalogue - }, -}); + source => "Borrower", + value => { + categorycode => 'S', + branchcode => 'NPL', + flags => 1, # editcatalogue + }, + }); my $session = C4::Auth::get_session(''); $session->param('number', $librarian->{ borrowernumber }); @@ -60,31 +66,34 @@ $session->param('ip', '127.0.0.1'); $session->param('lasttime', time()); $session->flush; +my ( $biblionumber, $itemnumber ); + subtest 'Create biblio' => sub { - plan tests => 5; - - my $tx = $t->ua->build_tx(POST => '/api/v1/biblios' => $record->as_xml()); - $tx->req->env({REMOTE_ADDR => '127.0.0.1'}); - $tx->req->cookies({name => 'CGISESSID', value => $session->id}); - $t->request_ok($tx) - ->status_is(201); - $biblionumber = $tx->res->json->{biblionumber}; - $itemnumber = $tx->res->json->{items}; - - $t->json_is('/biblionumber' => $biblionumber) - ->json_is('/items' => $itemnumber) - ->header_like(Location => qr/$biblionumber/, 'Location header contains biblionumber'); + plan tests => 5; + + my $tx = $t->ua->build_tx(POST => '/api/v1/biblios' => $record->as_xml()); + $tx->req->env({REMOTE_ADDR => '127.0.0.1'}); + $tx->req->cookies({name => 'CGISESSID', value => $session->id}); + $t->request_ok($tx) + ->status_is(201); + $biblionumber = $tx->res->json->{biblionumber}; + $itemnumber = $tx->res->json->{items}; + + $t->json_is('/biblionumber' => $biblionumber) + ->json_is('/items' => $itemnumber) + ->header_like(Location => qr/$biblionumber/, 'Location header contains biblionumber'); + }; subtest 'Delete biblio' => sub { - plan tests => 2; + plan tests => 2; - my $tx = $t->ua->build_tx(DELETE => "/api/v1/biblios/$biblionumber"); - $tx->req->env({REMOTE_ADDR => '127.0.0.1'}); - $tx->req->cookies({name => 'CGISESSID', value => $session->id}); - $t->request_ok($tx) - ->status_is(200); -}; + my $tx = $t->ua->build_tx(DELETE => "/api/v1/biblios/$biblionumber"); + $tx->req->env({REMOTE_ADDR => '127.0.0.1'}); + $tx->req->cookies({name => 'CGISESSID', value => $session->id}); + $t->request_ok($tx) + ->status_is(200); +}; $schema->storage->txn_rollback; diff --git a/t/db_dependent/api/v1/swagger/definitions.t b/t/db_dependent/api/v1/swagger/definitions.t index b1fb184..a5ca342 100644 --- a/t/db_dependent/api/v1/swagger/definitions.t +++ b/t/db_dependent/api/v1/swagger/definitions.t @@ -46,7 +46,12 @@ my $schema = Koha::Database->new->schema; my @definition_names = keys %{ $api_spec->{definitions} }; subtest 'api/v1/definitions/*.json up-to-date with corresponding Koha-object' => sub { - plan tests => 2*(scalar(@definition_names) - 1); + + # we have to subtract definitions which are not Koha-object (only one subtest for those): + # error + # holds + # biblios + plan tests => 2*(scalar(@definition_names)) - 3; foreach my $name (@definition_names) { my $definition = $api_spec->{definitions}->{$name}; -- 2.1.4