Commit ce298394 by Bach Dániel

dashboard: create tests for __add_port()

parent af6220f5
......@@ -1231,6 +1231,15 @@
}
},
{
"pk": 1366,
"model": "auth.permission",
"fields": {
"codename": "config_ports",
"name": "Can configure port forwards.",
"content_type": 28
}
},
{
"pk": 1,
"model": "auth.group",
"fields": {
......
......@@ -2,13 +2,17 @@ from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User, Group
from django.core.exceptions import SuspiciousOperation
from django.contrib.auth.models import Permission
from vm.models import Instance, InstanceTemplate, Lease, Node
from ..models import Profile
from storage.models import Disk
from firewall.models import Vlan
from firewall.models import Vlan, Host, VlanGroup
from mock import Mock
import django.conf
settings = django.conf.settings.FIREWALL_SETTINGS
class LoginMixin(object):
def login(self, client, username, password='password'):
......@@ -35,6 +39,8 @@ class VmDetailTest(LoginMixin, TestCase):
self.g1.user_set.add(self.u1)
self.g1.user_set.add(self.u2)
self.g1.save()
settings["default_vlangroup"] = 'public'
VlanGroup.objects.create(name='public')
def tearDown(self):
super(VmDetailTest, self).tearDown()
......@@ -282,6 +288,58 @@ class VmDetailTest(LoginMixin, TestCase):
self.assertEqual(response.status_code, 200)
assert self.u1.notification_set.get().status == 'read'
def test_unpermitted_add_port_wo_config_ports_perm(self):
c = Client()
self.login(c, "user2")
inst = Instance.objects.get(pk=1)
inst.set_level(self.u2, 'owner')
response = c.post("/dashboard/vm/1/", {'port': True,
'proto': 'tcp',
'port': '1337'})
self.assertEqual(response.status_code, 403)
def test_unpermitted_add_port_wo_obj_levels(self):
c = Client()
self.login(c, "user2")
self.u2.user_permissions.add(Permission.objects.get(
name='Can configure port forwards.'))
response = c.post("/dashboard/vm/1/", {'port': True,
'proto': 'tcp',
'port': '1337'})
self.assertEqual(response.status_code, 403)
def test_unpermitted_add_port_w_bad_host(self):
c = Client()
self.login(c, "user2")
inst = Instance.objects.get(pk=1)
inst.set_level(self.u2, 'owner')
self.u2.user_permissions.add(Permission.objects.get(
name='Can configure port forwards.'))
response = c.post("/dashboard/vm/1/", {'proto': 'tcp',
'host_pk': '9999',
'port': '1337'})
self.assertEqual(response.status_code, 403)
def test_permitted_add_port(self):
c = Client()
self.login(c, "user2")
inst = Instance.objects.get(pk=1)
inst.set_level(self.u2, 'owner')
vlan = Vlan.objects.get(id=1)
vlan.set_level(self.u2, 'user')
response = c.post("/dashboard/vm/1/",
{'new_network_vlan': 1})
host = Host.objects.get(
interface__in=inst.interface_set.all())
self.u2.user_permissions.add(Permission.objects.get(
name='Can configure port forwards.'))
port_count = len(host.list_ports())
response = c.post("/dashboard/vm/1/", {'proto': 'tcp',
'host_pk': host.pk,
'port': '1337'})
self.assertEqual(response.status_code, 302)
self.assertEqual(len(host.list_ports()), port_count + 1)
class VmDetailVncTest(LoginMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
......
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 sign in to comment