Commit 741bc86c by Paul Kilgo

implement and test forms

parent cccd8037
......@@ -27,22 +27,52 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from django import forms
from django_sshkey.models import Key
from django_sshkey.models import Key, UserKey
from django_sshkey.util import pubkey_parse
class UserKeyForm(forms.ModelForm):
name = forms.CharField(max_length=50, required=False)
class ApplicationKeyForm(forms.ModelForm):
key = forms.CharField(max_length=2000, required=True)
def clean(self):
cleaned_data = self.cleaned_data
if 'key' in cleaned_data:
key = cleaned_data['key'] = Key(key=cleaned_data['key'])
key.full_clean()
return cleaned_data
def save(self, commit=True):
instance = super(ApplicationKeyForm, self).save(commit=False)
if commit:
basekey = self.cleaned_data['key']
basekey.save()
instance.basekey = basekey
instance.save()
return instance
class NamedKeyForm(ApplicationKeyForm):
def clean(self):
cleaned_data = super(NamedKeyForm, self).clean()
if 'key' in cleaned_data and not cleaned_data.get('name'):
pubkey = pubkey_parse(cleaned_data['key'].key)
if not pubkey.comment:
raise ValidationError('Name or key comment required')
cleaned_data['name'] = pubkey.comment
return cleaned_data
class UserKeyForm(NamedKeyForm):
class Meta:
model = Key
model = UserKey
fields = ['name', 'key']
exclude = ['basekey']
widgets = {
'name': forms.TextInput(attrs={
'size': 50,
'placeholder': "username@hostname, or leave blank to use key comment",
}),
'key': forms.Textarea(attrs={
'cols': 72,
'rows': 15,
'placeholder': "Paste in the contents of your public key file here",
}),
'name': forms.TextInput(attrs={
'size': 50,
'placeholder': "username@hostname, or leave blank to use key comment",
})
}
......@@ -50,7 +50,10 @@ class Key(models.Model):
db_table = 'sshkey_key'
def __unicode__(self):
return unicode(self.user) + u': ' + self.name
if self.fingerprint:
return self.fingerprint
else:
return self.key[:20] + u'...'
def clean_fields(self, exclude=None):
if not exclude or 'key' not in exclude:
......@@ -133,10 +136,13 @@ class NamedKey(ApplicationKey):
def clean(self):
if not self.name:
try:
pubkey = pubkey_parse(self.key)
if not pubkey.comment:
raise ValidationError('Name or key comment required')
self.name = pubkey.comment
except Key.DoesNotExist:
pass
class UserKey(NamedKey):
user = models.ForeignKey(User, db_index=True)
......
......@@ -31,6 +31,7 @@ from django.test.client import Client
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django_sshkey.forms import UserKeyForm
from django_sshkey.models import Key, ApplicationKey, NamedKey, UserKey
from django_sshkey import settings
import os
......@@ -572,3 +573,43 @@ class KeyLookupTestCase(BaseTestCase):
self.assertIn('Content-Type', response)
self.assertEqual(response['Content-Type'], 'text/plain')
self.assertEqual(response.content, '')
class UserKeyFormTestCase(BaseTestCase):
@classmethod
def setUpClass(cls):
super(UserKeyFormTestCase, cls).setUpClass()
cls.user1 = User.objects.create(username='user1')
cls.key1_path = os.path.join(cls.key_dir, 'key1')
ssh_keygen(comment='comment', file=cls.key1_path)
def test_save_without_name(self):
instance = UserKey(user=self.user1)
post = {
'key': open(self.key1_path + '.pub').read(),
}
form = UserKeyForm(post, instance=instance)
self.assertTrue(form.is_valid(), form.errors)
key = form.save()
self.assertEqual('comment', key.name)
def test_save_with_name(self):
instance = UserKey(user=self.user1)
post = {
'key': open(self.key1_path + '.pub').read(),
'name': 'name',
}
form = UserKeyForm(post, instance=instance)
self.assertTrue(form.is_valid(), form.errors)
key = form.save()
self.assertEqual('name', key.name)
def test_save_blank_name(self):
instance = UserKey(user=self.user1)
post = {
'key': open(self.key1_path + '.pub').read(),
'name': '',
}
form = UserKeyForm(post, instance=instance)
self.assertTrue(form.is_valid(), form.errors)
key = form.save()
self.assertEqual('comment', key.name)
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