def test_sign_doc_no_keys(mocker, tmpdir, monkeypatch): pytest.importorskip('gpg') monkeypatch.setenv('GNUPGHOME', str(tmpdir)) inp = b'thebody' with pytest.raises(utils.SigningError) as e: utils.sign_doc(inp, 'key', 'pw') assert 'No key found in keyring' in e.value.args[0]
def test_sign_doc_invalid_key_id(mocker, tmp_path, monkeypatch): pytest.importorskip('gpg') tmp_path.chmod(0o700) monkeypatch.setenv('GNUPGHOME', str(tmp_path)) subprocess.run( ['gpg', '--yes', '--passphrase', '', '--batch', '--quick-generate-key', '--pinentry-mode', 'loopback', "'TEST SFA <*****@*****.**'"], check=True, capture_output=True ) inp = b'thebody' with pytest.raises(utils.SigningError) as e: utils.sign_doc(inp, 'key', '') assert 'No key found in keyring' in e.value.args[0]
def test_sign_doc_wrong_pw(mocker, tmp_path, monkeypatch, path, msg): pytest.importorskip('gpg') tmp_path.chmod(0o700) monkeypatch.setenv('GNUPGHOME', str(tmp_path)) passwd = b'jaljlj032904u2ojhsdf!@#43ljsdfa jsladf' with open(tmp_path / 'passwd', 'wb') as f: f.write(b'wrong') key_create = subprocess.run( ['gpg', '--yes', '--passphrase', passwd, '--batch', '--quick-generate-key', '--pinentry-mode', 'loopback', "'TEST SFA <*****@*****.**'"], check=True, capture_output=True ) key = re.match('(?<=key ).*(?= marked)', key_create.stderr.decode()) inp = b'thebody' with pytest.raises(utils.SigningError) as e: utils.sign_doc(inp, key, tmp_path / path) assert msg in e.value.args[0]
def test_sign_doc_no_pw_needed(mocker, tmp_path, monkeypatch): pytest.importorskip('gpg') tmp_path.chmod(0o700) monkeypatch.setenv('GNUPGHOME', str(tmp_path)) key_create = subprocess.run( ['gpg', '--yes', '--passphrase', '', '--batch', '--quick-generate-key', '--pinentry-mode', 'loopback', "'TEST SFA <*****@*****.**'"], check=True, capture_output=True ) key = re.match('(?<=key ).*(?= marked)', key_create.stderr.decode()) inp = b'thebody' out = utils.sign_doc(inp, key, '') assert out.startswith(b'-----BEGIN PGP SIGNATURE-----') assert out.endswith(b'-----END PGP SIGNATURE-----\n')
def test_sign_doc_no_gpg(mocker): mocker.patch.dict('sys.modules', {'gpg': None}) inp = b'TEST BODY' with pytest.raises(utils.SigningError) as e: utils.sign_doc(inp, 'key', 'pw') assert 'Could not import gpg' in e.value.args[0]