def test_get_expanded_subscription_info(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) sub_id = self.id1.split('/')[-1] #testing dump of existing logged in account extended_info = profile.get_expanded_subscription_info() self.assertEqual(self.user1, extended_info['userName']) self.assertEqual(sub_id, extended_info['subscriptionId']) self.assertEqual(self.display_name1, extended_info['subscriptionName']) self.assertEqual('https://login.microsoftonline.com', extended_info['endpoints'].active_directory) #testing dump of service principal by 'create-for-rbac' extended_info = profile.get_expanded_subscription_info( name='great-sp', password='******') self.assertEqual(sub_id, extended_info['subscriptionId']) self.assertEqual(self.display_name1, extended_info['subscriptionName']) self.assertEqual('great-sp', extended_info['client']) self.assertEqual('https://login.microsoftonline.com', extended_info['endpoints'].active_directory)
def test_get_login_credentials(self, mock_get_token, mock_read_cred_file): some_token_type = 'Bearer' mock_read_cred_file.return_value = [Test_Profile.token_entry1] mock_get_token.return_value = (some_token_type, Test_Profile.raw_token1) #setup storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False, ENV_DEFAULT) profile._set_subscriptions(consolidated) #action cred, subscription_id, _ = profile.get_login_credentials() #verify self.assertEqual(subscription_id, '1') #verify the cred._tokenRetriever is a working lambda token_type, token = cred._token_retriever() self.assertEqual(token, self.raw_token1) self.assertEqual(some_token_type, token_type) self.assertEqual(mock_read_cred_file.call_count, 1) mock_get_token.assert_called_once_with(mock.ANY, self.user1, self.tenant_id, 'https://management.core.windows.net/') self.assertEqual(mock_get_token.call_count, 1)
def test_get_raw_token(self, mock_get_token, mock_read_cred_file): some_token_type = 'Bearer' mock_read_cred_file.return_value = [Test_Profile.token_entry1] mock_get_token.return_value = (some_token_type, Test_Profile.raw_token1, Test_Profile.token_entry1) # setup storage_mock = {'subscriptions': None} profile = Profile(storage_mock, use_global_creds_cache=False) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) # action creds, sub, tenant = profile.get_raw_token(resource='https://foo') # verify self.assertEqual(creds[0], self.token_entry1['tokenType']) self.assertEqual(creds[1], self.raw_token1) # the last in the tuple is the whole token entry which has several fields self.assertEqual(creds[2]['expiresOn'], self.token_entry1['expiresOn']) mock_get_token.assert_called_once_with(mock.ANY, self.user1, self.tenant_id, 'https://foo') self.assertEqual(mock_get_token.call_count, 1) self.assertEqual(sub, '1') self.assertEqual(tenant, self.tenant_id)
def test_get_login_credentials(self, mock_get_token, mock_read_cred_file): some_token_type = 'Bearer' mock_read_cred_file.return_value = [Test_Profile.token_entry1] mock_get_token.return_value = (some_token_type, Test_Profile.raw_token1) #setup storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) #action cred, subscription_id, _ = profile.get_login_credentials() #verify self.assertEqual(subscription_id, '1') #verify the cred._tokenRetriever is a working lambda token_type, token = cred._token_retriever() self.assertEqual(token, self.raw_token1) self.assertEqual(some_token_type, token_type) self.assertEqual(mock_read_cred_file.call_count, 1) mock_get_token.assert_called_once_with( mock.ANY, self.user1, self.tenant_id, 'https://management.core.windows.net/') self.assertEqual(mock_get_token.call_count, 1)
def test_get_expanded_subscription_info(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) sub_id = self.id1.split('/')[-1] #testing dump of existing logged in account extended_info = profile.get_expanded_subscription_info() self.assertEqual(self.user1, extended_info['userName']) self.assertEqual(sub_id, extended_info['subscriptionId']) self.assertEqual(self.display_name1, extended_info['subscriptionName']) self.assertEqual('https://login.microsoftonline.com', extended_info['endpoints'].active_directory) #testing dump of service principal by 'create-for-rbac' extended_info = profile.get_expanded_subscription_info(name='great-sp', password='******') self.assertEqual(sub_id, extended_info['subscriptionId']) self.assertEqual(self.display_name1, extended_info['subscriptionName']) self.assertEqual('great-sp', extended_info['client']) self.assertEqual('https://login.microsoftonline.com', extended_info['endpoints'].active_directory)
def test_get_auth_info_fail_on_user_account(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock, use_global_creds_cache=False) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) # testing dump of existing logged in account self.assertRaises(CLIError, profile.get_sp_auth_info)
def test_default_active_subscription_to_non_disabled_one(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock, use_global_creds_cache=False) subscriptions = profile._normalize_properties( self.user2, [self.subscription2, self.subscription1], False) profile._set_subscriptions(subscriptions) # verify we skip the overdued subscription and default to the 2nd one in the list self.assertEqual(storage_mock['subscriptions'][1]['name'], self.subscription1.display_name) self.assertTrue(storage_mock['subscriptions'][1]['isDefault'])
def test_default_active_subscription_to_non_disabled_one(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock) subscriptions = profile._normalize_properties( self.user2, [self.subscription2, self.subscription1], False) profile._set_subscriptions(subscriptions) # verify we skip the overdued subscription and default to the 2nd one in the list self.assertEqual(storage_mock['subscriptions'][1]['name'], self.subscription1.display_name) self.assertTrue(storage_mock['subscriptions'][1]['isDefault'])
def test_update_add_two_different_subscriptions(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock) #add the first and verify consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) self.assertEqual(len(storage_mock['subscriptions']), 1) subscription1 = storage_mock['subscriptions'][0] self.assertEqual( subscription1, { 'environmentName': 'AzureCloud', 'id': '1', 'name': self.display_name1, 'state': self.state1.value, 'user': { 'name': self.user1, 'type': 'user' }, 'isDefault': True, 'tenantId': self.tenant_id }) #add the second and verify consolidated = Profile._normalize_properties(self.user2, [self.subscription2], False) profile._set_subscriptions(consolidated) self.assertEqual(len(storage_mock['subscriptions']), 2) subscription2 = storage_mock['subscriptions'][1] self.assertEqual( subscription2, { 'environmentName': 'AzureCloud', 'id': '2', 'name': self.display_name2, 'state': self.state2.value, 'user': { 'name': self.user2, 'type': 'user' }, 'isDefault': True, 'tenantId': self.tenant_id }) #verify the old one stays, but no longer active self.assertEqual(storage_mock['subscriptions'][0]['name'], subscription1['name']) self.assertFalse(storage_mock['subscriptions'][0]['isDefault'])
def test_update_add_two_different_subscriptions(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock) #add the first and verify consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False, ENV_DEFAULT) profile._set_subscriptions(consolidated) self.assertEqual(len(storage_mock['subscriptions']), 1) subscription1 = storage_mock['subscriptions'][0] self.assertEqual(subscription1, { 'environmentName': 'AzureCloud', 'id': '1', 'name': self.display_name1, 'state': self.state1.value, 'user': { 'name': self.user1, 'type': 'user' }, 'isDefault': True, 'tenantId': self.tenant_id }) #add the second and verify consolidated = Profile._normalize_properties(self.user2, [self.subscription2], False, ENV_DEFAULT) profile._set_subscriptions(consolidated) self.assertEqual(len(storage_mock['subscriptions']), 2) subscription2 = storage_mock['subscriptions'][1] self.assertEqual(subscription2, { 'environmentName': 'AzureCloud', 'id': '2', 'name': self.display_name2, 'state': self.state2.value, 'user': { 'name': self.user2, 'type': 'user' }, 'isDefault': True, 'tenantId': self.tenant_id }) #verify the old one stays, but no longer active self.assertEqual(storage_mock['subscriptions'][0]['name'], subscription1['name']) self.assertFalse(storage_mock['subscriptions'][0]['isDefault'])
def test_get_current_account_user(self, mock_read_cred_file): #setup mock_read_cred_file.return_value = [Test_Profile.token_entry1] storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) #action user = profile.get_current_account_user() #verify self.assertEqual(user, self.user1)
def test_get_subscription(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) self.assertEqual(self.display_name1, profile.get_subscription()['name']) self.assertEqual(self.display_name1, profile.get_subscription(subscription=self.display_name1)['name']) sub_id = self.id1.split('/')[-1] self.assertEqual(sub_id, profile.get_subscription()['id']) self.assertEqual(sub_id, profile.get_subscription(subscription=sub_id)['id']) self.assertRaises(CLIError, profile.get_subscription, "random_id")
def test_get_login_credentials_for_graph_client(self, mock_get_token, mock_read_cred_file): some_token_type = 'Bearer' mock_read_cred_file.return_value = [Test_Profile.token_entry1] mock_get_token.return_value = (some_token_type, Test_Profile.raw_token1) #setup storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False, ENV_DEFAULT) profile._set_subscriptions(consolidated) #action cred, _, tenant_id = profile.get_login_credentials(for_graph_client=True) _, _ = cred._token_retriever() #verify mock_get_token.assert_called_once_with(mock.ANY, self.user1, self.tenant_id, 'https://graph.windows.net/') self.assertEqual(tenant_id, self.tenant_id)
def test_get_subscription(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock, use_global_creds_cache=False) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) self.assertEqual(self.display_name1, profile.get_subscription()['name']) self.assertEqual(self.display_name1, profile.get_subscription(subscription=self.display_name1)['name']) sub_id = self.id1.split('/')[-1] self.assertEqual(sub_id, profile.get_subscription()['id']) self.assertEqual(sub_id, profile.get_subscription(subscription=sub_id)['id']) self.assertRaises(CLIError, profile.get_subscription, "random_id")
def test_logout(self, mock_persist_creds, mock_read_cred_file): #setup mock_read_cred_file.return_value = [Test_Profile.token_entry1] storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) self.assertEqual(1, len(storage_mock['subscriptions'])) #action profile.logout(self.user1) #verify self.assertEqual(0, len(storage_mock['subscriptions'])) self.assertEqual(mock_read_cred_file.call_count, 1) self.assertEqual(mock_persist_creds.call_count, 1)
def test_get_login_credentials_for_data_lake_client(self, mock_get_token, mock_read_cred_file): some_token_type = 'Bearer' mock_read_cred_file.return_value = [Test_Profile.token_entry1] mock_get_token.return_value = (some_token_type, Test_Profile.raw_token1) # setup storage_mock = {'subscriptions': None} profile = Profile(storage_mock, use_global_creds_cache=False) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) # action cred, _, tenant_id = profile.get_login_credentials( resource=CLOUD.endpoints.active_directory_data_lake_resource_id) _, _ = cred._token_retriever() # verify mock_get_token.assert_called_once_with(mock.ANY, self.user1, self.tenant_id, 'https://datalake.azure.net/') self.assertEqual(tenant_id, self.tenant_id)
def test_get_login_credentials_for_graph_client(self, mock_get_token, mock_read_cred_file): some_token_type = 'Bearer' mock_read_cred_file.return_value = [Test_Profile.token_entry1] mock_get_token.return_value = (some_token_type, Test_Profile.raw_token1) # setup storage_mock = {'subscriptions': None} profile = Profile(storage_mock, use_global_creds_cache=False) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) # action cred, _, tenant_id = profile.get_login_credentials( resource=CLOUD.endpoints.active_directory_graph_resource_id) _, _ = cred._token_retriever() # verify mock_get_token.assert_called_once_with(mock.ANY, self.user1, self.tenant_id, 'https://graph.windows.net/') self.assertEqual(tenant_id, self.tenant_id)
def test_logout_all(self, mock_delete_cred_file): #setup storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) consolidated2 = Profile._normalize_properties(self.user2, [self.subscription2], False) profile._set_subscriptions(consolidated + consolidated2) self.assertEqual(2, len(storage_mock['subscriptions'])) #action profile.logout_all() #verify self.assertEqual([], storage_mock['subscriptions']) self.assertEqual(mock_delete_cred_file.call_count, 1)
def test_set_active_subscription(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) consolidated = profile._normalize_properties(self.user2, [self.subscription2], False) profile._set_subscriptions(consolidated) self.assertTrue(storage_mock['subscriptions'][1]['isDefault']) profile.set_active_subscription(storage_mock['subscriptions'][0]['id']) self.assertFalse(storage_mock['subscriptions'][1]['isDefault']) self.assertTrue(storage_mock['subscriptions'][0]['isDefault'])
def test_update_with_same_subscription_added_twice(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock) #add one twice and verify we will have one but with new token consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) new_subscription1 = SubscriptionStub(self.id1, self.display_name1, self.state1, self.tenant_id) consolidated = Profile._normalize_properties(self.user1, [new_subscription1], False) profile._set_subscriptions(consolidated) self.assertEqual(len(storage_mock['subscriptions']), 1) self.assertTrue(storage_mock['subscriptions'][0]['isDefault'])
def test_set_active_subscription(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock, use_global_creds_cache=False) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False) profile._set_subscriptions(consolidated) consolidated = profile._normalize_properties(self.user2, [self.subscription2], False) profile._set_subscriptions(consolidated) self.assertTrue(storage_mock['subscriptions'][1]['isDefault']) profile.set_active_subscription(storage_mock['subscriptions'][0]['id']) self.assertFalse(storage_mock['subscriptions'][1]['isDefault']) self.assertTrue(storage_mock['subscriptions'][0]['isDefault'])
def test_set_active_subscription(self): storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False, ENV_DEFAULT) profile._set_subscriptions(consolidated) consolidated = profile._normalize_properties(self.user2, [self.subscription2], False, ENV_DEFAULT) profile._set_subscriptions(consolidated) subscription1 = storage_mock['subscriptions'][0] subscription2 = storage_mock['subscriptions'][1] self.assertTrue(subscription2['isDefault']) profile.set_active_subscription(subscription1['id']) self.assertFalse(subscription2['isDefault']) self.assertTrue(subscription1['isDefault'])
def test_get_login_credentials_for_graph_client(self, mock_get_token, mock_read_cred_file): some_token_type = 'Bearer' mock_read_cred_file.return_value = [Test_Profile.token_entry1] mock_get_token.return_value = (some_token_type, Test_Profile.raw_token1) #setup storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False, ENV_DEFAULT) profile._set_subscriptions(consolidated) #action cred, _, tenant_id = profile.get_login_credentials(resource=get_env()[ ENDPOINT_URLS.ACTIVE_DIRECTORY_GRAPH_RESOURCE_ID]) _, _ = cred._token_retriever() #verify mock_get_token.assert_called_once_with(mock.ANY, self.user1, self.tenant_id, 'https://graph.windows.net/') self.assertEqual(tenant_id, self.tenant_id)