test_views.py 72.4 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

20 21
import pyotp

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

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

39 40 41
import django.conf
settings = django.conf.settings.FIREWALL_SETTINGS

Őry Máté committed
42

43
class LoginMixin(object):
44
    def login(self, client, username, password='password', follow=False):
45
        response = client.post('/accounts/login/', {'username': username,
46 47
                                                    'password': password},
                               follow=follow)
48
        self.assertNotEqual(response.status_code, 403)
49
        return response
50 51


Bach Dániel committed
52
class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
53
    fixtures = ['test-vm-fixture.json', 'node.json']
54

Őry Máté committed
55
    def setUp(self):
56
        Instance.get_remote_queue_name = Mock(return_value='test')
Őry Máté committed
57
        self.u1 = User.objects.create(username='user1')
58 59
        self.u1.set_password('password')
        self.u1.save()
Őry Máté committed
60
        self.u2 = User.objects.create(username='user2', is_staff=True)
61 62
        self.u2.set_password('password')
        self.u2.save()
Őry Máté committed
63
        self.us = User.objects.create(username='superuser', is_superuser=True)
64
        self.us.set_password('password')
65
        self.us.save()
Őry Máté committed
66 67 68 69
        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
70 71
        self.u1.user_permissions.add(Permission.objects.get(
            codename='create_vm'))
72 73
        settings["default_vlangroup"] = 'public'
        VlanGroup.objects.create(name='public')
Őry Máté committed
74

75 76 77 78 79 80 81
    def tearDown(self):
        super(VmDetailTest, self).tearDown()
        self.u1.delete()
        self.u2.delete()
        self.us.delete()
        self.g1.delete()

Őry Máté committed
82 83
    def test_404_vm_page(self):
        c = Client()
Bach Dániel committed
84
        self.login(c, 'user1')
Őry Máté committed
85 86
        response = c.get('/dashboard/vm/235555/')
        self.assertEqual(response.status_code, 404)
87

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

    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):
109
        c = Client()
110 111 112
        self.login(c, 'user2')
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u2, 'user')
113 114
        response = c.get('/dashboard/vm/1/')
        self.assertEqual(response.status_code, 200)
115

116 117 118 119 120 121
    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
122
        response = c.post("/dashboard/vm/1/op/password_reset/")
123
        self.assertEqual(response.status_code, 403)
124
        self.assertEqual(password, Instance.objects.get(pk=1).pw)
125

126 127 128
    def test_unpermitted_network_add_wo_perm(self):
        c = Client()
        self.login(c, "user2")
Bach Dániel committed
129 130
        response = c.post("/dashboard/vm/1/op/add_interface/",
                          {'vlan': 1})
131 132 133
        self.assertEqual(response.status_code, 403)

    def test_unpermitted_network_add_wo_vlan_perm(self):
134 135 136
        c = Client()
        self.login(c, "user2")
        inst = Instance.objects.get(pk=1)
137
        inst.set_level(self.u2, 'owner')
Bach Dániel committed
138 139 140 141 142 143 144 145 146 147 148 149
        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)
150 151 152 153 154 155

    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
156 157
        vlan = Vlan.objects.get(id=1)
        vlan.set_level(self.u1, 'user')
158
        interface_count = inst.interface_set.count()
Bach Dániel committed
159 160 161 162
        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})
163
        self.assertEqual(response.status_code, 302)
Bach Dániel committed
164
        assert mock_method.called
165
        self.assertEqual(inst.interface_set.count(), interface_count + 1)
166

167 168 169 170 171 172
    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
173 174
        inst.status = 'RUNNING'
        inst.save()
175 176

        iface_count = inst.interface_set.count()
Bach Dániel committed
177 178 179 180 181 182
        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
183 184 185 186 187 188 189 190 191 192
        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
193 194 195 196
        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})
197 198 199
        self.assertEqual(iface_count, inst.interface_set.count())
        self.assertEqual(response.status_code, 403)

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

    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,
215
                           'system': "bubi",
216 217 218 219 220 221 222 223 224
                           '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')
Bach Dániel committed
225 226 227 228 229 230 231
        with patch.object(DeployOperation, 'async') as async:
            response = c.post('/dashboard/vm/create/',
                              {'template': 1,
                               'system': "bubi",
                               'cpu_priority': 1, 'cpu_count': 1,
                               'ram_size': 1000})
        assert async.called
232 233 234 235 236
        self.assertEqual(response.status_code, 302)

    def test_use_permitted_template_superuser(self):
        c = Client()
        self.login(c, 'superuser')
Bach Dániel committed
237 238 239 240 241 242 243
        with patch.object(DeployOperation, 'async') as async:
            response = c.post('/dashboard/vm/create/',
                              {'template': 1,
                               'system': "bubi",
                               'cpu_priority': 1, 'cpu_count': 1,
                               'ram_size': 1000})
        assert async.called
244 245 246 247 248 249 250 251
        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)
252

253 254 255 256 257
    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
258
        Vlan.objects.get(id=1).set_level(self.u1, 'user')
259 260 261 262 263 264 265 266 267 268 269
        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
270 271
        kwargs.update(name='t2', lease=1, disks=1,
                      raw_data='<devices></devices>')
272 273
        response = c.post('/dashboard/template/1/', kwargs)
        self.assertEqual(response.status_code, 302)
Kálmán Viktor committed
274 275
        self.assertEqual(InstanceTemplate.objects.get(id=1).raw_data,
                         "<devices></devices>")
276

277 278 279
    def test_notification_read(self):
        c = Client()
        self.login(c, "user1")
280
        self.u1.profile.notify('subj', '%(var)s %(user)s',
281 282 283 284 285
                               {'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'
286

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    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)

305 306 307 308 309
    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
310 311 312 313 314 315 316 317 318
        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'})
319 320 321 322 323 324
        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
325 326 327 328 329
        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())
330 331
        self.u2.user_permissions.add(Permission.objects.get(
            name='Can configure port forwards.'))
Bach Dániel committed
332 333 334 335 336
        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
337 338
        self.assertEqual(response.status_code, 403)

Bach Dániel committed
339
    def test_unpermitted_add_port_w_bad_host(self):
340 341 342 343 344 345
        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
346 347 348 349 350 351
        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)
352

353 354 355 356 357 358 359
    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
360
        inst.add_interface(user=self.u2, vlan=vlan)
361 362 363 364 365
        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
366 367 368 369 370
        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
371 372 373
        self.assertEqual(response.status_code, 302)
        self.assertEqual(len(host.list_ports()), port_count + 1)

Bach Dániel committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 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
    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')

466 467 468
    def test_permitted_wake_up_wrong_state(self):
        c = Client()
        self.login(c, "user2")
Őry Máté committed
469 470
        with patch.object(WakeUpOperation, 'async') as mock_method, \
                patch.object(Instance.WrongStateError, 'send_message') as wro:
471 472
            inst = Instance.objects.get(pk=1)
            mock_method.side_effect = inst.wake_up
Guba Sándor committed
473
            inst.status = 'RUNNING'
474
            inst.set_level(self.u2, 'owner')
Őry Máté committed
475
            c.post("/dashboard/vm/1/op/wake_up/")
Őry Máté committed
476 477
            inst = Instance.objects.get(pk=1)
            self.assertEqual(inst.status, 'RUNNING')  # mocked anyway
478
            assert mock_method.called
Őry Máté committed
479
            assert wro.called
480 481 482 483

    def test_permitted_wake_up(self):
        c = Client()
        self.login(c, "user2")
Őry Máté committed
484 485 486 487 488
        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
489
            inst._wake_up_vm = Mock()
Őry Máté committed
490 491 492 493 494 495
            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
496
            assert inst._wake_up_vm.called
Őry Máté committed
497 498 499 500
            self.assertEqual(response.status_code, 302)
            self.assertEqual(inst.status, 'RUNNING')
            assert new_wake_up.called
            assert not wro.called
501 502 503 504 505

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

511 512 513 514 515 516
    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)

517 518 519 520 521
    def test_permitted_customized_vm_create(self):
        c = Client()
        self.login(c, "superuser")

        instance_count = Instance.objects.all().count()
Bach Dániel committed
522 523 524 525 526 527 528 529 530
        with patch.object(DeployOperation, 'async') as async:
            response = c.post("/dashboard/vm/create/", {
                'name': 'vm',
                'amount': 2,
                'customized': 1,
                'template': 1,
                'cpu_priority': 10, 'cpu_count': 1, 'ram_size': 128,
                'network': [],
            })
531

Bach Dániel committed
532
        assert async.called
533 534 535
        self.assertEqual(response.status_code, 302)
        self.assertEqual(instance_count + 2, Instance.objects.all().count())

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
    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")

571

Bach Dániel committed
572
class NodeDetailTest(LoginMixin, MockCeleryMixin, TestCase):
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    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)
595
        self.patcher = patch("vm.tasks.vm_tasks.get_queues", return_value={
596 597 598
            'x': [{'name': "devenv.vm.fast"}],
            'y': [{'name': "devenv.vm.slow"}],
            'z': [{'name': "devenv.net.fast"}],
599 600
        })
        self.patcher.start()
601 602 603 604 605 606 607

    def tearDown(self):
        super(NodeDetailTest, self).tearDown()
        self.u1.delete()
        self.u2.delete()
        self.us.delete()
        self.g1.delete()
608
        self.patcher.stop()
609 610 611 612 613 614 615

    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)

616 617 618 619 620 621
    def test_200_superuser_node_page(self):
        c = Client()
        self.login(c, 'superuser')
        response = c.get('/dashboard/node/1/')
        self.assertEqual(response.status_code, 200)

622 623 624 625
    def test_302_user_node_page(self):
        c = Client()
        self.login(c, 'user1')
        response = c.get('/dashboard/node/25555/')
626
        self.assertEqual(response.status_code, 403)
627 628 629 630

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

    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/')
643
        self.assertEqual(response.status_code, 302)
644 645 646 647

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

    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'})
656
        self.assertEqual(response.status_code, 403)
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
        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'})
681
        self.assertEqual(response.status_code, 302)
682 683 684 685 686 687 688 689
        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'})
690
        self.assertEqual(response.status_code, 302)
691 692 693 694 695 696 697 698 699
        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/')
700 701
        self.assertEqual(Node.objects.get(pk=1).traits.count(),
                         trait_count + 1)
702 703 704 705 706 707 708 709

    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})
710
        self.assertEqual(response.status_code, 403)
711
        self.assertEqual(Node.objects.get(pk=1).traits.count(), trait_count)
712 713 714 715 716 717 718 719 720

    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)
721 722
        self.assertEqual(Node.objects.get(pk=1).traits.count(),
                         trait_count - 1)
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743

    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)
744 745 746 747 748 749 750 751 752 753 754
        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)

755

Bach Dániel committed
756
class GroupCreateTest(LoginMixin, MockCeleryMixin, TestCase):
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
    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()
775 776
        self.g1 = Group.objects.create(name='group1')
        self.g1.save()
777 778 779

    def tearDown(self):
        super(GroupCreateTest, self).tearDown()
780
        self.g1.delete()
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
        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)

808 809 810 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
    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)

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
    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'))

857

Bach Dániel committed
858
class GroupDeleteTest(LoginMixin, MockCeleryMixin, TestCase):
859 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
    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
902 903 904
        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
905 906 907 908 909
        self.assertEqual(response.status_code, 200)

    def test_unpermitted_group_page(self):
        c = Client()
        self.login(c, 'user1')
Bach Dániel committed
910 911 912
        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
913
        self.assertEqual(response.status_code, 302)
914 915 916

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


Bach Dániel committed
948
class GroupDetailTest(LoginMixin, MockCeleryMixin, TestCase):
949 950 951 952
    fixtures = ['test-vm-fixture.json', 'node.json']

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

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

    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()
1006
        self.login(c, 'user0')
1007 1008 1009 1010 1011
        response = c.get('/dashboard/group/25555/')
        self.assertEqual(response.status_code, 404)

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

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

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

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

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

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

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

1055
    def test_superuser_group_delete(self):
1056 1057 1058
        num_of_groups = Group.objects.count()
        c = Client()
        self.login(c, 'superuser')
1059
        response = c.post('/dashboard/group/delete/' + str(self.g1.pk) + '/')
1060 1061 1062 1063 1064 1065
        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()
1066
        self.login(c, 'user3')
1067
        response = c.post('/dashboard/group/delete/' + str(self.g1.pk) + '/')
1068
        self.assertEqual(response.status_code, 403)
1069 1070
        self.assertEqual(Group.objects.count(), num_of_groups)

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

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

1086
    # add to group
1087 1088 1089

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

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

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

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

    def test_permitted_add_multipleuser_to_group(self):
        c = Client()
1126
        self.login(c, 'user0')
1127 1128 1129
        user_in_group = self.g1.user_set.count()
        response = c.post('/dashboard/group/' +
                          str(self.g1.pk) + '/',
Bach Dániel committed
1130
                          {'new_members': 'user1\r\nuser2'})
1131
        self.assertEqual(user_in_group + 2, self.g1.user_set.count())
1132 1133 1134 1135 1136 1137 1138 1139
        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
1140
                          {'new_members': 'user1\r\nnoname\r\nuser2'})
1141
        self.assertEqual(user_in_group + 2, self.g1.user_set.count())
1142 1143 1144 1145 1146
        self.assertEqual(response.status_code, 302)

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

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

    def test_anon_add_acluser_to_group(self):
        c = Client()
1165
        gp = self.g1.profile
1166
        acl_users = len(gp.get_users_with_level())
1167
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1168
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1169
                          {'name': 'user3', 'level': 'owner'})
1170 1171 1172 1173 1174 1175
        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')
1176
        gp = self.g1.profile
1177
        acl_users = len(gp.get_users_with_level())
1178
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1179
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1180
                          {'name': 'user3', 'level': 'owner'})
1181
        self.assertEqual(acl_users, len(gp.get_users_with_level()))
1182
        self.assertEqual(response.status_code, 302)
1183 1184 1185

    def test_superuser_add_acluser_to_group(self):
        c = Client()
1186
        gp = self.g1.profile
1187 1188
        self.login(c, 'superuser')
        acl_users = len(gp.get_users_with_level())
1189
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1190
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1191
                          {'name': 'user3', 'level': 'owner'})
1192 1193 1194 1195 1196
        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()
1197
        gp = self.g1.profile
1198
        self.login(c, 'user0')
1199
        acl_users = len(gp.get_users_with_level())
1200
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1201
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1202
                          {'name': 'user3', 'level': 'owner'})
1203 1204 1205 1206 1207
        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()
1208
        gp = self.g1.profile
1209
        acl_groups = len(gp.get_groups_with_level())
1210
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1211
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1212
                          {'name': 'group2', 'level': 'owner'})
1213 1214 1215 1216 1217
        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()
1218
        gp = self.g1.profile
1219 1220
        self.login(c, 'user3')
        acl_groups = len(gp.get_groups_with_level())
1221
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1222
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1223
                          {'name': 'group2', 'level': 'owner'})
1224
        self.assertEqual(acl_groups, len(gp.get_groups_with_level()))
1225
        self.assertEqual(response.status_code, 302)
1226 1227 1228

    def test_superuser_add_aclgroup_to_group(self):
        c = Client()
1229
        gp = self.g1.profile
1230 1231
        self.login(c, 'superuser')
        acl_groups = len(gp.get_groups_with_level())
1232
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1233
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1234
                          {'name': 'group2', 'level': 'owner'})
1235 1236 1237 1238 1239
        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()
1240
        gp = self.g1.profile
1241
        self.login(c, 'user0')
1242
        acl_groups = len(gp.get_groups_with_level())
1243
        response = c.post('/dashboard/group/' +
Bach Dániel committed
1244
                          str(gp.pk) + '/acl/',
Bach Dániel committed
1245
                          {'name': 'group2', 'level': 'owner'})
1246
        self.assertEqual(acl_groups + 1, len(gp.get_groups_with_level()))
1247
        self.assertEqual(response.status_code, 302)
1248

1249 1250 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
    # 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
1287 1288 1289 1290
    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
1291 1292
        self.u1.user_permissions.add(Permission.objects.get(
            name='Can add user'))
Bach Dániel committed
1293
        response = c.post('/dashboard/profile/create/',
Bach Dániel committed
1294
                          {'username': 'userx1',
Bach Dániel committed
1295
                           'groups': self.g1.pk,
Bach Dániel committed
1296 1297
                           'password1': 'test123',
                           'password2': 'test123'})
Bach Dániel committed
1298
        self.assertEqual(response.status_code, 200)
Bach Dániel committed
1299 1300 1301 1302 1303 1304
        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')
Bach Dániel committed
1305
        response = c.post('/dashboard/profile/create/',
Bach Dániel committed
1306
                          {'username': 'userx2',
Bach Dániel committed
1307
                           'groups': self.g1.pk,
Bach Dániel committed
1308 1309
                           'password1': 'test123',
                           'password2': 'test123'})
Bach Dániel committed
1310 1311
        self.assertRedirects(
            response,
Bach Dániel committed
1312
            '/accounts/login/?next=/dashboard/profile/create/')
Bach Dániel committed
1313
        self.assertEqual(response.status_code, 302)
Bach Dániel committed
1314 1315 1316 1317 1318 1319 1320 1321
        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')
Bach Dániel committed
1322
        response = c.post('/dashboard/profile/create/',