Revision a9d34f3b

b/snf-cyclades-app/synnefo/userdata/tests.py
38 38
from django.test.client import Client
39 39
from django.utils import simplejson as json
40 40
from django.core.urlresolvers import reverse
41
from django.core.validators import MaxLengthValidator
42

  
43
from snf_django.utils.testing import with_settings
44

  
41 45
from mock import patch
42 46

  
43
from synnefo.userdata.models import PublicKeyPair
47
from synnefo.userdata.models import PublicKeyPair, SSH_KEY_MAX_CONTENT_LENGTH
44 48

  
45 49

  
46 50
def get_user_mock(request, *args, **kwargs):
......
88 92
        self.user = 'test'
89 93
        self.keys_url = reverse('ui_keys_collection')
90 94

  
95
    def test_key_content_length_limit(self):
96
        # patch validator
97
        PublicKeyPair._meta.fields[3].validators = [MaxLengthValidator(10)]
98
        resp = self.client.post(self.keys_url,
99
                                json.dumps({'name': 'key pair 2',
100
                                            'content': """0123456789+"""}),
101
                                content_type='application/json')
102
        self.assertEqual(resp.status_code, 422)
103
        resp = json.loads(resp.content)
104
        assert 'errors' in resp
105
        assert 'content' in resp['errors']
106
        self.assertEqual(PublicKeyPair.objects.count(), 0)
107
        PublicKeyPair._meta.fields[3].validators = \
108
            [MaxLengthValidator(SSH_KEY_MAX_CONTENT_LENGTH)]
109

  
91 110
    def test_keys_collection_get(self):
92 111
        resp = self.client.get(self.keys_url)
93 112
        self.assertEqual(resp.content, "[]")
......
201 220
                                json.dumps({'content': """key 2 content"""}),
202 221
                                content_type='application/json')
203 222

  
204
        self.assertEqual(resp.status_code, 500)
223
        self.assertEqual(resp.status_code, 422)
205 224
        self.assertEqual(resp.content,
206 225
                         """{"non_field_key": "__all__", "errors": """
207 226
                         """{"name": ["This field cannot be blank."]}}""")
......
221 240
                                json.dumps({'name': 'key1',
222 241
                                            'content': """key 1 content"""}),
223 242
                                content_type='application/json')
224
        self.assertEqual(resp.status_code, 500)
243
        self.assertEqual(resp.status_code, 422)
225 244
        self.assertEqual(resp.content,
226 245
                         """{"non_field_key": "__all__", "errors": """
227 246
                         """{"__all__": ["SSH keys limit exceeded."]}}""")

Also available in: Unified diff