Commit 78ac0909 authored by Michal Čihař's avatar Michal Čihař

Test parsing of known hosts

parent e068b1db
...@@ -39,6 +39,9 @@ import os ...@@ -39,6 +39,9 @@ import os
# List of default domain names on which warn user # List of default domain names on which warn user
DEFAULT_DOMAINS = ('example.net', 'example.com') DEFAULT_DOMAINS = ('example.net', 'example.com')
# SSH key files
KNOWN_HOSTS_FILE = os.path.expanduser('~/.ssh/known_hosts')
RSA_KEY_FILE = os.path.expanduser('~/.ssh/id_rsa.pub')
@staff_member_required @staff_member_required
def report(request): def report(request):
...@@ -165,7 +168,7 @@ def get_host_keys(): ...@@ -165,7 +168,7 @@ def get_host_keys():
''' '''
try: try:
result = [] result = []
with open(os.path.expanduser('~/.ssh/known_hosts'), 'r') as handle: with open(KNOWN_HOSTS_FILE, 'r') as handle:
for line in handle: for line in handle:
if ' ssh-rsa ' not in line: if ' ssh-rsa ' not in line:
continue continue
...@@ -181,13 +184,10 @@ def ssh(request): ...@@ -181,13 +184,10 @@ def ssh(request):
''' '''
Show information and manipulate with SSH key. Show information and manipulate with SSH key.
''' '''
# Path to key, we default to RSA keys
key_path = os.path.expanduser('~/.ssh/id_rsa.pub')
# Check whether we can generate SSH key # Check whether we can generate SSH key
try: try:
ret = subprocess.check_call(['which', 'ssh-keygen']) ret = subprocess.check_call(['which', 'ssh-keygen'])
can_generate = (ret == 0 and not os.path.exists(key_path)) can_generate = (ret == 0 and not os.path.exists(RSA_KEY_FILE))
except: except:
can_generate = False can_generate = False
...@@ -197,7 +197,7 @@ def ssh(request): ...@@ -197,7 +197,7 @@ def ssh(request):
# Generate key if it does not exist yet # Generate key if it does not exist yet
if can_generate and action == 'generate': if can_generate and action == 'generate':
# Create directory if it does not exist # Create directory if it does not exist
key_dir = os.path.dirname(key_path) key_dir = os.path.dirname(RSA_KEY_FILE)
if not os.path.exists(key_dir): if not os.path.exists(key_dir):
os.makedirs(key_dir) os.makedirs(key_dir)
...@@ -209,7 +209,7 @@ def ssh(request): ...@@ -209,7 +209,7 @@ def ssh(request):
'-N', '', '-N', '',
'-C', 'Weblate', '-C', 'Weblate',
'-t', 'rsa', '-t', 'rsa',
'-f', key_path[:-4] '-f', RSA_KEY_FILE[:-4]
], ],
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
) )
...@@ -221,8 +221,8 @@ def ssh(request): ...@@ -221,8 +221,8 @@ def ssh(request):
) )
# Read key data if it exists # Read key data if it exists
if os.path.exists(key_path): if os.path.exists(RSA_KEY_FILE):
key_data = file(key_path).read() key_data = file(RSA_KEY_FILE).read()
key_type, key_fingerprint, key_id = key_data.strip().split(None, 2) key_type, key_fingerprint, key_id = key_data.strip().split(None, 2)
key = { key = {
'key': key_data, 'key': key_data,
......
...@@ -19,7 +19,12 @@ ...@@ -19,7 +19,12 @@
# #
from trans.tests.views import ViewTestCase from trans.tests.views import ViewTestCase
import trans.admin_views
from django.test import TestCase
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from trans.tests.util import get_test_file
TEST_HOSTS = get_test_file('known_hosts')
class AdminTest(ViewTestCase): class AdminTest(ViewTestCase):
...@@ -47,3 +52,10 @@ class AdminTest(ViewTestCase): ...@@ -47,3 +52,10 @@ class AdminTest(ViewTestCase):
def test_report(self): def test_report(self):
response = self.client.get(reverse('admin-report')) response = self.client.get(reverse('admin-report'))
self.assertContains(response, 'On branch master') self.assertContains(response, 'On branch master')
class SSHKeysTest(TestCase):
def test_parse(self):
trans.admin_views.KNOWN_HOSTS_FILE = TEST_HOSTS
hosts = trans.admin_views.get_host_keys()
self.assertEquals(len(hosts), 50)
This diff is collapsed.
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 to comment