Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / userdata / tests.py @ 5facd191

History | View | Annotate | Download (7.3 kB)

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