def test_edit_command_loop(tmpdir, capsys): with EncryptedConfigFile(str(tmpdir / "asdf"), write_lock=True) as sf: editor = Editor("true", sf) editor.cleartext = "asdf" with pytest.raises(ValueError): editor.process_cmd('asdf') def broken_cmd(): raise RuntimeError('gpg is broken') editor.edit = broken_cmd editor.encrypt = broken_cmd cmds = ['edit', 'asdf', 'encrypt', 'quit'] def _input(): return cmds.pop(0) editor._input = _input editor.interact() out, err = capsys.readouterr() assert err == '' assert out == """\
def test_edit(tmpdir): with EncryptedConfigFile(str(tmpdir / "asdf"), write_lock=True) as sf: editor = Editor("true", environment='none', edit_file=list(sf.files.keys())[0]) editor.cleartext = "asdf" editor.edit() assert editor.cleartext == "asdf"
def test_open_nonexistent_file_for_write_should_create_template_file(): tf = tempfile.NamedTemporaryFile(prefix="new_encrypted.") tf.close() # deletes file encrypted = EncryptedConfigFile(tf.name, write_lock=True) with encrypted as secrets: assert secrets.main_file.cleartext == NEW_FILE_TEMPLATE # The file exists, because we set the write lock assert os.path.exists(secrets.main_file.encrypted_filename) # When saving a config without members, delete the file. assert not os.path.exists(secrets.main_file.encrypted_filename)
def test_secrets_override_without_interpolation(tmp_path): environment = tmp_path / "environments" / "env" environment.mkdir(parents=True) # `add_secrets_to_environment` assumes to be run in the batou root os.chdir(tmp_path) secret_file = environment / "secrets.cfg" encrypted = EncryptedConfigFile(secret_file, write_lock=True) with encrypted as secrets: secrets.main_file.cleartext = """\ [batou] members = batou [asdf] x = asdf%asdf% [host:localhost] data-asdf = 2 data-bsdf = 1 data-csdf = 3 """ secrets.read() with encrypted.add_file(environment / 'secret-asdf.txt') as f: f.cleartext = 'hello to me!' secrets.write() env = mock.Mock() env.name = "env" env.components = ["asdf"] env.overrides = {} env.hosts = {} env.hosts["localhost"] = host = mock.Mock() env.secret_files = {} env.secret_data = set() host.data = {"asdf": 1, "csdf": 3} add_secrets_to_environment(env) assert env.overrides == {"asdf": {"x": "asdf%asdf%"}} assert host.data == {"asdf": "2", "bsdf": "1", "csdf": "3"} assert env.secret_files['asdf.txt'] == 'hello to me!' assert env.secret_data == {'asdf%asdf%', 'hello', 'me!'}
def test_write_should_fail_unless_write_locked(encrypted_file): with EncryptedConfigFile(encrypted_file) as secrets: secrets.main_file.cleartext = """\ [batou] members = batou [asdf] x = 1 """ secrets.read() with pytest.raises(RuntimeError): secrets.write()
def test_write_fails_if_recipient_key_is_missing_keeps_old_file( encrypted_file): encrypted = EncryptedConfigFile(encrypted_file, write_lock=True) with encrypted as secrets: secrets.main_file.cleartext = """\ [batou] members = [email protected] [asdf] x = 1 """ secrets.read() with pytest.raises(RuntimeError): secrets.write() assert encrypted_file.read_bytes() == FIXTURE_ENCRYPTED_CONFIG.read_bytes()
def test_write_fails_without_recipients(encrypted_file): encrypted = EncryptedConfigFile(encrypted_file, write_lock=True) with encrypted as secrets: secrets.main_file.cleartext = """\ [batou] members = [asdf] x = 1 """ secrets.read() with pytest.raises(ValueError) as exc: secrets.write() assert str(exc.value).startswith('Need at least one recipient.')
def test_write(encrypted_file): encrypted = EncryptedConfigFile(encrypted_file, write_lock=True) with encrypted as secrets: secrets.main_file.cleartext = """\ [batou] members = batou [asdf] x = 1 """ secrets.read() secrets.write() assert FIXTURE_ENCRYPTED_CONFIG.read_bytes() != encrypted_file.read_bytes() assert 0 != encrypted_file.stat().st_size
def test_open_nonexistent_file_for_read_should_fail(): with pytest.raises(IOError): EncryptedConfigFile("/no/such/file").__enter__()
def test_decrypt_missing_key(monkeypatch, encrypted_file): monkeypatch.setitem(os.environ, "GNUPGHOME", '/tmp') with pytest.raises(batou.GPGCallError): EncryptedConfigFile(encrypted_file)
def test_caches_cleartext(encrypted_file): with EncryptedConfigFile(encrypted_file) as secrets: secrets.main_file.cleartext = "[foo]bar=2" secrets.read() assert secrets.main_file.cleartext == "[foo]bar=2"
def test_decrypt(encrypted_file): with EncryptedConfigFile(encrypted_file) as secrets: with open(cleartext_file) as cleartext: assert (cleartext.read().strip() == secrets.main_file.cleartext.strip())
def test_write_unparsable_raises_error(encrypted_file): encrypted = EncryptedConfigFile(encrypted_file, write_lock=True) with encrypted as secrets: secrets.main_file.cleartext = "some new file contents\n" with pytest.raises(configparser.Error): secrets.read()
def test_edit(tmpdir): with EncryptedConfigFile(str(tmpdir / "asdf"), write_lock=True) as sf: editor = Editor("true", sf) editor.cleartext = "asdf" editor.edit() assert editor.cleartext == "asdf"
def test_edit(tmpdir): with EncryptedConfigFile(str(tmpdir / 'asdf'), write_lock=True) as sf: editor = Editor('true', sf) editor.cleartext = 'asdf' editor.edit() assert editor.cleartext == 'asdf'