test_views.py 3.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User, Group
from mock import Mock

from dashboard.tests.test_views import LoginMixin

from vm.models import Instance
from firewall.models import Vlan, VlanGroup

import django.conf
settings = django.conf.settings.FIREWALL_SETTINGS


class VlanAclTest(LoginMixin, TestCase):
    fixtures = ['test-vm-fixture.json', 'node.json']

    def setUp(self):
        Instance.get_remote_queue_name = Mock(return_value='test')
        self.u1 = User.objects.create(username='user1')
        self.u1.set_password('password')
        self.u1.save()
        self.u2 = User.objects.create(username='user2', is_staff=True)
        self.u2.set_password('password')
        self.u2.save()
        self.us = User.objects.create(username='superuser', is_superuser=True)
        self.us.set_password('password')
        self.us.save()
        self.g1 = Group.objects.create(name='group1')
        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(VlanAclTest, self).tearDown()
        self.u1.delete()
        self.u2.delete()
        self.us.delete()
        self.g1.delete()

    def test_add_new_user_permission(self):
        c = Client()
        self.login(c, "superuser")
        vlan = Vlan.objects.get(vid=1)
        self.assertEqual([], vlan.get_users_with_level())

Bach Dániel committed
66
        resp = c.post("/network/vlans/2/acl/", {
Bach Dániel committed
67 68
            'name': "user1",
            'level': "user",
69 70 71 72 73 74 75 76 77 78 79 80 81 82
        })

        vlan = Vlan.objects.get(vid=1)
        self.assertTrue((self.u1, "user") in vlan.get_users_with_level())
        self.assertEqual(resp.status_code, 302)

    def test_make_user_operator(self):
        c = Client()
        self.login(c, "superuser")
        vlan = Vlan.objects.get(vid=1)

        vlan.set_level(self.u1, "user")
        self.assertTrue((self.u1, "user") in vlan.get_users_with_level())

Bach Dániel committed
83
        resp = c.post("/network/vlans/2/acl/", {
84
            'perm-u-%d' % self.u1.pk: "operator",
Bach Dániel committed
85 86
            'level': "",
            'name': "",
87 88 89 90 91 92 93 94 95 96 97 98 99
        })

        self.assertTrue((self.u1, "operator") in vlan.get_users_with_level())
        self.assertEqual(resp.status_code, 302)

    def test_remove_user_permission(self):
        c = Client()
        self.login(c, "superuser")
        vlan = Vlan.objects.get(vid=1)

        vlan.set_level(self.u1, "user")
        self.assertTrue((self.u1, "user") in vlan.get_users_with_level())

Bach Dániel committed
100
        resp = c.post("/network/vlans/2/acl/", {
101
            'remove-u-%d' % self.u1.pk: "",
Bach Dániel committed
102 103
            'level': "",
            'name': "",
104 105 106 107
        })

        self.assertTrue((self.u1, "user") not in vlan.get_users_with_level())
        self.assertEqual(resp.status_code, 302)