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']
14 global user
, user1
, superuser
, vm_admin
, cluster_admin
17 class TestVirtualMachineViewList(TestVirtualMachineViewsBase
):
21 def test_anonymous(self
):
23 Anonymous users viewing the list of VMs are redirected to the login
28 response
= c
.get(url
, follow
=True)
29 self
.assertEqual(200, response
.status_code
)
30 self
.assertTemplateUsed(response
, 'registration/login.html')
34 Users with no VM permissions may view the VM list, but there will be
40 self
.create_virtual_machine(cluster
, 'test1')
42 self
.assert_(c
.login(username
=user
.username
, password
='secret'))
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.
51 def test_user_permissions(self
):
53 Users with VM permissions have some VMs in their VM list.
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'))
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
):
75 Superusers see all VMs.
80 user2
= User(id=28, username
='tester2', is_superuser
=True)
81 user2
.set_password('secret')
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'))
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
):
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
):
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
)
130 key
= SSHKey(key
="ssh-rsa test test@test", user
=user
)
132 key1
= SSHKey(key
="ssh-dsa test asd@asd", user
=user
)
137 key
= settings
.WEB_MGR_API_KEY
140 response
= c
.get( reverse("instance-keys", args
=[cluster
.slug
, vm
.hostname
, key
+"a"]))
141 self
.assertEqual( 403, response
.status_code
)
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")