From 21764a52c3e072dc846e40d2966cfe9e65687f2f Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Fri, 21 Aug 2015 16:22:03 +0200 Subject: [PATCH] MT#14741 Fix reseller ops for /api/trustedsources/ Change-Id: Ie225f3f87ca878a3bf9c70d63cc336922fa5c688 --- .../Panel/Controller/API/TrustedSources.pm | 2 +- lib/NGCP/Panel/Role/API/TrustedSources.pm | 2 +- t/api-trustedsources.t | 52 +++++++++++++++++++ t/lib/Test/Collection.pm | 15 ++++-- 4 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 t/api-trustedsources.t diff --git a/lib/NGCP/Panel/Controller/API/TrustedSources.pm b/lib/NGCP/Panel/Controller/API/TrustedSources.pm index 235683fcaf..ab06b58800 100644 --- a/lib/NGCP/Panel/Controller/API/TrustedSources.pm +++ b/lib/NGCP/Panel/Controller/API/TrustedSources.pm @@ -159,7 +159,7 @@ sub POST :Allow { ); my $sub_rs = $c->model('DB')->resultset('voip_subscribers')->search({ - id => $resource->{subscriber_id}, + 'me.id' => $resource->{subscriber_id} }); if($c->user->roles eq "reseller") { $sub_rs = $sub_rs->search({ diff --git a/lib/NGCP/Panel/Role/API/TrustedSources.pm b/lib/NGCP/Panel/Role/API/TrustedSources.pm index c49f5419dd..2ba22a942a 100644 --- a/lib/NGCP/Panel/Role/API/TrustedSources.pm +++ b/lib/NGCP/Panel/Role/API/TrustedSources.pm @@ -97,7 +97,7 @@ sub update_item { ); my $sub_rs = $c->model('DB')->resultset('voip_subscribers')->search({ - id => $resource->{subscriber_id}, + 'me.id' => $resource->{subscriber_id}, }); if($c->user->roles eq "reseller") { $sub_rs = $sub_rs->search({ diff --git a/t/api-trustedsources.t b/t/api-trustedsources.t new file mode 100644 index 0000000000..9d494e6395 --- /dev/null +++ b/t/api-trustedsources.t @@ -0,0 +1,52 @@ +#use Sipwise::Base; +use strict; +#use Moose; +use Sipwise::Base; +use Net::Domain qw(hostfqdn); +use LWP::UserAgent; +use HTTP::Request::Common; +use JSON; +use Test::More; +use Data::Dumper; +use File::Basename; +use bignum qw/hex/; + +use Test::Collection; +use Test::FakeData; + +#init test_machine +my $test_machine = Test::Collection->new( + name => 'trustedsources', + embedded => [qw/subscribers/], +); +$test_machine->methods->{collection}->{allowed} = {map {$_ => 1} qw(GET HEAD OPTIONS POST)}; +$test_machine->methods->{item}->{allowed} = {map {$_ => 1} qw(GET HEAD OPTIONS PUT PATCH DELETE)}; + +my $fake_data = Test::FakeData->new; +$fake_data->set_data_from_script({ + trustedsources => { + data => { + subscriber_id => sub { return shift->get_id('subscribers',@_); }, + protocol => 'UDP', + src_ip => '1.2.3.4', + from_pattern => '^sip:test@example.org', + }, + query => ['subscriber_id'], + }, +}); + +#for item creation test purposes /post request data/ +$test_machine->DATA_ITEM_STORE($fake_data->process('trustedsources')); + + +$test_machine->form_data_item( ); +# create 3 new field pbx devices from DATA_ITEM +$test_machine->check_create_correct( 3, sub{ $_[0]->{src_ip} = sprintf('1.2.3.%d', $_[1]->{i}); } ); +$test_machine->check_get2put( ); +$test_machine->check_bundle(); +$test_machine->clear_test_data_all(); + + +done_testing; + +# vim: set tabstop=4 expandtab: diff --git a/t/lib/Test/Collection.pm b/t/lib/Test/Collection.pm index 8cc89a4590..62da946ff9 100644 --- a/t/lib/Test/Collection.pm +++ b/t/lib/Test/Collection.pm @@ -44,7 +44,8 @@ has 'base_uri' => ( default => sub { $_[0]->{local_test} ? ( length($_[0]->{local_test})>1 ? $_[0]->{local_test} : 'https://127.0.0.1:4443' ) - : $ENV{CATALYST_SERVER} || ('https://'.hostfqdn.':4443')}, + : $ENV{CATALYST_SERVER} || ('https://'.hostfqdn.':4443'); + }, ); has 'name' => ( is => 'rw', @@ -184,7 +185,11 @@ sub init_ua { my $self = shift; my $ua = LWP::UserAgent->new; if($self->local_test){ - $ua->credentials( $self->base_uri, '', 'administrator', 'administrator' ); + my $uri = $self->base_uri; + $uri =~ s/^https?:\/\///; + my $user = $ENV{API_USER} // 'administrator'; + my $pass = $ENV{API_PASS} // 'administrator'; + $ua->credentials( $uri, 'api_admin_http', $user, $pass); $ua->ssl_opts( verify_hostname => 0, SSL_verify_mode => 0x00, @@ -380,9 +385,11 @@ sub request_delete{ } sub request_get{ my($self,$uri) = @_; + print ">>>>>>>>>>>>>>>>>>>>>> fooooo\n"; $uri ||= $self->get_uri_current; my $req = HTTP::Request->new('GET', $uri); my $res = $self->request($req); + use Data::Dumper; print Dumper $res; my $content = $res->decoded_content ? JSON::from_json($res->decoded_content) : ''; return wantarray ? ($res, $content, $req) : $res; } @@ -441,7 +448,7 @@ sub check_create_correct{ } sub clear_test_data_all{ my($self,$uri) = @_; - my @uris = $uri ? (('ARRAY' eq ref $uri) ? @$uri : ($uri)) : keys $self->DATA_CREATED->{ALL}; + my @uris = $uri ? (('ARRAY' eq ref $uri) ? @$uri : ($uri)) : keys %{ $self->DATA_CREATED->{ALL} }; foreach my $del_uri(@uris){ my($req,$res,$content) = $self->request_delete($self->base_uri.$del_uri); $self->http_code_msg(204, "check delete item $del_uri",$res,$content); @@ -767,4 +774,4 @@ sub http_code_msg{ } $code and is($res->code, $code, $message_res); } -1; \ No newline at end of file +1;