diff --git a/lib/NGCP/Panel/Role/API.pm b/lib/NGCP/Panel/Role/API.pm index 41fb429b7d..fc1b09ddda 100644 --- a/lib/NGCP/Panel/Role/API.pm +++ b/lib/NGCP/Panel/Role/API.pm @@ -100,10 +100,10 @@ sub validate_form { if $resource->{$k}->$_isa('DateTime'); $resource->{$k} = $resource->{$k} + 0 if(defined $resource->{$k} && ( - $resource->{$k}->is_int || $resource->{$k}->is_decimal) && ( $form->field($k)->$_isa('HTML::FormHandler::Field::Integer') || $form->field($k)->$_isa('HTML::FormHandler::Field::Money') || - $form->field($k)->$_isa('HTML::FormHandler::Field::Float'))); + $form->field($k)->$_isa('HTML::FormHandler::Field::Float')) && + ($resource->{$k}->is_int || $resource->{$k}->is_decimal)); # only do this for converting back from obj to hal # otherwise it breaks db fields with the \0 and \1 notation @@ -192,10 +192,11 @@ sub valid_precondition { sub require_preference { my ($self, $c) = @_; + return 'minimal' unless $c->request->header('Prefer'); my @preference = grep { 'return' eq $_->[0] } split_header_words($c->request->header('Prefer')); return $preference[0][1] if 1 == @preference && ('minimal' eq $preference[0][1] || 'representation' eq $preference[0][1]); - return 'minimal'; + $self->error($c, HTTP_BAD_REQUEST, "Header 'Prefer' must be either 'return=minimal' or 'return=representation'."); } sub require_wellformed_json { diff --git a/t/api-billingfees.t b/t/api-billingfees.t index 3bf43ed25f..a58f844cc6 100644 --- a/t/api-billingfees.t +++ b/t/api-billingfees.t @@ -351,7 +351,7 @@ my @allfees = (); delete $fee->{_links}; delete $fee->{_embedded}; $req = HTTP::Request->new('PUT', $uri.'/'.$firstfee); - + # check if it fails without content type $req->remove_header('Content-Type'); $req->header('Prefer' => "return=minimal"); @@ -366,17 +366,11 @@ my @allfees = (); $req->remove_header('Content-Type'); $req->header('Content-Type' => 'application/json'); - # check if it fails with missing Prefer - $req->remove_header('Prefer'); - $res = $ua->request($req); - is($res->code, 400, "check put missing prefer"); - # check if it fails with invalid Prefer $req->header('Prefer' => "return=invalid"); $res = $ua->request($req); is($res->code, 400, "check put invalid prefer"); - $req->remove_header('Prefer'); $req->header('Prefer' => "return=representation"); diff --git a/t/api-billingprofiles.t b/t/api-billingprofiles.t index d45699e696..a3a8b6de02 100644 --- a/t/api-billingprofiles.t +++ b/t/api-billingprofiles.t @@ -205,11 +205,6 @@ my @allprofiles = (); $req->remove_header('Content-Type'); $req->header('Content-Type' => 'application/json'); - # check if it fails with missing Prefer - $req->remove_header('Prefer'); - $res = $ua->request($req); - is($res->code, 400, "check put missing prefer"); - # check if it fails with invalid Prefer $req->header('Prefer' => "return=invalid"); $res = $ua->request($req); diff --git a/t/api-contracts.t b/t/api-contracts.t index cf10628259..93d7dba08b 100644 --- a/t/api-contracts.t +++ b/t/api-contracts.t @@ -271,17 +271,11 @@ my @allcontracts = (); $req->remove_header('Content-Type'); $req->header('Content-Type' => 'application/json'); - # check if it fails with missing Prefer - $req->remove_header('Prefer'); - $res = $ua->request($req); - is($res->code, 400, "check put missing prefer"); - # check if it fails with invalid Prefer $req->header('Prefer' => "return=invalid"); $res = $ua->request($req); is($res->code, 400, "check put invalid prefer"); - $req->remove_header('Prefer'); $req->header('Prefer' => "return=representation"); diff --git a/t/api-customercontacts.t b/t/api-customercontacts.t index 08f18cb74a..eaf0fd3f53 100644 --- a/t/api-customercontacts.t +++ b/t/api-customercontacts.t @@ -207,11 +207,6 @@ my @allcontacts = (); $req->remove_header('Content-Type'); $req->header('Content-Type' => 'application/json'); - # check if it fails with missing Prefer - $req->remove_header('Prefer'); - $res = $ua->request($req); - is($res->code, 400, "check put missing prefer"); - # check if it fails with invalid Prefer $req->header('Prefer' => "return=invalid"); $res = $ua->request($req); diff --git a/t/api-customers.t b/t/api-customers.t index 9ce0aee712..24bfadff7c 100644 --- a/t/api-customers.t +++ b/t/api-customers.t @@ -305,17 +305,11 @@ my @allcustomers = (); $req->remove_header('Content-Type'); $req->header('Content-Type' => 'application/json'); - # check if it fails with missing Prefer - $req->remove_header('Prefer'); - $res = $ua->request($req); - is($res->code, 400, "check put missing prefer"); - # check if it fails with invalid Prefer $req->header('Prefer' => "return=invalid"); $res = $ua->request($req); is($res->code, 400, "check put invalid prefer"); - $req->remove_header('Prefer'); $req->header('Prefer' => "return=representation"); diff --git a/t/api-resellers.t b/t/api-resellers.t index 25aafbc9d5..717a200889 100644 --- a/t/api-resellers.t +++ b/t/api-resellers.t @@ -279,7 +279,7 @@ my @allresellers = (); delete $reseller->{_links}; delete $reseller->{_embedded}; $req = HTTP::Request->new('PUT', $uri.'/'.$firstreseller); - + # check if it fails without content type $req->remove_header('Content-Type'); $req->header('Prefer' => "return=minimal"); @@ -294,11 +294,6 @@ my @allresellers = (); $req->remove_header('Content-Type'); $req->header('Content-Type' => 'application/json'); - # check if it fails with missing Prefer - $req->remove_header('Prefer'); - $res = $ua->request($req); - is($res->code, 400, "check put missing prefer"); - # check if it fails with invalid Prefer $req->header('Prefer' => "return=invalid"); $res = $ua->request($req); diff --git a/t/api-rewriterulesets.t b/t/api-rewriterulesets.t index 19b6532e1d..7147ad68a7 100644 --- a/t/api-rewriterulesets.t +++ b/t/api-rewriterulesets.t @@ -304,17 +304,11 @@ my @allrules = (); $req->remove_header('Content-Type'); $req->header('Content-Type' => 'application/json'); - # check if it fails with missing Prefer - $req->remove_header('Prefer'); - $res = $ua->request($req); - is($res->code, 400, "check put missing prefer"); - # check if it fails with invalid Prefer $req->header('Prefer' => "return=invalid"); $res = $ua->request($req); is($res->code, 400, "check put invalid prefer"); - $req->remove_header('Prefer'); $req->header('Prefer' => "return=representation"); diff --git a/t/api-systemcontacts.t b/t/api-systemcontacts.t index b944c14e49..f8e960e9af 100644 --- a/t/api-systemcontacts.t +++ b/t/api-systemcontacts.t @@ -154,7 +154,7 @@ my @allcontacts = (); delete $contact->{_embedded}; $req = HTTP::Request->new('PUT', $uri.'/'.$firstcontact); $req->header('Prefer' => 'return=minimal'); - + # check if it fails without content type $req->remove_header('Content-Type'); $res = $ua->request($req); @@ -168,17 +168,11 @@ my @allcontacts = (); $req->remove_header('Content-Type'); $req->header('Content-Type' => 'application/json'); - # check if it fails with missing Prefer - $req->remove_header('Prefer'); - $res = $ua->request($req); - is($res->code, 400, "check put missing prefer"); - # check if it fails with invalid Prefer $req->header('Prefer' => "return=invalid"); $res = $ua->request($req); is($res->code, 400, "check put invalid prefer"); - $req->remove_header('Prefer'); $req->header('Prefer' => "return=representation"); diff --git a/t/api-valid-patch.t b/t/api-valid-patch.t index f615a69466..e4ea4255d6 100644 --- a/t/api-valid-patch.t +++ b/t/api-valid-patch.t @@ -24,11 +24,6 @@ $ua->ssl_opts( { $req = HTTP::Request->new('PATCH', $uri.'/api/systemcontacts/1'); - $res = $ua->request($req); - is($res->code, 400, "check patch missing Prefer code"); - $body = JSON::from_json($res->decoded_content); - ok($body->{message} =~ /Use the 'Prefer' header/, "check patch missing Prefer response"); - $req->header('Prefer' => 'return=minimal'); $res = $ua->request($req); is($res->code, 415, "check patch missing media type"); @@ -43,7 +38,7 @@ $ua->ssl_opts( $res = $ua->request($req); is($res->code, 400, "check patch missing body"); $body = JSON::from_json($res->decoded_content); - ok($body->{message} =~ /is missing a message body/, "check patch missing body response"); + like($body->{message}, qr/is missing a message body/, "check patch missing body response"); $req->content(JSON::to_json( { foo => 'bar' }, @@ -51,7 +46,7 @@ $ua->ssl_opts( $res = $ua->request($req); is($res->code, 400, "check patch no array body"); $body = JSON::from_json($res->decoded_content); - ok($body->{message} =~ /must be an array/, "check patch missing body response"); + like($body->{message}, qr/must be an array/, "check patch missing body response"); $req->content(JSON::to_json( [{ foo => 'bar' }], @@ -59,7 +54,7 @@ $ua->ssl_opts( $res = $ua->request($req); is($res->code, 400, "check patch no op in body"); $body = JSON::from_json($res->decoded_content); - ok($body->{message} =~ /must have an 'op' field/, "check patch no op in body response"); + like($body->{message}, qr/must have an 'op' field/, "check patch no op in body response"); $req->content(JSON::to_json( [{ op => 'bar' }], @@ -67,23 +62,23 @@ $ua->ssl_opts( $res = $ua->request($req); is($res->code, 400, "check patch invalid op in body"); $body = JSON::from_json($res->decoded_content); - ok($body->{message} =~ /Invalid PATCH op /, "check patch no op in body response"); + like($body->{message}, qr/Invalid PATCH op /, "check patch no op in body response"); $req->content(JSON::to_json( - [{ op => 'test' }], + [{ op => 'replace' }], )); $res = $ua->request($req); is($res->code, 400, "check patch missing fields for op"); $body = JSON::from_json($res->decoded_content); - ok($body->{message} =~ /Missing PATCH keys /, "check patch missing fields for op response"); + like($body->{message}, qr/Missing PATCH keys /, "check patch missing fields for op response"); $req->content(JSON::to_json( - [{ op => 'test', path => '/foo', value => 'bar', invalid => 'sna' }], + [{ op => 'replace', path => '/foo', value => 'bar', invalid => 'sna' }], )); $res = $ua->request($req); is($res->code, 400, "check patch extra fields for op"); $body = JSON::from_json($res->decoded_content); - ok($body->{message} =~ /Invalid PATCH key /, "check patch extra fields for op response"); + like($body->{message}, qr/Invalid PATCH key /, "check patch extra fields for op response"); } done_testing;