test_views.py 2.15 KB
Newer Older
Őry Máté committed
1 2 3 4
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User, Group

5 6
from vm.models import Instance

Őry Máté committed
7 8

class VmDetailTest(TestCase):
9 10
    fixtures = ['test-vm-fixture.json']

Őry Máté committed
11 12
    def setUp(self):
        self.u1 = User.objects.create(username='user1')
13 14
        self.u1.set_password('password')
        self.u1.save()
Őry Máté committed
15
        self.u2 = User.objects.create(username='user2', is_staff=True)
16 17
        self.u2.set_password('password')
        self.u2.save()
Őry Máté committed
18
        self.us = User.objects.create(username='superuser', is_superuser=True)
19
        self.us.save()
Őry Máté committed
20 21 22 23 24
        self.g1 = Group.objects.create(name='group1')
        self.g1.user_set.add(self.u1)
        self.g1.user_set.add(self.u2)
        self.g1.save()

25 26 27 28 29 30 31 32 33 34 35 36
    def tearDown(self):
        super(VmDetailTest, self).tearDown()
        self.u1.delete()
        self.u2.delete()
        self.us.delete()
        self.g1.delete()

    def login(self, client, username, password='password'):
        response = client.post('/login/', {'username': username,
                                           'password': password})
        self.assertNotEqual(response.status_code, 403)

Őry Máté committed
37 38 39 40
    def test_404_vm_page(self):
        c = Client()
        response = c.get('/dashboard/vm/235555/')
        self.assertEqual(response.status_code, 404)
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    def test_anon_vm_page(self):
        c = Client()
        response = c.get('/dashboard/vm/1/')
        self.assertEqual(response.status_code, 403)

    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):
62
        c = Client()
63 64 65
        self.login(c, 'user2')
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u2, 'user')
66 67
        response = c.get('/dashboard/vm/1/')
        self.assertEqual(response.status_code, 200)