Example #1
0
def update_to_new_config(config_dict : Dict, server_config_path : str, server : str):
    """
    Updates the config file because the AskAnna team changed the config file setup
    This function can be removed in the future.
    """

    remote = config_dict['askanna'].get('remote', '')
    remote = remote.replace('/v1/', '')

    ui = ''
    token = ''

    if remote == 'https://beta-api.askanna.eu':
        ui = 'https://beta.askanna.eu'

    del config_dict['askanna']

    if config_dict.get('auth'):
        token = config_dict['auth'].get('token', '')

    try:
        del config_dict['auth']
    except:  # noqa
        pass

    config_dict = add_or_update_server_in_config_dict(config_dict, server, remote, token, ui)

    store_config(server_config_path, config_dict)
    click.echo('[INFO] We updated your AskAnna config file to support the latest features')

    return read_config(server_config_path)
Example #2
0
    def test_store_config(self):
        path = f'{self.tempdir}/{fake.file_name(extension="yml")}'
        store_config(path, DEFAULT_SERVER_CONFIG)
        config = read_config(path)

        self.assertTrue(isinstance(config, Dict))
        self.assertEqual(config, DEFAULT_SERVER_CONFIG)
Example #3
0
    def test_store_config_empty(self):
        path = f'{self.tempdir}/{fake.file_name(extension="yml")}'
        store_config(path, {})
        config = read_config(path)

        self.assertTrue(isinstance(config, Dict))
        self.assertEqual(config, {})
Example #4
0
    def test_load_random_config(self):
        path = f'{self.tempdir}/{fake.file_name(extension="yml",)}'
        remote_value = fake.hostname()
        ui_value = fake.hostname()
        token_value = fake.password(length=40)
        config_value = {
            f'{fake.name()}': {
                'remote': f'{remote_value}',
                'ui': f'{ui_value}',
            },
            f'{fake.name()}': {
                'token': f'{token_value}',
            },
        }

        store_config(path, config_value)

        capture_output = io.StringIO()
        sys.stdout = capture_output
        config = load_config(path)

        output_value = '[INFO] We updated your AskAnna config file to support the latest features'
        self.assertNotIn(output_value, capture_output.getvalue())

        self.assertIsNotNone(config.remote)
        self.assertEqual(config.server, 'default')
        self.assertNotEqual(config.remote, remote_value)
        self.assertNotEqual(config.ui, ui_value)
        self.assertNotEqual(config.token, token_value)
Example #5
0
    def test_server_config_environment_variable(self):
        server_name = fake.name()
        os.environ['AA_SERVER'] = server_name

        path = f'{self.tempdir}/{fake.file_name(extension="yml")}'
        config_dict = {
            'server': {
                server_name: {
                    'remote': 'localhost',
                    'ui': 'localhost',
                    'token': 'localhost',
                }
            }
        }
        os.environ['AA_SERVER_CONFIG_FILE'] = path
        store_config(path, config_dict)

        try:
            del sys.modules['askanna.config.server']
        except:  # noqa
            pass
        from askanna.config.server import SERVER

        self.assertEqual(SERVER, server_name)
        self.assertNotEqual(SERVER, self.default_server_name)
Example #6
0
    def test_load_old_config_no_auth(self):
        path = f'{self.tempdir}/{fake.file_name(extension="yml")}'
        remote_value = fake.hostname()
        config_value = {'askanna': {'remote': f'{remote_value}'}}

        store_config(path, config_value)

        config_dict_before = read_config(path)
        self.assertIsNotNone(config_dict_before.get('askanna'))
        self.assertIsNone(config_dict_before.get('auth'))

        capture_output = io.StringIO()
        sys.stdout = capture_output
        config = load_config(path)

        output_value = '[INFO] We updated your AskAnna config file to support the latest features'
        self.assertIn(output_value, capture_output.getvalue())

        self.assertEqual(config.server, 'default')
        self.assertEqual(config.remote, remote_value)
        self.assertIs(config.ui, '')
        self.assertIs(config.token, '')
        self.assertFalse(config.is_authenticated)

        config_dict_after = read_config(path)
        self.assertIsNone(config_dict_after.get('askanna'))
        self.assertIsNone(config_dict_after.get('auth'))
Example #7
0
    def test_load_from_path(self):
        path = f'{self.tempdir}/{fake.file_name(extension="yml")}'
        remote_value = fake.hostname()
        ui_value = fake.hostname()
        token_value = fake.password(length=40)
        server_dict = {
            'remote': f'{remote_value}',
            'ui': f'{ui_value}',
            'token': f'{token_value}',
        }
        config_value = {
            'server': {
                'default': server_dict
            },
        }

        store_config(path, config_value)
        config = load_config(path)

        self.assertEqual(config.server, 'default')
        self.assertEqual(config.remote, remote_value)
        self.assertEqual(config.ui, ui_value)
        self.assertEqual(config.token, token_value)
        self.assertTrue(config.is_authenticated)
        self.assertEqual(config.config_dict['server']['default'], server_dict)
Example #8
0
 def save_server_to_config_file(self):
     self.config_dict = add_or_update_server_in_config_dict(
         self.config_dict,
         self.server,
         self.remote,
         self.token,
         self.ui
     )
     store_config(self.server_config_path, self.config_dict)
Example #9
0
    def test_store_overwrite(self):
        path = f'{self.tempdir}/{fake.file_name(extension="yml")}'
        config_1 = {"askanna": {"remote": "https://beta-api.askanna.eu"}}
        config_2 = {"config": "something"}

        store_config(path, config_1)
        config_read_1 = read_config(path)
        self.assertTrue(isinstance(config_read_1, Dict))
        self.assertEqual(config_read_1, config_1)

        store_config(path, config_2)
        config_read_2 = read_config(path)
        self.assertTrue(isinstance(config_read_2, Dict))
        self.assertEqual(config_read_2, config_2)
Example #10
0
    def test_load_non_default_server(self):
        path = f'{self.tempdir}/{fake.file_name(extension="yml")}'
        # default server
        remote_value_default = fake.hostname()
        ui_value_default = fake.hostname()
        token_value_default = fake.password(length=40)

        # non_default server
        server_name = fake.name()
        remote_value = fake.hostname()
        ui_value = fake.hostname()
        token_value = fake.password(length=40)
        server_dict = {
            'remote': f'{remote_value}',
            'ui': f'{ui_value}',
            'token': f'{token_value}',
        }

        config_value = {
            'server': {
                'default': {
                    'remote': f'{remote_value_default}',
                    'ui': f'{ui_value_default}',
                    'token': f'{token_value_default}',
                },
                server_name: server_dict,
            },
        }

        store_config(path, config_value)
        config = load_config(path, server_name)

        self.assertEqual(config.server, server_name)
        self.assertNotEqual(config.server, 'default')
        self.assertEqual(config.remote, remote_value)
        self.assertNotEqual(config.remote, remote_value_default)
        self.assertEqual(config.ui, ui_value)
        self.assertNotEqual(config.ui, ui_value_default)
        self.assertEqual(config.token, token_value)
        self.assertNotEqual(config.token, token_value_default)
        self.assertTrue(config.is_authenticated)
        self.assertEqual(config.config_dict['server'][server_name],
                         server_dict)
Example #11
0
    def test_load_non_existing_server(self):
        path = f'{self.tempdir}/{fake.file_name(extension="yml")}'
        # default server
        remote_value_default = fake.hostname()
        ui_value_default = fake.hostname()
        token_value_default = fake.password(length=40)

        # non_default server
        server_name = fake.name()
        remote_value = fake.hostname()
        ui_value = fake.hostname()
        token_value = fake.password(length=40)
        server_dict = {
            'remote': f'{remote_value}',
            'ui': f'{ui_value}',
            'token': f'{token_value}',
        }

        config_value = {
            'server': {
                'default': {
                    'remote': f'{remote_value_default}',
                    'ui': f'{ui_value_default}',
                    'token': f'{token_value_default}',
                },
                server_name: server_dict,
            },
        }

        store_config(path, config_value)
        not_existing_server = fake.name()

        capture_error = io.StringIO()
        sys.stderr = capture_error
        with pytest.raises(SystemExit):
            load_config(path, not_existing_server)

        output_value = f"Server settings for '{not_existing_server}' not found. Please try again with a valid " \
                       "server, or add this server."
        self.assertIn(output_value, capture_error.getvalue())
Example #12
0
    def setUp(self):
        self.environ_bck = dict(os.environ)
        self.tempdir = tempfile.mkdtemp(
            prefix='askanna-test-core-config-server')
        self.path = f'{self.tempdir}/{fake.file_name(extension="yml")}'

        self.server_name_setup = fake.name()
        self.remote_value_setup = fake.hostname()
        self.ui_value_setup = fake.hostname()
        self.token_value_setup = fake.password(length=40)
        server_dict = {
            'remote': f'{self.remote_value_setup}',
            'ui': f'{self.ui_value_setup}',
            'token': f'{self.token_value_setup}',
        }
        self.config_value_setup = {
            'server': {
                self.server_name_setup: server_dict
            },
        }

        store_config(self.path, self.config_value_setup)