Fix faxserversettings PUT/PATCH processing found after faxes tests run MT#15441 Tests for subscriber number changes MT#12347 Test framework small fixes and additions Some format changes and unused library removals Add put_and_get method as common Replace request_get & check 200 with check_item_get Changes for API that doesn't return created object json after POST Complete base_uri using replacement with normalize_uri Change-Id: Ia31a97eb9511f1ca1de63c72bcbe380b89dfed34changes/91/2891/2
parent
98ac54d7cb
commit
57edaa4689
@ -0,0 +1,124 @@
|
||||
use strict;
|
||||
|
||||
use Test::Collection;
|
||||
use Test::FakeData;
|
||||
use Test::More;
|
||||
use Data::Dumper;
|
||||
|
||||
#use NGCP::Panel::Utils::Subscriber;
|
||||
|
||||
my $test_machine = Test::Collection->new(
|
||||
name => 'subscribers',
|
||||
);
|
||||
my $fake_data = Test::FakeData->new;
|
||||
|
||||
$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)};
|
||||
|
||||
$fake_data->set_data_from_script({
|
||||
'subscribers' => {
|
||||
'data' => {
|
||||
administrative => 0,
|
||||
customer_id => sub { return shift->get_id('customers',@_); },
|
||||
primary_number => { ac => 111, cc=> 111, sn => 111 },
|
||||
alias_numbers => [ { ac => 11, cc=> 11, sn => 11 } ],
|
||||
username => 'api_test_username',
|
||||
password => 'api_test_password',
|
||||
webusername => 'api_test_webusername',
|
||||
webpassword => undef,
|
||||
domain_id => sub { return shift->get_id('domains',@_); },,
|
||||
#domain_id =>
|
||||
email => undef,
|
||||
external_id => undef,
|
||||
is_pbx_group => 1,
|
||||
is_pbx_pilot => 1,
|
||||
pbx_extension => '111',
|
||||
pbx_group_ids => [],
|
||||
pbx_groupmember_ids => [],
|
||||
profile_id => sub { return shift->get_id('subscriberprofiles',@_); },
|
||||
status => 'active',
|
||||
pbx_hunt_policy => 'parallel',
|
||||
pbx_hunt_timeout => '15',
|
||||
},
|
||||
'query' => ['username'],
|
||||
},
|
||||
});
|
||||
|
||||
$test_machine->DATA_ITEM_STORE($fake_data->process('subscribers'));
|
||||
$test_machine->form_data_item( );
|
||||
|
||||
# create new subscribers from DATA_ITEM. Item is not created in the fake_data->process.
|
||||
|
||||
$test_machine->check_create_correct( 1, sub{ $_[0]->{username} .= time().'_'.$_[1]->{i} ; } );
|
||||
##$test_machine->check_bundle();
|
||||
##$test_machine->check_get2put();
|
||||
|
||||
#------- MT#15441
|
||||
{
|
||||
my $intentional_cli = '111'.time();
|
||||
my $intentional_primary_number = {
|
||||
'cc' => '111',
|
||||
'ac' => '222',
|
||||
'sn' => '123'.time(),
|
||||
};
|
||||
|
||||
#prepare preconditions: cli should differ from primary_nuber
|
||||
my $subscriber = ($test_machine->get_created_first() || $fake_data->get_existent_item($test_machine->name) || $test_machine->get_item_hal());
|
||||
$subscriber->{uri} = $subscriber->{location};
|
||||
my ($preferences, $preferences_put) = ({'uri' => '/api/subscriberpreferences/'.$test_machine->get_id_from_created($subscriber)}) x 2;
|
||||
(undef, $preferences->{content}) = $test_machine->check_item_get($preferences->{uri});
|
||||
$preferences->{content}->{cli} = $intentional_cli;
|
||||
(undef, $preferences_put->{content}) = $test_machine->request_put($preferences->{content},$preferences->{uri});
|
||||
|
||||
is($preferences_put->{content}->{cli}, $intentional_cli, "check that cli was updated on subscriberpreferences put: $preferences_put->{content}->{cli} == $intentional_cli");
|
||||
|
||||
my ($subscriber_put, $subscriber_get, $preferences_get);
|
||||
|
||||
#1
|
||||
$subscriber->{content}->{primary_number} = $intentional_primary_number;
|
||||
($subscriber_put,$subscriber_get,$preferences_get) = $test_machine->put_and_get($subscriber, $preferences_put);
|
||||
is($preferences_get->{content}->{cli}, $intentional_cli, "check that cli was preserved on subscriber phones update: $preferences_get->{content}->{cli} == $intentional_cli");
|
||||
#/1
|
||||
#2
|
||||
delete $subscriber->{content}->{primary_number};
|
||||
($subscriber_put,$subscriber_get,$preferences_get) = $test_machine->put_and_get($subscriber, $preferences_put);
|
||||
is($preferences_get->{content}->{cli}, $intentional_cli, "check that cli was preserved on subscriber phones update: $preferences_get->{content}->{cli} == $intentional_cli");
|
||||
#/2
|
||||
#now prepare preferences for zero situation, when synchronization will be restarted again
|
||||
delete $preferences->{content}->{cli};
|
||||
(undef, $preferences_put->{content}) = $test_machine->request_put($preferences->{content},$preferences->{uri});
|
||||
is($preferences_put->{content}->{cli}, undef, "check that cli was deleted on subscriberpreferences put with empty cli");
|
||||
#3
|
||||
$subscriber->{content}->{primary_number} = $intentional_primary_number;
|
||||
($subscriber_put,$subscriber_get,$preferences_get) = $test_machine->put_and_get($subscriber, $preferences_put);
|
||||
is($preferences_get->{content}->{cli}, number_as_string($intentional_primary_number), "check that cli was created on subscriber phones update: $preferences_get->{content}->{cli} == ".number_as_string($intentional_primary_number) );
|
||||
#/3
|
||||
$intentional_primary_number = {
|
||||
'cc' => '222',
|
||||
'ac' => '333',
|
||||
'sn' => '444'.time(),
|
||||
};
|
||||
#4
|
||||
$subscriber->{content}->{primary_number} = $intentional_primary_number;
|
||||
($subscriber_put,$subscriber_get,$preferences_get) = $test_machine->put_and_get($subscriber, $preferences_put);
|
||||
is($preferences_get->{content}->{cli}, number_as_string($intentional_primary_number), "check that cli was updated on subscriber phones update: $preferences_get->{content}->{cli} == ".number_as_string($intentional_primary_number) );
|
||||
#/4
|
||||
#5
|
||||
delete $subscriber->{content}->{primary_number};
|
||||
($subscriber_put,$subscriber_get,$preferences_get) = $test_machine->put_and_get($subscriber, $preferences_put);
|
||||
is($preferences_get->{content}->{cli}, undef, "check that cli was deleted on subscriber phones update");
|
||||
#/5
|
||||
}
|
||||
|
||||
$test_machine->clear_test_data_all();
|
||||
done_testing;
|
||||
|
||||
|
||||
sub number_as_string{
|
||||
my ($number_row, %params) = @_;
|
||||
return 'HASH' eq ref $number_row
|
||||
? $number_row->{cc} . ($number_row->{ac} // '') . $number_row->{sn}
|
||||
: $number_row->cc . ($number_row->ac // '') . $number_row->sn;
|
||||
}
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
@ -0,0 +1,138 @@
|
||||
package Test::HTTPRequestAsCurl;
|
||||
use 5.008005;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
our $VERSION = "0.03";
|
||||
|
||||
use Carp;
|
||||
use String::ShellQuote qw/ shell_quote /;
|
||||
#use Win32::ShellQuote qw/ cmd_escape /;
|
||||
use Exporter::Shiny qw/ as_curl /;
|
||||
|
||||
sub as_curl {
|
||||
my ($request, %params) = @_;
|
||||
|
||||
my $content = $request->content;
|
||||
my @data = split '&', $content;
|
||||
my $method = $request->method;
|
||||
my $uri = $request->uri;
|
||||
my $headers = $request->headers;
|
||||
my $user = $headers->authorization_basic || ( $params{credentials} ? join(':', @{$params{credentials}}{qw/user password/}) : '');
|
||||
#my @h = grep { $_ !~ /(authorization|content-length|content-type)/i }
|
||||
my @h = grep { $_ !~ /(authorization|content-length)/i }
|
||||
$headers->header_field_names;
|
||||
|
||||
my @cmd = (["curl"]);
|
||||
push(@cmd, ["--request", $method, $uri]);
|
||||
push(@cmd, ["--dump-header", "-"]);
|
||||
push(@cmd, ["--insecure"]) if $user;
|
||||
push(@cmd, ["--user", $user]) if $user;
|
||||
push(@cmd, ["--header", "$_: " . $headers->header($_)]) for sort @h;
|
||||
push(@cmd, ["--data", $_]) for sort @data;
|
||||
|
||||
#return map { @$_ } @cmd unless keys %params;
|
||||
|
||||
return _make_it_pretty(\@cmd, %params);
|
||||
}
|
||||
|
||||
sub _make_it_pretty {
|
||||
my ($cmd, %params) = @_;
|
||||
|
||||
$params{shell} = $params{shell} || _default_shell_escape();
|
||||
#$params{newline} = $params{newline} || "\\ \n";
|
||||
$params{newline} = $params{newline} || "";
|
||||
|
||||
my $string;
|
||||
for my $part (@$cmd) {
|
||||
#if ($params{shell} eq 'win32') {
|
||||
# $string .= cmd_escape join " ", @$part;
|
||||
# $string .= ' ^' . $params{newline};
|
||||
#}
|
||||
#els
|
||||
if ($params{shell} eq 'bourne') {
|
||||
$string .= shell_quote @$part;
|
||||
$string .= ' ' . $params{newline};
|
||||
}
|
||||
else {
|
||||
croak "this shell is not currently supported: $params{shell}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
sub _default_shell_escape { $^O eq 'MSWin32' ? 'win32' : 'bourne' }
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
||||
|
||||
=encoding utf-8
|
||||
|
||||
=head1 NAME
|
||||
|
||||
HTTP::Request::AsCurl - Generate a curl command from an HTTP::Request object.
|
||||
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
use HTTP::Request::Common;
|
||||
use HTTP::Request::AsCurl qw/as_curl/;
|
||||
|
||||
my $request = POST('api.earth.defense/weapon1', {
|
||||
target => 'mothership',
|
||||
when => 'now'
|
||||
});
|
||||
|
||||
system as_curl($request);
|
||||
|
||||
print as_curl($request, pretty => 1, newline => "\n", shell => 'bourne');
|
||||
# curl \
|
||||
# --request POST api.earth.defense/weapon1 \
|
||||
# --dump-header - \
|
||||
# --data target=mothership \
|
||||
# --data when=now
|
||||
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
This module converts an HTTP::Request object to a curl command. It can be used
|
||||
for debugging REST APIs.
|
||||
|
||||
It handles headers and basic authentication.
|
||||
|
||||
|
||||
=head1 METHODS
|
||||
|
||||
=head2 as_curl($request, %params)
|
||||
|
||||
Accepts an HTTP::Request object and converts it to a curl command. If there
|
||||
are no C<%params>, C<as_curl()> returns the cmd as an array suitable for being
|
||||
passed to system().
|
||||
|
||||
If there are C<%params>, C<as_curl()> returns a formatted string. The string's
|
||||
format defaults to using "\n" for newlines and escaping the curl command using
|
||||
bourne shell rules unless you are on a win32 system in which case it defaults
|
||||
to using win32 cmd.exe escaping rules.
|
||||
|
||||
Available params are as follows
|
||||
|
||||
newline: defaults to "\n"
|
||||
shell: currently available options are 'bourne' and 'win32'
|
||||
|
||||
|
||||
=head1 LICENSE
|
||||
|
||||
Copyright (C) Eric Johnson.
|
||||
|
||||
This library is free software; you can redistribute it and/or modify
|
||||
it under the same terms as Perl itself.
|
||||
|
||||
|
||||
=head1 AUTHOR
|
||||
|
||||
Eric Johnson E<lt>eric.git@iijo.orgE<gt>
|
||||
|
||||
=cut
|
Loading…
Reference in new issue