Commit 1dfba7ed authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

cyclades: Fix userdata limit check

Argument passed to PublicKeyPair.user_limit_exceeded() was a dict
rather that a string (user uuid). Django would happily convert it
to a string and pass it through to the database query.

Also fix a minor bug in tests.
parent 39f64949
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
# or implied, of GRNET S.A. # or implied, of GRNET S.A.
# #
from django import http
from django.test import TransactionTestCase from django.test import TransactionTestCase
from django.conf import settings from django.conf import settings
from django.test.client import Client from django.test.client import Client
...@@ -54,10 +55,14 @@ class TestRestViews(TransactionTestCase): ...@@ -54,10 +55,14 @@ class TestRestViews(TransactionTestCase):
fixtures = ['users'] fixtures = ['users']
def setUp(self): def setUp(self):
settings.USERDATA_MAX_SSH_KEYS_PER_USER = 10
def get_user_mock(request, *Args, **kwargs): def get_user_mock(request, *Args, **kwargs):
if request.META.get('HTTP_X_AUTH_TOKEN', None) == '0000': if request.META.get('HTTP_X_AUTH_TOKEN', None) == '0000':
request.user_uniq = 'test' request.user_uniq = 'test'
request.user = {'uniq': 'test'} request.user = {'id': 'id',
'username': 'username',
'uuid': 'test'}
# mock the astakos authentication function # mock the astakos authentication function
from snf_django.lib import astakos from snf_django.lib import astakos
...@@ -135,7 +140,7 @@ class TestRestViews(TransactionTestCase): ...@@ -135,7 +140,7 @@ class TestRestViews(TransactionTestCase):
'content':"""key 2 content"""}), 'content':"""key 2 content"""}),
content_type='application/json') content_type='application/json')
self.assertEqual(PublicKeyPair.objects.count(), 1) self.assertEqual(PublicKeyPair.objects.count(), 1)
pk = PublicKeyPair.objects.get(pk=1) pk = PublicKeyPair.objects.get()
self.assertEqual(pk.name, "key pair 2") self.assertEqual(pk.name, "key pair 2")
self.assertEqual(pk.content, "key 2 content") self.assertEqual(pk.content, "key 2 content")
...@@ -166,6 +171,16 @@ class TestRestViews(TransactionTestCase): ...@@ -166,6 +171,16 @@ class TestRestViews(TransactionTestCase):
new_key.full_clean() new_key.full_clean()
new_key.save() new_key.save()
def test_generate_limit(self):
settings.USERDATA_MAX_SSH_KEYS_PER_USER = 1
resp = self.client.post(self.keys_url,
json.dumps({'name':'key1',
'content':"""key 1 content"""}),
content_type='application/json')
genpath = self.keys_url + "/generate"
r = self.client.post(genpath)
assert isinstance(r, http.HttpResponseServerError)
def test_invalid_data(self): def test_invalid_data(self):
resp = self.client.post(self.keys_url, resp = self.client.post(self.keys_url,
json.dumps({'content':"""key 2 content"""}), json.dumps({'content':"""key 2 content"""}),
......
...@@ -77,8 +77,8 @@ def generate_key_pair(request): ...@@ -77,8 +77,8 @@ def generate_key_pair(request):
if not SUPPORT_GENERATE_KEYS: if not SUPPORT_GENERATE_KEYS:
raise Exception("Application does not support ssh keys generation") raise Exception("Application does not support ssh keys generation")
if PublicKeyPair.user_limit_exceeded(request.user): if PublicKeyPair.user_limit_exceeded(request.user_uniq):
raise http.HttpResponseServerError("SSH keys limit exceeded") return http.HttpResponseServerError("SSH keys limit exceeded")
# generate RSA key # generate RSA key
from Crypto import Random from Crypto import Random
......
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