Merge branch 'develop' into feature/search_autocomplete_haste
[ganeti_webmgr.git] / ganeti / tests / views / virtual_machine / general.py
blob35915c709cf3328c0928184379a8c5d8734e4b05
1 import json
3 from django.contrib.auth.models import User
4 from django.core.urlresolvers import reverse
6 from ganeti.models import SSHKey
7 from ganeti.tests.views.virtual_machine.base import TestVirtualMachineViewsBase
9 __all__ = ['TestVirtualMachineViewList',
10 'TestVirtualMachineDetailView',
11 'TestVirtualMachineSSHKeysView']
13 global c, cluster, vm
14 global user, user1, superuser, vm_admin, cluster_admin
17 class TestVirtualMachineViewList(TestVirtualMachineViewsBase):
19 context = globals()
21 def test_anonymous(self):
22 """
23 Anonymous users viewing the list of VMs are redirected to the login
24 page.
25 """
26 url = '/vms/'
28 response = c.get(url, follow=True)
29 self.assertEqual(200, response.status_code)
30 self.assertTemplateUsed(response, 'registration/login.html')
32 def test_user(self):
33 """
34 Users with no VM permissions may view the VM list, but there will be
35 no VMs.
36 """
38 url = '/vms/'
40 self.create_virtual_machine(cluster, 'test1')
42 self.assert_(c.login(username=user.username, password='secret'))
43 response = c.get(url)
44 self.assertEqual(200, response.status_code)
45 self.assertEqual('text/html; charset=utf-8', response['content-type'])
46 self.assertTemplateUsed(response, 'virtual_machine/list.html')
47 vms = response.context['vms'].object_list
48 # There is (at least) one VM in the list; fail if we can see it.
49 self.assertFalse(vms)
51 def test_user_permissions(self):
52 """
53 Users with VM permissions have some VMs in their VM list.
54 """
56 url = '/vms/'
58 # setup vms and perms
59 vm1, cluster1 = self.create_virtual_machine(cluster, 'test1')
60 self.create_virtual_machine(cluster, 'test2')
61 user1.grant('admin', vm)
62 user1.grant('admin', vm1)
64 # user with some perms
65 self.assert_(c.login(username=user1.username, password='secret'))
66 response = c.get(url)
67 self.assertEqual(200, response.status_code)
68 self.assertEqual('text/html; charset=utf-8', response['content-type'])
69 self.assertTemplateUsed(response, 'virtual_machine/list.html')
70 vms = response.context['vms'].object_list
71 self.assertEqual(set(vms), set([vm, vm1]))
73 def test_superuser(self):
74 """
75 Superusers see all VMs.
76 """
78 url = '/vms/'
80 user2 = User(id=28, username='tester2', is_superuser=True)
81 user2.set_password('secret')
82 user2.save()
84 # setup vms and perms
85 vm1, cluster1 = self.create_virtual_machine(cluster, 'test1')
86 vm2, cluster1 = self.create_virtual_machine(cluster, 'test2')
87 vm3, cluster1 = self.create_virtual_machine(cluster, 'test3')
89 # authorized (superuser)
90 self.assert_(c.login(username=user2.username, password='secret'))
91 response = c.get(url)
92 self.assertEqual(200, response.status_code)
93 self.assertEqual('text/html; charset=utf-8', response['content-type'])
94 self.assertTemplateUsed(response, 'virtual_machine/list.html')
95 vms = response.context['vms'].object_list
96 self.assertEqual(set(vms), set([vm, vm1, vm2, vm3]))
99 class TestVirtualMachineDetailView(TestVirtualMachineViewsBase):
101 context = globals()
103 def test_view_detail(self):
105 Test showing virtual machine details
107 url = '/cluster/%s/%s/'
108 args = (cluster.slug, vm.hostname)
110 self.assert_standard_fails(url, args)
111 self.assert_200(url, args, [superuser, vm_admin, cluster_admin], template='virtual_machine/detail.html')
114 class TestVirtualMachineSSHKeysView(TestVirtualMachineViewsBase):
116 context = globals()
118 def test_view_ssh_keys(self):
120 Test getting SSH keys belonging to users, who have admin permission on
121 specified virtual machine
123 # second virtual machine created
124 vm1, cluster1 = self.create_virtual_machine(cluster, 'vm2.osuosl.bak')
126 # grant admin permission to first user
127 user.grant("admin", vm)
129 # add some keys
130 key = SSHKey(key="ssh-rsa test test@test", user=user)
131 key.save()
132 key1 = SSHKey(key="ssh-dsa test asd@asd", user=user)
133 key1.save()
135 # get API key
136 import settings
137 key = settings.WEB_MGR_API_KEY
139 # forbidden
140 response = c.get( reverse("instance-keys", args=[cluster.slug, vm.hostname, key+"a"]))
141 self.assertEqual( 403, response.status_code )
143 # not found
144 response = c.get( reverse("instance-keys", args=[cluster.slug, vm.hostname+"a", key]))
145 self.assertEqual( 404, response.status_code )
146 response = c.get( reverse("instance-keys", args=[cluster.slug+"a", vm.hostname, key]))
147 self.assertEqual( 404, response.status_code )
149 # vm with users who have admin perms
150 response = c.get( reverse("instance-keys", args=[cluster.slug, vm.hostname, key]))
151 self.assertEqual( 200, response.status_code )
152 self.assertEquals("application/json", response["content-type"])
153 self.assertEqual( len(json.loads(response.content)), 2 )
154 self.assertContains(response, "test@test", count=1)
155 self.assertContains(response, "asd@asd", count=1)
157 # vm without users who have admin perms
158 response = c.get( reverse("instance-keys", args=[cluster.slug, vm1.hostname, key]))
159 self.assertEqual( 200, response.status_code )
160 self.assertEquals("application/json", response["content-type"])
161 self.assertEqual( len(json.loads(response.content)), 0 )
162 self.assertNotContains(response, "test@test")
163 self.assertNotContains(response, "asd@asd")