From 6a15e8d13886f6ab8d25d751338cbdfd866a9618 Mon Sep 17 00:00:00 2001 From: Marian Beermann Date: Mon, 9 May 2016 12:02:16 +0200 Subject: [PATCH] testsuite/key: environment_variable -> monkeypatch.setenv --- borg/testsuite/__init__.py | 2 -- borg/testsuite/key.py | 37 +++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/borg/testsuite/__init__.py b/borg/testsuite/__init__.py index 978c1ab42..cccf97a82 100644 --- a/borg/testsuite/__init__.py +++ b/borg/testsuite/__init__.py @@ -45,8 +45,6 @@ class BaseTestCase(unittest.TestCase): assert_not_in = unittest.TestCase.assertNotIn assert_equal = unittest.TestCase.assertEqual assert_not_equal = unittest.TestCase.assertNotEqual - assert_raises = unittest.TestCase.assertRaises - assert_raises_regex = unittest.TestCase.assertRaisesRegex assert_true = unittest.TestCase.assertTrue if raises: diff --git a/borg/testsuite/key.py b/borg/testsuite/key.py index 65ad9a741..360b88a0c 100644 --- a/borg/testsuite/key.py +++ b/borg/testsuite/key.py @@ -10,7 +10,6 @@ import pytest from ..crypto import bytes_to_long, num_aes_blocks from ..key import PlaintextKey, PassphraseKey, KeyfileKey, Passphrase, PasswordRetriesExceeded, bin_to_hex from ..helpers import Location, Chunk, IntegrityError -from . import environment_variable @pytest.fixture(autouse=True) @@ -82,20 +81,21 @@ class TestKey: chunk = Chunk(b'foo') assert chunk == key2.decrypt(key.id_hash(chunk.data), key.encrypt(chunk)) - def test_keyfile_kfenv(self, tmpdir): + def test_keyfile_kfenv(self, tmpdir, monkeypatch): keyfile = tmpdir.join('keyfile') - with environment_variable(BORG_KEY_FILE=str(keyfile), BORG_PASSPHRASE='testkf'): - assert not keyfile.exists() - key = KeyfileKey.create(self.MockRepository(), self.MockArgs()) - assert keyfile.exists() - chunk = Chunk(b'XXX') - chunk_id = key.id_hash(chunk.data) - chunk_cdata = key.encrypt(chunk) - key = KeyfileKey.detect(self.MockRepository(), chunk_cdata) - assert chunk == key.decrypt(chunk_id, chunk_cdata) - keyfile.remove() - with pytest.raises(FileNotFoundError): - KeyfileKey.detect(self.MockRepository(), chunk_cdata) + monkeypatch.setenv('BORG_KEY_FILE', str(keyfile)) + monkeypatch.setenv('BORG_PASSPHRASE', 'testkf') + assert not keyfile.exists() + key = KeyfileKey.create(self.MockRepository(), self.MockArgs()) + assert keyfile.exists() + chunk = Chunk(b'XXX') + chunk_id = key.id_hash(chunk.data) + chunk_cdata = key.encrypt(chunk) + key = KeyfileKey.detect(self.MockRepository(), chunk_cdata) + assert chunk == key.decrypt(chunk_id, chunk_cdata) + keyfile.remove() + with pytest.raises(FileNotFoundError): + KeyfileKey.detect(self.MockRepository(), chunk_cdata) def test_keyfile2(self, monkeypatch): with open(os.path.join(os.environ['BORG_KEYS_DIR'], 'keyfile'), 'w') as fd: @@ -104,13 +104,14 @@ class TestKey: key = KeyfileKey.detect(self.MockRepository(), self.keyfile2_cdata) assert key.decrypt(self.keyfile2_id, self.keyfile2_cdata).data == b'payload' - def test_keyfile2_kfenv(self, tmpdir): + def test_keyfile2_kfenv(self, tmpdir, monkeypatch): keyfile = tmpdir.join('keyfile') with keyfile.open('w') as fd: fd.write(self.keyfile2_key_file) - with environment_variable(BORG_KEY_FILE=str(keyfile), BORG_PASSPHRASE='passphrase'): - key = KeyfileKey.detect(self.MockRepository(), self.keyfile2_cdata) - assert key.decrypt(self.keyfile2_id, self.keyfile2_cdata).data == b'payload' + monkeypatch.setenv('BORG_KEY_FILE', str(keyfile)) + monkeypatch.setenv('BORG_PASSPHRASE', 'passphrase') + key = KeyfileKey.detect(self.MockRepository(), self.keyfile2_cdata) + assert key.decrypt(self.keyfile2_id, self.keyfile2_cdata).data == b'payload' def test_passphrase(self, keys_dir, monkeypatch): monkeypatch.setenv('BORG_PASSPHRASE', 'test')