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/04/02 22:09:48 UTC

[7/9] incubator-trafficcontrol git commit: adding tenant to the user API

adding tenant to the user API


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

Branch: refs/heads/master
Commit: 3cbbb81f6260087d0516f7e23dcec7da54d2f044
Parents: 6903eae
Author: nir-sopher <ni...@gmail.com>
Authored: Mon Mar 20 10:41:08 2017 +0200
Committer: Jeremy Mitchell <mi...@gmail.com>
Committed: Sun Apr 2 16:08:51 2017 -0600

----------------------------------------------------------------------
 traffic_ops/app/lib/API/User.pm        |  22 ++++-
 traffic_ops/app/lib/Test/TestHelper.pm |   6 ++
 traffic_ops/app/lib/UI/Utils.pm        |  14 +++-
 traffic_ops/app/t/api/1.2/user.t       | 119 ++++++++++++++++++++--------
 4 files changed, 125 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/3cbbb81f/traffic_ops/app/lib/API/User.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/API/User.pm b/traffic_ops/app/lib/API/User.pm
index 690b176..51b68e8 100644
--- a/traffic_ops/app/lib/API/User.pm
+++ b/traffic_ops/app/lib/API/User.pm
@@ -97,7 +97,8 @@ sub index {
 				"rolename"        => $row->role->name,
 				"stateOrProvince" => $row->state_or_province,
 				"uid"             => $row->uid,
-				"username"        => $row->username
+				"username"        => $row->username,
+				"tenantId"        => $row->tenant_id
 			}
 		);
 	}
@@ -132,7 +133,8 @@ sub show {
 				"rolename"        => $row->role->name,
 				"stateOrProvince" => $row->state_or_province,
 				"uid"             => $row->uid,
-				"username"        => $row->username
+				"username"        => $row->username,
+				"tenantId"        => $row->tenant_id
 			}
 		);
 	}
@@ -159,6 +161,9 @@ sub update {
 		return $self->not_found();
 	}
 
+	#setting tenant_id to undef if tenant is not set. TODO(nirs): remove when tenancy is no longer optional in the API
+ 	my $tenant_id = exists($params->{tenantId}) ? $params->{tenantId} :  undef; 
+ 	
 	my $values = {
 		address_line1 			=> $params->{addressLine1},
 		address_line2 			=> $params->{addressLine2},
@@ -174,7 +179,9 @@ sub update {
 		registration_sent 		=> ( $params->{registrationSent} ) ? 1 : 0,
 		role 					=> $params->{role},
 		state_or_province 		=> $params->{stateOrProvince},
-		username 				=> $params->{username}
+		username 				=> $params->{username},
+		tenant_id 				=> $tenant_id
+		
 	};
 
 	if ( defined($params->{localPasswd}) && $params->{localPasswd} ne '' ) {
@@ -207,6 +214,7 @@ sub update {
 		$response->{stateOrProvince} 		= $rs->state_or_province;
 		$response->{uid} 					= $rs->uid;
 		$response->{username} 				= $rs->username;
+		$response->{tenantId} 				= $rs->tenant_id;
 
 		&log( $self, "Updated User with username '" . $rs->username . "' for id: " . $rs->id, "APICHANGE" );
 
@@ -277,6 +285,7 @@ sub current {
 			@data, {
 				"id"              => "0",
 				"username"        => $current_username,
+				"tenantId"	  => $self->current_user_tenant(),
 				"publicSshKey"  => "",
 				"role"            => $role,
 				"uid"             => "0",
@@ -321,6 +330,7 @@ sub current {
 					"phoneNumber"     => $row->phone_number,
 					"postalCode"      => $row->postal_code,
 					"country"         => $row->country,
+					"tenantId"        => $row->tenant_id,
 				}
 			);
 		}
@@ -375,6 +385,10 @@ sub update_current {
 		if ( defined( $user->{"username"} ) ) {
 			$db_user->{"username"} = $user->{"username"};
 		}
+		if ( exists( $user->{"tenantId"} ) ) {
+		        #if value is not set, it will be kept as is. Keeping consistency. Using "exists" and not "defined" to allow data clearing
+ 			$db_user->{"tenant_id"} = $user->{"tenantId"};
+ 		}
 		if ( defined( $user->{"public_ssh_key"} ) ) {
 			$db_user->{"public_ssh_key"} = $user->{"public_ssh_key"};
 		}
@@ -475,6 +489,8 @@ sub is_valid {
 					return $self->is_username_taken( $value, $params );
 				}
 			},
+			
+			#TODO(nirs) MAYBE when tenancy is not optional, add a tenant not null check
 
 		]
 	};

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/3cbbb81f/traffic_ops/app/lib/Test/TestHelper.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/Test/TestHelper.pm b/traffic_ops/app/lib/Test/TestHelper.pm
index b6d3174..2c57561 100644
--- a/traffic_ops/app/lib/Test/TestHelper.pm
+++ b/traffic_ops/app/lib/Test/TestHelper.pm
@@ -65,6 +65,12 @@ use constant STEERING_PASSWORD_1 => 'password';
 use constant STEERING_USER_2 => 'steering2';
 use constant STEERING_PASSWORD_2 => 'password';
 
+use constant ADMIN_ROOT_USER          => 'admin-root';
+use constant ADMIN_ROOT_USER_PASSWORD => 'password';
+
+use constant PORTAL_ROOT_USER          => 'portal-root';
+use constant PORTAL_ROOT_USER_PASSWORD => 'password';
+
 sub load_all_fixtures {
 	my $self    = shift;
 	my $fixture = shift;

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/3cbbb81f/traffic_ops/app/lib/UI/Utils.pm
----------------------------------------------------------------------
diff --git a/traffic_ops/app/lib/UI/Utils.pm b/traffic_ops/app/lib/UI/Utils.pm
index ddeb2d6..8345c0d 100644
--- a/traffic_ops/app/lib/UI/Utils.pm
+++ b/traffic_ops/app/lib/UI/Utils.pm
@@ -42,7 +42,7 @@ use constant ADMIN      => 30;
 our %EXPORT_TAGS = (
 	'all' => [
 		qw(trim_whitespace is_admin is_oper is_ldap is_privileged log is_ipaddress is_ip6address is_netmask in_same_net is_hostname admin_status_id type_id type_ids
-			profile_id profile_ids tm_version tm_url name_version_string is_regexp stash_role navbarpage rascal_hosts_by_cdn is_steering)
+			profile_id profile_ids tm_version tm_url name_version_string is_regexp stash_role navbarpage rascal_hosts_by_cdn is_steering defined_or_default current_user_tenant)
 	]
 );
 our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } );
@@ -401,4 +401,16 @@ sub exec_command {
 	return $result;
 }
 
+
+sub defined_or_default {
+	my $val = shift;
+	my $default = shift;
+	return defined($val) ? $val : $default;
+}
+
+sub current_user_tenant {
+    my $self = shift;
+    return $self->db->resultset('TmUser')->search( { username => $self->current_user()->{username} } )->get_column('tenant_id')->single();
+}    
+
 1;

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/3cbbb81f/traffic_ops/app/t/api/1.2/user.t
----------------------------------------------------------------------
diff --git a/traffic_ops/app/t/api/1.2/user.t b/traffic_ops/app/t/api/1.2/user.t
index 3501e8c..bda22c1 100644
--- a/traffic_ops/app/t/api/1.2/user.t
+++ b/traffic_ops/app/t/api/1.2/user.t
@@ -31,49 +31,104 @@ use Digest::SHA1 qw(sha1_hex);
 
 BEGIN { $ENV{MOJO_MODE} = "test" }
 
-my $dbh    = Schema->database_handle;
-my $schema = Schema->connect_to_database;
-my $t      = Test::Mojo->new('TrafficOps');
 
-Test::TestHelper->unload_core_data($schema);
-Test::TestHelper->teardown( $schema, 'Log' );
-Test::TestHelper->teardown( $schema, 'Role' );
-Test::TestHelper->teardown( $schema, 'TmUser' );
+sub run_ut {
+	my $t = shift;
+	my $schema = shift;
+	my $login_user = shift;
+	my $login_password = shift;
+
+	Test::TestHelper->unload_core_data($schema);
+	Test::TestHelper->teardown( $schema, 'Log' );
+	Test::TestHelper->teardown( $schema, 'Role' );
+	Test::TestHelper->teardown( $schema, 'TmUser' );
+
+	Test::TestHelper->load_core_data($schema);
+	
+	my $tenant_id = $schema->resultset('TmUser')->find( { username => $login_user } )->get_column('tenant_id');
+	my $tenant_name = defined ($tenant_id) ? $schema->resultset('Tenant')->find( { id => $tenant_id } )->get_column('name') : "null";
+
+	ok my $portal_user = $schema->resultset('TmUser')->find( { username => $login_user } ), 'Tenant $tenant_name: Does the portal user exist?';
 
-Test::TestHelper->load_core_data($schema);
+	# Verify the Portal user
+	$t->post_ok( '/api/1.2/user/login', json => { u => $login_user, p => $login_password} )->status_is(200);
+	$t->get_ok('/api/1.2/user/current.json')->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )
+		->json_is( "/response/username", $login_user )
+		->json_is( "/response/tenantId", $tenant_id);
 
-ok my $portal_user = $schema->resultset('TmUser')->find( { username => Test::TestHelper::PORTAL_USER } ), 'Does the portal user exist?';
+	# Test required fields
+	$t->post_ok( '/api/1.2/user/current/update',
+		json => { user => { username => $login_user, email => 'testportal1@kabletown.com', address_line1 => 'newaddress', tenantId => $tenant_id} } )
+		->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )
+		->json_is( "/alerts/0/text", "UserProfile was successfully updated." );
+	
+	#verify tenancy	
+	$t->get_ok('/api/1.2/user/current.json')->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )
+		->json_is( "/response/username", $login_user )
+		->json_is( "/response/tenantId", $tenant_id);
 
-# Verify the Portal user
-$t->post_ok( '/api/1.2/user/login', json => { u => Test::TestHelper::PORTAL_USER, p => Test::TestHelper::PORTAL_USER_PASSWORD } )->status_is(200);
-$t->get_ok('/api/1.2/user/current.json')->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )
-	->json_is( "/response/username", Test::TestHelper::PORTAL_USER );
+	# Test required fields
+	if (defined($tenant_id)){
+		#verify the update with no "tenant" do not removed the tenant
+		$t->post_ok( '/api/1.2/user/current/update',
+			json => { user => { username => $login_user, email => 'testportal1@kabletown.com', address_line1 => 'newaddress'} } )
+			->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )
+			->json_is( "/alerts/0/text", "UserProfile was successfully updated." );
+		#verify tenancy	
+		$t->get_ok('/api/1.2/user/current.json')->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )
+			->json_is( "/response/username", $login_user )
+			->json_is( "/response/tenantId", $tenant_id);
 
-# Test required fields
-$t->post_ok( '/api/1.2/user/current/update',
-	json => { user => { username => Test::TestHelper::PORTAL_USER, email => 'testportal1@kabletown.com', address_line1 => 'newaddress' } } )
-	->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )->json_is( "/alerts/0/text", "UserProfile was successfully updated." );
+		#removed the tenant
+		$t->post_ok( '/api/1.2/user/current/update',
+			json => { user => { username => $login_user, email => 'testportal1@kabletown.com', address_line1 => 'newaddress', tenantId => undef} } )
+			->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )
+			->json_is( "/alerts/0/text", "UserProfile was successfully updated." );
+		#verify tenancy	
+		$t->get_ok('/api/1.2/user/current.json')->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )
+			->json_is( "/response/username", $login_user )
+			->json_is( "/response/tenantId", undef);
+	
+		#putting the tenant back the update with no "tenant" removed the tenant
+		$t->post_ok( '/api/1.2/user/current/update',
+			json => { user => { username => $login_user, email => 'testportal1@kabletown.com', address_line1 => 'newaddress', tenantId => $tenant_id} } )
+			->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )
+			->json_is( "/alerts/0/text", "UserProfile was successfully updated." );
+		#verify tenancy	
+		$t->get_ok('/api/1.2/user/current.json')->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )
+			->json_is( "/response/username", $login_user )
+			->json_is( "/response/tenantId", $tenant_id);
+	}
+	
+	# Ensure unique emails
+	ok $t->post_ok( '/api/1.2/user/current/update', json => { user => { username => $login_user, email => 'testportal1@kabletown.com', tenantId => $tenant_id } } )
+		->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )->json_is( "/alerts/0/level", "success" ),
+		"Tenant $tenant_name: Verify that the emails are unique";
 
-# Ensure unique emails
-ok $t->post_ok( '/api/1.2/user/current/update', json => { user => { username => Test::TestHelper::PORTAL_USER, email => 'testportal1@kabletown.com' } } )
-	->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } )->json_is( "/alerts/0/level", "success" ),
-	"Verify that the emails are unique";
+	ok $t->post_ok( '/api/1.2/user/current/update', json => { user => { username => $login_user, email => '@kabletown.com', tenantId => $tenant_id } } )
+		->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content}; } )->json_is( "/alerts/0/level", "error" ),
+		"Tenant $tenant_name: Verify that the emails are properly formatted";
 
-ok $t->post_ok( '/api/1.2/user/current/update', json => { user => { username => Test::TestHelper::PORTAL_USER, email => '@kabletown.com' } } )
-	->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content}; } )->json_is( "/alerts/0/level", "error" ),
-	"Verify that the emails are properly formatted";
+	ok $t->post_ok( '/api/1.2/user/current/update', json => { user => { username => $login_user, email => '@kabletown.com', tenantId => $tenant_id } } )
+		->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content}; } )->json_is( "/alerts/0/level", "error" ),
+		"Tenant $tenant_name: Verify that the usernames are unique";
 
-ok $t->post_ok( '/api/1.2/user/current/update', json => { user => { username => Test::TestHelper::PORTAL_USER, email => '@kabletown.com' } } )
-	->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content}; } )->json_is( "/alerts/0/level", "error" ),
-	"Verify that the usernames are unique";
+	$t->post_ok( '/api/1.2/user/current/update', json => { user => { email => 'testportal1@kabletown.com', tenantId => $tenant_id } } )->status_is(400)
+		->or( sub { diag $t->tx->res->content->asset->{content}; } )->json_is( "/alerts/0/text", "username is required" );
 
-$t->post_ok( '/api/1.2/user/current/update', json => { user => { email => 'testportal1@kabletown.com' } } )->status_is(400)
-	->or( sub { diag $t->tx->res->content->asset->{content}; } )->json_is( "/alerts/0/text", "username is required" );
+	$t->post_ok( '/api/1.2/user/current/update', json => { user => { username => $login_user, tenantId => $tenant_id } } )->status_is(400)
+		->or( sub { diag $t->tx->res->content->asset->{content}; } )->json_is( "/alerts/0/text", "email is required" );
+
+	ok $t->post_ok('/api/1.2/user/logout')->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } );
+}
+
+my $dbh    = Schema->database_handle;
+my $schema = Schema->connect_to_database;
+my $t      = Test::Mojo->new('TrafficOps');
 
-$t->post_ok( '/api/1.2/user/current/update', json => { user => { username => Test::TestHelper::PORTAL_USER } } )->status_is(400)
-	->or( sub { diag $t->tx->res->content->asset->{content}; } )->json_is( "/alerts/0/text", "email is required" );
+run_ut($t, $schema, Test::TestHelper::PORTAL_USER,  Test::TestHelper::PORTAL_USER_PASSWORD);
+run_ut($t, $schema, Test::TestHelper::PORTAL_ROOT_USER,  Test::TestHelper::PORTAL_ROOT_USER_PASSWORD);
 
-ok $t->post_ok('/api/1.2/user/logout')->status_is(200)->or( sub { diag $t->tx->res->content->asset->{content}; } );
 $dbh->disconnect();
 
 done_testing();