TT#28508 Fix tests

Change-Id: Id4bf415aa4e5e3f59e559e528427db0c5614f1cc
changes/94/18194/20
Irina Peshinskaya 8 years ago
parent 7d4518010a
commit 3db78adb2d

@ -13,19 +13,28 @@ my($opt,$report,$api_config,$api_info,$config,$test_machine,$fake_data) =
({},{},{},{} );
$opt = {
'collections' => {'callcontrols' => 1,},
'collections' => {'sms' => 1,},
'collections' => {},
'ignore_existence' => 1,
'test_groups' => { post => 1, },#get2put,get2patch,bundle
'test_groups' => {
get2put => 1,
#get2patch => 1,
post =>1,
get =>1,
bundle =>1
},#post,get2put,get2patch,bundle
#'test_groups' => { post => 1 },#post,get2put,get2patch,bundle
};
$test_machine = Test::Collection->new(
name => '',
ALLOW_EMPTY_COLLECTION => 1,
QUIET_DELETION => 0,
runas_role => 'reseller',
runas_role => 'admin',
);
$fake_data = Test::FakeData->new;
$fake_data->test_machine->QUIET_DELETION(0);
$fake_data->test_machine->KEEP_CREATED(0);
$test_machine->clear_cache;
get_opt();
@ -36,10 +45,11 @@ init_report();
run();
$test_machine->clear_test_data_all();
done_testing;
undef $fake_data;
undef $test_machine;
done_testing;
print Dumper $report;
@ -48,8 +58,7 @@ print Dumper $report;
sub run{
#my($opt,$report,$api_info,$test_machine,$fake_data,$config)
foreach my $collection ( sort grep { collection4testing($_) } keys %{$api_info} ){
foreach my $collection ( sort grep { collection4testing($_) ? 1 : () } keys %{$api_info} ){
$test_machine->name($collection);
$test_machine->NO_ITEM_MODULE($api_info->{$collection}->{module_item} ? 0 : 1 );
$test_machine->methods({
@ -57,51 +66,63 @@ sub run{
item => { allowed => { map { $_ => 1 } keys %{$api_info->{$collection}->{allowed_methods_item}} }},
});
if($opt->{test_groups}->{bundle}){
$test_machine->check_bundle();
if(!$fake_data->{data}->{$collection}->{data}){
testscript_under_framework($collection);
}
print "collection: $collection;\n";
if($opt->{test_groups}->{post}){
if($test_machine->{methods}->{collection}->{allowed}->{POST}){
print "collection: $collection: post;\n";
#load date
if(!$fake_data->{data}->{$collection}->{data}){
testscript_under_framework($collection);
}
if($fake_data->{data}->{$collection}->{data}){
my $data = $fake_data->{data}->{$collection}->{data};
#$data->{json} && ( $data = $data->{json});
#TODO: ALL packages should have information about content_typr
if($data->{json} && ( $data = $data->{json})){
$test_machine->content_type->{POST} = 'multipart/form-data';
$fake_data->process($collection);
if($fake_data->{data}->{$collection}->{create_special}){
$fake_data->{data}->{$collection}->{create_special}->($fake_data,$collection,$test_machine);
}else{
$test_machine->content_type->{POST} = 'application/json';
$test_machine->check_create_correct(
1,
$fake_data->{data}->{$collection}->{data_callbacks}->{uniquizer_cb},
clone $fake_data->{data}->{$collection}->{data}
);
}
$fake_data->process($collection);
$test_machine->check_create_correct(
1,
$fake_data->{data}->{$collection}->{uniquizer_cb},
clone $fake_data->{data}->{$collection}->{data}
);
push @{$report->{post_tested}}, $collection;
}else{
push @{$report->{post_untested}}, $collection;
}
}
}
if($opt->{test_groups}->{bundle}){
@{$test_machine->content_type}{qw/POST PUT/} = (
$api_info->{$collection}->{allowed_methods}->{POST}->{ContentType}->[0] || 'application/json',
$api_info->{$collection}->{allowed_methods_item}->{PUT}->{ContentType}>[0] || 'application/json',
);
$test_machine->check_bundle();
}
if($opt->{test_groups}->{get}){
if($test_machine->{methods}->{collection}->{allowed}->{GET}){
my $item = $test_machine->get_item_hal( undef, undef, 1);#reload
}
}
if($opt->{test_groups}->{get2put}){
print "collection: $collection: get2put;\n";
#$test_machine->DATA_ITEM_STORE($fake_data->process($collection));
if($test_machine->{methods}->{collection}->{allowed}->{GET}){
my $item = $test_machine->get_item_hal( undef, undef, 1);#reload
if(!$test_machine->IS_EMPTY_COLLECTION){
if($test_machine->{methods}->{item}->{allowed}->{PUT}){
$test_machine->check_get2put();
my $ignore_fields_parameter = $fake_data->{data}->{$collection}->{update_change_fields};
$ignore_fields_parameter and $ignore_fields_parameter = { ignore_fields => $ignore_fields_parameter };
$test_machine->check_get2put( {
data_cb => $fake_data->{data}->{$collection}->{data_callbacks}->{get2put},
}, {
uri => $item->{location} ,
}, $ignore_fields_parameter );
}
}
}
}
$test_machine->clear_data_created;
if(!$test_machine->{methods}->{collection}->{allowed}->{GET}){
push @{$report->{'collections_no_get'}}, $collection;
}
@ -150,27 +171,37 @@ sub init_config{#init config
'domainpreferencedefs' => 1,
'peeringserverpreferencedefs' => 1,
'profilepreferencedefs' => 1,
'pbxdevicepreferencedefs' => 1,
'pbxdeviceprofilepreferencedefs' => 1,
'subscriberpreferences' => 1,
'customerpreferences' => 1,
'domainpreferences' => 1,
'peeringserverpreferences' => 1,
'profilepreferences' => 1,
'pbxdevicepreferencedefs' => 1,
'pbxdeviceprofilepreferencedefs' => 1,
'pbxdevicepreferences' => 1,
'pbxdeviceprofilepreferences' => 1,
#defs and preferences are tested in context of preferences
'pbxdevicefirmwares' => 1, #too hard, fails with timeout on get
#falis with: not ok 163 - ccmapentries: check_get2put: check put successful (Unprocessable Entity: Validation failed. field='mappings', input='ARRAY(0x1a53f278)', errors='Mappings field is required')
'ccmapentries' => 1,
#'ccmapentries' => 1,
#fails with:
# got: 'https://127.0.0.1:1443/api/customerzonecosts/?page=1&rows=5&start=2016-10-01T000000&end=2016-10-31T235959'
# expected: 'https://127.0.0.1:1443/api/customerzonecosts/?page=1&rows=5'
'customerzonecosts' => 1,
#'customerzonecosts' => 1,
#fails with: Unsupported media type 'application/json', accepting text/plain or text/xml only.)
'pbxdeviceconfigs' => 1,
#'pbxdeviceconfigs' => 1,
#fails with: not ok 1131 - rtcapps: check_get2put: check put successful (Unprocessable Entity: Invalid field 'apps'. Must be an array.)
'rtcapps' => 1,
#'rtcapps' => 1,
#fails with: not ok 1176 - rtcnetworks: check_get2put: check put successful (Unprocessable Entity: Invalid field 'networks'. Must be an array.)
'rtcnetworks' => 1,
#'rtcnetworks' => 1,
#--------- interceptions:
# No intercept agents configured in ngcp_panel.conf, rejecting request
#--------- callcontrols:
#Jan 3 12:48:20 sp1 ngcp-panel: INFO: received from dispatcher: $VAR1 = [#012 2,#012 1,#012 '<?xml version="1.0"?>#015#012<methodResponse><fault>#015#012#011<value><struct><member><name>faultCode</name><value><i4>-1</i4></value></member><member><name>faultString</name><value>dial_auth_b2b: unknown method name</value></member></struct></value>#015#012</fault></methodResponse>#015#012'#012];
#Jan 3 12:48:20 sp1 ngcp-panel: ERROR: failed to dial out: failed to trigger dial-out at /media/sf_/VMHost/ngcp-panel/lib/NGCP/Panel/Utils/Sems.pm line 326, <$fh> line 1.
#Jan 3 12:48:20 sp1 ngcp-panel: ERROR: error 500 - Failed to create call.
);
my %test_exists;
{
@ -205,20 +236,43 @@ sub testscript_under_framework{
}
sub collection4testing{
my $collection = shift;
my $r = (! ( scalar keys %{$opt->{collections}} ) ) || $opt->{collections}->{$collection};
if($r){
#by default we run requested collection scripts even when exists
if(!$opt->{collections}->{$collection}){
if(testscript_exists($collection) && !$opt->{ignore_existence}){
my ($collection,$method) = @_;
$method //= '';
#check if collection is among directly requested. If no particular collections were requested, then all are requested
my $run_collection_test = (! ( scalar keys %{$opt->{collections}} ) ) || $opt->{collections}->{$collection};
if($run_collection_test){
#we run requested collection scripts even when exists or is excluded. So Now check for not requested
if(! $opt->{collections}->{$collection}){
if( $config->{tests_exclude}->{$collection} ){
if($method && $method ne 'all'){
if ((
'HASH' eq ref $config->{tests_exclude}->{$collection}
&& $config->{tests_exclude}->{$collection}->{$method}
)
||
(
'ARRAY' eq ref $config->{tests_exclude}->{$collection}
&& grep { $method eq $_} @{$config->{tests_exclude}->{$collection}}
)){
#method excluded
$run_collection_test = 0;
} else {
$run_collection_test = 1;
}
} elsif (! ref $config->{tests_exclude}->{$collection}) {
$run_collection_test = 0 ;
} else {
$run_collection_test = 1;
}
}elsif(testscript_exists($collection) && !$opt->{ignore_existence}){
push @{$report->{'tests_exists_skipped'}}, $collection;
#we will not test the same twice
$r = 0;
$run_collection_test = 0;
}
$r = 0 if $config->{test_exclude}->{$collection};
}
}
return $r;
return $run_collection_test;
}
sub init_report{

@ -22,7 +22,9 @@ $fake_data->set_data_from_script({
detail => "api_test zone",
},
'query' => ['zone','billing_profile_id'],
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{zone}); },
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{zone}); },
}
},
});

@ -5,10 +5,25 @@ use Test::More;
use Test::Collection;
use Data::Dumper;
use Test::ForceArray qw/:all/;
use Test::FakeData;
my $test_machine = Test::Collection->new(
name => 'calllists',
);
my $fake_data = Test::FakeData->new;
$fake_data->set_data_from_script({
'calllists' => {
'data' => {
},
'mandatory_query_parameters' => {
'or' => {
subscriber_id => { 'subscribers' => 'id' },
customer_id => { 'customers' => 'id' },
}
},
},
});
diag('Note that the next tests require at least one subscriber to be present');

@ -22,7 +22,9 @@ $fake_data->set_data_from_script({
reseller_id => sub { return shift->get_id('resellers',@_); },
},
'query' => ['domain'],
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{domain}); },
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{domain}); },
},
},
});

@ -21,7 +21,9 @@ $fake_data->set_data_from_script({
name => "apitest",
},
'query' => ['name','reseller_id'],
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
},
},
});

@ -27,14 +27,7 @@ $fake_data->set_data_from_script({
},
file => [ (tempfile())[1] ],
},
'create_special'=> sub {
my ($self,$name,$test_machine) = @_;
my $prev_params = $test_machine->get_cloned('content_type');
@{$test_machine->content_type}{qw/POST PUT/} = (('multipart/form-data') x 2);
$test_machine->check_create_correct(1);
$test_machine->set(%$prev_params);
},
'query' => ['code','emergency_container_id'],
'query' => [['code','json','code'],['emergency_container_id','json','emergency_container_id']],
},
});
my $data = $fake_data->process('emergencymappings');
@ -55,7 +48,8 @@ $test_machine->DATA_ITEM_STORE($data);
$test_machine->DATA_ITEM($data);
{
my $empty_container_data = clone $test_machine->DATA_ITEM;
delete $empty_container_data->{json}->{emergency_container_id};
$empty_container_data = delete $empty_container_data->{json};
delete $empty_container_data->{emergency_container_id};
my($res,$content) = $test_machine->request_post($empty_container_data);
$test_machine->http_code_msg(422, "Emergency Mapping Container field is required", $res, $content);
}

@ -21,23 +21,26 @@ $fake_data->set_data_from_script({
},
'create_special'=> sub {
my ($self,$name,$test_machine) = @_;
my $prev_params = $test_machine->get_cloned('content_type');
@{$test_machine->content_type}{qw/POST PUT/} = (('multipart/form-data') x 2);
$test_machine->check_create_correct(1);
$test_machine->set(%$prev_params);
my $data = $self->data->{faxes}->{data};
set_faxes_preferences($data->{json}->{subscriber_id});
$test_machine->resource_fill_file($data->{faxfile}->[0]);
$test_machine->check_create_correct( 1, sub { $_[0] = clone $data; } );
$test_machine->resource_clear_file($data->{faxfile}->[0]);
},
'no_delete_available' => 1,
},
});
my $test_machine = Test::Collection->new(
name => 'faxes',
embedded_resources => [qw/subscribers/]
embedded_resources => [qw/subscribers/],
ALLOW_EMPTY_COLLECTION => 1,
);
my $remote_config = $test_machine->init_catalyst_config;
if( !$remote_config->{config}->{features}->{faxserver} ){
$remote_config->{config}->{features}->{faxserver} //= 0;
is($remote_config->{config}->{features}->{faxserver},0,"axserver feature isn't enabled");
is($remote_config->{config}->{features}->{faxserver}, 0, "faxserver feature isn't enabled");
}else{
@{$test_machine->content_type}{qw/POST PUT/} = (('multipart/form-data') x 2);
@ -48,14 +51,7 @@ if( !$remote_config->{config}->{features}->{faxserver} ){
$test_machine->form_data_item();
{
my $test_machine_aux = Test::Collection->new(name => 'faxserversettings');
my $uri = $test_machine_aux->get_uri($test_machine->DATA_ITEM->{json}->{subscriber_id});
my($res,$faxserversettings,$req) = $test_machine_aux->check_item_get($uri);
$faxserversettings->{active} = 1;
$faxserversettings->{password} = 'aaa111';
$test_machine_aux->request_put($faxserversettings,$uri);
}
set_faxes_preferences($test_machine->DATA_ITEM->{json}->{subscriber_id});
$test_machine->resource_fill_file($test_machine->DATA_ITEM->{faxfile}->[0]);
$test_machine->check_create_correct( 1 );
@ -67,13 +63,22 @@ if( !$remote_config->{config}->{features}->{faxserver} ){
$test_machine->check_create_correct( 1 );
}
#$test_machine->check_bundle();
#$test_machine->check_get2put( sub { $_[0] = { json => JSON::to_json($_[0]), 'faxfile' => $test_machine->DATA_ITEM_STORE->{faxfile} }; } );
$test_machine->check_bundle();
$test_machine->clear_test_data_all();
done_testing;
undef $fake_data;
undef $test_machine;
done_testing;
#---------------------- aux
sub set_faxes_preferences{
my($subscriber_id) = @_;
my $test_machine_aux = Test::Collection->new(name => 'faxserversettings');
my $uri = $test_machine_aux->get_uri($subscriber_id);
my($res,$faxserversettings,$req) = $test_machine_aux->check_item_get($uri);
$faxserversettings->{active} = 1;
$faxserversettings->{password} = 'aaa111';
$test_machine_aux->request_put($faxserversettings,$uri);
}
# vim: set tabstop=4 expandtab:

@ -23,7 +23,9 @@ $fake_data->set_data_from_script({
skip_rewrite => "0",
},
'query' => ['name'],
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
},
},
});

@ -29,14 +29,6 @@ $fake_data->set_data_from_script({
},
file => [ (tempfile())[1] ],
},
'create_special'=> sub {
my ($self,$name,$test_machine) = @_;
my $prev_params = $test_machine->get_cloned('content_type');
@{$test_machine->content_type}{qw/POST PUT/} = (('multipart/form-data') x 2);
$test_machine->check_create_correct(1);
$test_machine->set(%$prev_params);
},
'query' => ['code','emergency_container_id'],
},
});
my $data = $fake_data->process('lnpnumbers');
@ -45,7 +37,6 @@ $test_machine->DATA_ITEM_STORE($data);
{#test "usual" interface
$test_machine->DATA_ITEM($data->{json});
# create 3 new emergency mappings from DATA_ITEM
$test_machine->check_create_correct( 3, sub{ $_[0]->{number} .= $_[1]->{i}; } );
$test_machine->check_get2put();
$test_machine->check_bundle();

@ -24,6 +24,9 @@ $fake_data->set_data_from_script({
local_ac => '1',
intra_pbx => '1',
},
'data_callbacks' => {
'uniquizer_cb' => sub{ $_[0]->{level} .= $_[1]->{i}; },
},
},
});

@ -70,14 +70,12 @@ $fake_data->set_data_from_script({
front_image => [ dirname($0).'/resources/empty.txt' ],
},
'query' => [ ['model','json','model'] ],
'create_special'=> sub {
my ($self,$collection_name,$test_machine) = @_;
my $prev_params = $test_machine->get_cloned('content_type');
@{$test_machine->content_type}{qw/POST PUT/} = (('multipart/form-data') x 2);
$test_machine->check_create_correct(1);
$test_machine->set(%$prev_params);
},
'no_delete_available' => 1,
'data_callbacks' => {
'get2put' => $fake_data->get2put_upload_callback('pbxdevicemodels'),
#'get2put' => Test::FakeData::get2put_upload_callback(),
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{json}->{model}); },
}
},
});
my $test_machine = Test::Collection->new(
@ -114,12 +112,7 @@ foreach my $type(qw/extension phone/){
$_[0]->{json}->{connactable_devices} = $connactable_devices->{ get_connectable_type( $type) }->{ids};
} );
$test_machine->check_get2put( {
'data_cb' => sub {
$_[0] = {
'json' => JSON::to_json($_[0]),
'front_image' => $test_machine->DATA_ITEM_STORE->{front_image}
};
}
'data_cb' => $fake_data->{data}->{'pbxdevicemodels'}->{data_callbacks}->{get2put},
} );
$test_machine->check_bundle();

@ -23,7 +23,9 @@ $fake_data->set_data_from_script({
contract_id => sub { return shift->get_id('contracts',@_); },,
},
query => ['name'],
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name});},
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
},
},
});

@ -30,7 +30,9 @@ $fake_data->set_data_from_script({
enabled => '1',
},
query => ['group_id','name'],
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
},
},
});

@ -53,7 +53,9 @@ $fake_data->set_data_from_script({
],
},
'query' => ['name'],
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
},
},
});

@ -20,7 +20,7 @@ $fake_data->set_data_from_script({
subscriber_id => sub { return shift->get_id('subscribers',@_); },
coding => 0,
direction => 'out',
caller => '111111111',
caller => sub { my $primary_number = shift->get_field('subscribers','primary_number'); return join '', @$primary_number{qw/cc ac sn/}; },
callee => '+111111111',
text => 'Some text',
},

@ -27,6 +27,7 @@ $fake_data->set_data_from_script({
'q' => 0.5,
'subscriber_id' => sub { return shift->get_id('subscribers', @_); },
},
'update_change_fields' => [qw/_links expires id/],#expires seems like timezone difference
},
});

@ -56,8 +56,16 @@ $fake_data->set_data_from_script({
if($pilot->{total_count} <= 0){
undef $pilot;
}
$test_machine->check_create_correct(1, sub{$_[0]->{is_pbx_pilot} = ($pilot || $_[1]->{i} > 1)? 0 : 1;} );
$test_machine->check_create_correct(1, sub{
$_[0]->{is_pbx_pilot} = ($pilot || $_[1]->{i} > 1)? 0 : 1;
$_[0]->{pbx_extension} = time();
$_[0]->{webusername} .= time();
$_[0]->{username} .= time();
delete $_[0]->{alias_numbers};
$_[0]->{primary_number}->{sn} = time();
}, $self->data->{$collection_name}->{data} );
},
'update_change_fields' => [qw/modify_timestamp create_timestamp primary_number_id/],
},
});
@ -75,7 +83,7 @@ $test_machine->form_data_item();
my $remote_config = $test_machine->init_catalyst_config;
#modify time changes on every data change, and primary_number_id on every primary number change
my $put2get_check_params = {ignore_fields => [qw/modify_timestamp create_timestamp primary_number_id/]};
my $put2get_check_params = { ignore_fields => $fake_data->data->{subscribers}->{update_change_fields} };
{
#20369

@ -23,7 +23,9 @@ $fake_data->set_data_from_script({
greetingfile => [ dirname($0).'/resources/test.wav' ],
},
'query' => [ ['dir', 'json', 'dir'] ],
'create_special'=> $fake_data->create_special_upload(),
'data_callbacks' => {
'get2put' => $fake_data->get2put_upload_callback('voicemailgreetings'),
}
},
});
my $test_machine = Test::Collection->new(
@ -100,9 +102,8 @@ if(ok($soxi_output =~/GSM/, "Check that we converted wav to GSM encoding:".$soxi
$test_machine->http_code_msg(422, "check response code on put empty file", $res_put_empty, $content_put_empty);
diag("Check dirty file:");
#btw - other vriant of tha put data - closer to stored. will be changed by Collection::encode_content
($res_put_empty,$content_put_empty) = $test_machine->request_put( {
%{$test_machine->DATA_ITEM_STORE},
greetingfile => [ dirname($0).'/resources/empty.wav' ],
($res_put_empty,$content_put_empty) = $test_machine->request_put({
'data_cb' => $fake_data->{data}->{'voicemailgreetings'}->{data_callbacks}->{get2put},
} );
$test_machine->http_code_msg(422, "check response code on put empty file", $res_put_empty, $content_put_empty);
}

@ -575,43 +575,46 @@ sub get_uri_current{
}
sub encode_content{
my($self,$content, $type) = @_;
my($self, $content, $type) = @_;
$type //= $self->ENCODE_CONTENT;
my ($content_res,$content_type_res) = ($content, $type);
my %json_types = (
'application/json' => 1,
'application/json-patch+json' => 1,
'json' => 1,
);
#print "1. content=$content;\n\n";
#print Dumper ["encode_content.1",$content, $type] ;
if($content){
if( $json_types{$type} && (('HASH' eq ref $content) ||('ARRAY' eq ref $content)) ){
return JSON::to_json($content);
}elsif('multipart/form-data' eq $type
&& 'HASH' eq ref $content
if('HASH' eq ref $content
&& $content->{json}
&& (('HASH' eq ref $content->{json}) || ( 'ARRAY' eq ref $content->{json} ) )
){
$content->{json} = JSON::to_json($content->{json});
return [
$content_res = [
%{$content},
];
$content_type_res = 'multipart/form-data';
}elsif( $json_types{$type} && (('HASH' eq ref $content) ||('ARRAY' eq ref $content)) ){
$content_res = JSON::to_json($content);
$type eq 'json' and $content_type_res = 'application/json';
}
}
#print "2. content=$content;\n\n";
return $content;
#print Dumper ["encode_content.2",$content_res,$content_type_res] ;
return ($content_res,$content_type_res);
}
sub request{
my($self,$req) = @_;
my $credentials = {};
(@$credentials{qw/user password/},undef,undef) = $self->get_role_credentials();
if($self->DEBUG){
my $credentials = {};
(@$credentials{qw/user password/},undef,undef) = $self->get_role_credentials();
my $curl = Test::HTTPRequestAsCurl::as_curl($req, credentials => $credentials );
print $req->as_string;
print "$curl\n\n";
}
if(!$self->DEBUG_ONLY){
my $res = $self->ua->request($req);
diag(sprintf($self->name_prefix."request:%s: %s", $req->method, $req->uri));
#draft of the debug mode
if($self->DEBUG){
if($res->code >= 400){
@ -628,7 +631,7 @@ sub request{
sub request_process{
my($self,$req) = @_;
#print $req->as_string;
my $res = $self->ua->request($req);
my $res = $self->request($req);
my $rescontent = $self->get_response_content($res);
return ($res,$rescontent,$req);
}
@ -647,21 +650,24 @@ sub get_request_put{
$uri ||= $self->get_uri_current;
$uri = $self->normalize_uri($uri);
#This is for multipart/form-data cases
$content = $self->encode_content($content, $self->content_type->{PUT});
my $content_type;
($content,$content_type) = $self->encode_content($content, $self->content_type->{PUT});
my $req = POST $uri,
Content_Type => $self->content_type->{PUT},
Content_Type => $content_type,
$content ? ( Content => $content ) : ();
$req->method('PUT');
$req->header('Prefer' => 'return=representation');
return $req;
}
sub get_request_patch{
my($self,$uri) = @_;
my($self,$uri,$content) = @_;
$uri ||= $self->get_uri_current;
$uri = $self->normalize_uri($uri);
my $content_type;
($content,$content_type) = $self->encode_content($content, $self->content_type->{PATCH});
my $req = HTTP::Request->new('PATCH', $uri);
$req->header('Prefer' => 'return=representation');
$req->header('Content-Type' => $self->content_type->{PATCH} );
$req->header('Content-Type' => $content_type );
$content and $req->content($content);
return $req;
}
sub request_put{
@ -677,10 +683,8 @@ sub request_put{
sub request_patch{
my($self,$content, $uri, $req) = @_;
$uri ||= $self->get_uri_current;
$req ||= $self->get_request_patch($uri);
#patch is always a json
$content = $self->encode_content($content, $self->content_type->{PATCH});
$content and $req->content($content);
$req ||= $self->get_request_patch($uri, $content);
my $res = $self->request($req);
if($res){
my $rescontent = $self->get_response_content($res);
@ -694,10 +698,11 @@ sub request_post{
$uri ||= $self->get_uri_collection;
$uri = $self->normalize_uri($uri);
diag("request_post: uri: $uri;");
$content = $self->encode_content($content, $self->content_type->{POST} );
my $content_type;
($content,$content_type) = $self->encode_content($content, $self->content_type->{POST});
#form-data is set automatically, despite on $self->content_type->{POST}
$req ||= POST $uri,
Content_Type => $self->content_type->{POST},
Content_Type => $content_type,
Content => $content;
$req->header('Prefer' => 'return=representation');
my $res = $self->request($req);
@ -1105,25 +1110,17 @@ sub check_item_get{
sub process_data{
my($self, $data_cb, $data_in, $data_cb_data) = @_;
my $data = $data_in || clone($self->DATA_ITEM);
defined $data_cb and $data_cb->($data, $data_cb_data);
if(defined $data_cb && 'CODE' eq ref $data_cb){
$data_cb->($data, $data_cb_data, $self);
}
return $data;
}
sub get_item_post_content{
sub check_item_post{
my($self, $data_cb, $data_in, $data_cb_data) = @_;
my $data = $self->process_data($data_cb, $data_in, $data_cb_data);
#print Dumper $data;
my $content = {
$data->{json} ? ( json => JSON::to_json(delete $data->{json}) ) : (),
%$data,
};
return $content;
}
sub check_item_post{
my($self, $data_cb, $data_in, $data_cb_data) = @_;
my $content = $self->get_item_post_content($data_cb, $data_in, $data_cb_data);
#print Dumper $content;
my ($res,$rescontent,$req) = $self->request_post($content);#,$uri,$req
return wantarray ? ($res,$rescontent,$req,$content) : $res;
my ($res,$rescontent,$req) = $self->request_post($data);#,$uri,$req
return wantarray ? ($res,$rescontent,$req,$data) : $res;
};
sub check_item_delete{
my($self, $uri, $msg) = @_;
@ -1225,7 +1222,7 @@ sub check_get2put{
delete $put_out->{content_in}->{_links};
delete $put_out->{content_in}->{_embedded};
# check if put is ok
(defined $put_in->{data_cb}) and $put_in->{data_cb}->($put_out->{content_in});
$put_out->{content_in} = $self->process_data($put_in->{data_cb}, $put_out->{content_in});
@{$put_out}{qw/response content request/} = $self->request_put( $put_out->{content_in}, $put_in->{uri} );
foreach my $field (@{$ignore_fields}){
delete $get_out->{content}->{$field};
@ -1418,6 +1415,13 @@ sub http_code_msg{
}
$code and is($res->code, $code, $message_res);
}
sub name_prefix{
my($self,$name) = @_;
$name //= $self->name;
return $name ? $name.': ' : '';
}
sub get_cached_data{
my($self) = @_;
return (-e $self->data_cache_file) ? retrieve($self->data_cache_file) : {};

@ -67,6 +67,11 @@ has 'data' => (
isa => 'HashRef',
builder => 'build_data',
);
has 'use_data_callbacks' => (
is => 'rw',
isa => 'Bool',
default => sub { 0 },
);
has 'FLAVOUR' => (
is => 'rw',
isa => 'Str',
@ -186,6 +191,9 @@ sub build_data{
reseller_id => sub { return shift->get_id('resellers',@_); },
},
'query' => ['login'],
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{login}); },
},
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{login}); },
},
'applyrewrites' => {
@ -215,10 +223,13 @@ sub build_data{
'data' => {
sources => [{source => "test",}],
subscriber_id => sub { return shift->get_id('subscribers',@_); },
name => "from_test"
name => "from_test",
mode => "whitelist",
},
'query' => ['name','subscriber_id'],
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
},
},
'customerlocations' => {
'data' => {
@ -231,12 +242,15 @@ sub build_data{
description => "test_api",
},
'query' => ['name'],
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
},
},
'ncoslnpcarriers' => {
'data' => {
ncos_level_id => sub { return shift->get_id('ncoslevels',@_); },
description => "test_api",
carrier_id => sub { return shift->get_id('lnpcarriers',@_); },
},
},
'ncospatterns' => {
@ -375,9 +389,11 @@ sub build_data{
'default' => 'billing_profiles',
'no_delete_available' => 1,
'dependency_requires_recreation' => ['resellers'],
'uniquizer_cb' => sub {
Test::FakeData::string_uniquizer(\$_[0]->{name});
Test::FakeData::string_uniquizer(\$_[0]->{handle});
'data_callbacks' => {
'uniquizer_cb' => sub {
Test::FakeData::string_uniquizer(\$_[0]->{name});
Test::FakeData::string_uniquizer(\$_[0]->{handle});
},
},
},
'subscriberprofilesets' => {
@ -387,7 +403,9 @@ sub build_data{
description => 'api_test_subscriberprofileset',
},
'query' => ['name'],
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
},
},
'subscriberprofiles' => {
'data' => {
@ -396,7 +414,9 @@ sub build_data{
description => 'api_test subscriberprofile',
},
'query' => ['name'],
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
},
},
'pbxdeviceconfigs' => {
'data' => {
@ -422,7 +442,9 @@ sub build_data{
},
'query' => ['name'],
'no_delete_available' => 1,
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
},
},
'rewriterulesets' => {
'data' => {
@ -435,12 +457,15 @@ sub build_data{
callee_out_dpid => '4',
},
'query' => ['name'],
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
'data_callbacks' => {
'uniquizer_cb' => sub { Test::FakeData::string_uniquizer(\$_[0]->{name}); },
},
},
};
$self->process_data($data);
return $data;
}
sub process_data{
my($self,$data,$collections_slice) = @_;
$collections_slice //= [keys %$data];
@ -546,12 +571,14 @@ sub search_item{
$self->searched->{$collection_name} = [$res, $content, $req];
return ($res, $content, $req);
}
sub clear_cached_data{
my($self, @collections) = @_;
delete @{$self->loaded}{@collections};
delete @{$self->created}{@collections};
delete @{$self->searched}{@collections};
}
sub set_data_from_script{
my($self, $data_in) = @_;
while (my($collection_name,$collection_data) = each %$data_in ){
@ -603,6 +630,7 @@ sub load_collection_data{
$self->load_db(undef,[$collection_name]);
}
}
sub get_id{
my $self = shift;
#my( $collection_name, $parents_in, $params) = @_;
@ -620,12 +648,23 @@ sub get_id{
}
return $res_id;
}
sub get_field{
my $self = shift;
#my( $collection_name, $parents_in, $params) = @_;
my( $collection_name, $field ) = @_;
$self->get_id($collection_name);
my $item = $self->get_existent_item($collection_name);
return $item->{content}->{$field};
}
sub get_existent_item{
my($self, $collection_name) = @_;
my $item = $self->created->{$collection_name}->{values}->[0]
|| $self->loaded->{$collection_name}->[0];
return $item
}
sub get_existent_id{
my($self, $collection_name) = @_;
my $id;
@ -636,20 +675,30 @@ sub get_existent_id{
}
return $id
}
sub collection_id_exists{
my($self, $collection_name) = @_;
return (exists $self->loaded->{$collection_name}) || ( exists $self->created->{$collection_name});
}
sub set_collection_data_fields{
my($self, $collection_name, $fields) = @_;
@{ $self->data->{$collection_name}->{data}->{json} || $self->data->{$collection_name}->{data} }{keys %$fields} = values %$fields;
}
sub get_collection_data_fields{
my($self, $collection_name, @fields ) = @_;
my $data = $self->data->{$collection_name}->{data}->{json} || $self->data->{$collection_name}->{data};
my %res = map { $_ => $data->{$_} } @fields;
return wantarray ? %res : ( values %res )[0];
}
sub get_collection_interface{
my($self,$collection_name,$data) = @_;
$data //= $self->data;
return $data->{$collection_name}->{collection} ? $data->{$collection_name}->{collection} : $collection_name;
}
sub process{
my($self, $collection_name, $parents_in) = @_;
$self->load_collection_data($collection_name);
@ -691,7 +740,12 @@ sub create{
if(exists $self->data->{$collection_name}->{create_special} && 'CODE' eq ref $self->data->{$collection_name}->{create_special}){
$self->data->{$collection_name}->{create_special}->($self,$collection_name,$test_machine);
}else{
$test_machine->check_create_correct(1);
$test_machine->check_create_correct(1,
$self->{use_data_callbacks}
?
$self->data->{$collection_name}->{data_callbacks}->{uniquizer_cb}
:
undef);
}
$self->created->{$collection_name} = {values=>[values %{$test_machine->DATA_CREATED->{ALL}}], order => scalar keys %{$self->created}};
@ -727,17 +781,6 @@ sub create{
return $self->get_existent_id($collection_name);
}
sub create_special_upload{
my $self = shift;
return sub {
my ($self,$collection_name,$test_machine) = @_;
my $prev_params = $test_machine->get_cloned('content_type');
@{$test_machine->content_type}{qw/POST PUT/} = (('multipart/form-data') x 2);
$test_machine->check_create_correct(1);
$test_machine->set(%$prev_params);
};
}
sub clear_test_data_all{
my $self = shift;
my($force_delete) = @_;
@ -790,20 +833,7 @@ sub read_cached_data{
store {loaded => $self->loaded, created => $self->created}, $self->data_cache_file;
}
sub DEMOLISH{
my($self) = @_;
$self->clear_test_data_all();
if( keys %{$self->undeletable} ){
print "We have test items, which can't delete through API:\n";
print Dumper [ sort { $a cmp $b } keys %{$self->undeletable} ];
}
}
sub get_collection_interface{
my($self,$collection_name,$data) = @_;
$data //= $self->data;
return $data->{$collection_name}->{collection} ? $data->{$collection_name}->{collection} : $collection_name;
}
sub string_uniquizer{
my($field,$data,$additions) = @_;
state $i;
@ -816,6 +846,29 @@ sub string_uniquizer{
}
return $field;
}
sub get2put_upload_callback{
my($self,$collection_name) = @_;
return sub {
my($data,$data_add,$test_machine) = @_;
my $upload_data = clone $self->data->{$collection_name}->{data};
delete $upload_data->{json};
%$data = (
'json' => {%$data},
%$upload_data,
);
};
}
sub DEMOLISH{
my($self) = @_;
$self->clear_test_data_all();
if( keys %{$self->undeletable} ){
print "We have test items, which can't delete through API:\n";
print Dumper [ sort { $a cmp $b } keys %{$self->undeletable} ];
}
}
1;
__END__

Loading…
Cancel
Save