def test_get_login_credentials(self, mock_get_token, mock_read_cred_file): some_token_type = 'Bearer' mock_read_cred_file.return_value = json.dumps( [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_login_credentials(self, mock_get_token, mock_read_cred_file): some_token_type = 'Bearer' mock_read_cred_file.return_value = json.dumps([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_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, '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, '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, '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, '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 = json.dumps( [Test_Profile.token_entry1]) storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False, ENV_DEFAULT) profile._set_subscriptions(consolidated) #action user = profile.get_current_account_user() #verify self.assertEqual(user, self.user1)
def test_get_current_account_user(self, mock_read_cred_file): #setup mock_read_cred_file.return_value = json.dumps([Test_Profile.token_entry1]) storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False, ENV_DEFAULT) profile._set_subscriptions(consolidated) #action user = profile.get_current_account_user() #verify self.assertEqual(user, self.user1)
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 = json.dumps([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_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, ENV_DEFAULT) consolidated2 = Profile._normalize_properties(self.user2, [self.subscription2], False, ENV_DEFAULT) profile._set_subscriptions(consolidated + consolidated2) self.assertEqual(2, len(storage_mock['subscriptions'])) #action profile.logout_all() #verify self.assertEqual(0, len(storage_mock['subscriptions'])) self.assertEqual(mock_delete_cred_file.call_count, 1)
def test_logout(self, mock_persist_creds, mock_read_cred_file): #setup mock_read_cred_file.return_value = json.dumps( [Test_Profile.token_entry1]) storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False, ENV_DEFAULT) 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_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, ENV_DEFAULT) 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, ENV_DEFAULT) profile._set_subscriptions(consolidated) self.assertEqual(len(storage_mock['subscriptions']), 1) self.assertTrue(storage_mock['subscriptions'][0]['isDefault'])
def test_logout(self, mock_persist_creds, mock_read_cred_file): #setup mock_read_cred_file.return_value = json.dumps([Test_Profile.token_entry1]) storage_mock = {'subscriptions': None} profile = Profile(storage_mock) consolidated = Profile._normalize_properties(self.user1, [self.subscription1], False, ENV_DEFAULT) 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_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 = json.dumps( [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)