Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / userdata / tests.py @ 0c09b1c0

History | View | Annotate | Download (7.7 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 b47b110d Kostas Papadimitriou
from synnefo.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 fcdd2549 Kostas Papadimitriou
        def get_user_mock(request, *Args, **kwargs):
58 fcdd2549 Kostas Papadimitriou
            if request.META.get('HTTP_X_AUTH_TOKEN', None) == '0000':
59 fcdd2549 Kostas Papadimitriou
                request.user_uniq = 'test'
60 fcdd2549 Kostas Papadimitriou
                request.user = {'uniq': 'test'}
61 fcdd2549 Kostas Papadimitriou
62 fcdd2549 Kostas Papadimitriou
        # mock the astakos authentication function
63 04a1b675 Christos Stavrakakis
        from snf_django.lib import astakos
64 fcdd2549 Kostas Papadimitriou
        astakos.get_user = get_user_mock
65 fcdd2549 Kostas Papadimitriou
66 7469ff7d Kostas Papadimitriou
        settings.SKIP_SSH_VALIDATION = True
67 eee0487e Kostas Papadimitriou
        self.client = AaiClient()
68 244c552b Giorgos Verigakis
        self.user = 'test'
69 d194e869 Kostas Papadimitriou
        self.keys_url = reverse('ui_keys_collection')
70 eee0487e Kostas Papadimitriou
71 eee0487e Kostas Papadimitriou
    def test_keys_collection_get(self):
72 be2c0c51 Kostas Papadimitriou
        resp = self.client.get(self.keys_url)
73 eee0487e Kostas Papadimitriou
        self.assertEqual(resp.content, "[]")
74 eee0487e Kostas Papadimitriou
75 eee0487e Kostas Papadimitriou
        PublicKeyPair.objects.create(user=self.user, name="key pair 1",
76 eee0487e Kostas Papadimitriou
                content="content1")
77 eee0487e Kostas Papadimitriou
78 be2c0c51 Kostas Papadimitriou
        resp = self.client.get(self.keys_url)
79 be2c0c51 Kostas Papadimitriou
        resp_list = json.loads(resp.content);
80 be2c0c51 Kostas Papadimitriou
        exp_list = [{"content": "content1", "id": 1,
81 be2c0c51 Kostas Papadimitriou
                    "uri": self.keys_url + "/1", "name": "key pair 1",
82 be2c0c51 Kostas Papadimitriou
                    "fingerprint": "unknown fingerprint"}]
83 be2c0c51 Kostas Papadimitriou
        self.assertEqual(resp_list, exp_list)
84 eee0487e Kostas Papadimitriou
85 eee0487e Kostas Papadimitriou
        PublicKeyPair.objects.create(user=self.user, name="key pair 2",
86 eee0487e Kostas Papadimitriou
                content="content2")
87 eee0487e Kostas Papadimitriou
88 be2c0c51 Kostas Papadimitriou
        resp = self.client.get(self.keys_url)
89 be2c0c51 Kostas Papadimitriou
        resp_list = json.loads(resp.content)
90 be2c0c51 Kostas Papadimitriou
        exp_list = [{"content": "content1", "id": 1,
91 be2c0c51 Kostas Papadimitriou
                     "uri": self.keys_url + "/1", "name": "key pair 1",
92 be2c0c51 Kostas Papadimitriou
                     "fingerprint": "unknown fingerprint"},
93 be2c0c51 Kostas Papadimitriou
                    {"content": "content2", "id": 2,
94 be2c0c51 Kostas Papadimitriou
                     "uri": self.keys_url + "/2",
95 be2c0c51 Kostas Papadimitriou
                     "name": "key pair 2",
96 be2c0c51 Kostas Papadimitriou
                     "fingerprint": "unknown fingerprint"}]
97 be2c0c51 Kostas Papadimitriou
98 be2c0c51 Kostas Papadimitriou
        self.assertEqual(resp_list, exp_list)
99 eee0487e Kostas Papadimitriou
100 eee0487e Kostas Papadimitriou
    def test_keys_resourse_get(self):
101 be2c0c51 Kostas Papadimitriou
        resp = self.client.get(self.keys_url + "/1")
102 6ab78e7c Kostas Papadimitriou
        self.assertEqual(resp.status_code, 404)
103 6ab78e7c Kostas Papadimitriou
104 6ab78e7c Kostas Papadimitriou
        # create a public key
105 6ab78e7c Kostas Papadimitriou
        PublicKeyPair.objects.create(user=self.user, name="key pair 1",
106 6ab78e7c Kostas Papadimitriou
                content="content1")
107 be2c0c51 Kostas Papadimitriou
        resp = self.client.get(self.keys_url + "/1")
108 be2c0c51 Kostas Papadimitriou
        resp_dict = json.loads(resp.content);
109 be2c0c51 Kostas Papadimitriou
        exp_dict = {"content": "content1", "id": 1,
110 be2c0c51 Kostas Papadimitriou
                    "uri": self.keys_url + "/1", "name": "key pair 1",
111 be2c0c51 Kostas Papadimitriou
                    "fingerprint": "unknown fingerprint"}
112 be2c0c51 Kostas Papadimitriou
        self.assertEqual(resp_dict, exp_dict)
113 6ab78e7c Kostas Papadimitriou
114 6ab78e7c Kostas Papadimitriou
        # update
115 be2c0c51 Kostas Papadimitriou
        resp = self.client.put(self.keys_url + "/1",
116 be2c0c51 Kostas Papadimitriou
                               json.dumps({'name':'key pair 1 new name'}),
117 be2c0c51 Kostas Papadimitriou
                               content_type='application/json')
118 be2c0c51 Kostas Papadimitriou
119 6ab78e7c Kostas Papadimitriou
        pk = PublicKeyPair.objects.get(pk=1)
120 6ab78e7c Kostas Papadimitriou
        self.assertEqual(pk.name, "key pair 1 new name")
121 6ab78e7c Kostas Papadimitriou
122 6ab78e7c Kostas Papadimitriou
        # delete
123 be2c0c51 Kostas Papadimitriou
        resp = self.client.delete(self.keys_url + "/1")
124 6ab78e7c Kostas Papadimitriou
        self.assertEqual(PublicKeyPair.objects.count(), 0)
125 6ab78e7c Kostas Papadimitriou
126 be2c0c51 Kostas Papadimitriou
        resp = self.client.get(self.keys_url + "/1")
127 6ab78e7c Kostas Papadimitriou
        self.assertEqual(resp.status_code, 404)
128 6ab78e7c Kostas Papadimitriou
129 be2c0c51 Kostas Papadimitriou
        resp = self.client.get(self.keys_url)
130 6ab78e7c Kostas Papadimitriou
        self.assertEqual(resp.content, "[]")
131 9829e3b6 Kostas Papadimitriou
132 9829e3b6 Kostas Papadimitriou
        # test rest create
133 be2c0c51 Kostas Papadimitriou
        resp = self.client.post(self.keys_url,
134 be2c0c51 Kostas Papadimitriou
                                json.dumps({'name':'key pair 2',
135 be2c0c51 Kostas Papadimitriou
                                            'content':"""key 2 content"""}),
136 be2c0c51 Kostas Papadimitriou
                                content_type='application/json')
137 9829e3b6 Kostas Papadimitriou
        self.assertEqual(PublicKeyPair.objects.count(), 1)
138 9829e3b6 Kostas Papadimitriou
        pk = PublicKeyPair.objects.get(pk=1)
139 9829e3b6 Kostas Papadimitriou
        self.assertEqual(pk.name, "key pair 2")
140 9829e3b6 Kostas Papadimitriou
        self.assertEqual(pk.content, "key 2 content")
141 9829e3b6 Kostas Papadimitriou
142 3548024c Kostas Papadimitriou
    def test_generate_views(self):
143 3548024c Kostas Papadimitriou
        import base64
144 3548024c Kostas Papadimitriou
145 3548024c Kostas Papadimitriou
        # just test that
146 be2c0c51 Kostas Papadimitriou
        resp = self.client.post(self.keys_url + "/generate")
147 3548024c Kostas Papadimitriou
        self.assertNotEqual(resp, "")
148 3548024c Kostas Papadimitriou
149 3548024c Kostas Papadimitriou
        data = json.loads(resp.content)
150 3548024c Kostas Papadimitriou
        self.assertEqual(data.has_key('private'), True)
151 3548024c Kostas Papadimitriou
        self.assertEqual(data.has_key('private'), True)
152 3548024c Kostas Papadimitriou
153 3548024c Kostas Papadimitriou
        # public key is base64 encoded
154 3548024c Kostas Papadimitriou
        base64.b64decode(data['public'].replace("ssh-rsa ",""))
155 3548024c Kostas Papadimitriou
156 3548024c Kostas Papadimitriou
        # remove header/footer
157 3548024c Kostas Papadimitriou
        private = "".join(data['private'].split("\n")[1:-1])
158 3548024c Kostas Papadimitriou
159 3548024c Kostas Papadimitriou
        # private key is base64 encoded
160 3548024c Kostas Papadimitriou
        base64.b64decode(private)
161 3548024c Kostas Papadimitriou
162 386481eb Kostas Papadimitriou
        new_key = PublicKeyPair()
163 386481eb Kostas Papadimitriou
        new_key.content = data['public']
164 386481eb Kostas Papadimitriou
        new_key.name = "new key"
165 244c552b Giorgos Verigakis
        new_key.user = 'test'
166 386481eb Kostas Papadimitriou
        new_key.full_clean()
167 386481eb Kostas Papadimitriou
        new_key.save()
168 386481eb Kostas Papadimitriou
169 c72a830d Kostas Papadimitriou
    def test_invalid_data(self):
170 be2c0c51 Kostas Papadimitriou
        resp = self.client.post(self.keys_url,
171 be2c0c51 Kostas Papadimitriou
                                json.dumps({'content':"""key 2 content"""}),
172 be2c0c51 Kostas Papadimitriou
                                content_type='application/json')
173 c72a830d Kostas Papadimitriou
174 c72a830d Kostas Papadimitriou
        self.assertEqual(resp.status_code, 500)
175 c72a830d Kostas Papadimitriou
        self.assertEqual(resp.content, """{"non_field_key": "__all__", "errors": """
176 c72a830d Kostas Papadimitriou
                                       """{"name": ["This field cannot be blank."]}}""")
177 c72a830d Kostas Papadimitriou
178 1a97a220 Kostas Papadimitriou
        settings.USERDATA_MAX_SSH_KEYS_PER_USER = 2
179 c72a830d Kostas Papadimitriou
180 c72a830d Kostas Papadimitriou
        # test ssh limit
181 be2c0c51 Kostas Papadimitriou
        resp = self.client.post(self.keys_url,
182 be2c0c51 Kostas Papadimitriou
                                json.dumps({'name':'key1',
183 be2c0c51 Kostas Papadimitriou
                                            'content':"""key 1 content"""}),
184 be2c0c51 Kostas Papadimitriou
                                content_type='application/json')
185 be2c0c51 Kostas Papadimitriou
        resp = self.client.post(self.keys_url,
186 be2c0c51 Kostas Papadimitriou
                                json.dumps({'name':'key1',
187 be2c0c51 Kostas Papadimitriou
                                            'content':"""key 1 content"""}),
188 be2c0c51 Kostas Papadimitriou
                                content_type='application/json')
189 be2c0c51 Kostas Papadimitriou
        resp = self.client.post(self.keys_url,
190 be2c0c51 Kostas Papadimitriou
                                json.dumps({'name':'key1',
191 be2c0c51 Kostas Papadimitriou
                                            'content':"""key 1 content"""}),
192 be2c0c51 Kostas Papadimitriou
                                content_type='application/json')
193 c72a830d Kostas Papadimitriou
        self.assertEqual(resp.status_code, 500)
194 c72a830d Kostas Papadimitriou
        self.assertEqual(resp.content, """{"non_field_key": "__all__", "errors": """
195 c72a830d Kostas Papadimitriou
                                       """{"__all__": ["SSH keys limit exceeded."]}}""")