From 60bb147054bf5ae461983b75304ddd1e5bf981cd Mon Sep 17 00:00:00 2001 From: Irina Peshinskaya Date: Fri, 2 Feb 2018 11:30:47 +0200 Subject: [PATCH] TT#26800 Adapt API to generate "fake" sms on demand Also enable xmpp viewing and add test script draft. Change-Id: I4f9dbd8e526515decb683c3a037287f279bbd556 --- lib/NGCP/Panel/Controller/API/SMS.pm | 29 ++-- lib/NGCP/Panel/Role/API/Conversations.pm | 2 +- t/api-rest/api-conversations.t | 185 +++++++++++++++++++++++ 3 files changed, 201 insertions(+), 15 deletions(-) create mode 100644 t/api-rest/api-conversations.t diff --git a/lib/NGCP/Panel/Controller/API/SMS.pm b/lib/NGCP/Panel/Controller/API/SMS.pm index b3fc505c66..1b9b3f6b5e 100644 --- a/lib/NGCP/Panel/Controller/API/SMS.pm +++ b/lib/NGCP/Panel/Controller/API/SMS.pm @@ -114,7 +114,7 @@ sub create_item { $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Subscriber is not active"); return; } - + my $test_mode = $c->request->params->{test_mode} // ''; my $session; try { my $parts = NGCP::Panel::Utils::SMS::get_number_of_parts($resource->{text}); @@ -130,23 +130,24 @@ sub create_item { } $session->{coding} = NGCP::Panel::Utils::SMS::get_coding($resource->{text}); + if ( 'dont_send_sms' ne $test_mode ) { + + NGCP::Panel::Utils::SMS::send_sms( + c => $c, + caller => $resource->{caller}, + callee => $resource->{callee}, + text => $resource->{text}, + coding => $session->{coding}, + err_code => sub { + $session->{reason} = shift; + $session->{status} = 'failed'; + } + ); - NGCP::Panel::Utils::SMS::send_sms( - c => $c, - caller => $resource->{caller}, - callee => $resource->{callee}, - text => $resource->{text}, - coding => $session->{coding}, - err_code => sub { - $session->{reason} = shift; - $session->{status} = 'failed'; - } - ); - + } NGCP::Panel::Utils::SMS::perform_prepaid_billing(c => $c, session => $session ); - if ($session->{status} eq 'failed') { die $session->{reason}."\n"; } diff --git a/lib/NGCP/Panel/Role/API/Conversations.pm b/lib/NGCP/Panel/Role/API/Conversations.pm index d606f4a78e..9121d425d0 100644 --- a/lib/NGCP/Panel/Role/API/Conversations.pm +++ b/lib/NGCP/Panel/Role/API/Conversations.pm @@ -43,7 +43,7 @@ my %enabled_conversations = ( voicemail => 1, sms => 1, fax => 1, - xmpp => 0, + xmpp => 1, ); my %call_fields = (); diff --git a/t/api-rest/api-conversations.t b/t/api-rest/api-conversations.t new file mode 100644 index 0000000000..67b221e3b0 --- /dev/null +++ b/t/api-rest/api-conversations.t @@ -0,0 +1,185 @@ +use strict; +use warnings; + +use Test::More; +use Test::Collection; +use Test::FakeData; +use Data::Dumper; + +use File::Slurp qw/write_file/; +use IPC::Run3; +use Cwd; +use Log::Log4perl qw/get_logger :levels/; +use Clone qw/clone/; +use JSON; +use Cwd 'abs_path'; +use File::Basename; + +diag('Note that the next tests require at least one sip account customer,subscriber,call,fax,voicemail,sms,xmpp to be present'); + +my $SERVER = { + CALLS => '192.168.1.118', + XMPP => '192.168.1.118', + FAX => '127.0.0.1',#dummy for now, not used in the executor + SMS => '127.0.0.1',#dummy for now, not used in the executor +}; +my $AMOUNT = { + SUBSCRIBERS => 1,#so there will be x2 subscribers - N caller and N callee + CALLS => 0, + SMS => 1, + FAX => 1, + VOICEMAILS => 1, + XMPP => 1, +}; + + +my $test_machine = Test::Collection->new( + name => 'conversations', +); +#init test_machine +my $fake_data = Test::FakeData->new; +$fake_data->set_data_from_script({ + 'conversations' => { + 'data' => { + username_format => 'sub1_%04d', + username_format_caller => 'sub1_caller_%04d', + username_format_callee => 'sub1_callee_%04d', + display_name_format => 'sub1 %04d', + display_name_format_caller => 'sub1 caller %04d', + display_name_format_callee => 'sub1 callee %04d', + password_format => 'sub_pwd_%04d', + #we repeate it intentionally, to somplify logic + password_format_caller => 'sub_pwd_%04d', + password_format_callee => 'sub_pwd_%04d', + subscriber_template => { + customer_id => sub { return shift->get_id('customer_sipaccount',@_); }, + domain => '192.168.1.118', + administrative => 0, + primary_number => { + ac => '1', + cc => '1', + sn => time(), + }, + status => 'active', + username => undef, + password => undef, + display_name => undef, + }, + }, + }, +}); +my $FAKE_DATA_INIT = $fake_data->build_data; +my $FAKE_DATA_PROCESSED = $fake_data->process('conversations'); + + +my ($domain,$customer,$contact,$subscribers); + +SKIP: +{ #MT#16171 + + if(!defined $FAKE_DATA_PROCESSED->{subscriber_template}->{domain}){ + skip("Precondition not met: need a domain",1); + } + $customer = $test_machine->get_item_hal('customers','/api/customers/'.$FAKE_DATA_PROCESSED->{subscriber_template}->{customer_id}); + if(!$customer->{total_count}){ + skip("Precondition not met: Customer not found", 1); + } + $contact = $test_machine->get_item_hal('customercontacts','/api/customercontacts/'.$customer->{content}->{contact_id}); + if(!$contact->{total_count}){ + skip("Precondition not met: Customer contact not found", 1); + } + $domain = $test_machine->get_item_hal('domains','/api/domains/?domain='.$FAKE_DATA_PROCESSED->{subscriber_template}->{domain}); + if(!$domain->{total_count}){ + skip("Precondition not met: Domain not found", 1); + } + if($contact->{content}->{reseller_id} != $domain->{content}->{reseller_id}){ + skip("Precondition not met: Domain should belong to the reseller_id = ".$customer->{content}->{reseller_id}, 1); + } +} +{#prepare subscribers + $test_machine->name('subscribers'); + my $type_i = 1; + foreach my $type (qw/caller callee/){ + my $type_suffix = '_'.$type; + $subscribers->{$type} //= []; + my $found = 0; + for(my $i = 1; $i <= $AMOUNT->{SUBSCRIBERS}; $i++){ + my $subscriber = $test_machine->get_item_hal('subscribers','/api/subscribers/?username='.sprintf( $FAKE_DATA_PROCESSED->{'username_format'.$type_suffix}, $i )); + if(!$subscriber->{total_count}){ + $subscriber = $test_machine->check_create_correct( 1, sub{ + my $num = $i; + $_[0] = clone $FAKE_DATA_PROCESSED->{subscriber_template}; + $_[0]->{primary_number}->{sn} .= $num.$type_i; + foreach my $field(qw/username password display_name/){ + $_[0]->{$field} = sprintf( $FAKE_DATA_PROCESSED->{$field.'_format'.$type_suffix}, $num ); + } + } )->[0]; + } + my $preferences = {'uri' => '/api/subscriberpreferences/'.$subscriber->{content}->{id}}; + (undef, $preferences->{content}) = $test_machine->request_patch([ { op => 'add', path => '/allow_out_foreign_domain', value => JSON::true } ] ,$preferences->{uri}); + push @{$subscribers->{$type}}, $subscriber->{content}; + } + $type_i++; + } +} +{#generate + for (my $i_item=0; $i_item < $AMOUNT->{SUBSCRIBERS}; $i_item++){ + my $caller = $subscribers->{caller}->[$i_item]; + my $callee = $subscribers->{callee}->[$i_item]; + my $cmd_full; + my $cmd = 'perl '.dirname(abs_path($0)).'/resources/executor.pl'; + if($AMOUNT->{CALLS}){ + $cmd_full = "$cmd call $AMOUNT->{CALLS} $SERVER->{CALLS} 5060 0 @{$caller}{qw/domain username password/} 0 @{$callee}{qw/domain username password/} "; + print $cmd_full."\n"; + `$cmd_full`; + } + if($AMOUNT->{VOICEMAILS}){ + $test_machine->request_patch([ { + 'op' => 'add', + 'path' => '/cfu', + 'value' => { + 'destinations' => [ + { 'destination' => 'voicebox', 'timeout' => 200}, + ], + 'times' => undef, + }, + } ] ,'/api/callforwards/'.$callee->{id}); + + $cmd_full = "$cmd voicemail $AMOUNT->{VOICEMAILS} $SERVER->{CALLS} 5060 0 @{$caller}{qw/domain username password/} 0 @{$callee}{qw/domain username password/} "; + print $cmd_full."\n"; + `$cmd_full`; + $test_machine->request_patch([ { + 'op' => 'remove', + 'path' => '/cfu', + } ] ,'/api/callforwards/'.$callee->{id}); + } + if($AMOUNT->{XMPP}){ + $cmd_full = "$cmd xmpp $AMOUNT->{XMPP} $SERVER->{XMPP} 5222 0 @{$caller}{qw/domain username password/} 0 @{$callee}{qw/domain username password/} "; + print $cmd_full."\n"; + `$cmd_full`; + } + if($AMOUNT->{SMS}){ + $cmd_full = "$cmd sms $AMOUNT->{SMS} $SERVER->{SMS} 1443 0 @{$caller}{qw/domain username password/} 0 @{$callee}{qw/domain username password/} "; + print $cmd_full."\n"; + `$cmd_full`; + } + if($AMOUNT->{FAX}){ + my $test_machine_aux = Test::Collection->new(name => 'faxserversettings'); + foreach my $type_l (qw/caller callee/){ + my $uri = $test_machine_aux->get_uri($subscribers->{$type_l}->[$i_item]->{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); + } + $cmd_full = "$cmd fax $AMOUNT->{FAX} $SERVER->{FAX} 1443 0 @{$caller}{qw/domain username password/} 0 @{$callee}{qw/domain username password/} "; + print $cmd_full."\n"; + `$cmd_full`; + } + + } +} + +done_testing; + +# vim: set tabstop=4 expandtab: