def setUp(self, mock_config): self.core = Core() self.config = Config() self.config.set_accounts(get_test_accounts()) self.core.config = self.config self.success_result = get_success_result() self.fail_result = get_failed_result() self.error_result = get_error_result()
def ok(self): text = self.text_box.toPlainText() text = text.replace('\t', ' ') raw_config_dict = files.parse_yaml(text) if not raw_config_dict: self.set_error_text('config invalid') return config = Config() config.set_accounts(raw_config_dict) if config.valid: config.mfa_shell_command = self.mfa_command_input.text() self.gui.edit_config(config) self.hide() else: self.set_error_text(config.error)
def setUp(self): self.config = Config()
class TestConfig(TestCase): def setUp(self): self.config = Config() @mock.patch('app.core.config.files.load_config') @mock.patch('app.core.config.files.load_accounts') def test_load_from_disk__empty_config(self, mock_accounts, mock_config): mock_accounts.return_value = {} mock_config.return_value = {} self.config.load_from_disk() self.assertEqual(None, self.config.mfa_shell_command) @mock.patch('app.core.config.files.load_config') @mock.patch('app.core.config.files.load_accounts') def test_load_from_disk(self, mock_load_accounts, mock_load_config): mock_load_accounts.return_value = get_test_accounts() mock_load_config.return_value = { 'mfa_shell_command': 'some command', } self.config.load_from_disk() self.assertEqual('some command', self.config.mfa_shell_command) @mock.patch('app.core.config.files.save_config_file') @mock.patch('app.core.config.files.save_accounts_file') def test_save_to_disk(self, mock_save_accounts_file, mock_save_config_file): self.config.set_accounts(get_test_accounts()) self.config.save_to_disk() expected_accounts = [ call({ 'development': { 'color': '#388E3C', 'team': 'awesome-team', 'region': 'us-east-1', 'profiles': [{ 'profile': 'developer', 'account': '123495678901', 'role': 'developer', 'default': True }, { 'profile': 'readonly', 'account': '012349567890', 'role': 'readonly' }] }, 'live': { 'color': '#388E3C', 'team': 'awesome-team', 'region': 'us-east-1', 'profiles': [{ 'profile': 'developer', 'account': '123456789012', 'role': 'developer', 'default': True }, { 'profile': 'readonly', 'account': '012345678901', 'role': 'readonly' }] } }) ] expected_config = [call({'mfa_shell_command': None})] self.assertEqual(expected_accounts, mock_save_accounts_file.mock_calls) self.assertEqual(expected_config, mock_save_config_file.mock_calls) def test_set_accounts(self): self.config.set_accounts(get_test_accounts()) groups = ['development', 'live'] self.assertEqual(groups, list(self.config.profile_groups.keys())) development_group = self.config.get_group('development') self.assertEqual('development', development_group.name) self.assertEqual('awesome-team', development_group.team) self.assertEqual('us-east-1', development_group.region) self.assertEqual('#388E3C', development_group.color) profile = development_group.profiles[0] self.assertEqual(development_group, profile.group) self.assertEqual('developer', profile.profile) self.assertEqual('123495678901', profile.account) self.assertEqual('developer', profile.role) self.assertEqual(True, profile.default) profile = development_group.profiles[1] self.assertEqual(development_group, profile.group) self.assertEqual('readonly', profile.profile) self.assertEqual('012349567890', profile.account) self.assertEqual('readonly', profile.role) self.assertEqual(False, profile.default) def test_validate(self): self.config.set_accounts(get_test_accounts()) self.config.validate() self.assertEqual(True, self.config.valid) self.assertEqual('', self.config.error) def test_validate_empty_config(self): self.config.validate() self.assertEqual(False, self.config.valid) self.assertEqual('config is empty', self.config.error) def test_validate_calls_validate_and_use_return_value(self): mock_group1 = Mock() mock_group1.validate.return_value = True, 'no error' mock_group2 = Mock() mock_group2.validate.return_value = False, 'some error' mock_group3 = Mock() mock_group3.validate.return_value = True, 'everything is okay' self.config.profile_groups['1'] = mock_group1 self.config.profile_groups['2'] = mock_group2 self.config.profile_groups['3'] = mock_group3 self.config.validate() self.assertEqual(1, mock_group1.validate.call_count) self.assertEqual(1, mock_group2.validate.call_count) self.assertEqual(0, mock_group3.validate.call_count) self.assertEqual(False, self.config.valid) self.assertEqual('some error', self.config.error) def test_list_groups(self): self.config.profile_groups = {'1': 'group 1', '2': 'group 2'} expected = ['group 1', 'group 2'] self.assertEqual(expected, self.config.list_groups()) def test_get_group(self): self.config.profile_groups = {'1': 'group 1', '2': 'group 2'} expected = 'group 1' self.assertEqual(expected, self.config.get_group('1')) def test_to_dict(self): mock_group1 = Mock() mock_group1.to_dict.return_value = 'group 1' mock_group2 = Mock() mock_group2.to_dict.return_value = 'group 2' mock_group3 = Mock() mock_group3.to_dict.return_value = 'group 3' self.config.profile_groups['1'] = mock_group1 self.config.profile_groups['2'] = mock_group2 self.config.profile_groups['3'] = mock_group3 result = self.config.to_dict() self.assertEqual(1, mock_group1.to_dict.call_count) self.assertEqual(1, mock_group2.to_dict.call_count) self.assertEqual(1, mock_group3.to_dict.call_count) expected = {'1': 'group 1', '2': 'group 2', '3': 'group 3'} self.assertEqual(expected, result)
def show_dialog(self, config: Config): self.text_box.setPlainText(files.dump_yaml(config.to_dict())) self.update_error_text(config) self.mfa_command_input.setText(config.mfa_shell_command) self.show() self.activateWindow()
def __init__(self): self.config: Config = Config() self.config.load_from_disk() self.active_profile_group: ProfileGroup = None self.empty_profile_group: ProfileGroup = ProfileGroup('logout', {}) self.region_override: str = None
from flask_sqlalchemy import SQLAlchemy from flask_caching import Cache from app.core.config import Config from app.util.requests import get_requests_session CONFIG = Config() REQUESTS = get_requests_session() DB_ENGINE_OPTIONS = { "convert_unicode": True, "echo": CONFIG.IS_LOCAL, "pool_recycle": 3600, } DB_SESSION_OPTIONS = { "autocommit": False, "autoflush": False, } DB = SQLAlchemy( engine_options=DB_ENGINE_OPTIONS, session_options=DB_SESSION_OPTIONS, ) CACHE = Cache() __all__ = ["CONFIG", "REQUESTS"]
class TestCore(TestCase): @mock.patch('app.core.core.Config.load_from_disk') def setUp(self, mock_config): self.core = Core() self.config = Config() self.config.set_accounts(get_test_accounts()) self.core.config = self.config self.success_result = get_success_result() self.fail_result = get_failed_result() self.error_result = get_error_result() @mock.patch('app.core.core.credentials') def test_login__no_access_key(self, mock_credentials): mock_credentials.check_access_key.return_value = self.error_result result = self.core.login(self.config.get_group('development'), None) expected = [call.check_access_key()] self.assertEqual(expected, mock_credentials.mock_calls) self.assertEqual(self.error_result, result) @mock.patch('app.core.core.credentials') def test_login__session_token_error(self, mock_credentials): mock_credentials.check_access_key.return_value = self.success_result mock_credentials.check_session.return_value = self.error_result result = self.core.login(self.config.get_group('development'), None) expected = [call.check_access_key(), call.check_session()] self.assertEqual(expected, mock_credentials.mock_calls) self.assertEqual(self.error_result, result) @mock.patch('app.core.core.credentials') def test_login__mfa_error(self, mock_credentials): mock_credentials.check_access_key.return_value = self.success_result mock_credentials.check_session.return_value = self.fail_result self.core._renew_session = Mock() self.core._renew_session.return_value = self.error_result result = self.core.login(self.config.get_group('development'), None) expected = [call.check_access_key(), call.check_session()] self.assertEqual(expected, mock_credentials.mock_calls) expected = [call(None)] self.assertEqual(expected, self.core._renew_session.mock_calls) self.assertEqual(self.error_result, result) @mock.patch('app.core.core.files') @mock.patch('app.core.core.credentials') def test_login__successful_login(self, mock_credentials, _): mock_credentials.check_access_key.return_value = self.success_result mock_credentials.check_session.return_value = self.success_result self.core._renew_session = Mock() self.core._renew_session.return_value = self.success_result mock_credentials.get_user_name.return_value = 'test-user' mock_credentials.fetch_role_credentials.return_value = self.success_result mock_credentials.write_profile_config.return_value = self.success_result self.core._handle_support_files = Mock() mock_mfa_callback = Mock() profile_group = self.config.get_group('development') result = self.core.login(profile_group, mock_mfa_callback) expected = [call.check_access_key(), call.check_session(), call.get_user_name(), call.fetch_role_credentials('test-user', profile_group), call.write_profile_config(profile_group, 'us-east-1')] self.assertEqual(expected, mock_credentials.mock_calls) expected = [call(profile_group)] self.assertEqual(expected, self.core._handle_support_files.mock_calls) self.assertEqual(profile_group, self.core.active_profile_group) self.assertEqual(None, self.core.region_override) self.assertEqual(True, result.was_success) self.assertEqual(False, result.was_error) @mock.patch('app.core.core.credentials') def test_login__logout(self, mock_credentials): mock_credentials.fetch_role_credentials.return_value = self.success_result mock_credentials.write_profile_config.return_value = self.success_result result = self.core.logout() expected = [call.fetch_role_credentials(user_name='none', profile_group=self.core.empty_profile_group), call.write_profile_config(profile_group=self.core.empty_profile_group, region='')] self.assertEqual(expected, mock_credentials.mock_calls) self.assertEqual(True, result.was_success) self.assertEqual(False, result.was_error) @mock.patch('app.core.core.credentials') def test_login__logout_error(self, mock_credentials): mock_credentials.fetch_role_credentials.return_value = self.error_result result = self.core.logout() expected = [call.fetch_role_credentials(user_name='none', profile_group=self.core.empty_profile_group)] self.assertEqual(expected, mock_credentials.mock_calls) self.assertEqual(self.error_result, result) @mock.patch('app.core.core.credentials') def test_rotate_access_key__no_access_key(self, mock_credentials): mock_credentials.check_access_key.return_value = self.error_result result = self.core.rotate_access_key() expected = [call.check_access_key()] self.assertEqual(expected, mock_credentials.mock_calls) self.assertEqual(self.error_result, result) @mock.patch('app.core.core.iam') @mock.patch('app.core.core.credentials') def test_rotate_access_key__successful_rotate(self, mock_credentials, mock_iam): mock_credentials.check_access_key.return_value = self.success_result mock_credentials.check_session.return_value = self.success_result mock_credentials.get_user_name.return_value = 'test-user' mock_credentials.get_access_key_id.return_value = '12345' access_key_result = Result() access_key_result.add_payload({ 'AccessKeyId': 12345, 'SecretAccessKey': 67890 }) access_key_result.set_success() mock_iam.create_access_key.return_value = access_key_result result = self.core.rotate_access_key() expected = [call.check_access_key(), call.check_session(), call.get_user_name(), call.get_access_key_id(), call.set_access_key(key_id=12345, access_key=67890)] self.assertEqual(expected, mock_credentials.mock_calls) expected = [call.create_access_key('test-user'), call.delete_iam_access_key('test-user', '12345')] self.assertEqual(expected, mock_iam.mock_calls) self.assertEqual(True, result.was_success) self.assertEqual(False, result.was_error) def test_get_region__not_logged_in(self): region = self.core.get_region() self.assertEqual(None, region) def test_get_region__active_profile_group(self): self.core.active_profile_group = self.config.get_group('development') region = self.core.get_region() self.assertEqual('us-east-1', region) def test_get_region__region_overwrite(self): self.core.active_profile_group = self.config.get_group('development') self.core.region_override = 'eu-north-1' region = self.core.get_region() self.assertEqual('eu-north-1', region) @mock.patch('app.core.core.mfa') @mock.patch('app.core.core.credentials') def test__renew_session__token_from_shell(self, mock_credentials, mock_mfa_shell): mock_mfa_shell.fetch_mfa_token_from_shell.return_value = '12345' mock_credentials.fetch_session_token.return_value = self.success_result mock_mfa_callback = Mock() result = self.core._renew_session(mock_mfa_callback) self.assertEqual(0, mock_mfa_callback.call_count) expected = [call.fetch_session_token('12345')] self.assertEqual(expected, mock_credentials.mock_calls) self.assertEqual(True, result.was_success) self.assertEqual(False, result.was_error) @mock.patch('app.core.core.mfa') @mock.patch('app.core.core.credentials') def test__renew_session__no_token_from_mfa_callback(self, mock_credentials, mock_mfa_shell): mock_mfa_shell.fetch_mfa_token_from_shell.return_value = None mock_credentials.fetch_session_token.return_value = self.success_result mock_mfa_callback = Mock() mock_mfa_callback.return_value = '' result = self.core._renew_session(mock_mfa_callback) self.assertEqual(1, mock_mfa_callback.call_count) expected = [] self.assertEqual(expected, mock_credentials.mock_calls) self.assertEqual(False, result.was_success) self.assertEqual(True, result.was_error) @mock.patch('app.core.core.mfa') @mock.patch('app.core.core.credentials') def test__renew_session__token_from_mfa_callback(self, mock_credentials, mock_mfa_shell): mock_mfa_shell.fetch_mfa_token_from_shell.return_value = None mock_credentials.fetch_session_token.return_value = self.success_result mock_mfa_callback = Mock() mock_mfa_callback.return_value = '12345' result = self.core._renew_session(mock_mfa_callback) self.assertEqual(1, mock_mfa_callback.call_count) expected = [call.fetch_session_token('12345')] self.assertEqual(expected, mock_credentials.mock_calls) self.assertEqual(True, result.was_success) self.assertEqual(False, result.was_error) @mock.patch('app.core.core.credentials') def test__set_region__not_logged_in(self, mock_credentials): mock_credentials.write_profile_config.return_value = self.success_result result = self.core.set_region('eu-north-1') expected = [] self.assertEqual(expected, mock_credentials.mock_calls) self.assertEqual('eu-north-1', self.core.region_override) self.assertEqual(True, result.was_success) self.assertEqual(False, result.was_error) @mock.patch('app.core.core.credentials') def test__set_region__logged_in(self, mock_credentials): mock_credentials.write_profile_config.return_value = self.success_result self.core.active_profile_group = self.config.get_group('development') result = self.core.set_region('eu-north-1') expected = [call.write_profile_config(self.config.get_group('development'), 'eu-north-1')] self.assertEqual(expected, mock_credentials.mock_calls) self.assertEqual('eu-north-1', self.core.region_override) self.assertEqual(True, result.was_success) self.assertEqual(False, result.was_error)