Revision 13397b78 ui/userdata/tests.py
b/ui/userdata/tests.py | ||
---|---|---|
38 | 38 |
from django.core.urlresolvers import clear_url_caches |
39 | 39 |
from django.utils import simplejson as json |
40 | 40 |
from django.conf import settings |
41 |
from django.core.urlresolvers import reverse |
|
41 | 42 |
|
42 | 43 |
from synnefo.ui.userdata.models import User |
43 | 44 |
from synnefo.ui.userdata.models import * |
... | ... | |
54 | 55 |
fixtures = ['users'] |
55 | 56 |
|
56 | 57 |
def setUp(self): |
57 |
settings.ROOT_URLCONF = 'synnefo.ui.userdata.urls' |
|
58 | 58 |
settings.SKIP_SSH_VALIDATION = True |
59 |
clear_url_caches() |
|
60 | 59 |
self.client = AaiClient() |
61 | 60 |
self.user = User.objects.get(pk=1) |
61 |
self.keys_url = reverse('keys_collection') |
|
62 | 62 |
|
63 | 63 |
def test_keys_collection_get(self): |
64 |
resp = self.client.get("/keys")
|
|
64 |
resp = self.client.get(self.keys_url)
|
|
65 | 65 |
self.assertEqual(resp.content, "[]") |
66 | 66 |
|
67 | 67 |
PublicKeyPair.objects.create(user=self.user, name="key pair 1", |
68 | 68 |
content="content1") |
69 | 69 |
|
70 |
resp = self.client.get("/keys") |
|
71 |
self.assertEqual(resp.content, """[{"content": "content1", "id": 1, "uri": "/keys/1", "name": "key pair 1", "fingerprint": ""}]""") |
|
70 |
resp = self.client.get(self.keys_url) |
|
71 |
resp_list = json.loads(resp.content); |
|
72 |
exp_list = [{"content": "content1", "id": 1, |
|
73 |
"uri": self.keys_url + "/1", "name": "key pair 1", |
|
74 |
"fingerprint": "unknown fingerprint"}] |
|
75 |
self.assertEqual(resp_list, exp_list) |
|
72 | 76 |
|
73 | 77 |
PublicKeyPair.objects.create(user=self.user, name="key pair 2", |
74 | 78 |
content="content2") |
75 | 79 |
|
76 |
resp = self.client.get("/keys") |
|
77 |
self.assertEqual(resp.content, """[{"content": "content1", "id": 1, "uri": "/keys/1", "name": "key pair 1", "fingerprint": ""}, {"content": "content2", "id": 2, "uri": "/keys/2", "name": "key pair 2", "fingerprint": ""}]""") |
|
80 |
resp = self.client.get(self.keys_url) |
|
81 |
resp_list = json.loads(resp.content) |
|
82 |
exp_list = [{"content": "content1", "id": 1, |
|
83 |
"uri": self.keys_url + "/1", "name": "key pair 1", |
|
84 |
"fingerprint": "unknown fingerprint"}, |
|
85 |
{"content": "content2", "id": 2, |
|
86 |
"uri": self.keys_url + "/2", |
|
87 |
"name": "key pair 2", |
|
88 |
"fingerprint": "unknown fingerprint"}] |
|
89 |
|
|
90 |
self.assertEqual(resp_list, exp_list) |
|
78 | 91 |
|
79 | 92 |
def test_keys_resourse_get(self): |
80 |
resp = self.client.get("/keys/1")
|
|
93 |
resp = self.client.get(self.keys_url + "/1")
|
|
81 | 94 |
self.assertEqual(resp.status_code, 404) |
82 | 95 |
|
83 | 96 |
# create a public key |
84 | 97 |
PublicKeyPair.objects.create(user=self.user, name="key pair 1", |
85 | 98 |
content="content1") |
86 |
resp = self.client.get("/keys/1") |
|
87 |
self.assertEqual(resp.content, """{"content": "content1", "id": 1, "uri": "/keys/1", "name": "key pair 1", "fingerprint": ""}""") |
|
99 |
resp = self.client.get(self.keys_url + "/1") |
|
100 |
resp_dict = json.loads(resp.content); |
|
101 |
exp_dict = {"content": "content1", "id": 1, |
|
102 |
"uri": self.keys_url + "/1", "name": "key pair 1", |
|
103 |
"fingerprint": "unknown fingerprint"} |
|
104 |
self.assertEqual(resp_dict, exp_dict) |
|
88 | 105 |
|
89 | 106 |
# update |
90 |
resp = self.client.put("/keys/1", json.dumps({'name':'key pair 1 new name'}), |
|
91 |
content_type='application/json') |
|
107 |
resp = self.client.put(self.keys_url + "/1", |
|
108 |
json.dumps({'name':'key pair 1 new name'}), |
|
109 |
content_type='application/json') |
|
110 |
|
|
92 | 111 |
pk = PublicKeyPair.objects.get(pk=1) |
93 | 112 |
self.assertEqual(pk.name, "key pair 1 new name") |
94 | 113 |
|
95 | 114 |
# delete |
96 |
resp = self.client.delete("/keys/1")
|
|
115 |
resp = self.client.delete(self.keys_url + "/1")
|
|
97 | 116 |
self.assertEqual(PublicKeyPair.objects.count(), 0) |
98 | 117 |
|
99 |
resp = self.client.get("/keys/1")
|
|
118 |
resp = self.client.get(self.keys_url + "/1")
|
|
100 | 119 |
self.assertEqual(resp.status_code, 404) |
101 | 120 |
|
102 |
resp = self.client.get("/keys")
|
|
121 |
resp = self.client.get(self.keys_url)
|
|
103 | 122 |
self.assertEqual(resp.content, "[]") |
104 | 123 |
|
105 | 124 |
# test rest create |
106 |
resp = self.client.post("/keys", json.dumps({'name':'key pair 2', |
|
107 |
'content':"""key 2 content"""}), |
|
108 |
content_type='application/json') |
|
125 |
resp = self.client.post(self.keys_url, |
|
126 |
json.dumps({'name':'key pair 2', |
|
127 |
'content':"""key 2 content"""}), |
|
128 |
content_type='application/json') |
|
109 | 129 |
self.assertEqual(PublicKeyPair.objects.count(), 1) |
110 | 130 |
pk = PublicKeyPair.objects.get(pk=1) |
111 | 131 |
self.assertEqual(pk.name, "key pair 2") |
... | ... | |
115 | 135 |
import base64 |
116 | 136 |
|
117 | 137 |
# just test that |
118 |
resp = self.client.post("/keys/generate")
|
|
138 |
resp = self.client.post(self.keys_url + "/generate")
|
|
119 | 139 |
self.assertNotEqual(resp, "") |
120 | 140 |
|
121 | 141 |
data = json.loads(resp.content) |
... | ... | |
139 | 159 |
new_key.save() |
140 | 160 |
|
141 | 161 |
def test_invalid_data(self): |
142 |
resp = self.client.post("/keys", json.dumps({'content':"""key 2 content"""}), |
|
143 |
content_type='application/json') |
|
162 |
resp = self.client.post(self.keys_url, |
|
163 |
json.dumps({'content':"""key 2 content"""}), |
|
164 |
content_type='application/json') |
|
144 | 165 |
|
145 | 166 |
self.assertEqual(resp.status_code, 500) |
146 | 167 |
self.assertEqual(resp.content, """{"non_field_key": "__all__", "errors": """ |
... | ... | |
149 | 170 |
settings.USERDATA_MAX_SSH_KEYS_PER_USER = 2 |
150 | 171 |
|
151 | 172 |
# test ssh limit |
152 |
resp = self.client.post("/keys", json.dumps({'name':'key1', 'content':"""key 1 content"""}), |
|
153 |
content_type='application/json') |
|
154 |
resp = self.client.post("/keys", json.dumps({'name':'key1', 'content':"""key 1 content"""}), |
|
155 |
content_type='application/json') |
|
156 |
resp = self.client.post("/keys", json.dumps({'name':'key1', 'content':"""key 1 content"""}), |
|
157 |
content_type='application/json') |
|
173 |
resp = self.client.post(self.keys_url, |
|
174 |
json.dumps({'name':'key1', |
|
175 |
'content':"""key 1 content"""}), |
|
176 |
content_type='application/json') |
|
177 |
resp = self.client.post(self.keys_url, |
|
178 |
json.dumps({'name':'key1', |
|
179 |
'content':"""key 1 content"""}), |
|
180 |
content_type='application/json') |
|
181 |
resp = self.client.post(self.keys_url, |
|
182 |
json.dumps({'name':'key1', |
|
183 |
'content':"""key 1 content"""}), |
|
184 |
content_type='application/json') |
|
158 | 185 |
self.assertEqual(resp.status_code, 500) |
159 | 186 |
self.assertEqual(resp.content, """{"non_field_key": "__all__", "errors": """ |
160 | 187 |
"""{"__all__": ["SSH keys limit exceeded."]}}""") |
188 |
|
Also available in: Unified diff