Commit 7739d371 by Szeberényi Imre

Merge branch 'testcases' into 'master'

Testcases-Janos

See merge request !11
parents 7dc0aee0 40e7df8a
......@@ -18,7 +18,7 @@
import re
import logging
import sha
import hashlib
from django.conf import settings
from djangosaml2.backends import Saml2Backend as Saml2BackendBase
......@@ -43,8 +43,6 @@ class Saml2Backend(Saml2BackendBase):
match = match.group()
return '+%04x' % ord(match)
if isinstance(main_attribute, str):
main_attribute = main_attribute.decode('UTF-8')
assert isinstance(main_attribute, str)
attr = re.sub(r'[^\w.@-]', replace, main_attribute)
max_length = settings.SAML_MAIN_ATTRIBUTE_MAX_LENGTH
......
......@@ -202,7 +202,7 @@ class Operation(object):
return self.activity_name
except AttributeError:
try:
return self.name._proxy____args[0] # ewww!
return self.name # ewww!
except AttributeError:
raise ImproperlyConfigured(
"Set Operation.activity_name to an ugettext_nooped "
......
......@@ -34,7 +34,7 @@ class OperationTestCase(TestCase):
with patch.object(Operation, 'check_precond'):
with patch.object(Operation, 'create_activity') as create_act:
try:
op.async(system=True)
op._async(system=True)
except AbortEx:
self.assertTrue(create_act.called)
......
......@@ -45,10 +45,10 @@ def handler500(request):
logger.exception("unhandled exception")
ctx = get_context(request, exception)
try:
resp = render("500.html", ctx,
resp = render(request,"500.html", ctx,
RequestContext(request).flatten())
except:
resp = render("500.html", ctx)
resp = render(request,"500.html", ctx)
resp.status_code = 500
return resp
......@@ -56,6 +56,6 @@ def handler500(request):
def handler403(request, *args, **argv):
cls, exception, traceback = exc_info()
ctx = get_context(request, exception)
resp = render("403.html", ctx)
resp = render(request,"403.html", ctx)
resp.status_code = 403
return resp(request)
return resp
[
{
"pk": 1,
"model": "sites.site",
"fields": {
"domain": "example.com",
"name": "example.com"
}
},
{
"pk": 1,
"model": "vm.lease",
"fields": {
"suspend_interval_seconds": 18000,
......
......@@ -46,8 +46,8 @@ from django.forms.widgets import TextInput, HiddenInput
from django.template.loader import render_to_string
from django.utils.html import escape, format_html
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy
from django.utils.translation import ugettext_lazy as _
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _
from simplesshkey.models import UserKey
from sizefield.widgets import FileSizeWidget
......@@ -62,7 +62,7 @@ from vm.models import (
from .models import Profile, GroupProfile, Message
from .validators import domain_validator, meta_data_validator, user_data_validator
LANGUAGES_WITH_CODE = ((l[0], ugettext_lazy(l[1], " (", l[0], ")"))
LANGUAGES_WITH_CODE = ((l[0], format_lazy("{} ({})",l[1], l[0]))
for l in LANGUAGES)
priority_choices = (
......@@ -1103,7 +1103,7 @@ class VmPortRemoveForm(OperationForm):
class VmPortAddForm(OperationForm):
port = forms.IntegerField(required=True, label=_('Port'),
min_value=1, max_value=65535)
proto = forms.ChoiceField(initial=(('tcp', 'tcp'), ('udp', 'udp')),
proto = forms.ChoiceField(choices=(('tcp', 'tcp'), ('udp', 'udp')),
required=True, label=_('Protocol'))
def __init__(self, *args, **kwargs):
......@@ -1303,7 +1303,7 @@ class TraitForm(forms.ModelForm):
class MyProfileForm(forms.ModelForm):
preferred_language = forms.ChoiceField(
initial=LANGUAGES_WITH_CODE,
choices=LANGUAGES_WITH_CODE,
label=_("Preferred language"),
)
......
......@@ -28,6 +28,7 @@ from django.http import HttpRequest, Http404, QueryDict
from django.utils import baseconv
from ..models import Profile
from vm.models import Node
from ..views import InstanceActivityDetail, InstanceActivity
from ..views import vm_ops, vm_mass_ops, Instance, UnsubscribeFormView
from ..views import AclUpdateView
......@@ -35,11 +36,13 @@ from .. import views
class QuerySet(list):
model = MagicMock()
model = Node
def get(self, *args, **kwargs):
return self.pop()
def all(self):
return self
class ViewUserTestCase(unittest.TestCase):
......@@ -165,14 +168,14 @@ class VmOperationViewTestCase(unittest.TestCase):
inst = MagicMock(spec=Instance)
inst._meta.object_name = "Instance"
inst.migrate = Instance._ops['migrate'](inst)
inst.migrate.async = MagicMock()
inst.migrate._async = MagicMock()
inst.has_level.return_value = True
form_kwargs.return_value = {
'default': 100, 'choices': QuerySet([node])}
go.return_value = inst
assert view.as_view()(request, pk=1234)['location']
assert not msg.error.called
inst.migrate.async.assert_called_once_with(
inst.migrate._async.assert_called_once_with(
to_node=node, live_migration=True, user=request.user)
def test_migrate_failed(self):
......@@ -186,14 +189,14 @@ class VmOperationViewTestCase(unittest.TestCase):
inst = MagicMock(spec=Instance)
inst._meta.object_name = "Instance"
inst.migrate = Instance._ops['migrate'](inst)
inst.migrate.async = MagicMock()
inst.migrate.async.side_effect = Exception
inst.migrate._async = MagicMock()
inst.migrate._async.side_effect = Exception
inst.has_level.return_value = True
form_kwargs.return_value = {
'default': 100, 'choices': QuerySet([node])}
go.return_value = inst
assert view.as_view()(request, pk=1234)['location']
assert inst.migrate.async.called
assert inst.migrate._async.called
assert msg.error.called
def test_migrate_wo_permission(self):
......@@ -206,14 +209,14 @@ class VmOperationViewTestCase(unittest.TestCase):
inst = MagicMock(spec=Instance)
inst._meta.object_name = "Instance"
inst.migrate = Instance._ops['migrate'](inst)
inst.migrate.async = MagicMock()
inst.migrate._async = MagicMock()
inst.has_level.return_value = True
form_kwargs.return_value = {
'default': 100, 'choices': QuerySet([node])}
go.return_value = inst
with self.assertRaises(PermissionDenied):
assert view.as_view()(request, pk=1234)['location']
assert not inst.migrate.async.called
assert not inst.migrate._async.called
def test_migrate_template(self):
"""check if GET dialog's template can be rendered"""
......@@ -239,7 +242,7 @@ class VmOperationViewTestCase(unittest.TestCase):
inst.name = "asd"
inst._meta.object_name = "Instance"
inst.save_as_template = Instance._ops['save_as_template'](inst)
inst.save_as_template.async = MagicMock()
inst.save_as_template._async = MagicMock()
inst.has_level.return_value = True
go.return_value = inst
assert view.as_view()(request, pk=1234)
......@@ -256,7 +259,7 @@ class VmOperationViewTestCase(unittest.TestCase):
inst.name = "asd"
inst._meta.object_name = "Instance"
inst.save_as_template = Instance._ops['save_as_template'](inst)
inst.save_as_template.async = MagicMock()
inst.save_as_template._async = MagicMock()
inst.has_level.return_value = True
go.return_value = inst
assert view.as_view()(request, pk=1234)['location']
......@@ -329,7 +332,7 @@ class VmMassOperationViewTestCase(unittest.TestCase):
inst = MagicMock(spec=Instance)
inst._meta.object_name = "Instance"
inst.migrate = Instance._ops['migrate'](inst)
inst.migrate.async = MagicMock()
inst.migrate._async = MagicMock()
inst.has_level.return_value = True
go.return_value = [inst]
assert view.as_view()(request, pk=1234)['location']
......@@ -408,13 +411,13 @@ class RenewViewTest(unittest.TestCase):
inst._meta.object_name = "Instance"
inst.lease = MagicMock(pk=99)
inst.renew = Instance._ops['renew'](inst)
inst.renew.async = MagicMock()
inst.renew._async = MagicMock()
inst.has_level.return_value = True
go.return_value = inst
assert view.as_view()(request, pk=1234)
assert not msg.error.called
assert inst.renew.async.called_with(user=request.user, lease=None)
assert inst.renew.async.return_value.get.called
assert inst.renew._async.called_with(user=request.user, lease=None)
assert inst.renew._async.return_value.get.called
# success would redirect
def test_renew_by_owner_w_param(self):
......@@ -427,7 +430,7 @@ class RenewViewTest(unittest.TestCase):
inst._meta.object_name = "Instance"
inst.lease = MagicMock(pk=99)
inst.renew = Instance._ops['renew'](inst)
inst.renew.async = MagicMock()
inst.renew._async = MagicMock()
inst.has_level.return_value = True
go.return_value = inst
assert view.as_view()(request, pk=1234)
......@@ -441,7 +444,7 @@ class RenewViewTest(unittest.TestCase):
inst = MagicMock(spec=Instance)
inst._meta.object_name = "Instance"
inst.renew = Instance._ops['renew'](inst)
inst.renew.async = MagicMock()
inst.renew._async = MagicMock()
inst.has_level.return_value = False
go.return_value = inst
self.assertIn('login',
......@@ -455,7 +458,7 @@ class RenewViewTest(unittest.TestCase):
inst = MagicMock(spec=Instance)
inst._meta.object_name = "Instance"
inst.renew = Instance._ops['renew'](inst)
inst.renew.async = MagicMock()
inst.renew._async = MagicMock()
inst.has_level.return_value = False
go.return_value = inst
with self.assertRaises(PermissionDenied):
......@@ -470,7 +473,7 @@ class RenewViewTest(unittest.TestCase):
inst._meta.object_name = "Instance"
inst.lease = MagicMock(pk=99)
inst.renew = Instance._ops['renew'](inst)
inst.renew.async = MagicMock()
inst.renew._async = MagicMock()
inst.has_level.return_value = False
go.return_value = inst
with self.assertRaises(PermissionDenied):
......@@ -481,8 +484,9 @@ class RenewViewTest(unittest.TestCase):
view = vm_ops['renew']
inst = MagicMock(spec=Instance, pk=11)
inst._meta.object_name = "Instance"
inst.lease = MagicMock(pk=99)
inst.renew = Instance._ops['renew'](inst)
inst.renew.async = MagicMock()
inst.renew._async = MagicMock()
key = view.get_token_url(inst, user).split('?')[1].split('=')[1]
request = FakeRequestFactory(GET={'k': key}) # other user!
......@@ -499,8 +503,9 @@ class RenewViewTest(unittest.TestCase):
view = vm_ops['renew']
inst = MagicMock(spec=Instance, pk=11)
inst._meta.object_name = "Instance"
inst.lease = MagicMock(pk=99)
inst.renew = Instance._ops['renew'](inst)
inst.renew.async = MagicMock()
inst.renew._async = MagicMock()
inst.has_level = lambda user, level: user.is_authenticated
key = view.get_token_url(inst, user).split('?')[1].split('=')[1]
request = FakeRequestFactory(GET={'k': key}, authenticated=False)
......@@ -536,7 +541,7 @@ class RenewViewTest(unittest.TestCase):
inst = MagicMock(spec=Instance, pk=11)
inst._meta.object_name = "Instance"
inst.renew = Instance._ops['renew'](inst)
inst.renew.async = MagicMock()
inst.renew._async = MagicMock()
inst.has_level.return_value = False
key = view.get_token_url(inst, user).split('?')[1].split('=')[1]
with patch('dashboard.views.signing.loads') as loader, \
......
......@@ -25,14 +25,14 @@ from django.test.client import Client
from django.contrib.auth.models import User, Group, Permission
from django.contrib.auth import authenticate
from circle.common.tests.celery_mock import MockCeleryMixin
from circle.dashboard.views import VmAddInterfaceView
from circle.vm.models import Instance, InstanceTemplate, Lease, Node, Trait
from circle.vm.operations import (WakeUpOperation, AddInterfaceOperation,
from common.tests.celery_mock import MockCeleryMixin
from dashboard.views import VmAddInterfaceView
from vm.models import Instance, InstanceTemplate, Lease, Node, Trait
from vm.operations import (WakeUpOperation, AddInterfaceOperation,
AddPortOperation, RemoveInterfaceOperation,
DeployOperation, RenameOperation)
from ..models import Profile
from circle.firewall.models import Vlan, Host, VlanGroup
from firewall.models import Vlan, Host, VlanGroup
from unittest.mock import Mock, patch
from simplesshkey.models import UserKey
......@@ -137,7 +137,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
inst.set_level(self.u2, 'owner')
interface_count = inst.interface_set.count()
with patch.object(AddInterfaceOperation, 'async') as _async:
with patch.object(AddInterfaceOperation, '_async') as _async:
_async.side_effect = inst.add_interface.call
with patch.object(VmAddInterfaceView, 'get_form_kwargs',
autospec=True) as get_form_kwargs:
......@@ -156,7 +156,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
vlan = Vlan.objects.get(id=1)
vlan.set_level(self.u1, 'user')
interface_count = inst.interface_set.count()
with patch.object(AddInterfaceOperation, 'async') as mock_method:
with patch.object(AddInterfaceOperation, '_async') as mock_method:
mock_method.side_effect = inst.add_interface
response = c.post("/dashboard/vm/1/op/add_interface/",
{'vlan': 1})
......@@ -174,7 +174,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
inst.save()
iface_count = inst.interface_set.count()
with patch.object(RemoveInterfaceOperation, 'async') as mock_method:
with patch.object(RemoveInterfaceOperation, '_async') as mock_method:
mock_method.side_effect = inst.remove_interface
response = c.post("/dashboard/vm/1/op/remove_interface/",
{'interface': 1})
......@@ -190,7 +190,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
inst.add_interface(vlan=Vlan.objects.get(pk=1), user=self.us)
iface_count = inst.interface_set.count()
with patch.object(RemoveInterfaceOperation, 'async') as mock_method:
with patch.object(RemoveInterfaceOperation, '_async') as mock_method:
mock_method.side_effect = inst.remove_interface
response = c.post("/dashboard/vm/1/op/remove_interface/",
{'interface': 1})
......@@ -222,7 +222,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
self.login(c, 'user1')
InstanceTemplate.objects.get(id=1).set_level(self.u1, 'user')
Vlan.objects.get(id=1).set_level(self.u1, 'user')
with patch.object(DeployOperation, 'async') as _async:
with patch.object(DeployOperation, '_async') as _async:
response = c.post('/dashboard/vm/create/',
{'template': 1,
'system': "bubi",
......@@ -234,7 +234,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
def test_use_permitted_template_superuser(self):
c = Client()
self.login(c, 'superuser')
with patch.object(DeployOperation, 'async') as _async:
with patch.object(DeployOperation, '_async') as _async:
response = c.post('/dashboard/vm/create/',
{'template': 1,
'system': "bubi",
......@@ -257,7 +257,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
tmpl.set_level(self.u1, 'owner')
Vlan.objects.get(id=1).set_level(self.u1, 'user')
kwargs = tmpl.__dict__.copy()
kwargs.update(name='t1', lease=1, disks=1, raw_data='tst1')
kwargs.update(name='t1', lease=1, disks=1, raw_data='tst1',parent_id="")
response = c.post('/dashboard/template/1/', kwargs)
self.assertEqual(response.status_code, 302)
self.assertEqual(InstanceTemplate.objects.get(id=1).raw_data,
......@@ -267,7 +267,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
c = Client()
self.login(c, 'superuser')
kwargs = InstanceTemplate.objects.get(id=1).__dict__.copy()
kwargs.update(name='t2', lease=1, disks=1,
kwargs.update(name='t2', lease=1, disks=1,parent_id="",
raw_data='<devices></devices>')
response = c.post('/dashboard/template/1/', kwargs)
self.assertEqual(response.status_code, 302)
......@@ -312,7 +312,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
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:
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'})
......@@ -329,7 +329,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
interface__in=inst.interface_set.all())
self.u2.user_permissions.add(Permission.objects.get(
name='Can configure port forwards.'))
with patch.object(AddPortOperation, 'async') as mock_method:
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'})
......@@ -343,7 +343,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
inst.set_level(self.u2, 'owner')
self.u2.user_permissions.add(Permission.objects.get(
name='Can configure port forwards.'))
with patch.object(AddPortOperation, 'async') as mock_method:
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': '9999', 'port': '1337'})
......@@ -360,10 +360,11 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
inst.add_interface(user=self.u2, vlan=vlan)
host = Host.objects.get(
interface__in=inst.interface_set.all())
#import pdb; pdb.set_trace()
self.u2.user_permissions.add(Permission.objects.get(
name='Can configure port forwards.'))
port_count = len(host.list_ports())
with patch.object(AddPortOperation, 'async') as mock_method:
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'})
......@@ -437,7 +438,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
def test_unpermitted_set_name(self):
c = Client()
self.login(c, "user2")
with patch.object(RenameOperation, 'async') as mock_method:
with patch.object(RenameOperation, '_async') as mock_method:
inst = Instance.objects.get(pk=1)
mock_method.side_effect = inst.rename
inst.set_level(self.u2, 'user')
......@@ -451,7 +452,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
def test_permitted_set_name(self):
c = Client()
self.login(c, "user2")
with patch.object(RenameOperation, 'async') as mock_method:
with patch.object(RenameOperation, '_async') as mock_method:
inst = Instance.objects.get(pk=1)
mock_method.side_effect = inst.rename
inst.set_level(self.u2, 'owner')
......@@ -465,7 +466,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
c = Client()
self.login(c, "user2")
inst = Instance.objects.get(pk=1)
with patch.object(RenameOperation, 'async') as mock_method:
with patch.object(RenameOperation, '_async') as mock_method:
inst.set_level(self.u2, 'owner')
mock_method.side_effect = inst.rename
response = c.post("/dashboard/vm/1/op/rename/",
......@@ -478,7 +479,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
def test_permitted_wake_up_wrong_state(self):
c = Client()
self.login(c, "user2")
with patch.object(WakeUpOperation, 'async') as mock_method, \
with patch.object(WakeUpOperation, '_async') as mock_method, \
patch.object(Instance.WrongStateError, 'send_message') as wro:
inst = Instance.objects.get(pk=1)
mock_method.side_effect = inst.wake_up
......@@ -494,7 +495,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
c = Client()
self.login(c, "user2")
with patch.object(Instance, 'select_node', return_value=None), \
patch.object(WakeUpOperation, 'async') as new_wake_up, \
patch.object(WakeUpOperation, '_async') as new_wake_up, \
patch.object(Instance.WrongStateError, 'send_message') as wro:
inst = Instance.objects.get(pk=1)
new_wake_up.side_effect = inst.wake_up
......@@ -531,7 +532,7 @@ class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
self.login(c, "superuser")
instance_count = Instance.objects.all().count()
with patch.object(DeployOperation, 'async') as _async:
with patch.object(DeployOperation, '_async') as _async:
response = c.post("/dashboard/vm/create/", {
'name': 'vm',
'amount': 1,
......@@ -627,6 +628,7 @@ class NodeDetailTest(LoginMixin, MockCeleryMixin, TestCase):
def test_200_superuser_node_page(self):
c = Client()
#import pdb; pdb.set_trace()
self.login(c, 'superuser')
response = c.get('/dashboard/node/1/')
self.assertEqual(response.status_code, 200)
......@@ -828,6 +830,7 @@ class GroupCreateTest(LoginMixin, MockCeleryMixin, TestCase):
c = Client()
groupnum = Group.objects.count()
self.login(c, 'user1')
#import pdb; pdb.set_trace()
response = c.post('/dashboard/group/create/', {'name': 'newgroup'})
self.assertEqual(response.status_code, 403)
self.assertEqual(Group.objects.count(), groupnum)
......@@ -1811,6 +1814,7 @@ class LeaseDetailTest(LoginMixin, TestCase):
c = Client()
self.login(c, 'superuser')
leases = Lease.objects.count()
#import pdb; pdb.set_trace()
response = c.post("/dashboard/lease/delete/1/")
self.assertEqual(response.status_code, 400)
self.assertEqual(leases, Lease.objects.count())
......@@ -1900,7 +1904,7 @@ class TwoFactorTest(LoginMixin, TestCase):
def test_straight_to_2fa_as_anonymous(self):
c = Client()
response = c.get("/two-factor-login/", follow=True)
self.assertItemsEqual(
self.assertCountEqual(
response.redirect_chain,
[('/', 302),
('/dashboard/', 302),
......@@ -1911,7 +1915,7 @@ class TwoFactorTest(LoginMixin, TestCase):
c = Client()
self.login(c, 'user2')
response = c.get("/two-factor-login/", follow=True)
self.assertItemsEqual(
self.assertCountEqual(
response.redirect_chain,
[('/', 302),
('/dashboard/', 302)]
......
......@@ -485,7 +485,7 @@ class Vlan(AclBase, models.Model):
mask = int(IPAddress(tpl % {"a": 1, "b": 1, "c": 1, "d": 1}))
prefixlen = 128
while mask % 2 == 0:
mask /= 2
mask //= 2
prefixlen -= 1
return (tpl, prefixlen)
......
......@@ -35,7 +35,7 @@ from django.core.exceptions import PermissionDenied, SuspiciousOperation
from django.urls import reverse
from django.db.models import Q
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _, ugettext_noop
from django.utils.translation import ugettext as _, ugettext_noop
from re import search
from sizefield.utils import filesizeformat
......
......@@ -241,8 +241,7 @@ class InstanceActivityTestCase(TestCase):
original_create = InstanceActivity.create
mocked_create = types.MethodType(original_create.__func__,
mock_instance_activity_cls,
original_create.__self__.__class__)
mock_instance_activity_cls)
try:
mocked_create('test', instance, readable_name="test",
concurrency_check=False)
......
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