def test_environment_variable_multiple_global_items(): """Test the encrypt module's CLI function with the --env flag and a nonempty YAML file. The YAML file's global key is a list of items. The global key needs to be traversed and the secure key if found needs to be overwritten.""" runner = CliRunner() with runner.isolated_filesystem(): initial_data = OrderedDict([('language', 'python'), ('dist', 'trusty'), ('env', {'global': ['SOMETHING', 'OR_ANOTHER', {'secure': 'API_KEY="SUPER_INSECURE_KEY"'}]})]) with open('file.yml', 'w') as file: ordered_dump(initial_data, file) result = runner.invoke(cli, ['--env', 'mandeep', 'Travis-Encrypt', 'file.yml'], 'SUPER_SECURE_API_KEY') assert not result.exception with open('file.yml') as file: config = ordered_load(file) assert config['language'] == 'python' assert config['dist'] == 'trusty' assert ['language', 'dist', 'env'] == [key for key in config.keys()] for item in config['env']['global']: if 'secure' in item: assert base64.b64decode(item['secure'])
def test_deploy_nonempty_file(): """Test the encrypt module's CLI function with the --deploy flag and a nonempty YAML file. The YAML file includes information that needs to be overwritten.""" runner = CliRunner() with runner.isolated_filesystem(): initial_data = OrderedDict([('language', 'python'), ('dist', 'trusty'), ('deploy', {'password': {'secure': 'SUPER_INSECURE_PASSWORD'}})]) with open('file.yml', 'w') as file: ordered_dump(initial_data, file) result = runner.invoke(cli, ['--deploy', 'mandeep', 'Travis-Encrypt', 'file.yml'], 'SUPER_SECURE_PASSWORD') assert not result.exception with open('file.yml') as file: config = ordered_load(file) assert config['language'] == 'python' assert config['dist'] == 'trusty' assert base64.b64decode(config['deploy']['password']['secure']) assert ['language', 'dist', 'deploy'] == [key for key in config.keys()]
def test_deploy_empty_file(): """Test the encrypt module's CLI function with the --deploy flag and an empty YAML file.""" runner = CliRunner() with runner.isolated_filesystem(): initial_data = {'language': 'python'} with open('file.yml', 'w') as file: ordered_dump(initial_data, file) result = runner.invoke(cli, ['--deploy', 'mandeep', 'Travis-Encrypt', 'file.yml'], 'SUPER_SECURE_PASSWORD') assert not result.exception with open('file.yml') as file: config = ordered_load(file) assert config['language'] == 'python' assert base64.b64decode(config['deploy']['password']['secure'])
def test_dotenv_mutually_exclusive(): """Test the --env-file CLI option along with the --password option. The --password flag will also be passed to make sure that an exception is thrown. """ runner = CliRunner() with runner.isolated_filesystem(): with open('test.env', 'w') as env_file: env_file.write("API_KEY=MY_PASSWORD") initial_data = {'language': 'python'} with open('file.yml', 'w') as file: ordered_dump(initial_data, file) result = runner.invoke(cli, ['mandeep', 'Travis-Encrypt', 'file.yml', '--env-file=test.env', '--password', 'TEST']) assert 'Error: Illegal usage: `password` flag cannot be used with `env_file` flag.\n' in result.output assert result.exception
def dump_travis_configuration(config, path): """Dump the travis configuration settings to the travis.yml file. The configuration settings from the travis.yml will be dumped with ordering preserved. Thus, when a password is added to the travis.yml file, a diff will show that only the password was added. Parameters ---------- config: collections.OrderedDict The configuration settings to dump into the travis.yml file path: str The file path to the .travis.yml file Returns ------- None """ with open(path, 'w') as config_file: ordered_dump(config, config_file, default_flow_style=False)
def test_environment_variable_empty_file(): """Test the encrypt module's CLI function with the --env flag and an empty YAML file.""" runner = CliRunner() with runner.isolated_filesystem(): initial_data = {'language': 'python'} with open('file.yml', 'w') as file: ordered_dump(initial_data, file) result = runner.invoke(cli, ['--env', 'mandeep', 'Travis-Encrypt', 'file.yml'], 'API_KEY=SUPER_SECURE_KEY') assert not result.exception with open('file.yml') as file: config = ordered_load(file) assert config['language'] == 'python' assert base64.b64decode(config['env']['global']['secure']) assert ['language', 'env'] == [key for key in config.keys()]
def test_dotenv_empty_file(): """Test the --env-file CLI option with a dotenv file and a YAML file. The API key from the dotenv file will be added to the YAML configuration file. """ runner = CliRunner() with runner.isolated_filesystem(): with open('test.env', 'w') as env_file: env_file.write("API_KEY=MY_PASSWORD") initial_data = {'language': 'python'} with open('file.yml', 'w') as file: ordered_dump(initial_data, file) result = runner.invoke(cli, ['mandeep', 'Travis-Encrypt', 'file.yml', '--env-file=test.env']) assert not result.exception with open('file.yml') as file: config = ordered_load(file) assert 'env' in config assert 'global' in config['env'] assert config['language'] == 'python' assert base64.b64decode(config['env']['global']['API_KEY']['secure'])