Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.3 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 *
44

    
45

    
46
class AaiClient(Client):
47

    
48
    def request(self, **request):
49
        request['HTTP_X_AUTH_TOKEN'] = '0000'
50
        return super(AaiClient, self).request(**request)
51

    
52
class TestRestViews(TransactionTestCase):
53

    
54
    fixtures = ['users']
55

    
56
    def setUp(self):
57
        settings.SKIP_SSH_VALIDATION = True
58
        self.client = AaiClient()
59
        self.user = 'test'
60
        self.keys_url = reverse('keys_collection')
61

    
62
    def test_keys_collection_get(self):
63
        resp = self.client.get(self.keys_url)
64
        self.assertEqual(resp.content, "[]")
65

    
66
        PublicKeyPair.objects.create(user=self.user, name="key pair 1",
67
                content="content1")
68

    
69
        resp = self.client.get(self.keys_url)
70
        resp_list = json.loads(resp.content);
71
        exp_list = [{"content": "content1", "id": 1,
72
                    "uri": self.keys_url + "/1", "name": "key pair 1",
73
                    "fingerprint": "unknown fingerprint"}]
74
        self.assertEqual(resp_list, exp_list)
75

    
76
        PublicKeyPair.objects.create(user=self.user, name="key pair 2",
77
                content="content2")
78

    
79
        resp = self.client.get(self.keys_url)
80
        resp_list = json.loads(resp.content)
81
        exp_list = [{"content": "content1", "id": 1,
82
                     "uri": self.keys_url + "/1", "name": "key pair 1",
83
                     "fingerprint": "unknown fingerprint"},
84
                    {"content": "content2", "id": 2,
85
                     "uri": self.keys_url + "/2",
86
                     "name": "key pair 2",
87
                     "fingerprint": "unknown fingerprint"}]
88

    
89
        self.assertEqual(resp_list, exp_list)
90

    
91
    def test_keys_resourse_get(self):
92
        resp = self.client.get(self.keys_url + "/1")
93
        self.assertEqual(resp.status_code, 404)
94

    
95
        # create a public key
96
        PublicKeyPair.objects.create(user=self.user, name="key pair 1",
97
                content="content1")
98
        resp = self.client.get(self.keys_url + "/1")
99
        resp_dict = json.loads(resp.content);
100
        exp_dict = {"content": "content1", "id": 1,
101
                    "uri": self.keys_url + "/1", "name": "key pair 1",
102
                    "fingerprint": "unknown fingerprint"}
103
        self.assertEqual(resp_dict, exp_dict)
104

    
105
        # update
106
        resp = self.client.put(self.keys_url + "/1",
107
                               json.dumps({'name':'key pair 1 new name'}),
108
                               content_type='application/json')
109

    
110
        pk = PublicKeyPair.objects.get(pk=1)
111
        self.assertEqual(pk.name, "key pair 1 new name")
112

    
113
        # delete
114
        resp = self.client.delete(self.keys_url + "/1")
115
        self.assertEqual(PublicKeyPair.objects.count(), 0)
116

    
117
        resp = self.client.get(self.keys_url + "/1")
118
        self.assertEqual(resp.status_code, 404)
119

    
120
        resp = self.client.get(self.keys_url)
121
        self.assertEqual(resp.content, "[]")
122

    
123
        # test rest create
124
        resp = self.client.post(self.keys_url,
125
                                json.dumps({'name':'key pair 2',
126
                                            'content':"""key 2 content"""}),
127
                                content_type='application/json')
128
        self.assertEqual(PublicKeyPair.objects.count(), 1)
129
        pk = PublicKeyPair.objects.get(pk=1)
130
        self.assertEqual(pk.name, "key pair 2")
131
        self.assertEqual(pk.content, "key 2 content")
132

    
133
    def test_generate_views(self):
134
        import base64
135

    
136
        # just test that
137
        resp = self.client.post(self.keys_url + "/generate")
138
        self.assertNotEqual(resp, "")
139

    
140
        data = json.loads(resp.content)
141
        self.assertEqual(data.has_key('private'), True)
142
        self.assertEqual(data.has_key('private'), True)
143

    
144
        # public key is base64 encoded
145
        base64.b64decode(data['public'].replace("ssh-rsa ",""))
146

    
147
        # remove header/footer
148
        private = "".join(data['private'].split("\n")[1:-1])
149

    
150
        # private key is base64 encoded
151
        base64.b64decode(private)
152

    
153
        new_key = PublicKeyPair()
154
        new_key.content = data['public']
155
        new_key.name = "new key"
156
        new_key.user = 'test'
157
        new_key.full_clean()
158
        new_key.save()
159

    
160
    def test_invalid_data(self):
161
        resp = self.client.post(self.keys_url,
162
                                json.dumps({'content':"""key 2 content"""}),
163
                                content_type='application/json')
164

    
165
        self.assertEqual(resp.status_code, 500)
166
        self.assertEqual(resp.content, """{"non_field_key": "__all__", "errors": """
167
                                       """{"name": ["This field cannot be blank."]}}""")
168

    
169
        settings.USERDATA_MAX_SSH_KEYS_PER_USER = 2
170

    
171
        # test ssh limit
172
        resp = self.client.post(self.keys_url,
173
                                json.dumps({'name':'key1',
174
                                            'content':"""key 1 content"""}),
175
                                content_type='application/json')
176
        resp = self.client.post(self.keys_url,
177
                                json.dumps({'name':'key1',
178
                                            'content':"""key 1 content"""}),
179
                                content_type='application/json')
180
        resp = self.client.post(self.keys_url,
181
                                json.dumps({'name':'key1',
182
                                            'content':"""key 1 content"""}),
183
                                content_type='application/json')
184
        self.assertEqual(resp.status_code, 500)
185
        self.assertEqual(resp.content, """{"non_field_key": "__all__", "errors": """
186
                                       """{"__all__": ["SSH keys limit exceeded."]}}""")
187