From 970e998f4eda0303b446fc69dc75f9a50e3b1283 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 28 Jan 2019 07:39:47 -0300 Subject: [PATCH] Bug 22206: Unit tests Signed-off-by: Martin Renvoize Signed-off-by: Josef Moravec --- t/db_dependent/api/v1/holds.t | 78 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/api/v1/holds.t b/t/db_dependent/api/v1/holds.t index fd8987ade7..700af99d7a 100644 --- a/t/db_dependent/api/v1/holds.t +++ b/t/db_dependent/api/v1/holds.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 4; +use Test::More tests => 5; use Test::Mojo; use t::lib::TestBuilder; use t::lib::Mocks; @@ -328,6 +328,82 @@ subtest 'Reserves with itemtype' => sub { $schema->storage->txn_rollback; +subtest 'suspend and resume tests' => sub { + + plan tests => 20; + + $schema->storage->txn_begin; + + my $password = 'AbcdEFG123'; + + my $patron = $builder->build_object( + { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 1 } } ); + $patron->set_password($password); + my $userid = $patron->userid; + + # Disable logging + t::lib::Mocks::mock_preference( 'HoldsLog', 0 ); + t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + + my $hold = $builder->build_object( + { class => 'Koha::Holds', + value => { suspend => 0, suspend_until => undef, waitingdate => undef } + } + ); + + ok( !$hold->is_suspended, 'Hold is not suspended' ); + $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" ) + ->status_is( 201, 'Hold suspension created' ); + + $hold->discard_changes; # refresh object + + ok( $hold->is_suspended, 'Hold is suspended' ); + $t->json_is( + '/expiration_date', + output_pref( + { dt => dt_from_string( $hold->suspend_until ), + dateformat => 'rfc3339', + dateonly => 1 + } + ) + ); + + $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" ) + ->status_is( 204, "Correct status when deleting a resource" ) + ->json_is( undef ); + + # Pass a an expiration date for the suspension + my $date = dt_from_string()->add( days => 5 ); + $t->post_ok( + "//$userid:$password@/api/v1/holds/" + . $hold->id + . "/suspension" => json => { + expiration_date => + output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } ) + } + )->status_is( 201, 'Hold suspension created' ) + ->json_is( '/expiration_date', + output_pref( { dt => $date, dateformat => 'rfc3339', dateonly => 1 } ) ); + + $t->delete_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" ) + ->status_is( 204, "Correct status when deleting a resource" ) + ->json_is( undef ); + + $hold->set_waiting->discard_changes; + + $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" ) + ->status_is( 400, 'Cannot suspend waiting hold' ) + ->json_is( '/error', 'Found hold cannot be suspended. Status=W' ); + + $hold->set_waiting(1)->discard_changes; + + $t->post_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/suspension" ) + ->status_is( 400, 'Cannot suspend waiting hold' ) + ->json_is( '/error', 'Found hold cannot be suspended. Status=T' ); + + $schema->storage->txn_rollback; +}; + sub create_biblio { my ($title) = @_; -- 2.11.0