Beispiel #1
0
def test_get_with_partial_credentials(partial_credentials):
    config_dict = EnvironmentConfig(partial_credentials).to_dict()

    assert len(config_dict.keys()) == 3
    assert config_dict["server_config"] == "password"
    assert config_dict["deployment"] == "develop"
    assert config_dict["user"] == "test"
Beispiel #2
0
def test_get_with_credentials(with_credentials):
    config_dict = EnvironmentConfig(with_credentials).to_dict()

    assert len(config_dict.keys()) == 3
    assert config_dict["server_config"] == "password"
    assert config_dict["user"] == "test"
    assert config_dict["password_hash"] == "abcdef"
Beispiel #3
0
def test_get_users(standard_with_credentials):
    environment_config = EnvironmentConfig(standard_with_credentials)
    users = environment_config.get_users()

    assert len(users) == 1
    assert users[0].id == 1
    assert users[0].username == "test"
    assert users[0].secret == "abcdef"
Beispiel #4
0
    def test_to_dict(self):
        conf_json1 = json.dumps(config_mocks.CONFIG_WITH_CREDENTIALS)
        self._test_to_dict(EnvironmentConfig.get_from_json(conf_json1))

        conf_json2 = json.dumps(config_mocks.CONFIG_NO_CREDENTIALS)
        self._test_to_dict(EnvironmentConfig.get_from_json(conf_json2))

        conf_json3 = json.dumps(config_mocks.CONFIG_PARTIAL_CREDENTIALS)
        self._test_to_dict(EnvironmentConfig.get_from_json(conf_json3))
Beispiel #5
0
def test_save_to_file_preserve_log_level(config_file, with_credentials):
    shutil.copyfile(with_credentials, config_file)

    environment_config = EnvironmentConfig(config_file)
    environment_config.aws = "test_aws"
    environment_config.save_to_file()

    with open(config_file, "r") as f:
        from_file = json.load(f)

    assert "log_level" in from_file
    assert from_file["log_level"] == "NOTICE"
Beispiel #6
0
    def _test_save_to_file(self, config: Dict):
        user_creds = UserCreds.get_from_dict(config)
        env_config = EnvironmentConfig(server_config=config['server_config'],
                                       deployment=config['deployment'],
                                       user_creds=user_creds)

        env_config.save_to_file()
        file_path = get_server_config_file_path_test_version()
        with open(file_path, 'r') as f:
            content_from_file = f.read()
        os.remove(file_path)

        self.assertDictEqual(config, json.loads(content_from_file))
Beispiel #7
0
    def test_get_from_dict(self):
        config_dict = config_mocks.CONFIG_WITH_CREDENTIALS
        env_conf = EnvironmentConfig.get_from_dict(config_dict)
        self.assertEqual(env_conf.server_config, config_dict['server_config'])
        self.assertEqual(env_conf.deployment, config_dict['deployment'])
        self.assertEqual(env_conf.user_creds.username, config_dict['user'])
        self.assertEqual(env_conf.aws, None)

        config_dict = config_mocks.CONFIG_BOGUS_VALUES
        env_conf = EnvironmentConfig.get_from_dict(config_dict)
        self.assertEqual(env_conf.server_config, config_dict['server_config'])
        self.assertEqual(env_conf.deployment, config_dict['deployment'])
        self.assertEqual(env_conf.user_creds.username, config_dict['user'])
        self.assertEqual(env_conf.aws, config_dict['aws'])
Beispiel #8
0
 def test_get_server_config_file_path(self):
     if platform.system() == "Windows":
         server_file_path = MONKEY_ISLAND_ABS_PATH + r"\cc\server_config.json"
     else:
         server_file_path = MONKEY_ISLAND_ABS_PATH + "/cc/server_config.json"
     self.assertEqual(EnvironmentConfig.get_config_file_path(),
                      server_file_path)
Beispiel #9
0
def test_add_user(config_file, standard_with_credentials):
    new_user = "******"
    new_password_hash = "fedcba"
    new_user_creds = UserCreds(new_user, new_password_hash)

    shutil.copyfile(standard_with_credentials, config_file)

    environment_config = EnvironmentConfig(config_file)
    environment_config.add_user(new_user_creds)

    with open(config_file, "r") as f:
        from_file = json.load(f)

    assert len(from_file["environment"].keys()) == 4
    assert from_file["environment"]["user"] == new_user
    assert from_file["environment"]["password_hash"] == new_password_hash
Beispiel #10
0
def main():
    args = parse_args()
    file_path = EnvironmentConfig.get_config_file_path()

    if args.server_config == "restore":
        restore_previous_config(file_path)
        quit()

    # Read config
    with open(file_path) as config_file:
        config_data = json.load(config_file)

    # Backup the config
    with open(BACKUP_CONFIG_FILENAME, "w") as backup_file:
        json.dump(config_data, backup_file, indent=4)
        backup_file.write("\n")

    # Edit the config
    config_data[SERVER_CONFIG] = args.server_config

    # Write new config
    logger.info("Writing the following config: {}".format(json.dumps(config_data, indent=4)))
    with open(file_path, "w") as config_file:
        json.dump(config_data, config_file, indent=4)
        config_file.write("\n")  # Have to add newline at end of file, since json.dump does not.
Beispiel #11
0
    def _test_to_dict(self, env_config_object: EnvironmentConfig):
        test_dict = {'server_config': env_config_object.server_config,
                     'deployment': env_config_object.deployment}
        user_creds = env_config_object.user_creds
        if user_creds.username:
            test_dict.update({'user': user_creds.username})
        if user_creds.password_hash:
            test_dict.update({'password_hash': user_creds.password_hash})

        self.assertDictEqual(test_dict, env_config_object.to_dict())
Beispiel #12
0
 def _test_get_from_json(self, config: Dict):
     config_json = json.dumps(config)
     env_config_object = EnvironmentConfig.get_from_json(config_json)
     self.assertEqual(config['server_config'], env_config_object.server_config)
     self.assertEqual(config['deployment'], env_config_object.deployment)
     if 'user' in config:
         self.assertEqual(config['user'], env_config_object.user_creds.username)
     if 'password_hash' in config:
         self.assertEqual(config['password_hash'], env_config_object.user_creds.password_hash)
     if 'aws' in config:
         self.assertEqual(config['aws'], env_config_object.aws)
Beispiel #13
0
def main():
    args = parse_args()
    file_path = EnvironmentConfig.get_config_file_path()

    # Read config
    with open(file_path) as config_file:
        config_data = json.load(config_file)

    # Edit the config
    config_data[SERVER_CONFIG] = args.server_config

    # Write new config
    logger.info("Writing the following config: {}".format(json.dumps(config_data, indent=4)))
    with open(file_path, "w") as config_file:
        json.dump(config_data, config_file, indent=4)
        config_file.write("\n")  # Have to add newline at end of file, since json.dump does not.
Beispiel #14
0
def test_save_to_file(config_file, standard_with_credentials):
    shutil.copyfile(standard_with_credentials, config_file)

    environment_config = EnvironmentConfig(config_file)
    environment_config.aws = "test_aws"
    environment_config.save_to_file()

    with open(config_file, "r") as f:
        from_file = json.load(f)

    assert environment_config.to_dict() == from_file["environment"]
Beispiel #15
0
def test_get_with_no_credentials(no_credentials):
    config_dict = EnvironmentConfig(no_credentials).to_dict()

    assert len(config_dict.keys()) == 2
    assert config_dict["server_config"] == "password"
    assert config_dict["deployment"] == "develop"