Commit 931208aa authored by Kostas Papadimitriou's avatar Kostas Papadimitriou
Browse files

Helpdesk tests update

- Include suspend/release suspension tests
- Mock astakos displayname/uuid resolving methods
- Update fixtures with user uuids
- Test vmid lookup
parent e410eb6b
......@@ -3,7 +3,7 @@
"model": "db.VirtualMachine",
"pk": 1001,
"fields": {
"userid": "testuser@test.com",
"userid": "5edcb5aa-1111-4146-a8ed-2b6287824353",
"name": "user1 vm",
"created": "2011-02-06 00:00:00",
"updated": "2011-02-06 00:00:00",
......@@ -17,7 +17,7 @@
"model": "db.VirtualMachine",
"pk": 1002,
"fields": {
"userid": "testuser2@test.com",
"userid": "5edcb5aa-2222-4146-a8ed-2b6287824353",
"name": "user2 vm1",
"created": "2011-02-10 00:00:00",
"updated": "2011-02-10 00:00:00",
......@@ -31,7 +31,7 @@
"model": "db.VirtualMachine",
"pk": 1003,
"fields": {
"userid": "testuser2@test.com",
"userid": "5edcb5aa-2222-4146-a8ed-2b6287824353",
"name": "user2 vm2",
"created": "2011-02-10 00:00:00",
"updated": "2011-02-10 00:00:00",
......@@ -75,6 +75,7 @@
"model": "db.NetworkInterface",
"pk": 2,
"fields": {
"index": 0,
"ipv4": "195.251.222.211",
"network": 2,
"machine": 1002
......@@ -90,7 +91,7 @@
"state": "ACTIVE",
"public": 0,
"link": 1,
"userid": "testuser@test.com"
"userid": "5edcb5aa-1111-4146-a8ed-2b6287824353"
}
},
{
......
......@@ -36,15 +36,25 @@ from django.test import TestCase, Client
from django.conf import settings
from django.core.urlresolvers import reverse
from synnefo.db import models
class AaiClient(Client):
USER1 = "5edcb5aa-1111-4146-a8ed-2b6287824353"
USER2 = "5edcb5aa-2222-4146-a8ed-2b6287824353"
USERS_UUIDS = {}
USERS_UUIDS[USER1] = {'displayname': 'testuser@test.com'}
USERS_UUIDS[USER2] = {'displayname': 'testuser2@test.com'}
USERS_DISPLAYNAMES = dict(map(lambda k: (k[1]['displayname'], {'uuid': k[0]}),
USERS_UUIDS.iteritems()))
class AuthClient(Client):
def request(self, **request):
token = request.pop('user_token', '0000')
if token:
request['HTTP_X_AUTH_TOKEN'] = token
return super(AaiClient, self).request(**request)
return super(AuthClient, self).request(**request)
class HelpdeskTests(TestCase):
"""
......@@ -58,20 +68,34 @@ class HelpdeskTests(TestCase):
def get_user_mock(request, *args, **kwargs):
if request.META.get('HTTP_X_AUTH_TOKEN', None) == '0000':
request.user_uniq = 'test'
request.user = {'uniq': 'test'}
request.user = {'uniq': 'test', 'auth_token': '0000'}
if request.META.get('HTTP_X_AUTH_TOKEN', None) == '0001':
request.user_uniq = 'test'
request.user = {'uniq': 'test', 'groups':['default','helpdesk']}
request.user = {'uniq': 'test', 'groups': ['default',
'helpdesk'],
'auth_token': '0001'}
def get_uuid_mock(token, displayname, url):
try:
return USERS_DISPLAYNAMES.get(displayname)['uuid']
except TypeError:
return None
def get_displayname_mock(token, uuid, url):
try:
return USERS_UUIDS.get(uuid)['displayname']
except TypeError:
return None
# mock the astakos authentication function
from synnefo.lib import astakos
astakos.get_user = get_user_mock
astakos.get_displayname = get_displayname_mock
astakos.get_user_uuid = get_uuid_mock
settings.SKIP_SSH_VALIDATION = True
settings.HELPDESK_ENABLED = True
self.client = AaiClient()
self.user = 'test'
self.keys_url = reverse('ui_keys_collection')
self.client = AuthClient()
def test_enabled_setting(self):
settings.HELPDESK_ENABLED = False
......@@ -79,106 +103,142 @@ class HelpdeskTests(TestCase):
# helpdesk is disabled
r = self.client.get(reverse('helpdesk-index'), user_token="0001")
self.assertEqual(r.status_code, 404)
r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']),
user_token="0001")
r = self.client.get(reverse('helpdesk-details',
args=['testuser@test.com']),
user_token="0001")
self.assertEqual(r.status_code, 404)
def test_ip_lookup(self):
# ip does not exist, proper message gets displayed
r = self.client.get(reverse('helpdesk-details',
args=["195.251.221.122"]), user_token='0001')
args=["195.251.221.122"]), user_token='0001')
self.assertContains(r, 'User with IP')
# ip exists, 'test' account discovered
r = self.client.get(reverse('helpdesk-details',
args=["195.251.222.211"]), user_token='0001')
self.assertEqual(r.context['account'], 'testuser2@test.com')
args=["195.251.222.211"]), user_token='0001')
self.assertEqual(r.context['account'], USER2)
def test_view_permissions(self):
def test_vm_lookup(self):
# vm id does not exist
r = self.client.get(reverse('helpdesk-details',
args=["vm-123"]), user_token='0001')
self.assertContains(r, 'User with Virtual Machine')
# vm exists, 'test' account discovered
r = self.client.get(reverse('helpdesk-details',
args=["vm1001"]), user_token='0001')
self.assertEqual(r.context['account'], USER1)
r = self.client.get(reverse('helpdesk-details',
args=["vm1002"]), user_token='0001')
self.assertEqual(r.context['account'], USER2)
# dash also works
r = self.client.get(reverse('helpdesk-details',
args=["vm-1002"]), user_token='0001')
self.assertEqual(r.context['account'], USER2)
def test_view_permissions(self):
# anonymous user gets 403
r = self.client.get(reverse('helpdesk-index'), user_token=None)
self.assertEqual(r.status_code, 403)
r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']),
user_token=None)
r = self.client.get(reverse('helpdesk-details',
args=['testuser@test.com']),
user_token=None)
self.assertEqual(r.status_code, 403)
# user not in helpdesk group gets 403
r = self.client.get(reverse('helpdesk-index'))
self.assertEqual(r.status_code, 403)
r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']))
r = self.client.get(reverse('helpdesk-details',
args=['testuser@test.com']))
self.assertEqual(r.status_code, 403)
# user with helpdesk group gets 200
r = self.client.get(reverse('helpdesk-index'), user_token="0001")
self.assertEqual(r.status_code, 200)
r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']),
user_token="0001")
r = self.client.get(reverse('helpdesk-details',
args=['testuser@test.com']),
user_token="0001")
self.assertEqual(r.status_code, 200)
r = self.client.post(reverse('helpdesk-suspend-vm', args=(1001,)))
r = self.client.get(reverse('helpdesk-suspend-vm', args=(1001,)),
user_token="0001", data={'token': '1234'})
self.assertEqual(r.status_code, 403)
r = self.client.get(reverse('helpdesk-suspend-vm', args=(1001,)),
user_token="0001")
self.assertEqual(r.status_code, 403)
r = self.client.post(reverse('helpdesk-suspend-vm', args=(1001,)),
user_token="0001", data={'token': '0001'})
self.assertEqual(r.status_code, 302)
r = self.client.post(reverse('helpdesk-suspend-vm', args=(1001,)),
user_token="0000", data={'token': '0000'})
self.assertEqual(r.status_code, 403)
def test_suspend_vm(self):
r = self.client.get(reverse('helpdesk-details',
args=['testuser@test.com']),
user_token="0001")
self.assertEqual(r.status_code, 200)
vmid = r.context['vms'][0].pk
r = self.client.post(reverse('helpdesk-suspend-vm', args=(vmid,)),
data={'token': '0001'}, user_token="0001")
self.assertEqual(r.status_code, 302)
r = self.client.get(reverse('helpdesk-details',
args=['testuser@test.com']),
user_token="0001")
self.assertTrue(r.context['vms'][0].suspended)
r = self.client.post(reverse('helpdesk-suspend-vm-release',
args=(vmid,)), data={'token': '0001'},
user_token="0001")
self.assertEqual(r.status_code, 302)
r = self.client.get(reverse('helpdesk-details',
args=['testuser@test.com']),
user_token="0001")
self.assertFalse(r.context['vms'][0].suspended)
def test_results_get_filtered(self):
"""
Test that view context data are filtered based on userid provided.
Check helpdesk_test.json to see the existing database data.
"""
# 'testuser@test.com' details, see helpdes/fixtures/helpdesk_test.json for
# more details
r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']),
user_token="0001")
# 'testuser@test.com' details, see
# helpdes/fixtures/helpdesk_test.json for more details
r = self.client.get(reverse('helpdesk-details',
args=['testuser@test.com']),
user_token="0001")
account = r.context['account']
vms = r.context['vms']
nets = r.context['networks']
self.assertEqual(account, "testuser@test.com")
self.assertEqual(account, USER1)
self.assertEqual(vms[0].name, "user1 vm")
self.assertEqual(vms.count(), 1)
self.assertEqual(len(nets), 2)
self.assertEqual(len(nets), 1)
self.assertEqual(r.context['account_exists'], True)
# 'testuser2@test.com' details, see helpdes/fixtures/helpdesk_test.json for
# more details
r = self.client.get(reverse('helpdesk-details', args=['testuser2@test.com']),
user_token="0001")
# 'testuser2@test.com' details, see helpdesk
# /fixtures/helpdesk_test.json for more details
r = self.client.get(reverse('helpdesk-details',
args=['testuser2@test.com']),
user_token="0001")
account = r.context['account']
vms = r.context['vms']
nets = r.context['networks']
self.assertEqual(account, "testuser2@test.com")
self.assertEqual(vms[0].name, "user2 vm1")
self.assertEqual(vms[1].name, "user2 vm2")
self.assertEqual(account, USER2)
self.assertEqual(vms[0].name, "user2 vm2")
self.assertEqual(vms[1].name, "user2 vm1")
self.assertEqual(vms.count(), 2)
self.assertEqual(len(nets), 1)
self.assertEqual(len(nets), 0)
self.assertEqual(r.context['account_exists'], True)
# 'testuser5@test.com' does not exist, should be redirected to helpdesk home
r = self.client.get(reverse('helpdesk-details', args=['testuser5@test.com']),
user_token="0001")
# 'testuser5@test.com' does not exist, should be redirected to
# helpdesk home
r = self.client.get(reverse('helpdesk-details',
args=['testuser5@test.com']),
user_token="0001")
vms = r.context['vms']
self.assertEqual(r.context['account_exists'], False)
self.assertEqual(vms.count(), 0)
# 1 public network
self.assertEqual(len(nets), 1)
self.assertEqual(r.context['account_exists'], False)
def test_json_users(self):
# invalid prefix gives 404 response
r = self.client.get(reverse('helpdesk-userslist') + "?prefix=test",
user_token="0001")
self.assertEqual(r.status_code, 404)
# no users exist
r = self.client.get(reverse('helpdesk-userslist') + "?prefix=test@",
user_token="0001")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.content, "[]")
# 1 user exists
r = self.client.get(reverse('helpdesk-userslist') + "?prefix=testuser@",
user_token="0001")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.content, '["testuser@test.com"]')
r = self.client.get(reverse('helpdesk-userslist') + "?prefix=testuser@t",
user_token="0001")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.content, '["testuser@test.com"]')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment