From f3cfa0ec87bde8c560060a40930dbd439c8bcc8d Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Tue, 15 Sep 2015 12:07:07 +0200 Subject: [PATCH] MT#14803 option to access cfdestinationsets via reseller and cftimesets Change-Id: I740c508589ee0cd4c0405e1b8f84cda30fe41947 --- lib/NGCP/Panel/Role/API/CFDestinationSets.pm | 7 ++ lib/NGCP/Panel/Role/API/CFTimeSets.pm | 7 ++ t/api-rest/api-cfdestinationsets.t | 101 +++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 t/api-rest/api-cfdestinationsets.t diff --git a/lib/NGCP/Panel/Role/API/CFDestinationSets.pm b/lib/NGCP/Panel/Role/API/CFDestinationSets.pm index cb574a16e3..5313825408 100644 --- a/lib/NGCP/Panel/Role/API/CFDestinationSets.pm +++ b/lib/NGCP/Panel/Role/API/CFDestinationSets.pm @@ -82,6 +82,13 @@ sub item_rs { if($c->user->roles eq "admin") { $item_rs = $c->model('DB')->resultset('voip_cf_destination_sets'); } elsif ($c->user->roles eq "reseller") { + my $reseller_id = $c->user->reseller_id; + $item_rs = $c->model('DB')->resultset('voip_cf_destination_sets') + ->search_rs({ + 'reseller_id' => $reseller_id, + } , { + join => {'subscriber' => {'contract' => 'contact'} }, + }); } return $item_rs; diff --git a/lib/NGCP/Panel/Role/API/CFTimeSets.pm b/lib/NGCP/Panel/Role/API/CFTimeSets.pm index b9a264189e..93892abf0a 100644 --- a/lib/NGCP/Panel/Role/API/CFTimeSets.pm +++ b/lib/NGCP/Panel/Role/API/CFTimeSets.pm @@ -73,6 +73,13 @@ sub item_rs { if($c->user->roles eq "admin") { $item_rs = $c->model('DB')->resultset('voip_cf_time_sets'); } elsif ($c->user->roles eq "reseller") { + my $reseller_id = $c->user->reseller_id; + $item_rs = $c->model('DB')->resultset('voip_cf_time_sets') + ->search_rs({ + 'reseller_id' => $reseller_id, + } , { + join => {'subscriber' => {'contract' => 'contact'} }, + }); } return $item_rs; diff --git a/t/api-rest/api-cfdestinationsets.t b/t/api-rest/api-cfdestinationsets.t new file mode 100644 index 0000000000..1ce79a8e15 --- /dev/null +++ b/t/api-rest/api-cfdestinationsets.t @@ -0,0 +1,101 @@ +use Sipwise::Base; +use Net::Domain qw(hostfqdn); +use LWP::UserAgent; +use JSON qw(); +use Test::More; + +my $uri = $ENV{CATALYST_SERVER} || ('https://'.hostfqdn.':4443'); +my ($netloc) = ($uri =~ m!^https?://(.*)/?.*$!); + +my ($ua, $req, $res); +$ua = LWP::UserAgent->new; + +$ua->ssl_opts( + verify_hostname => 0, + SSL_verify_mode => 0, + ); +my $user = $ENV{API_USER} // 'administrator'; +my $pass = $ENV{API_PASS} // 'administrator'; +$ua->credentials($netloc, "api_admin_http", $user, $pass); + +diag('Note that the next tests require at least one subscriber to be present ' . + 'and accessible to the current API user.'); + +# fetch a cfdestinationset for testing that +{ + $req = HTTP::Request->new('GET', $uri.'/api/cfdestinationsets/?page=1&rows=10'); + $res = $ua->request($req); + is($res->code, 200, "fetch cfdestinationsets collection"); + + $req = HTTP::Request->new('GET', $uri.'/api/cftimesets/?page=1&rows=10'); + $res = $ua->request($req); + is($res->code, 200, "fetch cftimesets collection"); +} + +# fetch a cfdestinationset being a reseller +SKIP: +{ + my $user_reseller = $ENV{API_USER_RESELLER} // 'api_test'; + my $pass_reseller = $ENV{API_PASS_RESELLER} // 'api_test'; + $ua->credentials($netloc, "api_admin_http", $user_reseller, $pass_reseller); + $req = HTTP::Request->new('GET', $uri.'/api/cfdestinationsets/?page=1&rows=10'); + $res = $ua->request($req); + if ($res->code == 401) { # Authorization required + skip("Couldn't login as reseller", 1); + } + is($res->code, 200, "fetch cfdestinationsets collection as reseller"); + my $cf_collection1 = JSON::from_json($res->decoded_content); + + $req = HTTP::Request->new('GET', $uri.'/api/cftimesets/?page=1&rows=10'); + $res = $ua->request($req); + is($res->code, 200, "fetch cftimesets collection as reseller"); + my $cft_collection1 = JSON::from_json($res->decoded_content); + + $req = HTTP::Request->new('GET', $uri.'/api/subscribers/?page=1&rows=1'); + $res = $ua->request($req); + is($res->code, 200, "fetch a subscriber of our reseller for testing"); + my $sub1 = JSON::from_json($res->decoded_content); + 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]*)$!; + cmp_ok ($sub1_id, '>', 0, "should be positive integer"); + + $req = HTTP::Request->new('GET', $uri.'/api/cfdestinationsets/?page=1&rows=10&subscriber_id='.$sub1_id); + $res = $ua->request($req); + is($res->code, 200, "fetch cfdestinationsets collection as reseller with subscriber filter"); + + my $cf_collection2 = JSON::from_json($res->decoded_content); + cmp_ok($cf_collection1->{total_count}, '>=', $cf_collection2->{total_count}, + "filtered collection (cfdestinationsets) should be smaller or equal"); + + # -------- + + $req = HTTP::Request->new('GET', $uri.'/api/cftimesets/?page=1&rows=10&subscriber_id='.$sub1_id); + $res = $ua->request($req); + is($res->code, 200, "fetch cftimesets collection as reseller with subscriber filter"); + + my $cft_collection2 = JSON::from_json($res->decoded_content); + cmp_ok($cft_collection1->{total_count}, '>=', $cft_collection2->{total_count}, + "filtered collection (cftimesets) should be smaller or equal"); +} + +{ + $ua->credentials($netloc, "api_admin_http", $user, $pass); + + $req = HTTP::Request->new('GET', "$uri/api/callforwards/99987"); + $res = $ua->request($req); + is($res->code, 404, "check get nonexistent callforwards item"); + + $req = HTTP::Request->new('GET', "$uri/api/cfdestinationsets/99987"); + $res = $ua->request($req); + is($res->code, 404, "check get nonexistent cfdestinationsets item"); + + $req = HTTP::Request->new('GET', "$uri/api/cftimesets/99987"); + $res = $ua->request($req); + is($res->code, 404, "check get nonexistent cftimesets item"); +} + +done_testing; + +# vim: set tabstop=4 expandtab: