A tenant cannot be deleted if a user is assigned to it

Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/0219eded
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/0219eded
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/0219eded

Branch: refs/heads/master
Commit: 0219eded052f81920332fc9e1324b52c320aca8f
Parents: 659f72d
Author: nir-sopher <nirsop...@gmail.com>
Authored: Mon Mar 20 10:44:26 2017 +0200
Committer: Jeremy Mitchell <mitchell...@gmail.com>
Committed: Sun Apr 2 16:08:51 2017 -0600

----------------------------------------------------------------------
 traffic_ops/app/lib/API/Tenant.pm  | 7 +++++++
 traffic_ops/app/t/api/1.2/tenant.t | 6 ++++++
 2 files changed, 13 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/0219eded/traffic_ops/app/lib/API/Tenant.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/API/Tenant.pm 
b/traffic_ops/app/lib/API/Tenant.pm
index bb62c64..d9167e7 100644
--- a/traffic_ops/app/lib/API/Tenant.pm
+++ b/traffic_ops/app/lib/API/Tenant.pm
@@ -224,6 +224,13 @@ sub delete {
                return $self->alert("Tenant '$name' has children tenant(s): e.g 
'$existing_child'. Please update these tenants and retry.");
        }
 
+       #The order of the below tests is intentional - allowing UT to cover all 
cases - TODO(nirs) remove this comment when a full "tenancy" UT is added, 
including permissions and such (no use in putting effort into it yet)
+
+       my $existing_user = $self->db->resultset('TmUser')->search( { tenant_id 
=> $id })->get_column('username')->first();
+       if ($existing_user) {
+               return $self->alert("Tenant '$name' is assign with user(s): 
e.g. '$existing_user'. Please update these users and retry.");
+       }
+
        my $rs = $tenant->delete();
        if ($rs) {
                return $self->success_message("Tenant deleted.");

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/0219eded/traffic_ops/app/t/api/1.2/tenant.t
----------------------------------------------------------------------
diff --git a/traffic_ops/app/t/api/1.2/tenant.t 
b/traffic_ops/app/t/api/1.2/tenant.t
index def39ae..7ebc8a6 100644
--- a/traffic_ops/app/t/api/1.2/tenant.t
+++ b/traffic_ops/app/t/api/1.2/tenant.t
@@ -152,6 +152,12 @@ ok $t->delete_ok('/api/1.2/tenants/' . 
$tenantE_id)->status_is(200)->or( sub { d
 ok $t->delete_ok('/api/1.2/tenants/' . $tenantD_id)->status_is(200)->or( sub { 
diag $t->tx->res->content->asset->{content}; } );
 ok $t->delete_ok('/api/1.2/tenants/' . $tenantA_id)->status_is(200)->or( sub { 
diag $t->tx->res->content->asset->{content}; } );
 
+#TODO(nirs): move to a "tenancy" UT when written
+#cannot delete a tenant that have a user
+ok $t->delete_ok('/api/1.2/tenants/' . 10**9)->status_is(400)
+       ->json_is( "/alerts/0/text" => "Tenant 'root' is assign with user(s): 
e.g. 'admin-root'. Please update these users and retry." )
+       ->or( sub { diag $t->tx->res->content->asset->{content}; } );
+
 ok $t->get_ok('/logout')->status_is(302)->or( sub { diag 
$t->tx->res->content->asset->{content}; } );
 $dbh->disconnect();
 done_testing();

Reply via email to