Revision d3e9d04b snf-cyclades-app/synnefo/userdata/tests.py

b/snf-cyclades-app/synnefo/userdata/tests.py
36 36
from django.test import TransactionTestCase
37 37
from django.conf import settings
38 38
from django.test.client import Client
39
from django.core.urlresolvers import clear_url_caches
40 39
from django.utils import simplejson as json
41
from django.conf import settings
42 40
from django.core.urlresolvers import reverse
43 41
from mock import patch
44 42

  
45
from synnefo.userdata.models import *
43
from synnefo.userdata.models import PublicKeyPair
46 44

  
47 45

  
48 46
def get_user_mock(request, *args, **kwargs):
......
82 80
        self.assertEqual(resp.content, "[]")
83 81

  
84 82
        PublicKeyPair.objects.create(user=self.user, name="key pair 1",
85
                content="content1")
83
                                     content="content1")
86 84

  
87 85
        resp = self.client.get(self.keys_url)
88
        resp_list = json.loads(resp.content);
86
        resp_list = json.loads(resp.content)
89 87
        exp_list = [{"content": "content1", "id": 1,
90
                    "uri": self.keys_url + "/1", "name": "key pair 1",
91
                    "fingerprint": "unknown fingerprint"}]
88
                     "uri": self.keys_url + "/1", "name": "key pair 1",
89
                     "fingerprint": "unknown fingerprint"}]
92 90
        self.assertEqual(resp_list, exp_list)
93 91

  
94 92
        PublicKeyPair.objects.create(user=self.user, name="key pair 2",
95
                content="content2")
93
                                     content="content2")
96 94

  
97 95
        resp = self.client.get(self.keys_url)
98 96
        resp_list = json.loads(resp.content)
......
112 110

  
113 111
        # create a public key
114 112
        PublicKeyPair.objects.create(user=self.user, name="key pair 1",
115
                content="content1")
113
                                     content="content1")
116 114
        resp = self.client.get(self.keys_url + "/1")
117
        resp_dict = json.loads(resp.content);
115
        resp_dict = json.loads(resp.content)
118 116
        exp_dict = {"content": "content1", "id": 1,
119 117
                    "uri": self.keys_url + "/1", "name": "key pair 1",
120 118
                    "fingerprint": "unknown fingerprint"}
......
122 120

  
123 121
        # update
124 122
        resp = self.client.put(self.keys_url + "/1",
125
                               json.dumps({'name':'key pair 1 new name'}),
123
                               json.dumps({'name': 'key pair 1 new name'}),
126 124
                               content_type='application/json')
127 125

  
128 126
        pk = PublicKeyPair.objects.get(pk=1)
......
140 138

  
141 139
        # test rest create
142 140
        resp = self.client.post(self.keys_url,
143
                                json.dumps({'name':'key pair 2',
144
                                            'content':"""key 2 content"""}),
141
                                json.dumps({'name': 'key pair 2',
142
                                            'content': """key 2 content"""}),
145 143
                                content_type='application/json')
146 144
        self.assertEqual(PublicKeyPair.objects.count(), 1)
147 145
        pk = PublicKeyPair.objects.get()
......
156 154
        self.assertNotEqual(resp, "")
157 155

  
158 156
        data = json.loads(resp.content)
159
        self.assertEqual(data.has_key('private'), True)
160
        self.assertEqual(data.has_key('private'), True)
157
        self.assertEqual('private' in data, True)
158
        self.assertEqual('private' in data, True)
161 159

  
162 160
        # public key is base64 encoded
163
        base64.b64decode(data['public'].replace("ssh-rsa ",""))
161
        base64.b64decode(data['public'].replace("ssh-rsa ", ""))
164 162

  
165 163
        # remove header/footer
166 164
        private = "".join(data['private'].split("\n")[1:-1])
......
177 175

  
178 176
    def test_generate_limit(self):
179 177
        settings.USERDATA_MAX_SSH_KEYS_PER_USER = 1
180
        resp = self.client.post(self.keys_url,
181
                                json.dumps({'name':'key1',
182
                                            'content':"""key 1 content"""}),
183
                                content_type='application/json')
178
        self.client.post(self.keys_url,
179
                         json.dumps({'name': 'key1',
180
                                     'content': """key 1 content"""}),
181
                         content_type='application/json')
184 182
        genpath = self.keys_url + "/generate"
185 183
        r = self.client.post(genpath)
186 184
        assert isinstance(r, http.HttpResponseServerError)
187 185

  
188 186
    def test_invalid_data(self):
189 187
        resp = self.client.post(self.keys_url,
190
                                json.dumps({'content':"""key 2 content"""}),
188
                                json.dumps({'content': """key 2 content"""}),
191 189
                                content_type='application/json')
192 190

  
193 191
        self.assertEqual(resp.status_code, 500)
194
        self.assertEqual(resp.content, """{"non_field_key": "__all__", "errors": """
195
                                       """{"name": ["This field cannot be blank."]}}""")
192
        self.assertEqual(resp.content,
193
                         """{"non_field_key": "__all__", "errors": """
194
                         """{"name": ["This field cannot be blank."]}}""")
196 195

  
197 196
        settings.USERDATA_MAX_SSH_KEYS_PER_USER = 2
198 197

  
199 198
        # test ssh limit
200 199
        resp = self.client.post(self.keys_url,
201
                                json.dumps({'name':'key1',
202
                                            'content':"""key 1 content"""}),
200
                                json.dumps({'name': 'key1',
201
                                            'content': """key 1 content"""}),
203 202
                                content_type='application/json')
204 203
        resp = self.client.post(self.keys_url,
205
                                json.dumps({'name':'key1',
206
                                            'content':"""key 1 content"""}),
204
                                json.dumps({'name': 'key1',
205
                                            'content': """key 1 content"""}),
207 206
                                content_type='application/json')
208 207
        resp = self.client.post(self.keys_url,
209
                                json.dumps({'name':'key1',
210
                                            'content':"""key 1 content"""}),
208
                                json.dumps({'name': 'key1',
209
                                            'content': """key 1 content"""}),
211 210
                                content_type='application/json')
212 211
        self.assertEqual(resp.status_code, 500)
213
        self.assertEqual(resp.content, """{"non_field_key": "__all__", "errors": """
214
                                       """{"__all__": ["SSH keys limit exceeded."]}}""")
215

  
212
        self.assertEqual(resp.content,
213
                         """{"non_field_key": "__all__", "errors": """
214
                         """{"__all__": ["SSH keys limit exceeded."]}}""")

Also available in: Unified diff