def test_initialize_session_authentication_error(self, mock_log, mock_cred): with self.assertRaises(SystemExit): adal_err = AdalError("test") adal_err.error_response = {'error': 'test'} err = AuthenticationError('test') err.inner_exception = adal_err mock_cred.side_effect = err with patch.dict(os.environ, { constants.ENV_TENANT_ID: DEFAULT_TENANT_ID, constants.ENV_SUB_ID: DEFAULT_SUBSCRIPTION_ID, constants.ENV_CLIENT_ID: 'client', constants.ENV_CLIENT_SECRET: 'secret' }, clear=True): s = Session() s.get_credentials().get_token() mock_log.assert_called_once()
def test_get_token_with_adalerror(): """ Should raise an exception """ # Set up adal mock functions mock = AdalMock() mock.setup(AdalError('Invalid Client'), True) # Call get_token azure = create_azure_instance() with pytest.raises(AzureException): azure.get_token() # Reinstate mocked functions mock.restore()
def test_find_subscriptions_thru_username_password_with_account_disabled(self, mock_logger, mock_auth_context): mock_auth_context.acquire_token_with_username_password.return_value = self.token_entry1 mock_auth_context.acquire_token.side_effect = AdalError('Account is disabled') mock_arm_client = mock.MagicMock() mock_arm_client.tenants.list.return_value = [TenantStub(self.tenant_id)] finder = SubscriptionFinder(lambda _, _2: mock_auth_context, None, lambda _: mock_arm_client) mgmt_resource = 'https://management.core.windows.net/' # action subs = finder.find_from_user_account(self.user1, 'bar', None, mgmt_resource) # assert self.assertEqual([], subs) mock_logger.warning.assert_called_once_with(mock.ANY, mock.ANY, mock.ANY)