Commit 6da29f4e by Bach Dániel

dashboard: fix tests

parent 6a9390f9
...@@ -26,7 +26,8 @@ from django.contrib.auth import authenticate ...@@ -26,7 +26,8 @@ from django.contrib.auth import authenticate
from dashboard.views import VmAddInterfaceView from dashboard.views import VmAddInterfaceView
from vm.models import Instance, InstanceTemplate, Lease, Node, Trait from vm.models import Instance, InstanceTemplate, Lease, Node, Trait
from vm.operations import WakeUpOperation, AddInterfaceOperation from vm.operations import (WakeUpOperation, AddInterfaceOperation,
AddPortOperation)
from ..models import Profile from ..models import Profile
from firewall.models import Vlan, Host, VlanGroup from firewall.models import Vlan, Host, VlanGroup
from mock import Mock, patch from mock import Mock, patch
...@@ -335,51 +336,48 @@ class VmDetailTest(LoginMixin, TestCase): ...@@ -335,51 +336,48 @@ class VmDetailTest(LoginMixin, TestCase):
self.login(c, "user2") self.login(c, "user2")
inst = Instance.objects.get(pk=1) inst = Instance.objects.get(pk=1)
inst.set_level(self.u2, 'owner') inst.set_level(self.u2, 'owner')
response = c.post("/dashboard/vm/1/", {'port': True, vlan = Vlan.objects.get(id=1)
'proto': 'tcp', vlan.set_level(self.u2, 'user')
'port': '1337'}) inst.add_interface(user=self.u2, vlan=vlan)
host = Host.objects.get(
interface__in=inst.interface_set.all())
with patch.object(AddPortOperation, 'async') as mock_method:
mock_method.side_effect = inst.add_port
response = c.post("/dashboard/vm/1/op/add_port/", {
'proto': 'tcp', 'host': host.pk, 'port': '1337'})
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
def test_unpermitted_add_port_wo_obj_levels(self): def test_unpermitted_add_port_wo_obj_levels(self):
c = Client() c = Client()
self.login(c, "user2") 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 = Instance.objects.get(pk=1)
inst.set_level(self.u2, 'owner') vlan = Vlan.objects.get(id=1)
vlan.set_level(self.u2, 'user')
inst.add_interface(user=self.u2, vlan=vlan, system=True)
host = Host.objects.get(
interface__in=inst.interface_set.all())
self.u2.user_permissions.add(Permission.objects.get( self.u2.user_permissions.add(Permission.objects.get(
name='Can configure port forwards.')) name='Can configure port forwards.'))
response = c.post("/dashboard/vm/1/", {'proto': 'tcp', with patch.object(AddPortOperation, 'async') as mock_method:
'host_pk': '9999', mock_method.side_effect = inst.add_port
'port': '1337'}) response = c.post("/dashboard/vm/1/op/add_port/", {
'proto': 'tcp', 'host': host.pk, 'port': '1337'})
assert not mock_method.called
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
def test_permitted_add_port_w_unhandled_exception(self): def test_unpermitted_add_port_w_bad_host(self):
c = Client() c = Client()
self.login(c, "user2") self.login(c, "user2")
inst = Instance.objects.get(pk=1) inst = Instance.objects.get(pk=1)
inst.set_level(self.u2, 'owner') inst.set_level(self.u2, 'owner')
vlan = Vlan.objects.get(id=1)
vlan.set_level(self.u2, 'user')
inst.add_interface(user=self.u2, vlan=vlan)
host = Host.objects.get(
interface__in=inst.interface_set.all())
self.u2.user_permissions.add(Permission.objects.get( self.u2.user_permissions.add(Permission.objects.get(
name='Can configure port forwards.')) name='Can configure port forwards.'))
port_count = len(host.list_ports()) with patch.object(AddPortOperation, 'async') as mock_method:
response = c.post("/dashboard/vm/1/", {'proto': 'tcp', mock_method.side_effect = inst.add_port
'host_pk': host.pk, response = c.post("/dashboard/vm/1/op/add_port/", {
'port': 'invalid_port'}) 'proto': 'tcp', 'host': '9999', 'port': '1337'})
self.assertEqual(response.status_code, 302) assert not mock_method.called
self.assertEqual(len(host.list_ports()), port_count) self.assertEqual(response.status_code, 200)
def test_permitted_add_port(self): def test_permitted_add_port(self):
c = Client() c = Client()
...@@ -394,9 +392,11 @@ class VmDetailTest(LoginMixin, TestCase): ...@@ -394,9 +392,11 @@ class VmDetailTest(LoginMixin, TestCase):
self.u2.user_permissions.add(Permission.objects.get( self.u2.user_permissions.add(Permission.objects.get(
name='Can configure port forwards.')) name='Can configure port forwards.'))
port_count = len(host.list_ports()) port_count = len(host.list_ports())
response = c.post("/dashboard/vm/1/", {'proto': 'tcp', with patch.object(AddPortOperation, 'async') as mock_method:
'host_pk': host.pk, mock_method.side_effect = inst.add_port
'port': '1337'}) response = c.post("/dashboard/vm/1/op/add_port/", {
'proto': 'tcp', 'host': host.pk, 'port': '1337'})
assert mock_method.called
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(len(host.list_ports()), port_count + 1) self.assertEqual(len(host.list_ports()), port_count + 1)
......
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