Add basic unit tests for GnuPG module
- Cover basic GnuPG functionality with tests (confirm_key, public_keys). - Add a test public key file. - Fix resource leak by closing streams opened by Popen.
This commit is contained in:
parent
5c3fecfb85
commit
588b447e69
3 changed files with 54 additions and 8 deletions
|
@ -63,6 +63,9 @@ def public_keys(keyhome):
|
|||
keys[fingerprint] = email
|
||||
fingerprint = None
|
||||
email = None
|
||||
|
||||
p.stdout.close()
|
||||
p.stderr.close()
|
||||
return keys
|
||||
|
||||
|
||||
|
|
13
test/keys/bob@disposlab.pub
Normal file
13
test/keys/bob@disposlab.pub
Normal file
|
@ -0,0 +1,13 @@
|
|||
-----BEGIN PGP PUBLIC KEY BLOCK-----
|
||||
|
||||
mDMEYdTFkRYJKwYBBAHaRw8BAQdA2tgdP1pMt3cv3XAW7ov5AFn74mMZvyTksp9Q
|
||||
eO1PkpK0GkJvYiBGb29iYXIgPGJvYkBkaXNwb3NsYWI+iJYEExYIAD4WIQQZz0tH
|
||||
7MnEevqE1L2W85/aDjG7ZwUCYdTFkQIbAwUJA8JnAAULCQgHAgYVCgkICwIEFgID
|
||||
AQIeAQIXgAAKCRCW85/aDjG7ZxVnAP49t7BU2H+/WCpa3fCAlMEcik82sU4p+U9D
|
||||
pMsbjawwYgEA1SbA5CF835cMjoEufy1h+2M4T9gI/0X2lk8OAtwwggm4OARh1MXg
|
||||
EgorBgEEAZdVAQUBAQdAUVNKx2OsGtNdRsnl3J/uv6obkUC0KcO4ikdRs+iejlMD
|
||||
AQgHiHgEGBYIACAWIQQZz0tH7MnEevqE1L2W85/aDjG7ZwUCYdTF4AIbDAAKCRCW
|
||||
85/aDjG7Z039APwLGP5ibqCC9yIr4YVbdWff1Ch+2C91MR2ObF93Up9+ogD8D2zd
|
||||
OjjB6xRD0Q2FN+alsNGCtdutAs18AZ5l33RMzws=
|
||||
=wWoq
|
||||
-----END PGP PUBLIC KEY BLOCK-----
|
|
@ -2,15 +2,45 @@ import GnuPG
|
|||
|
||||
import unittest
|
||||
|
||||
class GnuPGUtilitiesTest(unittest.TestCase):
|
||||
def test_build_default_command(self):
|
||||
cmd = GnuPG._build_command("test/keyhome")
|
||||
self.assertEqual(cmd, ["gpg", "--homedir", "test/keyhome"])
|
||||
|
||||
def test_build_command_extended_with_args(self):
|
||||
cmd = GnuPG._build_command("test/keyhome", "--foo", "--bar")
|
||||
self.assertEqual(cmd, ["gpg", "--homedir", "test/keyhome", "--foo", "--bar"])
|
||||
class GnuPGUtilitiesTest(unittest.TestCase):
|
||||
def test_build_default_command(self):
|
||||
cmd = GnuPG._build_command("test/keyhome")
|
||||
self.assertEqual(cmd, ["gpg", "--homedir", "test/keyhome"])
|
||||
|
||||
def test_build_command_extended_with_args(self):
|
||||
cmd = GnuPG._build_command("test/keyhome", "--foo", "--bar")
|
||||
self.assertEqual(cmd, ["gpg", "--homedir", "test/keyhome", "--foo", "--bar"])
|
||||
|
||||
def test_key_confirmation_with_matching_email(self):
|
||||
armored_key = self._load('test/keys/bob@disposlab')
|
||||
matching_email = 'bob@disposlab'
|
||||
|
||||
is_confirmed = GnuPG.confirm_key(armored_key, matching_email)
|
||||
self.assertTrue(is_confirmed)
|
||||
|
||||
def test_key_confirmation_email_mismatch(self):
|
||||
armored_key = self._load('test/keys/bob@disposlab')
|
||||
not_matching_email = 'lucy@disposlab'
|
||||
|
||||
is_confirmed = GnuPG.confirm_key(armored_key, not_matching_email)
|
||||
self.assertFalse(is_confirmed)
|
||||
|
||||
def test_key_listing(self):
|
||||
keys = GnuPG.public_keys('test/keyhome')
|
||||
|
||||
known_identities = {
|
||||
'1CD245308F0963D038E88357973CF4D9387C44D7': 'alice@disposlab',
|
||||
'19CF4B47ECC9C47AFA84D4BD96F39FDA0E31BB67': 'bob@disposlab',
|
||||
'530B1BB2D0CC7971648198BBA4774E507D3AF5BC': 'evan@disposlab'
|
||||
}
|
||||
|
||||
self.assertDictEqual(keys, known_identities)
|
||||
|
||||
def _load(self, filename):
|
||||
with open(filename) as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
unittest.main()
|
||||
|
|
Loading…
Reference in a new issue