MT#5255 Fix boolean handling and HFH validation

agranig/rest
Andreas Granig 12 years ago
parent 5ba6edcadc
commit cd2d9e47aa

@ -145,6 +145,7 @@ sub POST :Allow {
}
my $form = NGCP::Panel::Form::BillingProfile::Admin->new;
$resource->{reseller_id} //= undef;
last unless $self->validate_form(
c => $c,
resource => $resource,

@ -106,7 +106,8 @@ sub PATCH :Allow {
last unless $resource;
my $form = NGCP::Panel::Form::BillingProfile::Admin->new;
last unless $self->update_profile($c, $profile, $old_resource, $resource, $form);
$profile = $self->update_profile($c, $profile, $old_resource, $resource, $form);
last unless $profile;
$guard->commit;
@ -145,7 +146,10 @@ sub PUT :Allow {
my $old_resource = { $profile->get_inflated_columns };
my $form = NGCP::Panel::Form::BillingProfile::Admin->new;
last unless $self->update_profile($c, $profile, $old_resource, $resource, $form);
use Data::Printer; p $profile;
$profile = $self->update_profile($c, $profile, $old_resource, $resource, $form);
use Data::Printer; p $profile;
last unless $profile;
$guard->commit;

@ -145,6 +145,7 @@ sub POST :Allow {
last unless $resource;
my $product_class = delete $resource->{type};
$resource->{contact_id} //= undef;
my $form = NGCP::Panel::Form::Contract::PeeringReseller->new;
last unless $self->validate_form(
c => $c,

@ -108,7 +108,8 @@ sub PATCH :Allow {
last unless $resource;
my $form = NGCP::Panel::Form::Contract::PeeringReseller->new;
last unless $self->update_contract($c, $contract, $old_resource, $resource, $form);
$contract = $self->update_contract($c, $contract, $old_resource, $resource, $form);
last unless $contract;
$guard->commit;
@ -147,7 +148,8 @@ sub PUT :Allow {
my $old_resource = { $contract->get_inflated_columns };
my $form = NGCP::Panel::Form::Contract::PeeringReseller->new;
last unless $self->update_contract($c, $contract, $old_resource, $resource, $form);
$contract = $self->update_contract($c, $contract, $old_resource, $resource, $form);
last unless $contract;
$guard->commit;

@ -101,20 +101,26 @@ sub validate_form {
$form->field($k)->$_isa('HTML::FormHandler::Field::Integer') ||
$form->field($k)->$_isa('HTML::FormHandler::Field::Money') ||
$form->field($k)->$_isa('HTML::FormHandler::Field::Float')));
$resource->{$k} = JSON::Types::bool($resource->{$k})
if(defined $resource->{$k} &&
$form->field($k)->$_isa('HTML::FormHandler::Field::Boolean'));
# only do this for converting back from obj to hal
# otherwise it breaks db fields with the \0 and \1 notation
unless($run) {
$resource->{$k} = JSON::Types::bool($resource->{$k})
if(defined $resource->{$k} &&
$form->field($k)->$_isa('HTML::FormHandler::Field::Boolean'));
}
}
if($run) {
# check keys/vals
my $result = $form->run(params => $resource);
use Data::Printer; p $result; p $result->error_results;
if ($result->error_results->size) {
my $f = $result->error_results->[0];
my $e = $result->error_results->map(sub {
sprintf 'field=\'%s\', input=\'%s\', errors=\'%s\'', ($_->parent->$_isa('HTML::FormHandler::Field::Result') ? $_->parent->name . '_' : '') . $_->name, $_->input // '', $_->errors->join(q())
})->join("; ");
$form->process(params => $resource, posted => 1);
unless($form->validated) {
my $e = join '; ', map {
sprintf 'field=\'%s\', input=\'%s\', errors=\'%s\'',
($_->parent->$_isa('HTML::FormHandler::Field') ? $_->parent->name . '_' : '') . $_->name,
$_->input // '',
$_->errors->join(q())
} $form->error_fields;
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Validation failed. $e");
return;
}

@ -71,6 +71,8 @@ sub update_profile {
my ($self, $c, $profile, $old_resource, $resource, $form) = @_;
$form //= NGCP::Panel::Form::BillingProfile::Admin->new;
# TODO: for some reason, formhandler lets missing reseller slip thru
$resource->{reseller_id} //= undef;
return unless $self->validate_form(
c => $c,
form => $form,

@ -102,6 +102,8 @@ sub update_contract {
$resource->{billing_profile_id} //= $old_resource->{billing_profile_id};
$form //= NGCP::Panel::Form::Contract::PeeringReseller->new;
# TODO: for some reason, formhandler lets missing contact_id slip thru
$resource->{contact_id} //= undef;
return unless $self->validate_form(
c => $c,
form => $form,

@ -49,8 +49,8 @@ my @allprofiles = ();
$req->header('Content-Type' => 'application/json');
$req->content(JSON::to_json({
reseller_id => $reseller_id,
handle => "testapihandle$i",
name => "test api name $i",
handle => "testapihandle$i".time,
name => "test api name $i".time,
}));
$res = $ua->request($req);
ok($res->code == 201, "create test billing profile $i");
@ -274,14 +274,14 @@ my @allprofiles = ();
$req = HTTP::Request->new('PATCH', $uri.'/'.$firstprofile);
$req->header('Prefer' => 'return=representation');
$req->header('Content-Type' => 'application/json-patch+json');
my $t = time;
$req->content(JSON::to_json(
[ { op => 'replace', path => '/name', value => 'patched name' } ]
[ { op => 'replace', path => '/name', value => 'patched name '.$t } ]
));
$res = $ua->request($req);
ok($res->code == 200, "check patched profile item");
my $mod_profile = JSON::from_json($res->decoded_content);
ok($mod_profile->{name} eq "patched name", "check patched replace op");
ok($mod_profile->{name} eq "patched name $t", "check patched replace op");
ok($mod_profile->{_links}->{self}->{href} eq $firstprofile, "check patched self link");
ok($mod_profile->{_links}->{collection}->{href} eq '/api/billingprofiles/', "check patched collection link");

Loading…
Cancel
Save