def test_set_appid_from_account_id(self, isfile_mock, parse_mock): isfile_mock.return_value = True parse_mock.return_value = None config = Config(["aws_okta_keyman.py"]) config.accounts = [{"appid": "A123"}] config.set_appid_from_account_id(0) self.assertEqual(config.appid, "A123")
def test_validate_short_duration(self): config = Config(["aws_okta_keyman.py"]) config.org = "example" config.duration = 1 with self.assertRaises(ValueError): config.validate()
def test_full_app_url(self): config = Config(['aws_okta_keyman.py']) config.org = 'example' config.appid = 'some/thing' ret = config.full_app_url() self.assertEqual(ret, 'https://example.okta.com/some/thing')
def test_parse_args_no_req_main(self): argv = ['aws_okta_keyman.py', '-D'] config = Config(argv) config.parse_args(main_required=False) # Should succeed without throwing due to missing args self.assertEqual(config.debug, True)
def test_full_app_url(self): config = Config(["aws_okta_keyman.py"]) config.org = "example" config.appid = "some/thing" ret = config.full_app_url() self.assertEqual(ret, "https://example.okta.com/some/thing")
def test_get_config_auto_config_only( self, isfile_mock, parse_mock, valid_mock, config_mock, expuser_mock, ): isfile_mock.return_value = True parse_mock.return_value = None valid_mock.return_value = None config_mock.return_value = None expuser_mock.return_value = "" config = Config(["aws_okta_keyman.py"]) config.get_config() parse_mock.assert_has_calls( [ mock.call(main_required=False), ], ) config_mock.assert_has_calls( [ mock.call("/.config/aws_okta_keyman.yml"), ], )
def test_parse_args_req_main_missing(self): argv = ['aws_okta_keyman.py', '-D'] config = Config(argv) # Main required but not passed, should raise with self.assertRaises(SystemExit): config.parse_args(main_required=True)
def test_set_appid_from_account_id(self, isfile_mock, parse_mock): isfile_mock.return_value = True parse_mock.return_value = None config = Config(['aws_okta_keyman.py']) config.accounts = [{'appid': 'A123'}] config.set_appid_from_account_id(0) self.assertEqual(config.appid, 'A123')
def test_write_config_new_file(self, isfile_mock): isfile_mock.return_value = False config = Config(['aws_okta_keyman.py']) config.writepath = './.config/aws_okta_keyman.yml' config.username = '******' config.appid = 'app/id' config.org = 'example' m = mock.mock_open() with mock.patch('aws_okta_keyman.config.open', m): config.write_config() m.assert_has_calls([ mock.call().write('org'), mock.call().write(':'), mock.call().write(' '), mock.call().write('example'), mock.call().write('\n'), mock.call().write('reup'), mock.call().write(':'), mock.call().write(' '), mock.call().write('null'), mock.call().write('\n'), mock.call().write('username'), mock.call().write(':'), mock.call().write(' '), mock.call().write('*****@*****.**'), mock.call().write('\n'), mock.call().flush(), mock.call().flush(), mock.call().__exit__(None, None, None) ])
def test_validate_automatic_username_from_none(self, getpass_mock): getpass_mock.getuser.return_value = 'user' config = Config(['aws_okta_keyman.py']) config.accounts = [{'appid': 'A123'}] config.org = 'example' config.validate() self.assertEqual(config.username, 'user')
def test_validate_long_duration(self): config = Config(['aws_okta_keyman.py']) config.org = 'example' config.duration = 100000000 with self.assertRaises(ValueError): config.validate()
def test_write_config(self, isfile_mock): isfile_mock.return_value = True config = Config(['aws_okta_keyman.py']) config.writepath = './.config/aws_okta_keyman.yml' config.username = '******' yaml = ("username: [email protected]\n" "org: example\n" "appid: app/id\n" "accounts:\n" " - name: Dev\n" " appid: A123/123\n") m = mock.mock_open(read_data=yaml) with mock.patch('aws_okta_keyman.config.open', m): config.write_config() m.assert_has_calls([ mock.call('./.config/aws_okta_keyman.yml', 'r'), ]) m.assert_has_calls([ mock.call(u'./.config/aws_okta_keyman.yml', 'w'), ]) m.assert_has_calls([ mock.call().write('accounts'), mock.call().write(':'), mock.call().write('\n'), mock.call().write('-'), mock.call().write(' '), mock.call().write('appid'), mock.call().write(':'), mock.call().write(' '), mock.call().write('A123/123'), mock.call().write('\n'), mock.call().write(' '), mock.call().write('name'), mock.call().write(':'), mock.call().write(' '), mock.call().write('Dev'), mock.call().write('\n'), mock.call().write('org'), mock.call().write(':'), mock.call().write(' '), mock.call().write('example'), mock.call().write('\n'), mock.call().write('reup'), mock.call().write(':'), mock.call().write(' '), mock.call().write('null'), mock.call().write('\n'), mock.call().write('username'), mock.call().write(':'), mock.call().write(' '), mock.call().write('*****@*****.**'), mock.call().write('\n'), mock.call().flush(), mock.call().flush(), mock.call().__exit__(None, None, None) ])
def test_validate_automatic_username_from_full_config(self, getpass_mock): getpass_mock.getuser.return_value = "user" config = Config(["aws_okta_keyman.py"]) config.accounts = [{"appid": "A123"}] config.org = "example" config.username = "******" config.validate() self.assertEqual(config.username, "*****@*****.**")
def test_interactive_config_auto_account(self, input_mock, _getpass_mock): input_mock.side_effect = ['org', 'user', ''] config = Config(['aws_okta_keyman.py']) config.write_config = mock.MagicMock() config.interactive_config() self.assertEqual(config.accounts, None)
def test_interactive_config_keyboardexit(self, input_mock, getpass_mock): input_mock.side_effect = ['org', 'user', KeyboardInterrupt] getpass_mock.return_value = 'fakeuser' config = Config(['aws_okta_keyman.py']) config.write_config = mock.MagicMock() ret = config.interactive_config() self.assertEqual(ret, None) assert not config.write_config.called
def test_interactive_config_auto_user(self, input_mock, getpass_mock): input_mock.side_effect = ['org', '', 'appid', 'test', ''] getpass_mock.return_value = 'fakeuser' config = Config(['aws_okta_keyman.py']) config.write_config = mock.MagicMock() config.interactive_config() self.assertEqual(config.username, 'automatic-username')
def test_interactive_config_auto_user(self, input_mock, getpass_mock): input_mock.side_effect = ["org", "", "appid", "test", ""] getpass_mock.return_value = "fakeuser" config = Config(["aws_okta_keyman.py"]) config.write_config = mock.MagicMock() config.interactive_config() self.assertEqual(config.username, "automatic-username")
def test_parse_args_req_main_present(self): argv = [ 'aws_okta_keyman.py', '-a', 'app/id', '-o', 'foobar', '-u', 'test' ] config = Config(argv) config.parse_args(main_required=True) # Should succeed without throwing due to missing args self.assertEqual(config.appid, 'app/id') self.assertEqual(config.org, 'foobar') self.assertEqual(config.username, 'test')
def __init__(self, argv): self.okta_client = None self.log = self.setup_logging() self.log.info('{} v{}'.format(__desc__, __version__)) self.config = Config(argv) try: self.config.get_config() except ValueError as err: self.log.fatal(err) sys.exit(1) if self.config.debug: self.log.setLevel(logging.DEBUG)
def test_interactive_config(self, input_mock, getpass_mock): input_mock.side_effect = ['org', 'user', 'appid', 'test', ''] getpass_mock.return_value = 'fakeuser' config = Config(['aws_okta_keyman.py']) config.write_config = mock.MagicMock() config.interactive_config() self.assertEqual(config.org, 'org') self.assertEqual(config.username, 'user') self.assertEqual(config.accounts, [{'name': 'test', 'appid': 'appid'}]) config.write_config.assert_has_calls([mock.call()])
def test_interactive_config(self, input_mock, getpass_mock): input_mock.side_effect = ["org", "user", "appid", "test", ""] getpass_mock.return_value = "fakeuser" config = Config(["aws_okta_keyman.py"]) config.write_config = mock.MagicMock() config.interactive_config() self.assertEqual(config.org, "org") self.assertEqual(config.username, "user") self.assertEqual(config.accounts, [{"name": "test", "appid": "appid"}]) config.write_config.assert_has_calls([mock.call()])
def test_get_config_args_only(self, isfile_mock, parse_mock, valid_mock): isfile_mock.return_value = False parse_mock.return_value = None valid_mock.return_value = None argv = [ 'aws_okta_keyman.py', '-a', 'app/id', '-o', 'foobar', '-u', 'test' ] config = Config(argv) config.get_config() parse_mock.assert_has_calls([ mock.call(), ])
def test_parse_config(self): config = Config(['aws_okta_keyman.py']) config.read_yaml = mock.MagicMock() config.read_yaml.return_value = { 'username': '******', 'org': 'example', 'appid': 'app/id', } config.parse_config('./.config/aws_okta_keyman.yml') self.assertEqual(config.appid, 'app/id') self.assertEqual(config.org, 'example') self.assertEqual(config.username, '*****@*****.**')
def __init__(self, argv): self.okta_client = None self.log = LOG self.log.info(f"{__desc__} 🔐 v{__version__}") self.config = Config(argv) self.role = None try: self.config.get_config() except ValueError as err: self.log.fatal(err) sys.exit(1) if self.config.debug: self.log.setLevel(logging.DEBUG) self.debug_requests_on()
def test_parse_config(self): config = Config(["aws_okta_keyman.py"]) config.read_yaml = mock.MagicMock() config.read_yaml.return_value = { "username": "******", "org": "example", "appid": "app/id", } config.parse_config("./.config/aws_okta_keyman.yml") self.assertEqual(config.appid, "app/id") self.assertEqual(config.org, "example") self.assertEqual(config.username, "*****@*****.**")
def test_get_config_specified_config_only(self, isfile_mock, valid_mock, config_mock, expuser_mock, _parse_mock): isfile_mock.return_value = True valid_mock.return_value = None config_mock.return_value = None expuser_mock.return_value = '' config = Config(['aws_okta_keyman.py', '-c']) config.config = '/.config/aws_okta_keyman.yml' config.get_config() config_mock.assert_has_calls([ mock.call('/.config/aws_okta_keyman.yml'), ])
def test_write_config_path_create_when_missing(self, os_mock): config = Config(['aws_okta_keyman.py']) config.clean_config_for_write = mock.MagicMock() config.clean_config_for_write.return_value = {} config.read_yaml = mock.MagicMock() config.read_yaml.return_value = {} folderpath = '/home/user/.config/' os_mock.path.dirname.return_value = folderpath os_mock.path.exists.return_value = False m = mock.mock_open() with mock.patch('aws_okta_keyman.config.open', m): config.write_config() os_mock.assert_has_calls([mock.call.makedirs(folderpath)])
def test_parse_config(self, isfile_mock): isfile_mock.return_value = True config = Config(['aws_okta_keyman.py']) yaml = ("username: [email protected]\n" "org: example\n" "appid: app/id\n") m = mock.mock_open(read_data=yaml) with mock.patch('aws_okta_keyman.config.open', m): config.parse_config('./.config/aws_okta_keyman.yml') self.assertEquals(config.appid, 'app/id') self.assertEquals(config.org, 'example') self.assertEquals(config.username, '*****@*****.**')
def test_write_config_path_expansion(self, isfile_mock): isfile_mock.return_value = False config = Config(['aws_okta_keyman.py']) config.writepath = '~/.config/aws_okta_keyman.yml' config.username = '******' config.appid = 'app/id' config.org = 'example' expected_path = os.path.expanduser(config.writepath) m = mock.mock_open() with mock.patch('aws_okta_keyman.config.open', m): config.write_config() m.assert_has_calls([mock.call(expected_path, 'w')])
def test_get_config_write_mixed_config(self, isfile_mock, _parse_mock, valid_mock, expuser_mock, write_mock): isfile_mock.return_value = True valid_mock.return_value = None write_mock.return_value = None expuser_mock.return_value = '' config = Config(['aws_okta_keyman.py', '-w']) config.get_config() config.write = './.config/aws_okta_keyman.yml' self.assertEqual(config.write, './.config/aws_okta_keyman.yml') write_mock.assert_has_calls([ mock.call(), ])