MT#3949 API: implement contracts handling.

agranig/rest
Andreas Granig 13 years ago
parent 14d9374e8c
commit 45a460a1ce

@ -156,23 +156,32 @@ sub POST :Allow {
$resource->{create_timestamp} = $now;
$resource->{modify_timestamp} = $now;
my $contract;
my $billing_profile_id = delete $resource->{billing_profile_id};
my $billing_profile = $schema->resultset('billing_profiles')->find($billing_profile_id);
unless($billing_profile) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id'.");
last;
}
my $product = $schema->resultset('products')->find({ class => $product_class });
unless($product) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'type'.");
last;
}
try {
my $billing_profile_id = delete $resource->{billing_profile_id};
my $billing_profile = $schema->resultset('billing_profiles')->find($billing_profile_id);
unless($billing_profile) {
$self->error($c, HTTP_NOT_FOUND, "Invalid 'billing_profile_id'.");
last;
}
my $product = $schema->resultset('products')->find({ class => $product_class });
unless($product) {
$self->error($c, HTTP_NOT_FOUND, "Invalid 'type'.");
last;
}
$contract = $schema->resultset('contracts')->create($resource);
if($contract->contact->reseller_id) {
$self->error($c, HTTP_NOT_FOUND, "The contact_id is not a valid ngcp:systemcontacts item, but an ngcp:customercontacts item");
last;
}
} catch($e) {
$c->log->error("failed to create contract: $e"); # TODO: user, message, trace, ...
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create contract.");
last;
}
if($contract->contact->reseller_id) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "The contact_id is not a valid ngcp:systemcontacts item, but an ngcp:customercontacts item");
last;
}
try {
$contract->billing_mappings->create({
billing_profile_id => $billing_profile->id,
product_id => $product->id,
@ -182,7 +191,6 @@ sub POST :Allow {
profile => $billing_profile,
contract => $contract,
);
} catch($e) {
$c->log->error("failed to create contract: $e"); # TODO: user, message, trace, ...
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create contract.");

@ -104,7 +104,8 @@ sub PATCH :Allow {
my $contract = $self->contract_by_id($c, $id);
last unless $self->resource_exists($c, contract => $contract);
my $old_resource = { $contract->get_inflated_columns };
my $resource = $self->apply_patch($c, $old_resource, $json);
# make sure we don't modify $old_resource, but use a copy:
my $resource = $self->apply_patch($c, { %{ $old_resource } }, $json);
last unless $resource;
my $form = NGCP::Panel::Form::Contract::PeeringReseller->new;
@ -142,6 +143,7 @@ sub PUT :Allow {
id => $id,
media_type => 'application/json',
);
last unless $resource;
my $old_resource = { $contract->get_inflated_columns };
my $form = NGCP::Panel::Form::Contract::PeeringReseller->new;

@ -183,7 +183,8 @@ sub DELETE :Allow {
my $contact = $self->contact_by_id($c, $id);
last unless $self->resource_exists($c, systemcontact => $contact);
my $contract_count = $c->model('DB')->resultset('contracts')->search({
contact_id => $id
contact_id => $id,
status => { '!=' => 'terminated' },
});
if($contract_count > 0) {
$self->error($c, HTTP_LOCKED, "Contact is still in use.");

@ -1,13 +0,0 @@
package NGCP::Panel::Field::ContractStatus;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler::Field::Compound';
has_field 'status' => (
type => '+NGCP::Panel::Field::ContractStatusSelect',
label => 'Status',
required => 1,
);
1;
# vim: set tabstop=4 expandtab:

@ -1,18 +0,0 @@
package NGCP::Panel::Field::ContractStatusSelect;
use Sipwise::Base;
extends 'HTML::FormHandler::Field::Select';
sub build_options {
my ($self) = @_;
return [
{ label => 'active', value => 'active' },
{ label => 'pending', value => 'pending' },
{ label => 'locked', value => 'locked' },
{ label => 'terminated', value => 'terminated' },
];
}
1;
# vim: set tabstop=4 expandtab:

@ -23,8 +23,15 @@ has_field 'billing_profile' => (
);
has_field 'status' => (
type => '+NGCP::Panel::Field::ContractStatusSelect',
not_nullable => 1,
type => 'Select',
required => 1,
label => 'Status',
options => [
{ label => 'active', value => 'active' },
{ label => 'pending', value => 'pending' },
{ label => 'locked', value => 'locked' },
{ label => 'terminated', value => 'terminated' },
],
);
has_field 'external_id' => (

@ -256,12 +256,14 @@ sub require_valid_patch {
my ($self, $c, $json) = @_;
my $valid_ops = {
'test' => { 'path' => 1, 'value' => 1 },
'remove' => { 'path' => 1 },
'add' => { 'path' => 1, 'value' => 1 },
'replace' => { 'path' => 1, 'value' => 1 },
'move' => { 'from' => 1, 'path' => 1 },
'copy' => { 'from' => 1, 'path' => 1 },
# we don't support those, as they're quite useless in our case
# TODO: maybe except for remove, which might set it to null?
#'test' => { 'path' => 1, 'value' => 1 },
#'move' => { 'from' => 1, 'path' => 1 },
#'remove' => { 'path' => 1 },
#'add' => { 'path' => 1, 'value' => 1 },
};
my $patch = JSON::from_json($json);

@ -50,9 +50,9 @@ sub hal_from_contract {
name => 'ngcp',
templated => true,
),
Data::HAL::Link->new(relation => 'collection', href => sprintf('/%s', $c->request->path)),
Data::HAL::Link->new(relation => 'collection', href => sprintf('/api/%s/', $self->resource_name)),
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
Data::HAL::Link->new(relation => 'self', href => sprintf("/%s%d", $c->request->path, $contract->id)),
Data::HAL::Link->new(relation => 'self', href => sprintf("/%s", $c->request->path)),
Data::HAL::Link->new(relation => 'ngcp:systemcontacts', href => sprintf("/api/systemcontacts/%d", $contract->contact->id)),
Data::HAL::Link->new(relation => 'ngcp:billingprofiles', href => sprintf("/api/billingprofiles/%d", $billing_profile_id)),
Data::HAL::Link->new(relation => 'ngcp:contractbalances', href => sprintf("/api/contractbalances/%d", $contract_balance->id)),
@ -91,7 +91,7 @@ sub contract_by_id {
'+as' => 'bmid',
});
return $contracts->find({'me.id' => $id});
return $contracts->find($id);
}
sub update_contract {
@ -114,7 +114,7 @@ sub update_contract {
if($old_resource->{billing_profile_id} != $resource->{billing_profile_id}) {
my $billing_profile = $c->model('DB')->resultset('billing_profiles')->find($resource->{billing_profile_id});
unless($billing_profile) {
$self->error($c, HTTP_NOT_FOUND, "Invalid 'billing_profile_id'");
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id'");
return;
}
$contract->billing_mappings->create({
@ -125,6 +125,17 @@ sub update_contract {
}
delete $resource->{billing_profile_id};
if($old_resource->{contact_id} != $resource->{contact_id}) {
my $syscontact = $c->model('DB')->resultset('contacts')
->search({ reseller_id => undef })
->find($resource->{contact_id});
unless($syscontact) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'contact_id'");
return;
}
}
$contract->update($resource);
if($old_resource->{status} ne $resource->{status}) {

@ -47,7 +47,7 @@ sub contact_by_id {
# we only return system contacts, that is, a contact without reseller
my $contact_rs = $c->model('DB')->resultset('contacts')
->search({ reseller_id => undef });
return $contact_rs->find({'me.id' => $id});
return $contact_rs->find($id);
}
1;

@ -1,6 +1,369 @@
# TODO: try to set reseller_id of contact of a system contract, which should fail
# TODO: try to delete used contact
# $req = HTTP::Request->new('DELETE', $uri.'/api/systemcontacts/1');
# $res = $ua->request($req);
# ok($res->code == 423, "check delete of used contact");
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 $valid_ssl_client_cert = $ENV{API_SSL_CLIENT_CERT} ||
"/etc/ssl/ngcp/api/NGCP-API-client-certificate.pem";
my $valid_ssl_client_key = $ENV{API_SSL_CLIENT_KEY} ||
$valid_ssl_client_cert;
my $ssl_ca_cert = $ENV{API_SSL_CA_CERT} || "/etc/ssl/ngcp/api/ca-cert.pem";
my ($ua, $req, $res);
$ua = LWP::UserAgent->new;
$ua->ssl_opts(
SSL_cert_file => $valid_ssl_client_cert,
SSL_key_file => $valid_ssl_client_key,
SSL_ca_file => $ssl_ca_cert,
);
# OPTIONS tests
{
$req = HTTP::Request->new('OPTIONS', $uri.'/api/contracts/');
$res = $ua->request($req);
ok($res->code == 200, "check options request");
ok($res->header('Accept-Post') eq "application/hal+json; profile=http://purl.org/sipwise/ngcp-api/#rel-contracts", "check Accept-Post header in options response");
my $opts = JSON::from_json($res->decoded_content);
my @hopts = split /\s*,\s*/, $res->header('Allow');
ok(exists $opts->{methods} && ref $opts->{methods} eq "ARRAY", "check for valid 'methods' in body");
foreach my $opt(qw( GET HEAD OPTIONS POST )) {
ok(grep(/^$opt$/, @hopts), "check for existence of '$opt' in Allow header");
ok(grep(/^$opt$/, @{ $opts->{methods} }), "check for existence of '$opt' in body");
}
}
# TODO: create billing profile first
my $billing_profile_id = 1;
# TODO: create customer contact first
my $customer_contact_id = 1;
# collection test
my $firstcontract = undef;
my $syscontact = undef;
my @allcontracts = ();
{
# first, create a contact
$req = HTTP::Request->new('POST', $uri.'/api/systemcontacts/');
$req->header('Content-Type' => 'application/json');
$req->content(JSON::to_json({
firstname => "sys_contact_first",
lastname => "sys_contact_last",
email => "sys_contact\@syscontact.invalid",
}));
$res = $ua->request($req);
ok($res->code == 201, "create system contact");
$req = HTTP::Request->new('GET', $uri.'/'.$res->header('Location'));
$res = $ua->request($req);
ok($res->code == 200, "fetch system contact");
$syscontact = JSON::from_json($res->decoded_content);
# create 6 new reseller contracts
my %contracts = ();
for(my $i = 1; $i <= 6; ++$i) {
$req = HTTP::Request->new('POST', $uri.'/api/contracts/');
$req->header('Content-Type' => 'application/json');
$req->content(JSON::to_json({
status => "active",
contact_id => $syscontact->{id},
type => "reseller",
billing_profile_id => $billing_profile_id,
}));
$res = $ua->request($req);
ok($res->code == 201, "create test reseller contract $i");
$contracts{$res->header('Location')} = 1;
push @allcontracts, $res->header('Location');
$firstcontract = $res->header('Location') unless $firstcontract;
}
# try to create invalid contract with wrong type
$req = HTTP::Request->new('POST', $uri.'/api/contracts/');
$req->header('Content-Type' => 'application/json');
$req->content(JSON::to_json({
status => "active",
contact_id => $syscontact->{id},
billing_profile_id => $billing_profile_id,
type => "invalid",
}));
$res = $ua->request($req);
ok($res->code == 422, "create contract with invalid type");
my $err = JSON::from_json($res->decoded_content);
ok($err->{code} eq "422", "check error code in body");
ok($err->{message} =~ /Invalid 'type'/, "check error message in body");
# try to create invalid contract with wrong billing profile
$req->content(JSON::to_json({
status => "active",
contact_id => $syscontact->{id},
type => "reseller",
billing_profile_id => 999999,
}));
$res = $ua->request($req);
ok($res->code == 422, "create contract with invalid billing profile");
$err = JSON::from_json($res->decoded_content);
ok($err->{code} eq "422", "check error code in body");
ok($err->{message} =~ /Invalid 'billing_profile_id'/, "check error message in body");
# try to create invalid contract with customercontact
$req->content(JSON::to_json({
status => "active",
type => "reseller",
billing_profile_id => $billing_profile_id,
contact_id => $customer_contact_id,
}));
$res = $ua->request($req);
ok($res->code == 422, "create contract with invalid contact");
$err = JSON::from_json($res->decoded_content);
ok($err->{code} eq "422", "check error code in body");
ok($err->{message} =~ /The contact_id is not a valid ngcp:systemcontacts item/, "check error message in body");
# try to create invalid contract with invalid status
$req->content(JSON::to_json({
type => "reseller",
billing_profile_id => $billing_profile_id,
contact_id => $syscontact->{id},
status => "invalid",
}));
$res = $ua->request($req);
ok($res->code == 422, "create contract with invalid status");
$err = JSON::from_json($res->decoded_content);
ok($err->{code} eq "422", "check error code in body");
ok($err->{message} =~ /field='status'/, "check error message in body");
# iterate over contracts collection to check next/prev links and status
my $nexturi = $uri.'/api/contracts/?page=1&rows=5';
do {
$res = $ua->get($nexturi);
ok($res->code == 200, "fetch contacts page");
my $collection = JSON::from_json($res->decoded_content);
my $selfuri = $uri . $collection->{_links}->{self}->{href};
ok($selfuri eq $nexturi, "check _links.self.href of collection");
my $colluri = URI->new($selfuri);
ok($collection->{total_count} > 0, "check 'total_count' of collection");
my %q = $colluri->query_form;
ok(exists $q{page} && exists $q{rows}, "check existence of 'page' and 'row' in 'self'");
my $page = int($q{page});
my $rows = int($q{rows});
if($page == 1) {
ok(!exists $collection->{_links}->{prev}->{href}, "check absence of 'prev' on first page");
} else {
ok(exists $collection->{_links}->{prev}->{href}, "check existence of 'prev'");
}
if(($collection->{total_count} / $rows) <= $page) {
ok(!exists $collection->{_links}->{next}->{href}, "check absence of 'next' on last page");
} else {
ok(exists $collection->{_links}->{next}->{href}, "check existence of 'next'");
}
if($collection->{_links}->{next}->{href}) {
$nexturi = $uri . $collection->{_links}->{next}->{href};
} else {
$nexturi = undef;
}
# TODO: I'd expect that to be an array ref in any case!
ok((ref $collection->{_links}->{'ngcp:contracts'} eq "ARRAY" ||
ref $collection->{_links}->{'ngcp:contracts'} eq "HASH"), "check if 'ngcp:contracts' is array/hash-ref");
# remove any contact we find in the collection for later check
if(ref $collection->{_links}->{'ngcp:contracts'} eq "HASH") {
# TODO: handle hashref
ok($collection->{_embedded}->{'ngcp:contracts'}->{status} ne "terminated", "check if we don't have terminated contracts in response");
ok(exists $collection->{_embedded}->{'ngcp:contracts'}->{_links}->{'ngcp:systemcontacts'}, "check presence of ngcp:systemcontacts relation");
ok(exists $collection->{_embedded}->{'ngcp:contracts'}->{_links}->{'ngcp:billingprofiles'}, "check presence of ngcp:billingprofiles relation");
ok(exists $collection->{_embedded}->{'ngcp:contracts'}->{_links}->{'ngcp:contractbalances'}, "check presence of ngcp:contractbalances relation");
delete $contracts{$collection->{_links}->{'ngcp:contracts'}->{href}};
} else {
foreach my $c(@{ $collection->{_links}->{'ngcp:contracts'} }) {
delete $contracts{$c->{href}};
}
foreach my $c(@{ $collection->{_embedded}->{'ngcp:contracts'} }) {
ok($c->{status} ne "terminated", "check if we don't have terminated contracts in response");
ok(exists $c->{_links}->{'ngcp:systemcontacts'}, "check presence of ngcp:systemcontacts relation");
ok(exists $c->{_links}->{'ngcp:billingprofiles'}, "check presence of ngcp:billingprofiles relation");
ok(exists $c->{_links}->{'ngcp:contractbalances'}, "check presence of ngcp:contractbalances relation");
delete $contracts{$c->{_links}->{self}->{href}};
}
}
} while($nexturi);
ok(keys %contracts == 0, "check if all test contracts have been found");
}
# test contacts item
{
$req = HTTP::Request->new('OPTIONS', $uri.'/'.$firstcontract);
$res = $ua->request($req);
ok($res->code == 200, "check options on item");
my @hopts = split /\s*,\s*/, $res->header('Allow');
my $opts = JSON::from_json($res->decoded_content);
ok(exists $opts->{methods} && ref $opts->{methods} eq "ARRAY", "check for valid 'methods' in body");
foreach my $opt(qw( GET HEAD OPTIONS PUT PATCH )) {
ok(grep(/^$opt$/, @hopts), "check for existence of '$opt' in Allow header");
ok(grep(/^$opt$/, @{ $opts->{methods} }), "check for existence of '$opt' in body");
}
foreach my $opt(qw( POST DELETE )) {
ok(!grep(/^$opt$/, @hopts), "check for absence of '$opt' in Allow header");
ok(!grep(/^$opt$/, @{ $opts->{methods} }), "check for absence of '$opt' in body");
}
$req = HTTP::Request->new('GET', $uri.'/'.$firstcontract);
$res = $ua->request($req);
ok($res->code == 200, "fetch one contract item");
my $contract = JSON::from_json($res->decoded_content);
ok(exists $contract->{status}, "check existence of status");
ok(exists $contract->{type}, "check existence of type");
ok(exists $contract->{billing_profile_id} && $contract->{billing_profile_id}->is_int, "check existence of billing_profile_id");
ok(exists $contract->{contact_id} && $contract->{contact_id}->is_int, "check existence of contact_id");
ok(exists $contract->{id} && $contract->{id}->is_int, "check existence of id");
# PUT same result again
my $old_contract = { %$contract };
delete $contract->{_links};
delete $contract->{_embedded};
$req = HTTP::Request->new('PUT', $uri.'/'.$firstcontract);
# check if it fails without content type
$req->remove_header('Content-Type');
$req->header('Prefer' => "return=minimal");
$res = $ua->request($req);
ok($res->code == 415, "check put missing content type");
# check if it fails with unsupported content type
$req->header('Content-Type' => 'application/xxx');
$res = $ua->request($req);
ok($res->code == 415, "check put invalid content type");
$req->remove_header('Content-Type');
$req->header('Content-Type' => 'application/json');
# check if it fails with missing Prefer
$req->remove_header('Prefer');
$res = $ua->request($req);
ok($res->code == 400, "check put missing prefer");
# check if it fails with invalid Prefer
$req->header('Prefer' => "return=invalid");
$res = $ua->request($req);
ok($res->code == 400, "check put invalid prefer");
$req->remove_header('Prefer');
$req->header('Prefer' => "return=representation");
# check if it fails with missing body
$res = $ua->request($req);
ok($res->code == 400, "check put no body");
# check if put is ok
$req->content(JSON::to_json($contract));
$res = $ua->request($req);
ok($res->code == 200, "check put successful");
my $new_contract = JSON::from_json($res->decoded_content);
is_deeply($old_contract, $new_contract, "check put if unmodified put returns the same");
# check if we have the proper links
ok(exists $new_contract->{_links}->{'ngcp:systemcontacts'}, "check put presence of ngcp:systemcontacts relation");
ok(exists $new_contract->{_links}->{'ngcp:billingprofiles'}, "check put presence of ngcp:billingprofiles relation");
ok(exists $new_contract->{_links}->{'ngcp:contractbalances'}, "check put presence of ngcp:contractbalances relation");
$req = HTTP::Request->new('PATCH', $uri.'/'.$firstcontract);
$req->header('Prefer' => 'return=representation');
$req->header('Content-Type' => 'application/json-patch+json');
$req->content(JSON::to_json(
[ { op => 'replace', path => '/status', value => 'pending' } ]
));
$res = $ua->request($req);
ok($res->code == 200, "check patched contract item");
my $mod_contact = JSON::from_json($res->decoded_content);
ok($mod_contact->{status} eq "pending", "check patched replace op");
ok($mod_contact->{_links}->{self}->{href} eq $firstcontract, "check patched self link");
ok($mod_contact->{_links}->{collection}->{href} eq '/api/contracts/', "check patched collection link");
$req->content(JSON::to_json(
[ { op => 'replace', path => '/status', value => undef } ]
));
$res = $ua->request($req);
ok($res->code == 422, "check patched undef status");
$req->content(JSON::to_json(
[ { op => 'replace', path => '/status', value => 'invalid' } ]
));
$res = $ua->request($req);
ok($res->code == 422, "check patched invalid status");
$req->content(JSON::to_json(
[ { op => 'replace', path => '/contact_id', value => 99999 } ]
));
$res = $ua->request($req);
ok($res->code == 422, "check patched invalid contact_id");
$req->content(JSON::to_json(
[ { op => 'replace', path => '/contact_id', value => $customer_contact_id } ]
));
$res = $ua->request($req);
ok($res->code == 422, "check patched customer contact_id");
$req->content(JSON::to_json(
[ { op => 'replace', path => '/billing_profile_id', value => undef } ]
));
$res = $ua->request($req);
ok($res->code == 422, "check patched undef billing_profile_id");
$req->content(JSON::to_json(
[ { op => 'replace', path => '/billing_profile_id', value => 99999 } ]
));
$res = $ua->request($req);
ok($res->code == 422, "check patched invalid billing_profile_id");
}
# terminate
{
# check if deletion of contact fails before terminating the contracts
$req = HTTP::Request->new('DELETE', $uri.'/'.$syscontact->{_links}->{self}->{href});
$res = $ua->request($req);
ok($res->code == 423, "check locked status for deleting used contact");
my $pc;
foreach my $contract(@allcontracts) {
$req = HTTP::Request->new('PATCH', $uri.'/'.$contract);
$req->header('Content-Type' => 'application/json-patch+json');
$req->header('Prefer' => 'return=representation');
$req->content(JSON::to_json([
{ "op" => "replace", "path" => "/status", "value" => "terminated" }
]));
$res = $ua->request($req);
ok($res->code == 200, "check termination of contract");
$pc = JSON::from_json($res->decoded_content);
ok($pc->{status} eq "terminated", "check termination status of contract");
}
# check if we can still get the terminated contract
$req = HTTP::Request->new('GET', $uri.'/'.$pc->{_links}->{self}->{href});
$res = $ua->request($req);
ok($res->code == 404, "check fetching of terminated contract");
# check if deletion of contact is now ok
# TODO: are we supposed to be able to delete a contact for a terminated
# contract? there are still DB contstraints in the way!
#$req = HTTP::Request->new('DELETE', $uri.'/'.$syscontact->{_links}->{self}->{href});
#$res = $ua->request($req);
#ok($res->code == 204, "check deletion of unused contact");
}
done_testing;
# vim: set tabstop=4 expandtab:

@ -206,12 +206,12 @@ my @allcontacts = ();
$req->header('Content-Type' => 'application/json-patch+json');
$req->content(JSON::to_json(
[ { op => 'add', path => '/firstname', value => 'patchedfirst' } ]
[ { op => 'replace', path => '/firstname', value => 'patchedfirst' } ]
));
$res = $ua->request($req);
ok($res->code == 200, "check patched contact item");
my $mod_contact = JSON::from_json($res->decoded_content);
ok($mod_contact->{firstname} eq "patchedfirst", "check patched add op");
ok($mod_contact->{firstname} eq "patchedfirst", "check patched replace op");
$req->content(JSON::to_json(
[ { op => 'replace', path => '/firstname', value => undef } ]

Loading…
Cancel
Save