Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / helpdesk / tests.py @ 9fd36718

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 TestCase, Client
36
from django.conf import settings
37
from django.core.urlresolvers import reverse
38

    
39
from synnefo.db import models
40

    
41
class AaiClient(Client):
42

    
43
    def request(self, **request):
44
        token = request.pop('user_token', '0000')
45
        if token:
46
            request['HTTP_X_AUTH_TOKEN'] = token
47
        return super(AaiClient, self).request(**request)
48

    
49
class HelpdeskTests(TestCase):
50
    """
51
    Helpdesk tests. Test correctness of permissions and returned data.
52
    """
53

    
54
    fixtures = ['helpdesk_test']
55

    
56
    def setUp(self):
57

    
58
        def get_user_mock(request, *args, **kwargs):
59
            if request.META.get('HTTP_X_AUTH_TOKEN', None) == '0000':
60
                request.user_uniq = 'test'
61
                request.user = {'uniq': 'test'}
62
            if request.META.get('HTTP_X_AUTH_TOKEN', None) == '0001':
63
                request.user_uniq = 'test'
64
                request.user = {'uniq': 'test', 'groups':['default','helpdesk']}
65

    
66
        # mock the astakos authentication function
67
        from synnefo.lib import astakos
68
        astakos.get_user = get_user_mock
69

    
70
        settings.SKIP_SSH_VALIDATION = True
71
        settings.HELPDESK_ENABLED = True
72
        self.client = AaiClient()
73
        self.user = 'test'
74
        self.keys_url = reverse('ui_keys_collection')
75

    
76
    def test_enabled_setting(self):
77
        settings.HELPDESK_ENABLED = False
78

    
79
        # helpdesk is disabled
80
        r = self.client.get(reverse('helpdesk-index'), user_token="0001")
81
        self.assertEqual(r.status_code, 404)
82
        r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']),
83
                user_token="0001")
84
        self.assertEqual(r.status_code, 404)
85

    
86
    def test_ip_lookup(self):
87
        # ip does not exist, proper message gets displayed
88
        r = self.client.get(reverse('helpdesk-details',
89
            args=["195.251.221.122"]), user_token='0001')
90
        self.assertContains(r, 'User with IP')
91

    
92
        # ip exists, 'test' account discovered
93
        r = self.client.get(reverse('helpdesk-details',
94
            args=["195.251.222.211"]), user_token='0001')
95
        self.assertEqual(r.context['account'], 'testuser2@test.com')
96

    
97
    def test_view_permissions(self):
98

    
99
        # anonymous user gets 403
100
        r = self.client.get(reverse('helpdesk-index'), user_token=None)
101
        self.assertEqual(r.status_code, 403)
102
        r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']),
103
                user_token=None)
104
        self.assertEqual(r.status_code, 403)
105

    
106
        # user not in helpdesk group gets 403
107
        r = self.client.get(reverse('helpdesk-index'))
108
        self.assertEqual(r.status_code, 403)
109
        r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']))
110
        self.assertEqual(r.status_code, 403)
111

    
112
        # user with helpdesk group gets 200
113
        r = self.client.get(reverse('helpdesk-index'), user_token="0001")
114
        self.assertEqual(r.status_code, 200)
115
        r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']),
116
                user_token="0001")
117
        self.assertEqual(r.status_code, 200)
118

    
119
    def test_results_get_filtered(self):
120
        """
121
        Test that view context data are filtered based on userid provided.
122
        Check helpdesk_test.json to see the existing database data.
123
        """
124

    
125
        # 'testuser@test.com' details, see helpdes/fixtures/helpdesk_test.json for
126
        # more details
127
        r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']),
128
                user_token="0001")
129
        account = r.context['account']
130
        vms = r.context['vms']
131
        nets = r.context['networks']
132
        self.assertEqual(account, "testuser@test.com")
133
        self.assertEqual(vms[0].name, "user1 vm")
134
        self.assertEqual(vms.count(), 1)
135
        self.assertEqual(len(nets), 2)
136
        self.assertEqual(r.context['account_exists'], True)
137

    
138

    
139
        # 'testuser2@test.com' details, see helpdes/fixtures/helpdesk_test.json for
140
        # more details
141
        r = self.client.get(reverse('helpdesk-details', args=['testuser2@test.com']),
142
                user_token="0001")
143
        account = r.context['account']
144
        vms = r.context['vms']
145
        nets = r.context['networks']
146
        self.assertEqual(account, "testuser2@test.com")
147
        self.assertEqual(vms[0].name, "user2 vm1")
148
        self.assertEqual(vms[1].name, "user2 vm2")
149
        self.assertEqual(vms.count(), 2)
150
        self.assertEqual(len(nets), 1)
151
        self.assertEqual(r.context['account_exists'], True)
152

    
153
        # 'testuser5@test.com' does not exist, should be redirected to helpdesk home
154
        r = self.client.get(reverse('helpdesk-details', args=['testuser5@test.com']),
155
                user_token="0001")
156
        vms = r.context['vms']
157
        self.assertEqual(r.context['account_exists'], False)
158
        self.assertEqual(vms.count(), 0)
159
        # 1 public network
160
        self.assertEqual(len(nets), 1)
161
        self.assertEqual(r.context['account_exists'], False)
162

    
163
    def test_json_users(self):
164
        # invalid prefix gives 404 response
165
        r = self.client.get(reverse('helpdesk-userslist') + "?prefix=test",
166
                user_token="0001")
167
        self.assertEqual(r.status_code, 404)
168

    
169
        # no users exist
170
        r = self.client.get(reverse('helpdesk-userslist') + "?prefix=test@",
171
                user_token="0001")
172
        self.assertEqual(r.status_code, 200)
173
        self.assertEqual(r.content, "[]")
174

    
175
        # 1 user exists
176
        r = self.client.get(reverse('helpdesk-userslist') + "?prefix=testuser@",
177
                user_token="0001")
178
        self.assertEqual(r.status_code, 200)
179
        self.assertEqual(r.content, '["testuser@test.com"]')
180
        r = self.client.get(reverse('helpdesk-userslist') + "?prefix=testuser@t",
181
                user_token="0001")
182
        self.assertEqual(r.status_code, 200)
183
        self.assertEqual(r.content, '["testuser@test.com"]')
184