You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by mi...@apache.org on 2017/05/24 02:40:11 UTC
[06/14] incubator-trafficcontrol git commit: Unit tests for new
capabilities & api_capabilities APIs
Unit tests for new capabilities & api_capabilities APIs
(cherry picked from commit 78f2f846074d61171a88aa78fccccd07ec994686)
Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/7a5ce13e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/7a5ce13e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/7a5ce13e
Branch: refs/heads/master
Commit: 7a5ce13e7d7249883450bcaac25676691c7e3ae8
Parents: 9546e8a
Author: naamashoresh <na...@qwilt.com>
Authored: Thu May 4 10:29:00 2017 +0300
Committer: Jeremy Mitchell <mi...@gmail.com>
Committed: Tue May 23 20:39:02 2017 -0600
----------------------------------------------------------------------
traffic_ops/app/t/api/1.2/api_capabilities.t | 232 ++++++++++++++++++++++
traffic_ops/app/t/api/1.2/capabilities.t | 183 +++++++++++++++++
2 files changed, 415 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/7a5ce13e/traffic_ops/app/t/api/1.2/api_capabilities.t
----------------------------------------------------------------------
diff --git a/traffic_ops/app/t/api/1.2/api_capabilities.t b/traffic_ops/app/t/api/1.2/api_capabilities.t
new file mode 100644
index 0000000..71371fe
--- /dev/null
+++ b/traffic_ops/app/t/api/1.2/api_capabilities.t
@@ -0,0 +1,232 @@
+package main;
+#
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use Mojo::Base -strict;
+use Test::More;
+use Test::Mojo;
+use DBI;
+use strict;
+use warnings;
+no warnings 'once';
+use warnings 'all';
+use Test::TestHelper;
+
+#no_transactions=>1 ==> keep fixtures after every execution, beware of duplicate data!
+#no_transactions=>0 ==> delete fixtures after every execution
+
+BEGIN { $ENV{MOJO_MODE} = "test" }
+
+my $schema = Schema->connect_to_database;
+my $dbh = Schema->database_handle;
+my $t = Test::Mojo->new('TrafficOps');
+
+my $false = 0;
+my $true = 1;
+
+Test::TestHelper->unload_core_data($schema);
+Test::TestHelper->load_core_data($schema);
+
+ok $t->post_ok( '/login', => form => { u => Test::TestHelper::ADMIN_USER, p => Test::TestHelper::ADMIN_USER_PASSWORD } )->status_is(302)
+ ->or( sub { diag $t->tx->res->content->asset->{content}; } ), 'Should login?';
+
+#clear the tables
+my $th = $dbh->prepare("TRUNCATE TABLE capability CASCADE");
+$th->execute();
+
+#add capabilities required for the tests (basic-read & cdn-write)
+my $description = "Basic read operations";
+my $cap_name = "basic-read";
+$t->post_ok("/api/1.2/capabilities" => {Accept => 'application/json'} => json => {
+ "name" => $cap_name, "description" => $description
+ })->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/response/description" => $description )
+ ->json_is( "/response/name" => $cap_name )
+ , 'Does capability details return?';
+
+$description = "CDN write operations";
+$cap_name = "cdn-write";
+$t->post_ok("/api/1.2/capabilities" => {Accept => 'application/json'} => json => {
+ "name" => $cap_name, "description" => $description
+ })->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/response/description" => $description )
+ ->json_is( "/response/name" => $cap_name )
+ , 'Does capability details return?';
+
+$t->get_ok("/api/1.2/api_capabilities")->status_is(200)->json_is( "/response", [] )->or( sub { diag $t->tx->res->content->asset->{content}; } );;
+
+# adding valid entry
+my $http_method = "GET";
+my $route = "sample/route";
+my $cap_name = "basic-read";
+$t->post_ok("/api/1.2/api_capabilities" => {Accept => 'application/json'} => json => {
+ "httpMethod" => $http_method, "route" => $route, "capName" => $cap_name
+ })->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/response/id" => 1 )
+ ->json_is( "/response/httpMethod" => $http_method )
+ ->json_is( "/response/route" => $route )
+ ->json_is( "/response/capName" => $cap_name )
+ , 'Does mapping details return?';
+
+#verifying the create worked
+$t->get_ok("/api/1.2/api_capabilities")->status_is(200)
+ ->json_is( "/response/0/id" => 1 )
+ ->json_is( "/response/0/httpMethod" => $http_method )
+ ->json_is( "/response/0/route" => $route )
+ ->json_is( "/response/0/capName" => $cap_name )
+ ->or( sub { diag $t->tx->res->content->asset->{content}; } );;
+
+#verifying get single
+$t->get_ok("/api/1.2/api_capabilities/1")->status_is(200)
+ ->json_is( "/response/0/id" => 1 )
+ ->json_is( "/response/0/httpMethod" => $http_method )
+ ->json_is( "/response/0/route" => $route )
+ ->json_is( "/response/0/capName" => $cap_name )
+ ->or( sub { diag $t->tx->res->content->asset->{content}; } );;
+
+#insert the same mapping twice - fails
+$t->post_ok("/api/1.2/api_capabilities" => {Accept => 'application/json'} => json => {
+ "httpMethod" => $http_method, "route" => $route, "capName" => $cap_name
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "HTTP method \'$http_method\', route \'$route\' are already mapped to capability: $cap_name" )
+ , 'Is same entry twice?';
+
+#edit a mapping
+my $cap_name_updated = "cdn-write";
+$t->put_ok("/api/1.2/api_capabilities/1" => {Accept => 'application/json'} => json => {
+ "httpMethod" => $http_method, "route" => $route, "capName" => $cap_name_updated
+ })->status_is(200)
+ ->json_is( "/response/id" => 1 )
+ ->json_is( "/response/httpMethod" => $http_method )
+ ->json_is( "/response/route" => $route )
+ ->json_is( "/response/capName" => $cap_name_updated )
+ , 'Did update succeed?';
+
+#get after update
+$t->get_ok("/api/1.2/api_capabilities/1" => {Accept => 'application/json'} )->status_is(200)
+ ->json_is( "/response/0/id" => 1 )
+ ->json_is( "/response/0/httpMethod" => $http_method )
+ ->json_is( "/response/0/route" => $route )
+ ->json_is( "/response/0/capName" => $cap_name_updated )
+ , 'Did get after update succeed?';
+
+#edit the mapping back
+$t->put_ok("/api/1.2/api_capabilities/1" => {Accept => 'application/json'} => json => {
+ "httpMethod" => $http_method, "route" => $route, "capName" => $cap_name
+ })->status_is(200)
+ ->json_is( "/response/id" => 1 )
+ ->json_is( "/response/httpMethod" => $http_method )
+ ->json_is( "/response/route" => $route )
+ ->json_is( "/response/capName" => $cap_name )
+ , 'Did update succeed?';
+
+#get after update
+$t->get_ok("/api/1.2/api_capabilities/1" => {Accept => 'application/json'} )->status_is(200)
+ ->json_is( "/response/0/id" => 1 )
+ ->json_is( "/response/0/httpMethod" => $http_method )
+ ->json_is( "/response/0/route" => $route )
+ ->json_is( "/response/0/capName" => $cap_name )
+ , 'Did get after update back succeed?';
+
+#insert another mapping
+my $http_method_post = "POST";
+my $route_sample2 = "sample/route2";
+$t->post_ok("/api/1.2/api_capabilities" => {Accept => 'application/json'} => json => {
+ "httpMethod" => $http_method_post, "route" => $route_sample2, "capName" => $cap_name
+ })->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/response/id" => 2 )
+ ->json_is( "/response/httpMethod" => $http_method_post )
+ ->json_is( "/response/route" => $route_sample2 )
+ ->json_is( "/response/capName" => $cap_name )
+ , 'Does mapping details return?';
+
+#get by cap name
+$t->get_ok("/api/1.2/api_capabilities/capability/$cap_name")->status_is(200)
+ ->json_is( "/response/0/id" => 1 )
+ ->json_is( "/response/0/httpMethod" => $http_method )
+ ->json_is( "/response/0/route" => $route )
+ ->json_is( "/response/0/capName" => $cap_name )
+ ->json_is( "/response/1/id" => 2 )
+ ->json_is( "/response/1/httpMethod" => $http_method_post )
+ ->json_is( "/response/1/route" => $route_sample2 )
+ ->json_is( "/response/1/capName" => $cap_name )
+ ->or( sub { diag $t->tx->res->content->asset->{content}; } );;
+
+#test delete
+$t->delete_ok("/api/1.2/api_capabilities/2")->status_is(200)
+ ->json_is( "/alerts/0/text" => "API-capability mapping deleted." )
+ , 'Did delete succeed?';
+
+#make sure mapping was deleted
+$t->get_ok("/api/1.2/api_capabilities/2")->status_is(200)->json_is( "/response", [] )->or( sub { diag $t->tx->res->content->asset->{content}; } );;
+
+#negative tests
+
+# adding invalid entry - no httpMethod
+$t->post_ok("/api/1.2/api_capabilities" => {Accept => 'application/json'} => json => {
+ "route" => $route, "capName" => $cap_name
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "HTTP method is required." )
+ , 'Was invalid insert (no httpMethod) reject correctly?';
+
+# adding invalid entry - no route
+$t->post_ok("/api/1.2/api_capabilities" => {Accept => 'application/json'} => json => {
+ "httpMethod" => $http_method, "capName" => $cap_name
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "Route is required." )
+ , 'Was invalid insert (no route) reject correctly?';
+
+# adding invalid entry - empty route
+$t->post_ok("/api/1.2/api_capabilities" => {Accept => 'application/json'} => json => {
+ "httpMethod" => $http_method, "capName" => $cap_name, "route" => ""
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "Route is required." )
+ , 'Was invalid insert (no route) reject correctly?';
+
+# adding invalid entry - no capName
+$t->post_ok("/api/1.2/api_capabilities" => {Accept => 'application/json'} => json => {
+ "httpMethod" => $http_method, "route" => $route
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "Capability name is required." )
+ , 'Was invalid insert (no capName) reject correctly?';
+
+# adding invalid entry - empty capName
+$t->post_ok("/api/1.2/api_capabilities" => {Accept => 'application/json'} => json => {
+ "httpMethod" => $http_method, "route" => $route, "capName" => ""
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "Capability name is required." )
+ , 'Was invalid insert (no capName) reject correctly?';
+
+# adding invalid entry - invalid httpMethod
+my $invalid_http_method = 'BAD';
+$t->post_ok("/api/1.2/api_capabilities" => {Accept => 'application/json'} => json => {
+ "httpMethod" => $invalid_http_method, "route" => $route, "capName" => $cap_name
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "HTTP method \'$invalid_http_method\' is invalid. Valid values are: DELETE, GET, PATCH, POST, PUT" )
+ , 'Was invalid insert (no capName) reject correctly?';
+
+# adding invalid entry - non-existing capability
+my $non_existing_cap = "non-existing";
+$t->post_ok("/api/1.2/api_capabilities" => {Accept => 'application/json'} => json => {
+ "httpMethod" => $http_method, "route" => $route, "capName" => $non_existing_cap
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "Capability \'$non_existing_cap\' does not exist." )
+ , 'Was invalid insert (no capName) reject correctly?';
+
+
+ok $t->get_ok('/logout')->status_is(302)->or( sub { diag $t->tx->res->content->asset->{content}; } );
+
+$dbh->disconnect();
+done_testing();
+
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/7a5ce13e/traffic_ops/app/t/api/1.2/capabilities.t
----------------------------------------------------------------------
diff --git a/traffic_ops/app/t/api/1.2/capabilities.t b/traffic_ops/app/t/api/1.2/capabilities.t
new file mode 100644
index 0000000..b1031ef
--- /dev/null
+++ b/traffic_ops/app/t/api/1.2/capabilities.t
@@ -0,0 +1,183 @@
+package main;
+#
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use Mojo::Base -strict;
+use Test::More;
+use Test::Mojo;
+use DBI;
+use strict;
+use warnings;
+no warnings 'once';
+use warnings 'all';
+use Test::TestHelper;
+
+#no_transactions=>1 ==> keep fixtures after every execution, beware of duplicate data!
+#no_transactions=>0 ==> delete fixtures after every execution
+
+BEGIN { $ENV{MOJO_MODE} = "test" }
+
+my $schema = Schema->connect_to_database;
+my $dbh = Schema->database_handle;
+my $t = Test::Mojo->new('TrafficOps');
+
+my $false = 0;
+my $true = 1;
+
+Test::TestHelper->unload_core_data($schema);
+Test::TestHelper->load_core_data($schema);
+
+ok $t->post_ok( '/login', => form => { u => Test::TestHelper::ADMIN_USER, p => Test::TestHelper::ADMIN_USER_PASSWORD } )->status_is(302)
+ ->or( sub { diag $t->tx->res->content->asset->{content}; } ), 'Should login?';
+
+#clear the table
+my $th = $dbh->prepare("TRUNCATE TABLE capability CASCADE");
+$th->execute();
+
+$t->get_ok("/api/1.2/capabilities")->status_is(200)->json_is( "/response", [] )->or( sub { diag $t->tx->res->content->asset->{content}; } );;
+
+# adding valid entry
+my $description = "Basic read operations";
+my $cap_name = "basic-read";
+$t->post_ok("/api/1.2/capabilities" => {Accept => 'application/json'} => json => {
+ "name" => $cap_name, "description" => $description
+ })->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/response/description" => $description )
+ ->json_is( "/response/name" => $cap_name )
+ , 'Does capability details return?';
+
+#verifying the create worked
+$t->get_ok("/api/1.2/capabilities")->status_is(200)
+ ->json_is( "/response/0/name" => $cap_name )
+ ->json_is( "/response/0/description" => $description )
+ ->or( sub { diag $t->tx->res->content->asset->{content}; } );;
+
+#verifying get by capapbility
+$t->get_ok("/api/1.2/capabilities/$cap_name")->status_is(200)
+ ->json_is( "/response/0/name" => $cap_name )
+ ->json_is( "/response/0/description" => $description )
+ ->or( sub { diag $t->tx->res->content->asset->{content}; } );;
+
+#insert the same capability twice - fails
+$t->post_ok("/api/1.2/capabilities" => {Accept => 'application/json'} => json => {
+ "name" => $cap_name, "description" => $description
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "Capability \'$cap_name\' already exists." )
+ , 'Is same entry twice?';
+
+#edit a capability
+my $description_updated = "edited desctiption";
+$t->put_ok("/api/1.2/capabilities/$cap_name" => {Accept => 'application/json'} => json => {
+ "description" => $description_updated
+ })->status_is(200)
+ ->json_is( "/response/name" => $cap_name )
+ ->json_is( "/response/description" => $description_updated )
+ , 'Did update succeed?';
+
+#get after update
+$t->get_ok("/api/1.2/capabilities/$cap_name" => {Accept => 'application/json'} )->status_is(200)
+ ->json_is( "/response/0/name" => $cap_name )
+ ->json_is( "/response/0/description" => $description_updated )
+ , 'Did get after update succeed?';
+
+#edit the mapping back
+$t->put_ok("/api/1.2/capabilities/$cap_name" => {Accept => 'application/json'} => json => {
+ "name" => $cap_name, "description" => $description
+ })->status_is(200)
+ ->json_is( "/response/name" => $cap_name )
+ ->json_is( "/response/description" => $description )
+ , 'Did update succeed?';
+
+#get after update
+$t->get_ok("/api/1.2/capabilities/$cap_name" => {Accept => 'application/json'} )->status_is(200)
+ ->json_is( "/response/0/name" => $cap_name )
+ ->json_is( "/response/0/description" => $description )
+ , 'Did get after update back succeed?';
+
+#insert another capability
+my $cap_name_basic_write = "basic-write";
+my $description_basic_write = "Basic write operations";
+$t->post_ok("/api/1.2/capabilities" => {Accept => 'application/json'} => json => {
+ "name" => $cap_name_basic_write, "description" => $description_basic_write
+ })->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/response/name" => $cap_name_basic_write )
+ ->json_is( "/response/description" => $description_basic_write )
+ , 'Does capability details return?';
+
+#get by cap name
+$t->get_ok("/api/1.2/capabilities/$cap_name_basic_write")->status_is(200)
+ ->json_is( "/response/0/name" => $cap_name_basic_write )
+ ->json_is( "/response/0/description" => $description_basic_write )
+ ->or( sub { diag $t->tx->res->content->asset->{content}; } );;
+
+#test delete
+$t->delete_ok("/api/1.2/capabilities/$cap_name_basic_write")->status_is(200)
+ ->json_is( "/alerts/0/text" => "Capability deleted." )
+ , 'Did delete succeed?';
+
+#make sure mapping was deleted
+$t->get_ok("/api/1.2/capabilities/$cap_name_basic_write")->status_is(200)->json_is( "/response", [] )->or( sub { diag $t->tx->res->content->asset->{content}; } );;
+
+#negative tests
+# adding invalid entry - no description
+$t->post_ok("/api/1.2/capabilities" => {Accept => 'application/json'} => json => {
+ "name" => $cap_name
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "Description is required." )
+ , 'Was invalid insert (no description) reject correctly?';
+
+# adding invalid entry - empty description
+$t->post_ok("/api/1.2/capabilities" => {Accept => 'application/json'} => json => {
+ "name" => $cap_name, "description" => ""
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "Description is required." )
+ , 'Was invalid insert (no description) reject correctly?';
+
+# adding invalid entry - no name
+$t->post_ok("/api/1.2/capabilities" => {Accept => 'application/json'} => json => {
+ "description" => $description
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "Name is required." )
+ , 'Was invalid insert (no route) reject correctly?';
+
+# adding invalid entry - empty name
+$t->post_ok("/api/1.2/capabilities" => {Accept => 'application/json'} => json => {
+ "description" => $description, "name" => ""
+ })->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/alerts/0/text" => "Name is required." )
+ , 'Was invalid insert (no route) reject correctly?';
+
+# trying to delete a referenced capability. first add a mapping to it.
+my $http_method = "GET";
+my $route = "sample/route";
+$t->post_ok("/api/1.2/api_capabilities" => {Accept => 'application/json'} => json => {
+ "httpMethod" => $http_method, "route" => $route, "capName" => $cap_name
+ })->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content};} )
+ ->json_is( "/response/id" => 1 )
+ ->json_is( "/response/httpMethod" => $http_method )
+ ->json_is( "/response/route" => $route )
+ ->json_is( "/response/capName" => $cap_name )
+ , 'Does mapping details return?';
+
+#test delete - should fail
+$t->delete_ok("/api/1.2/capabilities/$cap_name")->status_is(400)
+ ->json_is( "/alerts/0/text" => "Capability \'$cap_name\' is refered by an api_capability mapping: 1. Deletion failed." )
+ , 'Did delete succeed?';
+
+
+ok $t->get_ok('/logout')->status_is(302)->or( sub { diag $t->tx->res->content->asset->{content}; } );
+
+$dbh->disconnect();
+done_testing();
+