root / snf-app / synnefo / ui / userdata / tests.py @ 38d247df
History | View | Annotate | Download (7.5 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
#
|
34 |
|
35 |
from django.test import TransactionTestCase |
36 |
from django.conf import settings |
37 |
from django.test.client import Client |
38 |
from django.core.urlresolvers import clear_url_caches |
39 |
from django.utils import simplejson as json |
40 |
from django.conf import settings |
41 |
from django.core.urlresolvers import reverse |
42 |
|
43 |
from synnefo.ui.userdata.models import User |
44 |
from synnefo.ui.userdata.models import * |
45 |
from synnefo.db.models import SynnefoUser |
46 |
|
47 |
class AaiClient(Client): |
48 |
|
49 |
def request(self, **request): |
50 |
request['HTTP_X_AUTH_TOKEN'] = settings.BYPASS_AUTHENTICATION_TOKEN
|
51 |
return super(AaiClient, self).request(**request) |
52 |
|
53 |
class TestRestViews(TransactionTestCase): |
54 |
|
55 |
fixtures = ['users']
|
56 |
|
57 |
def setUp(self): |
58 |
settings.SKIP_SSH_VALIDATION = True
|
59 |
self.client = AaiClient()
|
60 |
self.user = User.objects.get(pk=1) |
61 |
self.keys_url = reverse('keys_collection') |
62 |
|
63 |
def test_keys_collection_get(self): |
64 |
resp = self.client.get(self.keys_url) |
65 |
self.assertEqual(resp.content, "[]") |
66 |
|
67 |
PublicKeyPair.objects.create(user=self.user, name="key pair 1", |
68 |
content="content1")
|
69 |
|
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)
|
76 |
|
77 |
PublicKeyPair.objects.create(user=self.user, name="key pair 2", |
78 |
content="content2")
|
79 |
|
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)
|
91 |
|
92 |
def test_keys_resourse_get(self): |
93 |
resp = self.client.get(self.keys_url + "/1") |
94 |
self.assertEqual(resp.status_code, 404) |
95 |
|
96 |
# create a public key
|
97 |
PublicKeyPair.objects.create(user=self.user, name="key pair 1", |
98 |
content="content1")
|
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)
|
105 |
|
106 |
# update
|
107 |
resp = self.client.put(self.keys_url + "/1", |
108 |
json.dumps({'name':'key pair 1 new name'}), |
109 |
content_type='application/json')
|
110 |
|
111 |
pk = PublicKeyPair.objects.get(pk=1)
|
112 |
self.assertEqual(pk.name, "key pair 1 new name") |
113 |
|
114 |
# delete
|
115 |
resp = self.client.delete(self.keys_url + "/1") |
116 |
self.assertEqual(PublicKeyPair.objects.count(), 0) |
117 |
|
118 |
resp = self.client.get(self.keys_url + "/1") |
119 |
self.assertEqual(resp.status_code, 404) |
120 |
|
121 |
resp = self.client.get(self.keys_url) |
122 |
self.assertEqual(resp.content, "[]") |
123 |
|
124 |
# test rest create
|
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')
|
129 |
self.assertEqual(PublicKeyPair.objects.count(), 1) |
130 |
pk = PublicKeyPair.objects.get(pk=1)
|
131 |
self.assertEqual(pk.name, "key pair 2") |
132 |
self.assertEqual(pk.content, "key 2 content") |
133 |
|
134 |
def test_generate_views(self): |
135 |
import base64 |
136 |
|
137 |
# just test that
|
138 |
resp = self.client.post(self.keys_url + "/generate") |
139 |
self.assertNotEqual(resp, "") |
140 |
|
141 |
data = json.loads(resp.content) |
142 |
self.assertEqual(data.has_key('private'), True) |
143 |
self.assertEqual(data.has_key('private'), True) |
144 |
|
145 |
# public key is base64 encoded
|
146 |
base64.b64decode(data['public'].replace("ssh-rsa ","")) |
147 |
|
148 |
# remove header/footer
|
149 |
private = "".join(data['private'].split("\n")[1:-1]) |
150 |
|
151 |
# private key is base64 encoded
|
152 |
base64.b64decode(private) |
153 |
|
154 |
new_key = PublicKeyPair() |
155 |
new_key.content = data['public']
|
156 |
new_key.name = "new key"
|
157 |
new_key.user = SynnefoUser.objects.all()[0]
|
158 |
new_key.full_clean() |
159 |
new_key.save() |
160 |
|
161 |
def test_invalid_data(self): |
162 |
resp = self.client.post(self.keys_url, |
163 |
json.dumps({'content':"""key 2 content"""}), |
164 |
content_type='application/json')
|
165 |
|
166 |
self.assertEqual(resp.status_code, 500) |
167 |
self.assertEqual(resp.content, """{"non_field_key": "__all__", "errors": """ |
168 |
"""{"name": ["This field cannot be blank."]}}""")
|
169 |
|
170 |
settings.USERDATA_MAX_SSH_KEYS_PER_USER = 2
|
171 |
|
172 |
# test ssh limit
|
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')
|
185 |
self.assertEqual(resp.status_code, 500) |
186 |
self.assertEqual(resp.content, """{"non_field_key": "__all__", "errors": """ |
187 |
"""{"__all__": ["SSH keys limit exceeded."]}}""")
|
188 |
|