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())
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):
53
48
def test_signing_command_line(self):
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'''))
49
my_gpg = gpg.GPGStrategy(FakeConfig())
71
50
self.assertEqual(['false', '--clearsign', '-u', 'amy@example.com'],
72
51
my_gpg._command_line())
74
53
def test_checks_return_code(self):
75
54
# This test needs a unix like platform - one with 'false' to run.
76
55
# if you have one, please make this work :)
77
self.assertRaises(errors.SigningFailed, self.my_gpg.sign, 'content')
56
my_gpg = gpg.GPGStrategy(FakeConfig())
57
self.assertRaises(errors.SigningFailed, my_gpg.sign, 'content')
79
59
def assertProduces(self, content):
80
60
# This needs a 'cat' command or similar to work.
61
my_gpg = gpg.GPGStrategy(FakeConfig())
81
62
if sys.platform == 'win32':
82
63
# Windows doesn't come with cat, and we don't require it
83
64
# so lets try using python instead.
84
65
# But stupid windows and line-ending conversions.
85
66
# It is too much work to make sys.stdout be in binary mode.
86
67
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65443
87
self.my_gpg._command_line = lambda:[sys.executable, '-c',
68
my_gpg._command_line = lambda:[sys.executable, '-c',
88
69
'import sys; sys.stdout.write(sys.stdin.read())']
89
70
new_content = content.replace('\n', '\r\n')
91
self.assertEqual(new_content, self.my_gpg.sign(content))
72
self.assertEqual(new_content, my_gpg.sign(content))
93
self.my_gpg._command_line = lambda:['cat', '-']
94
self.assertEqual(content, self.my_gpg.sign(content))
74
my_gpg._command_line = lambda:['cat', '-']
75
self.assertEqual(content, my_gpg.sign(content))
96
77
def test_returns_output(self):
97
78
content = "some content\nwith newlines\n"