Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
django-sshkey
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
741bc86c
authored
Oct 08, 2014
by
Paul Kilgo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement and test forms
parent
cccd8037
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
9 deletions
+86
-9
django_sshkey/forms.py
+38
-8
django_sshkey/models.py
+7
-1
django_sshkey/tests.py
+41
-0
No files found.
django_sshkey/forms.py
View file @
741bc86c
...
...
@@ -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
=
User
Key
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"
,
})
}
django_sshkey/models.py
View file @
741bc86c
...
...
@@ -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
)
...
...
django_sshkey/tests.py
View file @
741bc86c
...
...
@@ -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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment