test_views.py 66.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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/>.

18 19
import json

Bach Dániel committed
20
# from unittest import skip
Őry Máté committed
21 22 23
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User, Group
24
from django.contrib.auth.models import Permission
25
from django.contrib.auth import authenticate
Őry Máté committed
26

Bach Dániel committed
27
from dashboard.views import VmAddInterfaceView
28
from vm.models import Instance, InstanceTemplate, Lease, Node, Trait
Bach Dániel committed
29
from vm.operations import (WakeUpOperation, AddInterfaceOperation,
30
                           AddPortOperation, RemoveInterfaceOperation)
31
from ..models import Profile
32
from firewall.models import Vlan, Host, VlanGroup
33
from mock import Mock, patch
Bach Dániel committed
34
from django_sshkey.models import UserKey
35

36 37 38
import django.conf
settings = django.conf.settings.FIREWALL_SETTINGS

Őry Máté committed
39

40 41 42 43 44 45 46 47
class LoginMixin(object):
    def login(self, client, username, password='password'):
        response = client.post('/accounts/login/', {'username': username,
                                                    'password': password})
        self.assertNotEqual(response.status_code, 403)


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

Őry Máté committed
50
    def setUp(self):
51
        Instance.get_remote_queue_name = Mock(return_value='test')
Őry Máté committed
52
        self.u1 = User.objects.create(username='user1')
53 54
        self.u1.set_password('password')
        self.u1.save()
Őry Máté committed
55
        self.u2 = User.objects.create(username='user2', is_staff=True)
56 57
        self.u2.set_password('password')
        self.u2.save()
Őry Máté committed
58
        self.us = User.objects.create(username='superuser', is_superuser=True)
59
        self.us.set_password('password')
60
        self.us.save()
Őry Máté committed
61 62 63 64
        self.g1 = Group.objects.create(name='group1')
        self.g1.user_set.add(self.u1)
        self.g1.user_set.add(self.u2)
        self.g1.save()
Bach Dániel committed
65 66
        self.u1.user_permissions.add(Permission.objects.get(
            codename='create_vm'))
67 68
        settings["default_vlangroup"] = 'public'
        VlanGroup.objects.create(name='public')
Őry Máté committed
69

70 71 72 73 74 75 76
    def tearDown(self):
        super(VmDetailTest, self).tearDown()
        self.u1.delete()
        self.u2.delete()
        self.us.delete()
        self.g1.delete()

Őry Máté committed
77 78
    def test_404_vm_page(self):
        c = Client()
Bach Dániel committed
79
        self.login(c, 'user1')
Őry Máté committed
80 81
        response = c.get('/dashboard/vm/235555/')
        self.assertEqual(response.status_code, 404)
82

83 84 85
    def test_anon_vm_page(self):
        c = Client()
        response = c.get('/dashboard/vm/1/')
Bach Dániel committed
86 87
        self.assertRedirects(response, '/accounts/login/'
                                       '?next=/dashboard/vm/1/')
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

    def test_unauth_vm_page(self):
        c = Client()
        self.login(c, 'user1')
        response = c.get('/dashboard/vm/1/')
        self.assertEqual(response.status_code, 403)

    def test_operator_vm_page(self):
        c = Client()
        self.login(c, 'user2')
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u2, 'operator')
        response = c.get('/dashboard/vm/1/')
        self.assertEqual(response.status_code, 200)

    def test_user_vm_page(self):
104
        c = Client()
105 106 107
        self.login(c, 'user2')
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u2, 'user')
108 109
        response = c.get('/dashboard/vm/1/')
        self.assertEqual(response.status_code, 200)
110

111 112 113 114 115 116
    def test_unpermitted_password_change(self):
        c = Client()
        self.login(c, "user2")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u1, 'owner')
        password = inst.pw
Bach Dániel committed
117
        response = c.post("/dashboard/vm/1/op/password_reset/")
118
        self.assertEqual(response.status_code, 403)
119
        self.assertEqual(password, Instance.objects.get(pk=1).pw)
120

121 122 123
    def test_unpermitted_network_add_wo_perm(self):
        c = Client()
        self.login(c, "user2")
Bach Dániel committed
124 125
        response = c.post("/dashboard/vm/1/op/add_interface/",
                          {'vlan': 1})
126 127 128
        self.assertEqual(response.status_code, 403)

    def test_unpermitted_network_add_wo_vlan_perm(self):
129 130 131
        c = Client()
        self.login(c, "user2")
        inst = Instance.objects.get(pk=1)
132
        inst.set_level(self.u2, 'owner')
Bach Dániel committed
133 134 135 136 137 138 139 140 141 142 143 144
        interface_count = inst.interface_set.count()

        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:
                get_form_kwargs.return_value = {'choices': Vlan.objects.all()}
                response = c.post("/dashboard/vm/1/op/add_interface/",
                                  {'vlan': 1})
        self.assertEqual(response.status_code, 302)
        assert async.called
        self.assertEqual(inst.interface_set.count(), interface_count)
145 146 147 148 149 150

    def test_permitted_network_add(self):
        c = Client()
        self.login(c, "user1")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u1, 'owner')
Bach Dániel committed
151 152
        vlan = Vlan.objects.get(id=1)
        vlan.set_level(self.u1, 'user')
153
        interface_count = inst.interface_set.count()
Bach Dániel committed
154 155 156 157
        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})
158
        self.assertEqual(response.status_code, 302)
Bach Dániel committed
159
        assert mock_method.called
160
        self.assertEqual(inst.interface_set.count(), interface_count + 1)
161

162 163 164 165 166 167
    def test_permitted_network_delete(self):
        c = Client()
        self.login(c, "user1")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u1, 'owner')
        inst.add_interface(vlan=Vlan.objects.get(pk=1), user=self.us)
Bach Dániel committed
168 169
        inst.status = 'RUNNING'
        inst.save()
170 171

        iface_count = inst.interface_set.count()
Bach Dániel committed
172 173 174 175 176 177
        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})
        self.assertEqual(response.status_code, 302)
        assert mock_method.called
178 179 180 181 182 183 184 185 186 187
        self.assertEqual(inst.interface_set.count(), iface_count - 1)

    def test_unpermitted_network_delete(self):
        c = Client()
        self.login(c, "user1")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u1, 'user')
        inst.add_interface(vlan=Vlan.objects.get(pk=1), user=self.us)
        iface_count = inst.interface_set.count()

Bach Dániel committed
188 189 190 191
        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})
192 193 194
        self.assertEqual(iface_count, inst.interface_set.count())
        self.assertEqual(response.status_code, 403)

195 196
    def test_create_vm_w_unpermitted_network(self):
        c = Client()
Bach Dániel committed
197
        self.login(c, 'user2')
198 199 200 201 202
        response = c.post('/dashboard/vm/create/',
                          {'template': 1,
                           'cpu_priority': 1, 'cpu_count': 1,
                           'ram_size': 1000})
        self.assertEqual(response.status_code, 403)
203 204 205 206 207 208 209

    def test_use_unpermitted_template(self):
        c = Client()
        self.login(c, 'user1')
        Vlan.objects.get(id=1).set_level(self.u1, 'user')
        response = c.post('/dashboard/vm/create/',
                          {'template': 1,
210
                           'system': "bubi",
211 212 213 214 215 216 217 218 219 220 221
                           'cpu_priority': 1, 'cpu_count': 1,
                           'ram_size': 1000})
        self.assertEqual(response.status_code, 403)

    def test_use_permitted_template(self):
        c = Client()
        self.login(c, 'user1')
        InstanceTemplate.objects.get(id=1).set_level(self.u1, 'user')
        Vlan.objects.get(id=1).set_level(self.u1, 'user')
        response = c.post('/dashboard/vm/create/',
                          {'template': 1,
222
                           'system': "bubi",
223 224 225 226 227 228 229 230 231
                           'cpu_priority': 1, 'cpu_count': 1,
                           'ram_size': 1000})
        self.assertEqual(response.status_code, 302)

    def test_use_permitted_template_superuser(self):
        c = Client()
        self.login(c, 'superuser')
        response = c.post('/dashboard/vm/create/',
                          {'template': 1,
232
                           'system': "bubi",
233 234 235 236 237 238 239 240 241 242
                           'cpu_priority': 1, 'cpu_count': 1,
                           'ram_size': 1000})
        self.assertEqual(response.status_code, 302)

    def test_edit_unpermitted_template(self):
        c = Client()
        self.login(c, 'user1')
        InstanceTemplate.objects.get(id=1).set_level(self.u1, 'user')
        response = c.post('/dashboard/template/1/', {})
        self.assertEqual(response.status_code, 403)
243

244 245 246 247 248
    def test_edit_unpermitted_template_raw_data(self):
        c = Client()
        self.login(c, 'user1')
        tmpl = InstanceTemplate.objects.get(id=1)
        tmpl.set_level(self.u1, 'owner')
Kálmán Viktor committed
249
        Vlan.objects.get(id=1).set_level(self.u1, 'user')
250 251 252 253 254 255 256 257 258 259 260
        kwargs = tmpl.__dict__.copy()
        kwargs.update(name='t1', lease=1, disks=1, raw_data='tst1')
        response = c.post('/dashboard/template/1/', kwargs)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(InstanceTemplate.objects.get(id=1).raw_data,
                         tmpl.raw_data)

    def test_edit_permitted_template_raw_data(self):
        c = Client()
        self.login(c, 'superuser')
        kwargs = InstanceTemplate.objects.get(id=1).__dict__.copy()
Kálmán Viktor committed
261 262
        kwargs.update(name='t2', lease=1, disks=1,
                      raw_data='<devices></devices>')
263 264
        response = c.post('/dashboard/template/1/', kwargs)
        self.assertEqual(response.status_code, 302)
Kálmán Viktor committed
265 266
        self.assertEqual(InstanceTemplate.objects.get(id=1).raw_data,
                         "<devices></devices>")
267

268
    def test_permitted_lease_delete_w_template_using_it(self):
269 270 271 272
        c = Client()
        self.login(c, 'superuser')
        leases = Lease.objects.count()
        response = c.post("/dashboard/lease/delete/1/")
273 274 275 276 277 278 279 280 281 282
        self.assertEqual(response.status_code, 400)
        self.assertEqual(leases, Lease.objects.count())

    def test_permitted_lease_delete_w_template_not_using_it(self):
        c = Client()
        self.login(c, 'superuser')
        lease = Lease.objects.create(name="yay")
        leases = Lease.objects.count()

        response = c.post("/dashboard/lease/delete/%d/" % lease.pk)
283 284 285 286 287 288 289 290 291
        self.assertEqual(response.status_code, 302)
        self.assertEqual(leases - 1, Lease.objects.count())

    def test_unpermitted_lease_delete(self):
        c = Client()
        self.login(c, 'user1')
        leases = Lease.objects.count()
        response = c.post("/dashboard/lease/delete/1/")
        # redirect to the login page
292
        self.assertEqual(response.status_code, 403)
293
        self.assertEqual(leases, Lease.objects.count())
294

295 296 297
    def test_notification_read(self):
        c = Client()
        self.login(c, "user1")
298
        self.u1.profile.notify('subj', '%(var)s %(user)s',
299 300 301 302 303
                               {'var': 'testme'})
        assert self.u1.notification_set.get().status == 'new'
        response = c.get("/dashboard/notifications/")
        self.assertEqual(response.status_code, 200)
        assert self.u1.notification_set.get().status == 'read'
304

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    def test_unpermitted_activity_get(self):
        c = Client()
        self.login(c, "user2")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u1, 'owner')

        response = c.get("/dashboard/vm/1/activity/")
        self.assertEqual(response.status_code, 403)

    def test_permitted_activity_get(self):
        c = Client()
        self.login(c, "user1")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u1, 'owner')

        response = c.get("/dashboard/vm/1/activity/")
        self.assertEqual(response.status_code, 200)

323 324 325 326 327
    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')
Bach Dániel committed
328 329 330 331 332 333 334 335 336
        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())
        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'})
337 338 339 340 341 342
        self.assertEqual(response.status_code, 403)

    def test_unpermitted_add_port_wo_obj_levels(self):
        c = Client()
        self.login(c, "user2")
        inst = Instance.objects.get(pk=1)
Bach Dániel committed
343 344 345 346 347
        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())
348 349
        self.u2.user_permissions.add(Permission.objects.get(
            name='Can configure port forwards.'))
Bach Dániel committed
350 351 352 353 354
        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'})
            assert not mock_method.called
355 356
        self.assertEqual(response.status_code, 403)

Bach Dániel committed
357
    def test_unpermitted_add_port_w_bad_host(self):
358 359 360 361 362 363
        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.'))
Bach Dániel committed
364 365 366 367 368 369
        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'})
            assert not mock_method.called
        self.assertEqual(response.status_code, 200)
370

371 372 373 374 375 376 377
    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')
Bach Dániel committed
378
        inst.add_interface(user=self.u2, vlan=vlan)
379 380 381 382 383
        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())
Bach Dániel committed
384 385 386 387 388
        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'})
            assert mock_method.called
389 390 391
        self.assertEqual(response.status_code, 302)
        self.assertEqual(len(host.list_ports()), port_count + 1)

Bach Dániel committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    def test_unpermitted_add_tag(self):
        c = Client()
        self.login(c, "user2")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u2, 'user')
        response = c.post("/dashboard/vm/1/", {'new_tag': 'test1'})
        self.assertEqual(response.status_code, 403)

    def test_permitted_add_tag(self):
        c = Client()
        self.login(c, "user2")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u2, 'owner')
        tag_count = inst.tags.count()
        response = c.post("/dashboard/vm/1/", {'new_tag': 'test2'})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(inst.tags.count(), tag_count + 1)

    def test_permitted_add_tag_w_too_long_or_empty_tag(self):
        c = Client()
        self.login(c, "user2")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u2, 'owner')
        tag_count = inst.tags.count()
        response = c.post("/dashboard/vm/1/", {'new_tag': 't' * 30})
        self.assertEqual(response.status_code, 302)
        response = c.post("/dashboard/vm/1/", {'new_tag': ''})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(inst.tags.count(), tag_count)

    def test_unpermitted_remove_tag(self):
        c = Client()
        self.login(c, "user2")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u2, 'user')
        tag_count = inst.tags.count()
        response = c.post("/dashboard/vm/1/", {'to_remove': 'test1'})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(inst.tags.count(), tag_count)

    def test_permitted_remove_tag(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/", {'new_tag': 'test1'})
        tag_count = inst.tags.count()
        response = c.post("/dashboard/vm/1/", {'to_remove': 'test1'})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(inst.tags.count(), tag_count - 1)

    def test_permitted_remove_tag_w_ajax(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/", {'new_tag': 'test1'})
        tag_count = inst.tags.count()
        response = c.post("/dashboard/vm/1/", {'to_remove': 'test1'},
                          HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(inst.tags.count(), tag_count - 1)

    def test_unpermitted_set_name(self):
        c = Client()
        self.login(c, "user2")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u2, 'user')
        old_name = inst.name
        response = c.post("/dashboard/vm/1/", {'new_name': 'test1235'})
        self.assertEqual(response.status_code, 403)
        self.assertEqual(Instance.objects.get(pk=1).name, old_name)

    def test_permitted_set_name(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/", {'new_name': 'test1234'})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Instance.objects.get(pk=1).name, 'test1234')

    def test_permitted_set_name_w_ajax(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/", {'new_name': 'test123'},
                          HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Instance.objects.get(pk=1).name, 'test123')

484 485 486
    def test_permitted_wake_up_wrong_state(self):
        c = Client()
        self.login(c, "user2")
Őry Máté committed
487 488
        with patch.object(WakeUpOperation, 'async') as mock_method, \
                patch.object(Instance.WrongStateError, 'send_message') as wro:
489 490
            inst = Instance.objects.get(pk=1)
            mock_method.side_effect = inst.wake_up
Guba Sándor committed
491
            inst.status = 'RUNNING'
492
            inst.set_level(self.u2, 'owner')
Őry Máté committed
493
            c.post("/dashboard/vm/1/op/wake_up/")
Őry Máté committed
494 495
            inst = Instance.objects.get(pk=1)
            self.assertEqual(inst.status, 'RUNNING')  # mocked anyway
496
            assert mock_method.called
Őry Máté committed
497
            assert wro.called
498 499 500 501

    def test_permitted_wake_up(self):
        c = Client()
        self.login(c, "user2")
Őry Máté committed
502 503 504 505 506
        with patch.object(Instance, 'select_node', return_value=None), \
                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
507
            inst._wake_up_vm = Mock()
Őry Máté committed
508 509 510 511 512 513
            inst.get_remote_queue_name = Mock(return_value='test')
            inst.status = 'SUSPENDED'
            inst.set_level(self.u2, 'owner')
            with patch('dashboard.views.messages') as msg:
                response = c.post("/dashboard/vm/1/op/wake_up/")
                assert not msg.error.called
514
            assert inst._wake_up_vm.called
Őry Máté committed
515 516 517 518
            self.assertEqual(response.status_code, 302)
            self.assertEqual(inst.status, 'RUNNING')
            assert new_wake_up.called
            assert not wro.called
519 520 521 522 523

    def test_unpermitted_wake_up(self):
        c = Client()
        self.login(c, "user2")
        inst = Instance.objects.get(pk=1)
Guba Sándor committed
524
        inst.status = 'SUSPENDED'
525
        inst.set_level(self.u2, 'user')
526 527
        response = c.post("/dashboard/vm/1/op/wake_up/")
        self.assertEqual(response.status_code, 403)
528

529 530 531 532 533 534
    def test_non_existing_template_get(self):
        c = Client()
        self.login(c, "superuser")
        response = c.get("/dashboard/template/111111/")
        self.assertEqual(response.status_code, 404)

535 536 537 538 539 540 541 542 543 544
    def test_permitted_customized_vm_create(self):
        c = Client()
        self.login(c, "superuser")

        instance_count = Instance.objects.all().count()
        response = c.post("/dashboard/vm/create/", {
            'name': 'vm',
            'amount': 2,
            'customized': 1,
            'template': 1,
Kálmán Viktor committed
545
            'cpu_priority': 10, 'cpu_count': 1, 'ram_size': 128,
546 547 548 549 550 551
            'network': [],
        })

        self.assertEqual(response.status_code, 302)
        self.assertEqual(instance_count + 2, Instance.objects.all().count())

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
    def test_unpermitted_description_update(self):
        c = Client()
        self.login(c, "user1")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u2, 'owner')
        inst.set_level(self.u1, 'user')
        old_desc = inst.description
        response = c.post("/dashboard/vm/1/", {'new_description': 'test1234'})

        self.assertEqual(response.status_code, 403)
        self.assertEqual(Instance.objects.get(pk=1).description, old_desc)

    def test_permitted_description_update_w_ajax(self):
        c = Client()
        self.login(c, "user1")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u1, 'owner')
        response = c.post("/dashboard/vm/1/", {'new_description': "naonyo"},
                          HTTP_X_REQUESTED_WITH='XMLHttpRequest')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.content)['new_description'],
                         "naonyo")
        self.assertEqual(Instance.objects.get(pk=1).description, "naonyo")

    def test_permitted_description_update(self):
        c = Client()
        self.login(c, "user1")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u1, 'owner')
        response = c.post("/dashboard/vm/1/", {'new_description': "naonyo"})

        self.assertEqual(response.status_code, 302)
        self.assertEqual(Instance.objects.get(pk=1).description, "naonyo")

587

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
class NodeDetailTest(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')
        node = Node.objects.get(pk=1)
        trait, created = Trait.objects.get_or_create(name='testtrait')
        node.traits.add(trait)

    def tearDown(self):
        super(NodeDetailTest, self).tearDown()
        self.u1.delete()
        self.u2.delete()
        self.us.delete()
        self.g1.delete()

    def test_404_superuser_node_page(self):
        c = Client()
        self.login(c, 'superuser')
        response = c.get('/dashboard/node/25555/')
        self.assertEqual(response.status_code, 404)

    def test_302_user_node_page(self):
        c = Client()
        self.login(c, 'user1')
        response = c.get('/dashboard/node/25555/')
629
        self.assertEqual(response.status_code, 403)
630 631 632 633

    def test_anon_node_page(self):
        c = Client()
        response = c.get('/dashboard/node/1/')
634
        self.assertEqual(response.status_code, 302)
635 636 637 638 639 640 641 642 643 644 645

    def test_permitted_node_delete(self):
        c = Client()
        self.login(c, 'superuser')
        response = c.post('/dashboard/node/delete/1/')
        self.assertEqual(response.status_code, 302)

    def test_not_permitted_node_delete(self):
        c = Client()
        self.login(c, 'user1')
        response = c.post('/dashboard/node/delete/1/')
646
        self.assertEqual(response.status_code, 302)
647 648 649 650

    def test_anon_node_delete(self):
        c = Client()
        response = c.post('/dashboard/node/delete/1/')
651
        self.assertEqual(response.status_code, 302)
652 653 654 655 656 657 658

    def test_unpermitted_set_name(self):
        c = Client()
        self.login(c, "user2")
        node = Node.objects.get(pk=1)
        old_name = node.name
        response = c.post("/dashboard/node/1/", {'new_name': 'test1235'})
659
        self.assertEqual(response.status_code, 403)
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
        self.assertEqual(Node.objects.get(pk=1).name, old_name)

    def test_permitted_set_name(self):
        c = Client()
        self.login(c, "superuser")
        response = c.post("/dashboard/node/1/", {'new_name': 'test1234'})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Node.objects.get(pk=1).name, 'test1234')

    def test_permitted_set_name_w_ajax(self):
        c = Client()
        self.login(c, "superuser")
        response = c.post("/dashboard/node/1/", {'new_name': 'test123'},
                          HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Node.objects.get(pk=1).name, 'test123')

    def test_unpermitted_add_trait(self):
        c = Client()
        self.login(c, "user2")
        node = Node.objects.get(pk=1)
        trait_count = node.traits.count()
        response = c.post("/dashboard/node/1/add-trait/",
                          {'name': 'test1'})
684
        self.assertEqual(response.status_code, 302)
685 686 687 688 689 690 691 692
        self.assertEqual(len(Node.objects.get(pk=1).traits.all()), trait_count)

    def test_anon_add_trait(self):
        c = Client()
        node = Node.objects.get(pk=1)
        trait_count = node.traits.count()
        response = c.post("/dashboard/node/1/add-trait/",
                          {'name': 'test2'})
693
        self.assertEqual(response.status_code, 302)
694 695 696 697 698 699 700 701 702
        self.assertEqual(len(Node.objects.get(pk=1).traits.all()), trait_count)

    def test_permitted_add_trait(self):
        c = Client()
        self.login(c, "superuser")
        node = Node.objects.get(pk=1)
        trait_count = node.traits.count()
        response = c.post("/dashboard/node/1/add-trait/", {'name': 'test3'})
        self.assertRedirects(response, '/dashboard/node/1/')
703 704
        self.assertEqual(Node.objects.get(pk=1).traits.count(),
                         trait_count + 1)
705 706 707 708 709 710 711 712

    def test_unpermitted_remove_trait(self):
        node = Node.objects.get(pk=1)
        trait_count = node.traits.count()
        traitid = node.traits.get(name='testtrait')
        c = Client()
        self.login(c, "user2")
        response = c.post("/dashboard/node/1/", {'to_remove': traitid})
713
        self.assertEqual(response.status_code, 403)
714
        self.assertEqual(Node.objects.get(pk=1).traits.count(), trait_count)
715 716 717 718 719 720 721 722 723

    def test_permitted_remove_trait(self):
        node = Node.objects.get(pk=1)
        trait_count = node.traits.count()
        traitid = node.traits.get(name='testtrait').pk
        c = Client()
        self.login(c, "superuser")
        response = c.post("/dashboard/node/1/", {'to_remove': traitid})
        self.assertEqual(response.status_code, 302)
724 725
        self.assertEqual(Node.objects.get(pk=1).traits.count(),
                         trait_count - 1)
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

    def test_permitted_remove_trait_w_ajax(self):
        node = Node.objects.get(pk=1)
        trait_count = Node.objects.get(pk=1).traits.count()
        traitid = node.traits.get(name='testtrait').pk
        c = Client()
        self.login(c, "superuser")
        response = c.post("/dashboard/node/1/", {'to_remove': traitid},
                          HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Node.objects.get(pk=1).
                         traits.count(), trait_count - 1)

    def test_add_too_long_name_trait(self):
        c = Client()
        self.login(c, "superuser")
        node = Node.objects.get(pk=1)
        trait_count = node.traits.count()
        s = 'x' * 100
        response = c.post("/dashboard/node/1/add-trait/", {'name': s})
        self.assertEqual(response.status_code, 200)
747 748 749 750 751 752 753 754 755 756 757
        self.assertEqual(Node.objects.get(pk=1).traits.count(), trait_count)

    def test_anon_remove_trait(self):
        c = Client()
        node = Node.objects.get(pk=1)
        trait_count = node.traits.count()
        traitid = node.traits.get(name='testtrait').pk
        response = c.post("/dashboard/node/1/", {'to_remove': traitid})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(len(Node.objects.get(pk=1).traits.all()), trait_count)

758

759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
class GroupCreateTest(LoginMixin, TestCase):
    fixtures = ['test-vm-fixture.json', 'node.json']

    def setUp(self):
        # u0 - user with creating group permissions
        self.u0 = User.objects.create(username='user0')
        self.u0.set_password('password')
        self.u0.save()
        permlist = Permission.objects.all()
        self.u0.user_permissions.add(
            filter(lambda element: 'group' in element.name and
                   'add' in element.name, permlist)[0])
        # u1 simple user without permissions
        self.u1 = User.objects.create(username='user1')
        self.u1.set_password('password')
        self.u1.save()
        self.us = User.objects.create(username='superuser', is_superuser=True)
        self.us.set_password('password')
        self.us.save()
778 779
        self.g1 = Group.objects.create(name='group1')
        self.g1.save()
780 781 782

    def tearDown(self):
        super(GroupCreateTest, self).tearDown()
783
        self.g1.delete()
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
        self.u0.delete()
        self.u1.delete()
        self.us.delete()

    def test_anon_group_page(self):
        c = Client()
        response = c.get('/dashboard/group/create/')
        self.assertEqual(response.status_code, 302)

    def test_superuser_group_page(self):
        c = Client()
        self.login(c, 'superuser')
        response = c.get('/dashboard/group/create/')
        self.assertEqual(response.status_code, 200)

    def test_permitted_group_page(self):
        c = Client()
        self.login(c, 'user0')
        response = c.get('/dashboard/group/create/')
        self.assertEqual(response.status_code, 200)

    def test_unpermitted_group_page(self):
        c = Client()
        self.login(c, 'user1')
        response = c.get('/dashboard/group/create/')
        self.assertEqual(response.status_code, 403)

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    def test_anon_group_create(self):
        c = Client()
        groupnum = Group.objects.count()
        response = c.post('/dashboard/group/create/', {'name': 'newgroup'})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Group.objects.count(), groupnum)

    def test_unpermitted_group_create(self):
        c = Client()
        groupnum = Group.objects.count()
        self.login(c, 'user1')
        response = c.post('/dashboard/group/create/', {'name': 'newgroup'})
        self.assertEqual(response.status_code, 403)
        self.assertEqual(Group.objects.count(), groupnum)

    def test_permitted_group_create(self):
        c = Client()
        groupnum = Group.objects.count()
        self.login(c, 'user0')
        response = c.post('/dashboard/group/create/', {'name': 'newgroup'})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Group.objects.count(), groupnum + 1)

    def test_superuser_group_create(self):
        c = Client()
        groupnum = Group.objects.count()
        self.login(c, 'superuser')
        response = c.post('/dashboard/group/create/', {'name': 'newgroup'})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Group.objects.count(), groupnum + 1)

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
    def test_namecollision_group_create(self):
        # hint: group1 is in setUp, the tests checks creating group with the
        # same name
        c = Client()
        groupnum = Group.objects.count()
        self.login(c, 'superuser')
        response = c.post('/dashboard/group/create/', {'name': 'group1'})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Group.objects.count(), groupnum)

    def test_creator_is_owner_when_group_create(self):
        # has owner rights in the group the user who created the group?
        c = Client()
        self.login(c, 'user0')
        c.post('/dashboard/group/create/', {'name': 'newgroup'})
        newgroup = Group.objects.get(name='newgroup')
        self.assertTrue(newgroup.profile.has_level(self.u0, 'owner'))

860

861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
class GroupDeleteTest(LoginMixin, TestCase):
    fixtures = ['test-vm-fixture.json', 'node.json']

    def setUp(self):
        # u0 - user with creating group permissions
        self.u0 = User.objects.create(username='user0')
        self.u0.set_password('password')
        self.u0.save()
        permlist = Permission.objects.all()
        self.u0.user_permissions.add(
            filter(lambda element: 'group' in element.name and
                   'delete' in element.name, permlist)[0])
        # u1 simple user without permissions
        self.u1 = User.objects.create(username='user1')
        self.u1.set_password('password')
        self.u1.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.profile.set_user_level(self.u0, 'owner')
        self.g1.save()

    def tearDown(self):
        super(GroupDeleteTest, self).tearDown()
        self.g1.delete()
        self.u0.delete()
        self.u1.delete()
        self.us.delete()

    def test_anon_group_page(self):
        c = Client()
        response = c.get('/dashboard/group/delete/' + str(self.g1.pk) + '/')
        self.assertEqual(response.status_code, 302)

    def test_superuser_group_page(self):
        c = Client()
        self.login(c, 'superuser')
        response = c.get('/dashboard/group/delete/' + str(self.g1.pk) + '/')
        self.assertEqual(response.status_code, 200)

    def test_permitted_group_page(self):
        c = Client()
        self.login(c, 'user0')
Bach Dániel committed
905 906 907
        with patch('dashboard.views.util.messages') as msg:
            response = c.get('/dashboard/group/delete/%d/' % self.g1.pk)
            assert not msg.error.called and not msg.warning.called
908 909 910 911 912
        self.assertEqual(response.status_code, 200)

    def test_unpermitted_group_page(self):
        c = Client()
        self.login(c, 'user1')
Bach Dániel committed
913 914 915
        with patch('dashboard.views.util.messages') as msg:
            response = c.get('/dashboard/group/delete/%d/' % self.g1.pk)
            assert msg.error.called or msg.warning.called
Bach Dániel committed
916
        self.assertEqual(response.status_code, 302)
917 918 919

    def test_anon_group_delete(self):
        c = Client()
Bach Dániel committed
920 921 922 923
        response = c.get('/dashboard/group/delete/%d/' % self.g1.pk)
        self.assertRedirects(
            response, '/accounts/login/?next=/dashboard/group/delete/5/',
            status_code=302)
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
        self.assertEqual(response.status_code, 302)

    def test_unpermitted_group_delete(self):
        c = Client()
        groupnum = Group.objects.count()
        self.login(c, 'user1')
        response = c.post('/dashboard/group/delete/' + str(self.g1.pk) + '/')
        self.assertEqual(response.status_code, 403)
        self.assertEqual(Group.objects.count(), groupnum)

    def test_permitted_group_delete(self):
        c = Client()
        groupnum = Group.objects.count()
        self.login(c, 'user0')
        response = c.post('/dashboard/group/delete/' + str(self.g1.pk) + '/')
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Group.objects.count(), groupnum - 1)

    def test_superuser_group_delete(self):
        c = Client()
        groupnum = Group.objects.count()
        self.login(c, 'superuser')
        response = c.post('/dashboard/group/delete/' + str(self.g1.pk) + '/')
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Group.objects.count(), groupnum - 1)


951 952 953 954 955
class GroupDetailTest(LoginMixin, TestCase):
    fixtures = ['test-vm-fixture.json', 'node.json']

    def setUp(self):
        Instance.get_remote_queue_name = Mock(return_value='test')
956
        # u0 - owner for group1
957 958 959
        self.u0 = User.objects.create(username='user0')
        self.u0.set_password('password')
        self.u0.save()
960 961 962 963 964 965
        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()
966 967 968
        self.u3 = User.objects.create(username='user3')
        self.u3.set_password('password')
        self.u3.save()
969 970 971 972
        # u4 - removable user for group1
        self.u4 = User.objects.create(username='user4')
        self.u4.set_password('password')
        self.u4.save()
973 974 975 976
        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')
977
        self.g1.profile.set_user_level(self.u0, 'owner')
978
        self.g1.profile.set_user_level(self.u4, 'operator')
979
        self.g1.user_set.add(self.u4)
980
        self.g1.save()
981 982
        self.g2 = Group.objects.create(name='group2')
        self.g2.save()
983 984 985
        self.g3 = Group.objects.create(name='group3')
        self.g3.save()
        self.g1.profile.set_group_level(self.g3, 'operator')
986 987 988 989 990
        settings["default_vlangroup"] = 'public'
        VlanGroup.objects.create(name='public')

    def tearDown(self):
        super(GroupDetailTest, self).tearDown()
991 992
        self.g1.delete()
        self.g2.delete()
993
        self.g3.delete()
994
        self.u0.delete()
995 996 997
        self.u1.delete()
        self.u2.delete()
        self.us.delete()
998
        self.u3.delete()
999
        self.u4.delete()
1000 1001 1002 1003 1004 1005 1006 1007 1008

    def test_404_superuser_group_page(self):
        c = Client()
        self.login(c, 'superuser')
        response = c.get('/dashboard/group/25555/')
        self.assertEqual(response.status_code, 404)

    def test_404_user_group_page(self):
        c = Client()
1009
        self.login(c, 'user0')
1010 1011 1012 1013 1014
        response = c.get('/dashboard/group/25555/')
        self.assertEqual(response.status_code, 404)

    def test_anon_group_page(self):
        c = Client()
1015
        response = c.get('/dashboard/group/' + str(self.g1.pk) + '/')
1016 1017
        self.assertEqual(response.status_code, 302)

1018 1019 1020
    def test_superuser_group_page(self):
        c = Client()
        self.login(c, 'superuser')
1021
        response = c.get('/dashboard/group/' + str(self.g1.pk) + '/')
1022 1023 1024 1025
        self.assertEqual(response.status_code, 200)

    def test_acluser_group_page(self):
        c = Client()
1026
        self.login(c, 'user0')
1027
        response = c.get('/dashboard/group/' + str(self.g1.pk) + '/')
1028 1029 1030
        self.assertEqual(response.status_code, 200)

    def test_acluser2_group_page(self):
1031
        self.g1.profile.set_user_level(self.u1, 'operator')
1032 1033
        c = Client()
        self.login(c, 'user1')
1034
        response = c.get('/dashboard/group/' + str(self.g1.pk) + '/')
1035 1036 1037 1038 1039
        self.assertEqual(response.status_code, 200)

    def test_unpermitted_user_group_page(self):
        c = Client()
        self.login(c, 'user1')
1040
        response = c.get('/dashboard/group/' + str(self.g1.pk) + '/')
1041 1042 1043
        self.assertEqual(response.status_code, 403)

    def test_user_in_userlist_group_page(self):
1044
        self.g1.user_set.add(self.u1)
1045 1046
        c = Client()
        self.login(c, 'user1')
1047
        response = c.get('/dashboard/group/' + str(self.g1.pk) + '/')
1048 1049 1050
        self.assertEqual(response.status_code, 403)

    def test_groupmember_group_page(self):
1051 1052
        self.g2.user_set.add(self.u1)
        self.g1.profile.set_group_level(self.g2, 'owner')
1053 1054
        c = Client()
        self.login(c, 'user1')
1055
        response = c.get('/dashboard/group/' + str(self.g1.pk) + '/')
1056 1057
        self.assertEqual(response.status_code, 200)

1058
    def test_superuser_group_delete(self):
1059 1060 1061
        num_of_groups = Group.objects.count()
        c = Client()
        self.login(c, 'superuser')
1062
        response = c.post('/dashboard/group/delete/' + str(self.g1.pk) + '/')
1063 1064 1065 1066 1067 1068
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Group.objects.count(), num_of_groups - 1)

    def test_unpermitted_group_delete(self):
        num_of_groups = Group.objects.count()
        c = Client()
1069
        self.login(c, 'user3')
1070
        response = c.post('/dashboard/group/delete/' + str(self.g1.pk) + '/')
1071
        self.assertEqual(response.status_code, 403)
1072 1073
        self.assertEqual(Group.objects.count(), num_of_groups)

1074 1075 1076
    def test_acl_group_delete(self):
        num_of_groups = Group.objects.count()
        c = Client()
1077
        self.login(c, 'user0')
1078
        response = c.post('/dashboard/group/delete/' + str(self.g1.pk) + '/')
1079 1080 1081
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Group.objects.count(), num_of_groups - 1)

1082 1083 1084
    def test_anon_group_delete(self):
        num_of_groups = Group.objects.count()
        c = Client()
1085
        response = c.post('/dashboard/group/delete/' + str(self.g1.pk) + '/')
1086 1087 1088
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Group.objects.count(), num_of_groups)

1089
    # add to group
1090 1091 1092

    def test_anon_add_user_to_group(self):
        c = Client()
1093 1094
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1095
                          str(self.g1.pk) + '/', {'new_member': 'user3'})
1096
        self.assertEqual(user_in_group,
1097
                         self.g1.user_set.count())
1098 1099 1100 1101 1102
        self.assertEqual(response.status_code, 302)

    def test_unpermitted_add_user_to_group(self):
        c = Client()
        self.login(c, 'user3')
1103 1104
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1105
                          str(self.g1.pk) + '/', {'new_member': 'user3'})
1106
        self.assertEqual(user_in_group, self.g1.user_set.count())
1107 1108 1109 1110 1111
        self.assertEqual(response.status_code, 403)

    def test_superuser_add_user_to_group(self):
        c = Client()
        self.login(c, 'superuser')
1112 1113
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1114
                          str(self.g1.pk) + '/', {'new_member': 'user3'})
1115
        self.assertEqual(user_in_group + 1, self.g1.user_set.count())
1116 1117 1118 1119
        self.assertEqual(response.status_code, 302)

    def test_permitted_add_user_to_group(self):
        c = Client()
1120
        self.login(c, 'user0')
1121 1122
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1123
                          str(self.g1.pk) + '/', {'new_member': 'user3'})
1124
        self.assertEqual(user_in_group + 1, self.g1.user_set.count())
1125 1126 1127 1128
        self.assertEqual(response.status_code, 302)

    def test_permitted_add_multipleuser_to_group(self):
        c = Client()
1129
        self.login(c, 'user0')
1130 1131 1132
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' +
                          str(self.g1.pk) + '/',
Bach Dániel committed
1133
                          {'new_members': 'user1\r\nuser2'})
1134
        self.assertEqual(user_in_group + 2, self.g1.user_set.count())
1135 1136 1137 1138 1139 1140 1141 1142
        self.assertEqual(response.status_code, 302)

    def test_add_multipleuser_skip_badname_to_group(self):
        c = Client()
        self.login(c, 'user0')
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' +
                          str(self.g1.pk) + '/',
Bach Dániel committed
1143
                          {'new_members': 'user1\r\nnoname\r\nuser2'})
1144
        self.assertEqual(user_in_group + 2, self.g1.user_set.count())
1145 1146 1147 1148 1149
        self.assertEqual(response.status_code, 302)

    def test_unpermitted_add_multipleuser_to_group(self):
        c = Client()
        self.login(c, 'user3')
1150 1151 1152
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' +
                          str(self.g1.pk) + '/',
Bach Dániel committed
1153
                          {'new_members': 'user1\r\nuser2'})
1154
        self.assertEqual(user_in_group, self.g1.user_set.count())
1155 1156 1157 1158
        self.assertEqual(response.status_code, 403)

    def test_anon_add_multipleuser_to_group(self):
        c = Client()
1159 1160 1161
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' +
                          str(self.g1.pk) + '/',
Bach Dániel committed
1162
                          {'new_members': 'user1\r\nuser2'})
1163
        self.assertEqual(user_in_group, self.g1.user_set.count())
1164 1165 1166 1167
        self.assertEqual(response.status_code, 302)

    def test_anon_add_acluser_to_group(self):
        c = Client()
1168
        gp = self.g1.profile
1169
        acl_users = len(gp.get_users_with_level())
1170
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1171
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1172
                          {'name': 'user3', 'level': 'owner'})
1173 1174 1175 1176 1177 1178
        self.assertEqual(acl_users, len(gp.get_users_with_level()))
        self.assertEqual(response.status_code, 302)

    def test_unpermitted_add_acluser_to_group(self):
        c = Client()
        self.login(c, 'user3')
1179
        gp = self.g1.profile
1180
        acl_users = len(gp.get_users_with_level())
1181
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1182
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1183
                          {'name': 'user3', 'level': 'owner'})
1184
        self.assertEqual(acl_users, len(gp.get_users_with_level()))
1185
        self.assertEqual(response.status_code, 302)
1186 1187 1188

    def test_superuser_add_acluser_to_group(self):
        c = Client()
1189
        gp = self.g1.profile
1190 1191
        self.login(c, 'superuser')
        acl_users = len(gp.get_users_with_level())
1192
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1193
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1194
                          {'name': 'user3', 'level': 'owner'})
1195 1196 1197 1198 1199
        self.assertEqual(acl_users + 1, len(gp.get_users_with_level()))
        self.assertEqual(response.status_code, 302)

    def test_permitted_add_acluser_to_group(self):
        c = Client()
1200
        gp = self.g1.profile
1201
        self.login(c, 'user0')
1202
        acl_users = len(gp.get_users_with_level())
1203
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1204
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1205
                          {'name': 'user3', 'level': 'owner'})
1206 1207 1208 1209 1210
        self.assertEqual(acl_users + 1, len(gp.get_users_with_level()))
        self.assertEqual(response.status_code, 302)

    def test_anon_add_aclgroup_to_group(self):
        c = Client()
1211
        gp = self.g1.profile
1212
        acl_groups = len(gp.get_groups_with_level())
1213
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1214
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1215
                          {'name': 'group2', 'level': 'owner'})
1216 1217 1218 1219 1220
        self.assertEqual(acl_groups, len(gp.get_groups_with_level()))
        self.assertEqual(response.status_code, 302)

    def test_unpermitted_add_aclgroup_to_group(self):
        c = Client()
1221
        gp = self.g1.profile
1222 1223
        self.login(c, 'user3')
        acl_groups = len(gp.get_groups_with_level())
1224
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1225
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1226
                          {'name': 'group2', 'level': 'owner'})
1227
        self.assertEqual(acl_groups, len(gp.get_groups_with_level()))
1228
        self.assertEqual(response.status_code, 302)
1229 1230 1231

    def test_superuser_add_aclgroup_to_group(self):
        c = Client()
1232
        gp = self.g1.profile
1233 1234
        self.login(c, 'superuser')
        acl_groups = len(gp.get_groups_with_level())
1235
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1236
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1237
                          {'name': 'group2', 'level': 'owner'})
1238 1239 1240 1241 1242
        self.assertEqual(acl_groups + 1, len(gp.get_groups_with_level()))
        self.assertEqual(response.status_code, 302)

    def test_permitted_add_aclgroup_to_group(self):
        c = Client()
1243
        gp = self.g1.profile
1244
        self.login(c, 'user0')
1245
        acl_groups = len(gp.get_groups_with_level())
1246
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1247
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1248
                          {'name': 'group2', 'level': 'owner'})
1249
        self.assertEqual(acl_groups + 1, len(gp.get_groups_with_level()))
1250
        self.assertEqual(response.status_code, 302)
1251

1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
    # remove from group

    def test_anon_remove_user_from_group(self):
        c = Client()
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' + str(self.g1.pk) +
                          '/remove/user/' + str(self.u4.pk) + '/')
        self.assertEqual(user_in_group,
                         self.g1.user_set.count())
        self.assertEqual(response.status_code, 302)

    def test_unpermitted_remove_user_from_group(self):
        c = Client()
        self.login(c, 'user3')
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' + str(self.g1.pk) +
                          '/remove/user/' + str(self.u4.pk) + '/')
        self.assertEqual(user_in_group, self.g1.user_set.count())
        self.assertEqual(response.status_code, 403)

    def test_superuser_remove_user_from_group(self):
        c = Client()
        self.login(c, 'superuser')
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' + str(self.g1.pk) +
                          '/remove/user/' + str(self.u4.pk) + '/')
        self.assertEqual(user_in_group - 1, self.g1.user_set.count())
        self.assertEqual(response.status_code, 302)

    def test_permitted_remove_user_from_group(self):
        c = Client()
        self.login(c, 'user0')
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' + str(self.g1.pk) +
                          '/remove/user/' + str(self.u4.pk) + '/')
        self.assertEqual(user_in_group - 1, self.g1.user_set.count())
        self.assertEqual(response.status_code, 302)

Bach Dániel committed
1290 1291 1292 1293
    def test_unpermitted_user_add_wo_group_perm(self):
        user_count = self.g1.user_set.count()
        c = Client()
        self.login(c, 'user1')
Bach Dániel committed
1294 1295
        self.u1.user_permissions.add(Permission.objects.get(
            name='Can add user'))
Bach Dániel committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
        response = c.post('/dashboard/group/%d/create/' % self.g1.pk,
                          {'username': 'userx1',
                           'password1': 'test123',
                           'password2': 'test123'})
        self.assertEqual(response.status_code, 403)
        self.assertEqual(user_count, self.g1.user_set.count())

    def test_permitted_user_add_wo_can_add_user_perm(self):
        user_count = self.g1.user_set.count()
        c = Client()
        self.login(c, 'user0')
        response = c.post('/dashboard/group/%d/create/' % self.g1.pk,
                          {'username': 'userx2',
                           'password1': 'test123',
                           'password2': 'test123'})
Bach Dániel committed
1311 1312 1313 1314
        self.assertRedirects(
            response,
            '/accounts/login/?next=/dashboard/group/%d/create/' % self.g1.pk)
        self.assertEqual(response.status_code, 302)
Bach Dániel committed
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
        self.assertEqual(user_count, self.g1.user_set.count())

    def test_permitted_user_add(self):
        user_count = self.g1.user_set.count()
        self.u0.user_permissions.add(Permission.objects.get(
            name='Can add user'))
        c = Client()
        self.login(c, 'user0')
        response = c.post('/dashboard/group/%d/create/' % self.g1.pk,
                          {'username': 'userx2',
                           'password1': 'test123',
                           'password2': 'test123'})
Bach Dániel committed
1327
        self.assertRedirects(response, '/dashboard/group/%d/' % self.g1.pk)
Bach Dániel committed
1328 1329 1330
        self.assertEqual(user_count + 1, self.g1.user_set.count())
        self.assertEqual(response.status_code, 302)

1331

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
class GroupListTest(LoginMixin, TestCase):
    fixtures = ['test-vm-fixture.json', 'node.json']

    def setUp(self):
        self.u1 = User.objects.create(username='user1')
        self.u1.set_password('password')
        self.u1.save()
        permlist = Permission.objects.all()
        self.u1.user_permissions.add(
            filter(lambda element: 'group' in element.name and
                   'add' in element.name, permlist)[0])
        self.u2 = User.objects.create(username='user2')
        self.u2.set_password('password')
        self.u2.save()
        self.g1 = Group.objects.create(name='group1')
        self.g1.profile.set_user_level(self.u1, 'owner')
        self.g1.save()
        self.g2 = Group.objects.create(name='group2')
        self.g2.profile.set_user_level(self.u1, 'owner')
        self.g2.save()
        self.g3 = Group.objects.create(name='group3')
        self.g3.profile.set_user_level(self.u1, 'owner')
        self.g3.save()

    def test_anon_filter(self):
        c = Client()
        response = c.get('/dashboard/group/list/?s="3"')
        self.assertEqual(response.status_code, 302)

    def test_permitteduser_filter(self):
        c = Client()
        self.login(c, 'user1')
        response = c.get('/dashboard/group/list/?s="3"')
        self.assertEqual(response.status_code, 200)

    def tearDown(self):
        super(GroupListTest, self).tearDown()
        self.u1.delete()
        self.u2.delete()
        self.g1.delete()
        self.g2.delete()


1375
class VmDetailVncTest(LoginMixin, TestCase):
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
    fixtures = ['test-vm-fixture.json', 'node.json']

    def setUp(self):
        self.u1 = User.objects.create(username='user1')
        self.u1.set_password('password')
        self.u1.save()

    def test_permitted_vm_console(self):
        c = Client()
        self.login(c, 'user1')
        inst = Instance.objects.get(pk=1)
        inst.node = Node.objects.all()[0]
        inst.save()
        inst.set_level(