Adopt tests from vouchers. Suggest to use in vouchers.
Change-Id: Ia157c2c6028a2d2d323fdc1d1d766e6e1f182e56
(cherry picked from commit dd5c984e61)
			
			
				changes/80/1880/16
			
			
		
							parent
							
								
									c7db27fb34
								
							
						
					
					
						commit
						039fd497b5
					
				| @ -0,0 +1,46 @@ | ||||
| #use Sipwise::Base; | ||||
| use strict; | ||||
| 
 | ||||
| #use Moose; | ||||
| use Sipwise::Base; | ||||
| use Test::Collection; | ||||
| use Test::FakeData; | ||||
| use Net::Domain qw(hostfqdn); | ||||
| use LWP::UserAgent; | ||||
| use HTTP::Request::Common; | ||||
| use JSON; | ||||
| use Test::More; | ||||
| use Data::Dumper; | ||||
| 
 | ||||
| 
 | ||||
| #init test_machine | ||||
| my $test_machine = Test::Collection->new( | ||||
|     name => 'billingzones', | ||||
| ); | ||||
| 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({ | ||||
|     'billingzones' => { | ||||
|         data => { | ||||
|             billing_profile_id => sub { return shift->get_id('billingprofiles', @_); }, | ||||
|             zone               => "apitestzone", | ||||
|             detail             => "api_test zone", | ||||
|         }, | ||||
|         'query' => ['zone'], | ||||
|     }, | ||||
| }); | ||||
| 
 | ||||
| $test_machine->DATA_ITEM_STORE($fake_data->process('billingzones')); | ||||
| $test_machine->form_data_item( ); | ||||
| 
 | ||||
| # create 3 new billing zones from DATA_ITEM | ||||
| $test_machine->check_create_correct( 3, sub{ $_[0]->{zone} .= $_[1]->{i} ; } ); | ||||
| $test_machine->check_get2put(  ); | ||||
| $test_machine->check_bundle(); | ||||
| $test_machine->clear_test_data_all(); | ||||
| done_testing; | ||||
| 
 | ||||
| # vim: set tabstop=4 expandtab: | ||||
| @ -1,199 +1,73 @@ | ||||
| #use Sipwise::Base; | ||||
| use strict; | ||||
| 
 | ||||
| #use Moose; | ||||
| use Sipwise::Base; | ||||
| use Net::Domain qw(hostfqdn); | ||||
| use LWP::UserAgent; | ||||
| use JSON qw(); | ||||
| use Test::Collection; | ||||
| use Test::FakeData; | ||||
| use Test::More; | ||||
| use Storable qw(); | ||||
| use Data::Printer; | ||||
| 
 | ||||
| use JSON::PP; | ||||
| use LWP::Debug; | ||||
| 
 | ||||
| BEGIN { | ||||
|     unshift(@INC,'../lib'); | ||||
| } | ||||
| 
 | ||||
| my $json = JSON::PP->new(); | ||||
| $json->allow_blessed(1); | ||||
| $json->convert_blessed(1); | ||||
| 
 | ||||
| my $is_local_env = $ENV{LOCAL_TEST} // 0; | ||||
| my $mysql_sqlstrict = 1; #https://bugtracker.sipwise.com/view.php?id=12565 | ||||
| 
 | ||||
| use Config::General; | ||||
| my $catalyst_config; | ||||
| if ($is_local_env) { | ||||
|     my $panel_config; | ||||
|     for my $path(qw#../ngcp_panel.conf ngcp_panel.conf#) { | ||||
|         if(-f $path) { | ||||
|             $panel_config = $path; | ||||
|             last; | ||||
|         } | ||||
|     } | ||||
|     $panel_config //= '../ngcp_panel.conf'; | ||||
|     $catalyst_config = Config::General->new($panel_config);    | ||||
| } else { | ||||
|     #taken 1:1 from /lib/NGCP/Panel.pm | ||||
|     my $panel_config; | ||||
|     for my $path(qw#/etc/ngcp-panel/ngcp_panel.conf etc/ngcp_panel.conf ngcp_panel.conf#) { | ||||
|         if(-f $path) { | ||||
|             $panel_config = $path; | ||||
|             last; | ||||
|         } | ||||
|     } | ||||
|     $panel_config //= 'ngcp_panel.conf'; | ||||
|     $catalyst_config = Config::General->new($panel_config);    | ||||
| } | ||||
| my %config = $catalyst_config->getall(); | ||||
| 
 | ||||
| my $uri = $ENV{CATALYST_SERVER} || ('https://'.hostfqdn.':4443'); | ||||
| use Data::Dumper; | ||||
| 
 | ||||
| my $valid_ssl_client_cert = $ENV{API_SSL_CLIENT_CERT} ||  | ||||
|     "/etc/ngcp-panel/api_ssl/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/ngcp-panel/api_ssl/api_ca.crt"; | ||||
| 
 | ||||
| my ($ua, $req, $res); | ||||
| $ua = LWP::UserAgent->new; | ||||
| 
 | ||||
| if ($is_local_env) { | ||||
|     $ua->ssl_opts( | ||||
|         verify_hostname => 0, | ||||
|     ); | ||||
|     my $realm = $uri; $realm =~ s/^https?:\/\///; | ||||
|     $ua->credentials($realm, "api_admin_http", 'administrator', 'administrator'); | ||||
|     #$ua->timeout(500); #useless, need to change the nginx timeout | ||||
| } else { | ||||
|     $ua->ssl_opts( | ||||
|         SSL_cert_file => $valid_ssl_client_cert, | ||||
|         SSL_key_file  => $valid_ssl_client_key, | ||||
|         SSL_ca_file   => $ssl_ca_cert, | ||||
| #init test_machine | ||||
| my $test_machine = Test::Collection->new( | ||||
|     name => 'vouchers', | ||||
| ); | ||||
| } | ||||
| 
 | ||||
| my $t = time; | ||||
| my $default_reseller_id = 1; | ||||
| my $fake_data = Test::FakeData->new; | ||||
| 
 | ||||
| test_voucher(); | ||||
| done_testing(); | ||||
| $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)}; | ||||
| 
 | ||||
| 
 | ||||
| sub test_voucher { | ||||
|     my $code = 'testcode'.$t; | ||||
|     my $voucher = { | ||||
| $fake_data->set_data_from_script({ | ||||
|     'vouchers' => { | ||||
|         data => { | ||||
|             amount => 100, | ||||
|         code => $code, | ||||
|             code => 'apitestcode', | ||||
|             customer_id => undef, | ||||
|         reseller_id => $default_reseller_id, | ||||
|             reseller_id => sub { return shift->get_id('resellers', @_); },, | ||||
|             valid_until => '2037-01-01 12:00:00', | ||||
|     }; | ||||
|     $req = HTTP::Request->new('POST', $uri.'/api/vouchers/'); | ||||
|     $req->header('Content-Type' => 'application/json'); | ||||
|     $req->content(JSON::to_json($voucher)); | ||||
|     $res = $ua->request($req); | ||||
|     is($res->code, 201, _get_request_test_message("POST test voucher")); | ||||
|     my $voucher_uri = $uri.'/'.$res->header('Location'); | ||||
|     $req = HTTP::Request->new('GET', $voucher_uri); | ||||
|     $res = $ua->request($req); | ||||
|     is($res->code, 200, _get_request_test_message("fetch POSTed test voucher")); | ||||
|     my $post_voucher = JSON::from_json($res->decoded_content); | ||||
|     delete $post_voucher->{_links}; | ||||
|     my $voucher_id = delete $post_voucher->{id}; | ||||
|     is_deeply($voucher, $post_voucher, "check POSTed voucher against fetched"); | ||||
|     $post_voucher->{id} = $voucher_id; | ||||
| 
 | ||||
|     $req = HTTP::Request->new('PUT', $voucher_uri); | ||||
|     $req->header('Content-Type' => 'application/json'); | ||||
|     $req->header('Prefer' => 'return=representation'); | ||||
|     $req->content(JSON::to_json($post_voucher)); | ||||
|     $res = $ua->request($req); | ||||
|     is($res->code, 200, _get_request_test_message("PUT test voucher")); | ||||
|     $req = HTTP::Request->new('GET', $voucher_uri); | ||||
|     $res = $ua->request($req); | ||||
|     is($res->code, 200, _get_request_test_message("fetch PUT test voucher")); | ||||
|     my $put_voucher = JSON::from_json($res->decoded_content); | ||||
|     delete $put_voucher->{_links}; | ||||
|     $voucher_id = delete $put_voucher->{id}; | ||||
|     is_deeply($voucher, $put_voucher, "check PUTed voucher against POSTed voucher"); | ||||
| 
 | ||||
|     $req = HTTP::Request->new('PATCH', $voucher_uri); | ||||
|     $req->header('Content-Type' => 'application/json-patch+json'); | ||||
|     $req->header('Prefer' => 'return=representation'); | ||||
|     $req->content(JSON::to_json([{op=>"replace", path=>"/code", value=>$put_voucher->{code}}])); | ||||
|     $res = $ua->request($req); | ||||
|     is($res->code, 200, _get_request_test_message("PATCH test voucher")); | ||||
|     $req = HTTP::Request->new('GET', $voucher_uri); | ||||
|     $res = $ua->request($req); | ||||
|     is($res->code, 200, _get_request_test_message("fetch PATCH test voucher")); | ||||
|     my $patch_voucher = JSON::from_json($res->decoded_content); | ||||
|     delete $patch_voucher->{_links}; | ||||
|     $voucher_id = delete $patch_voucher->{id}; | ||||
|     is_deeply($voucher, $patch_voucher, "check PATCHed voucher against POSTed voucher"); | ||||
|         }, | ||||
|         'query' => ['code'], | ||||
|     }, | ||||
| }); | ||||
| 
 | ||||
| $test_machine->DATA_ITEM_STORE($fake_data->process('vouchers')); | ||||
| $test_machine->form_data_item( ); | ||||
| 
 | ||||
|     $req = HTTP::Request->new('POST', $uri.'/api/vouchers/'); | ||||
|     $req->header('Content-Type' => 'application/json'); | ||||
|     $req->content(JSON::to_json($put_voucher)); | ||||
|     $res = $ua->request($req); | ||||
|     is($res->code, 422, _get_request_test_message("POST same voucher code again")); | ||||
| # create 3 new vouchers from DATA_ITEM | ||||
| $test_machine->check_create_correct( 3, sub{ $_[0]->{code} .= $_[1]->{i} ; } ); | ||||
| $test_machine->check_get2put(); | ||||
| $test_machine->check_bundle(); | ||||
| 
 | ||||
|     $put_voucher->{id} = $voucher_id; | ||||
| my $voucher = $test_machine->{DATA_ITEM}; | ||||
| print Dumper $voucher; | ||||
| my $voucher_uri; | ||||
| 
 | ||||
| { | ||||
|     #todo: move request processing results to separate package inside collection, to don't return these chains | ||||
|     my($res_post,$result_item_post,$req_post,$content_post_in,$location_post,$content_get) = $test_machine->check_post2get(); | ||||
|     my($res_put,$result_item_put,$req_put,$item_put_data,$get_res,$result_item_get,$get_req) = $test_machine->check_put2get(undef, undef, $location_post); | ||||
| 
 | ||||
| #    $req = HTTP::Request->new('PATCH', $billingzone_uri); | ||||
| #    $req->header('Content-Type' => 'application/json-patch+json'); | ||||
| #    $req->header('Prefer' => 'return=representation'); | ||||
| #    $req->content(JSON::to_json( | ||||
| #        [ { op => 'replace', path => '/zone', value => 'AT' } ] | ||||
| #    )); | ||||
| #    $res = $ua->request($req); | ||||
| #    is($res->code, 200, _get_request_test_message("PATCH test billingzone")); | ||||
| #    $req = HTTP::Request->new('GET', $billingzone_uri); | ||||
| #    $res = $ua->request($req); | ||||
| #    is($res->code, 200, _get_request_test_message("fetch PATCHed test billingzone")); | ||||
| #    $billingzone = JSON::from_json($res->decoded_content); | ||||
|     $voucher_uri = $location_post; | ||||
|     $voucher = $result_item_get; | ||||
|      | ||||
| 
 | ||||
|     # mysql has an issue with datetime overruns,check for max date | ||||
|     $req = HTTP::Request->new('PATCH', $voucher_uri); | ||||
|     $req->header('Content-Type' => 'application/json-patch+json'); | ||||
|     $req->header('Prefer' => 'return=representation'); | ||||
|     $req->content(JSON::to_json( | ||||
|         [ { op => 'replace', path => '/valid_until', value => '2099-01-01 00:00:00' } ] | ||||
|     )); | ||||
|     $res = $ua->request($req); | ||||
|     is($res->code, 422, _get_request_test_message("PATCH too far valid_until in voucher")); | ||||
| 
 | ||||
|     $req = HTTP::Request->new('DELETE', $voucher_uri); | ||||
|     $res = $ua->request($req); | ||||
|     is($res->code, 204, _get_request_test_message("delete POSTed test voucher")); | ||||
|     $req = HTTP::Request->new('GET', $voucher_uri); | ||||
|     $res = $ua->request($req); | ||||
|     is($res->code, 404, _get_request_test_message("fetch DELETEd test voucher")); | ||||
|     my($res,$result_item,$req) = $test_machine->request_post(undef,$voucher); | ||||
|     $test_machine->http_code_msg(422, "POST same voucher code again", $res, $result_item); | ||||
| } | ||||
| 
 | ||||
| sub _to_json { | ||||
|     return $json->encode(shift); | ||||
| { | ||||
|     my($res,$content) = $test_machine->request_patch(  [ { op => 'replace', path => '/valid_until', value => '2099-01-01 00:00:00' } ] ); | ||||
|     $test_machine->http_code_msg(422, "check patched invalid billing_zone_id",$res,,$content); | ||||
| } | ||||
| 
 | ||||
| sub _from_json { | ||||
|     return $json->decode(shift); | ||||
| } | ||||
| $test_machine->clear_test_data_all(); | ||||
| 
 | ||||
| sub _get_request_test_message { | ||||
|     my ($message) = @_; | ||||
|     my $code = $res->code; | ||||
|     if ($code == 200 || $code == 201 || $code == 204) { | ||||
|         return $message; | ||||
|     } else { | ||||
|         my $error_content = _from_json($res->content); | ||||
|         if (defined $error_content && defined $error_content->{message}) { | ||||
|             return $message . ' (' . $res->message . ': ' . $error_content->{message} . ')'; | ||||
|         } else { | ||||
|             return $message . ' (' . $res->message . ')'; | ||||
|         } | ||||
|     } | ||||
| { | ||||
|     my $uri = $test_machine->get_uri($voucher->{id}); | ||||
|     my($req,$res,$content) = $test_machine->request_delete($uri); | ||||
|     $test_machine->http_code_msg(204, "check delete of voucher", $res, $content); | ||||
|     ($res, $content, $req) = $test_machine->request_get($uri); | ||||
|     is($res->code, 404, "check if deleted voucher is really gone"); | ||||
| } | ||||
| done_testing; | ||||
| 
 | ||||
| # vim: set tabstop=4 expandtab: | ||||
|  | ||||
					Loading…
					
					
				
		Reference in new issue