From e5ff95d09c972d23725e0020799176ddfbd4a0fe Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Sat, 30 Nov 2019 20:16:44 -0600 Subject: [PATCH] Create gnupg fixture This fixture is a session scoped gnupg home directory, along with a method to set the mocked password which will be used by the pinentry-mock program. --- test/conftest.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/conftest.py b/test/conftest.py index 5cd3c19..8982a6c 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -8,6 +8,7 @@ import os import platform import pwd from subprocess import Popen, PIPE +import py import pytest @@ -544,3 +545,29 @@ def ds1(ds1_work_copy, paths, ds1_dset): dscopy = copy.deepcopy(ds1_dset) dscopy.relative_to(copy.deepcopy(paths.work)) return dscopy + + +@pytest.fixture(scope='session') +def gnupg(tmpdir_factory, runner): + """Location of GNUPGHOME""" + + def register_gpg_password(password): + """Publish a new GPG mock password""" + py.path.local('/tmp/mock-password').write(password) + + home = tmpdir_factory.mktemp('gnupghome') + home.chmod(0o700) + conf = home.join('gpg-agent.conf') + conf.write( + f'pinentry-program {os.path.abspath("test/pinentry-mock")}\n' + 'max-cache-ttl 0\n' + ) + conf.chmod(0o600) + data = collections.namedtuple('GNUPG', ['home', 'pw']) + env = os.environ.copy() + env['GNUPGHOME'] = home + + # this pre-populates std files in the GNUPGHOME + runner(['gpg', '-k'], env=env) + + return data(home, register_gpg_password)