Commit cffe528a by Scott Duckworth

add tests for exporting keys

parent a9c91bba
...@@ -50,9 +50,14 @@ def ssh_keygen(type=None, passphrase='', comment=None, file=None): ...@@ -50,9 +50,14 @@ def ssh_keygen(type=None, passphrase='', comment=None, file=None):
cmd += ['-f', file] cmd += ['-f', file]
subprocess.check_call(cmd) subprocess.check_call(cmd)
def ssh_key_export(input_file, output_file, format='RFC4716'): def ssh_key_export(input_path, output_path, format='RFC4716'):
cmd = ['ssh-keygen', '-e', '-m', format, '-f', input_file] cmd = ['ssh-keygen', '-e', '-m', format, '-f', input_path]
with open(output_file, 'wb') as f: with open(output_path, 'wb') as f:
subprocess.check_call(cmd, stdout=f)
def ssh_key_import(input_path, output_path, format='RFC4716'):
cmd = ['ssh-keygen', '-i', '-m', format, '-f', input_path]
with open(output_path, 'wb') as f:
subprocess.check_call(cmd, stdout=f) subprocess.check_call(cmd, stdout=f)
def ssh_fingerprint(pubkey_path): def ssh_fingerprint(pubkey_path):
...@@ -274,7 +279,7 @@ class RFC4716TestCase(BaseTestCase): ...@@ -274,7 +279,7 @@ class RFC4716TestCase(BaseTestCase):
def tearDown(self): def tearDown(self):
UserKey.objects.all().delete() UserKey.objects.all().delete()
def test_with_comment(self): def test_import_with_comment(self):
key = UserKey( key = UserKey(
user = self.user1, user = self.user1,
name = 'name', name = 'name',
...@@ -284,7 +289,7 @@ class RFC4716TestCase(BaseTestCase): ...@@ -284,7 +289,7 @@ class RFC4716TestCase(BaseTestCase):
key.save() key.save()
self.assertEqual(key.key.split()[:2], open(self.key1_path+'.pub').read().split()[:2]) self.assertEqual(key.key.split()[:2], open(self.key1_path+'.pub').read().split()[:2])
def test_without_comment(self): def test_import_without_comment(self):
key = UserKey( key = UserKey(
user = self.user1, user = self.user1,
name = 'name', name = 'name',
...@@ -294,6 +299,21 @@ class RFC4716TestCase(BaseTestCase): ...@@ -294,6 +299,21 @@ class RFC4716TestCase(BaseTestCase):
key.save() key.save()
self.assertEqual(key.key.split()[:2], open(self.key2_path+'.pub').read().split()[:2]) self.assertEqual(key.key.split()[:2], open(self.key2_path+'.pub').read().split()[:2])
def test_export(self):
key = UserKey(
user = self.user1,
name = 'name',
key = open(self.key1_path+'.pub').read(),
)
key.full_clean()
key.save()
export_path = os.path.join(self.key_dir, 'export')
import_path = os.path.join(self.key_dir, 'import')
with open(export_path, 'w') as f:
f.write(key.export('RFC4716'))
ssh_key_import(export_path, import_path, 'RFC4716')
self.assertEqual(open(import_path).read().split()[:2], open(self.key1_path+'.pub').read().split()[:2])
class PemTestCase(BaseTestCase): class PemTestCase(BaseTestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
...@@ -312,7 +332,7 @@ class PemTestCase(BaseTestCase): ...@@ -312,7 +332,7 @@ class PemTestCase(BaseTestCase):
def tearDown(self): def tearDown(self):
UserKey.objects.all().delete() UserKey.objects.all().delete()
def test(self): def test_import(self):
key = UserKey( key = UserKey(
user = self.user1, user = self.user1,
name = 'name', name = 'name',
...@@ -322,6 +342,21 @@ class PemTestCase(BaseTestCase): ...@@ -322,6 +342,21 @@ class PemTestCase(BaseTestCase):
key.save() key.save()
self.assertEqual(key.key.split()[:2], open(self.key1_path+'.pub').read().split()[:2]) self.assertEqual(key.key.split()[:2], open(self.key1_path+'.pub').read().split()[:2])
def test_export(self):
key = UserKey(
user = self.user1,
name = 'name',
key = open(self.key1_path+'.pub').read(),
)
key.full_clean()
key.save()
export_path = os.path.join(self.key_dir, 'export')
import_path = os.path.join(self.key_dir, 'import')
with open(export_path, 'w') as f:
f.write(key.export('PEM'))
ssh_key_import(export_path, import_path, 'PEM')
self.assertEqual(open(import_path).read().split()[:2], open(self.key1_path+'.pub').read().split()[:2])
class UserKeyLookupTestCase(BaseTestCase): class UserKeyLookupTestCase(BaseTestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment