From 052e7763174def8efc01aa3e43e4f2774a820979 Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Fri, 24 Mar 2017 22:20:57 +0100 Subject: [PATCH] TT#7453 Fix fixing removal of r/o fields and tests It's fine to remove read-only fields only if $run==1, but in any case remove unknown fields. Also the force-array change broke additional tests, fixed them. Change-Id: I48ffcf201bd1eedb8fb317f1ca248af9e4a01e60 --- lib/NGCP/Panel/Role/API.pm | 7 ++++++- t/api-rest/api-calllists.t | 6 +++--- t/api-rest/api-calls.t | 2 +- t/api-rest/api-cfdestinationsets.t | 2 +- t/api-rest/api-customers.t | 6 +++++- t/lib/Test/Collection.pm | 8 +++++++- 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/NGCP/Panel/Role/API.pm b/lib/NGCP/Panel/Role/API.pm index da079d7a2b..e754c36594 100644 --- a/lib/NGCP/Panel/Role/API.pm +++ b/lib/NGCP/Panel/Role/API.pm @@ -232,13 +232,18 @@ sub validate_fields { if($resource->{$k}->$_isa('JSON::PP::Boolean')) { $resource->{$k} = $resource->{$k} ? 1 : 0; } + unless(exists $fields->{$k}) { + delete $resource->{$k}; + next; + } if($run){ #Prepare resource for the PATCH considering readonly fields. #PATCH is supposed to take full item content and so will get readonly fields into resource too. And apply patch. #It leads to the situation when we may try to change some not existing fields in the DB #All readonly fields are considered as representation only and should never be applied. - if( (!exists $fields->{$k}) || $fields->{$k}->readonly) { + if($fields->{$k}->readonly) { delete $resource->{$k}; + next; } } $resource->{$k} = DateTime::Format::RFC3339->format_datetime($resource->{$k}) diff --git a/t/api-rest/api-calllists.t b/t/api-rest/api-calllists.t index 923cca17a6..109d8c149d 100644 --- a/t/api-rest/api-calllists.t +++ b/t/api-rest/api-calllists.t @@ -23,7 +23,7 @@ SKIP: if ($sub1->{total_count} < 1) { skip("Precondition not met: need a subscriber",1); } - ($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->{_links}{self}{href} =~ m!subscribers/([0-9]*)$!; + ($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->[0]->{_links}->{self}{href} =~ m!subscribers/([0-9]*)$!; } cmp_ok ($sub1_id, '>', 0, "should be positive integer"); #---- @@ -71,7 +71,7 @@ SKIP: if ($cust1->{total_count} < 1) { skip("Precondition not met: need a customer",1); } - ($cust1_id) = $cust1->{_embedded}->{'ngcp:customers'}->{_links}{self}{href} =~ m!customers/([0-9]*)$!; + ($cust1_id) = $cust1->{_embedded}->{'ngcp:customers'}->[0]->{_links}{self}{href} =~ m!customers/([0-9]*)$!; } cmp_ok ($cust1_id, '>', 0, "should be positive integer"); @@ -95,7 +95,7 @@ SKIP: if ($sub1->{total_count} < 1) { skip("Precondition not met: need a subscriber",1); } - ($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->{_links}{self}{href} =~ m!subscribers/([0-9]*)$!; + ($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->[0]->{_links}{self}{href} =~ m!subscribers/([0-9]*)$!; #$sub1_user = $sub1->{_embedded}->{'ngcp:subscribers'}->{'username'}; #$sub1_pass = $sub1->{_embedded}->{'ngcp:subscribers'}->{'webpassword'} // ''; diff --git a/t/api-rest/api-calls.t b/t/api-rest/api-calls.t index e3515aa643..b39d17797a 100644 --- a/t/api-rest/api-calls.t +++ b/t/api-rest/api-calls.t @@ -18,7 +18,7 @@ SKIP: if ($sub1->{total_count} < 1) { skip("Precondition not met: need a subscriber",1); } - ($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->{_links}{self}{href} =~ m!subscribers/([0-9]*)$!; + ($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->[0]->{_links}{self}{href} =~ m!subscribers/([0-9]*)$!; cmp_ok ($sub1_id, '>', 0, "should be positive integer"); diff --git a/t/api-rest/api-cfdestinationsets.t b/t/api-rest/api-cfdestinationsets.t index 295587c1da..756da5aabd 100644 --- a/t/api-rest/api-cfdestinationsets.t +++ b/t/api-rest/api-cfdestinationsets.t @@ -74,7 +74,7 @@ SKIP: if ($sub1->{total_count} < 1) { skip("Precondition not met: need a subscriber",1); } - my ($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->{_links}{self}{href} =~ m!subscribers/([0-9]*)$!; + my ($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->[0]->{_links}{self}{href} =~ m!subscribers/([0-9]*)$!; cmp_ok ($sub1_id, '>', 0, "should be positive integer"); diff --git a/t/api-rest/api-customers.t b/t/api-rest/api-customers.t index f1ae1600a7..65c6622e88 100644 --- a/t/api-rest/api-customers.t +++ b/t/api-rest/api-customers.t @@ -54,7 +54,7 @@ $req = HTTP::Request->new('GET', $uri.'/api/systemcontacts/?page=1&rows=1'); $res = $ua->request($req); is($res->code, 200, "fetch system contacts"); my $sysct = JSON::from_json($res->decoded_content); -my $system_contact_id = $sysct->{_embedded}->{'ngcp:systemcontacts'}->{id}; +my $system_contact_id = $sysct->{_embedded}->{'ngcp:systemcontacts'}->[0]->{id}; # first, create a contact $req = HTTP::Request->new('POST', $uri.'/api/customercontacts/'); @@ -325,7 +325,11 @@ my @allcustomers = (); is($res->code, 200, "check put successful"); my $new_customer = JSON::from_json($res->decoded_content); + my $old_modify = delete $old_customer->{modify_timestamp}; + my $new_modify = delete $new_customer->{modify_timestamp}; is_deeply($old_customer, $new_customer, "check put if unmodified put returns the same"); + $old_customer->{modify_timestamp} = $old_modify; + $new_customer->{modify_timestamp} = $new_modify; # check if we have the proper links ok(exists $new_customer->{_links}->{'ngcp:customercontacts'}, "check put presence of ngcp:customercontacts relation"); diff --git a/t/lib/Test/Collection.pm b/t/lib/Test/Collection.pm index fc281ec1dd..17ed2ee4b6 100644 --- a/t/lib/Test/Collection.pm +++ b/t/lib/Test/Collection.pm @@ -843,7 +843,13 @@ sub check_embedded { my($self, $embedded, $check_embedded_cb) = @_; defined $check_embedded_cb and $check_embedded_cb->($embedded); foreach my $embedded_name(@{$self->embedded_resources}){ - ok(exists $embedded->{_links}->{'ngcp:'.$embedded_name}, "$self->{name}: check presence of ngcp:$embedded_name relation"); + if(ref $embedded eq "ARRAY") { + foreach my $emb(@{ $embedded }) { + ok(exists $emb->{_links}->{'ngcp:'.$embedded_name}, "$self->{name}: check presence of ngcp:$embedded_name relation"); + } + } else { + ok(exists $embedded->{_links}->{'ngcp:'.$embedded_name}, "$self->{name}: check presence of ngcp:$embedded_name relation"); + } } }