34
class FakeConfig(object):
36
def gpg_signing_key(self):
37
return "amy@example.com"
39
def gpg_signing_command(self):
42
def acceptable_keys(self):
46
class TestCommandLine(TestCase):
37
class FakeConfig(config.MemoryStack):
39
def __init__(self, content=None):
42
gpg_signing_key=amy@example.com
43
gpg_signing_command=false'''
44
super(FakeConfig, self).__init__(content)
47
class TestCommandLine(tests.TestCase):
50
super(TestCommandLine, self).setUp()
51
self.my_gpg = gpg.GPGStrategy(FakeConfig())
48
53
def test_signing_command_line(self):
49
my_gpg = gpg.GPGStrategy(FakeConfig())
54
self.assertEqual(['false', '--clearsign', '-u', 'amy@example.com'],
55
self.my_gpg._command_line())
57
def test_signing_command_line_from_default(self):
58
# Using 'default' for gpg_signing_key will use the mail part of 'email'
59
my_gpg = gpg.GPGStrategy(FakeConfig('''
60
email=Amy <amy@example.com>
61
gpg_signing_key=default
62
gpg_signing_command=false'''))
63
self.assertEqual(['false', '--clearsign', '-u', 'amy@example.com'],
64
my_gpg._command_line())
66
def test_signing_command_line_from_email(self):
67
# Not setting gpg_signing_key will use the mail part of 'email'
68
my_gpg = gpg.GPGStrategy(FakeConfig('''
69
email=Amy <amy@example.com>
70
gpg_signing_command=false'''))
50
71
self.assertEqual(['false', '--clearsign', '-u', 'amy@example.com'],
51
72
my_gpg._command_line())
53
74
def test_checks_return_code(self):
54
75
# This test needs a unix like platform - one with 'false' to run.
55
76
# if you have one, please make this work :)
56
my_gpg = gpg.GPGStrategy(FakeConfig())
57
self.assertRaises(errors.SigningFailed, my_gpg.sign, 'content')
77
self.assertRaises(errors.SigningFailed, self.my_gpg.sign, 'content')
59
79
def assertProduces(self, content):
60
80
# This needs a 'cat' command or similar to work.
61
my_gpg = gpg.GPGStrategy(FakeConfig())
62
81
if sys.platform == 'win32':
63
82
# Windows doesn't come with cat, and we don't require it
64
83
# so lets try using python instead.
65
84
# But stupid windows and line-ending conversions.
66
85
# It is too much work to make sys.stdout be in binary mode.
67
86
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65443
68
my_gpg._command_line = lambda:[sys.executable, '-c',
87
self.my_gpg._command_line = lambda:[sys.executable, '-c',
69
88
'import sys; sys.stdout.write(sys.stdin.read())']
70
89
new_content = content.replace('\n', '\r\n')
72
self.assertEqual(new_content, my_gpg.sign(content))
91
self.assertEqual(new_content, self.my_gpg.sign(content))
74
my_gpg._command_line = lambda:['cat', '-']
75
self.assertEqual(content, my_gpg.sign(content))
93
self.my_gpg._command_line = lambda:['cat', '-']
94
self.assertEqual(content, self.my_gpg.sign(content))
77
96
def test_returns_output(self):
78
97
content = "some content\nwith newlines\n"