Merge branch 'develop' into feature/search_autocomplete_haste
[ganeti_webmgr.git] / ganeti / tests / views / virtual_machine / edit.py
blob7cee0203764184e549bbc57e0cb2be95838239d3
1 import json
3 from django.contrib.auth.models import User
5 from ganeti import models
6 from ganeti.tests.rapi_proxy import JOB_RUNNING
7 from ganeti.tests.views.virtual_machine.base import TestVirtualMachineViewsBase
8 from ganeti.utilities import cluster_os_list
10 __all__ = ['TestVirtualMachineEditViews',
11 'TestVirtualMachineDeleteViews',
12 'TestVirtualMachineReinstallViews',
13 'TestVirtualMachineRenameViews',
14 'TestVirtualMachineReparentViews']
16 VirtualMachine = models.VirtualMachine
18 global c, cluster, vm
19 global user, user1, superuser, cluster_admin, vm_admin, vm_modify, cluster_migrate
22 class TestVirtualMachineEditViews(TestVirtualMachineViewsBase):
24 context = globals()
26 def test_view_modify(self):
27 """
28 Test modifying an instance
29 """
30 vm = globals()['vm']
31 args = (cluster.slug, vm.hostname)
32 url = '/cluster/%s/%s/edit' % args
34 user = User(id=52, username='modifier')
35 user.set_password('secret2')
36 user.save()
38 ## GET
39 # Anonymous User
40 response = c.get(url)
41 self.assertEqual(302, response.status_code)
43 # User with Modify Permissions
44 user.grant('modify', vm)
45 self.assertTrue(c.login(username=user.username, password='secret2'))
46 self.assertFalse(user.is_superuser)
47 self.assertTrue(user.has_perm('modify', vm))
48 self.assertFalse(user.has_perm('admin', vm))
49 response = c.get(url)
50 self.assertEqual(200, response.status_code)
51 self.assertEqual('text/html; charset=utf-8', response['content-type'])
52 self.assertTemplateUsed(response, 'virtual_machine/edit.html')
53 user.revoke_all(vm)
54 c.logout()
56 # User with Admin Permissions
57 user.grant('admin', vm)
58 self.assertTrue(c.login(username=user.username, password='secret2'))
59 self.assertFalse(user.is_superuser)
60 response = c.get(url)
61 self.assertEqual(200, response.status_code)
62 self.assertEqual('text/html; charset=utf-8', response['content-type'])
63 self.assertTemplateUsed(response, 'virtual_machine/edit.html')
64 user.revoke_all(vm)
65 c.logout()
67 # Superuser
68 user.is_superuser = True
69 user.save()
70 self.assertTrue(c.login(username=user.username, password='secret2'))
71 response = c.get(url)
72 self.assertEqual(200, response.status_code)
73 self.assertEqual('text/html; charset=utf-8', response['content-type'])
74 self.assertTemplateUsed(response, 'virtual_machine/edit.html')
75 c.logout()
76 user.is_superuser = False
77 user.save()
79 ## POST
80 os_list = cluster_os_list(cluster)
81 data = dict(vcpus=2,
82 acpi=True,
83 disk_cache='default',
84 initrd_path='',
85 kernel_args='ro',
86 kvm_flag='',
87 mem_path='',
88 migration_downtime=30,
89 security_domain='',
90 security_model='none',
91 usb_mouse='',
92 use_chroot=False,
93 use_localtime=False,
94 vnc_bind_addres='0.0.0.0',
95 vnc_tls=False,
96 vnc_x509_path='',
97 vnc_x509_verify=False,
98 memory=512,
99 os_name='image+debian-osgeo',
100 disk_type='paravirtual',
101 boot_order='disk',
102 nic_type='paravirtual',
103 nic_link='br0',
104 nic_mac='aa:bb:00:00:33:d2',
105 root_path='/dev/vda1',
106 kernel_path='/boot/vmlinuz-2.32.6-27-generic',
107 serial_console=True,
108 cdrom_image_path='')
110 # Required Values
111 user.grant('modify', vm)
112 self.assertTrue(c.login(username=user.username, password='secret2'))
113 session = c.session
114 session['os_list'] = os_list
115 session.save()
116 for property in ['vcpus', 'memory', 'disk_type', 'boot_order',
117 'nic_type']:
118 data_ = data.copy()
119 del data_[property]
120 self.assertFalse(user.is_superuser)
121 response = c.post(url, data_)
122 # If failure then a field that is not required by the model, but
123 # should be required by the form, is not being required by
124 # the form. See the ModifyVirtualMachineForm.required field.
125 self.assertNotEqual(response.context['form'][property].errors, [], msg=property)
126 self.assertEqual(200, response.status_code) # 302 if success (BAD)
127 self.assertEqual('text/html; charset=utf-8', response['content-type'])
128 self.assertTemplateUsed(response, 'virtual_machine/edit.html')
129 c.logout()
130 user.revoke_all(vm)
133 # Anonymous User
134 response = c.post(url, data)
135 self.assertEqual(302, response.status_code)
137 # Superuser
138 user.is_superuser = True
139 user.save()
140 self.assertTrue(c.login(username=user.username, password='secret2'))
141 self.assertTrue(user.is_superuser)
142 session = c.session
143 session['os_list'] = os_list
144 session.save()
145 response = c.post(url, data)
146 self.assertEqual(302, response.status_code)
147 self.assertEqual('text/html; charset=utf-8', response['content-type'])
148 c.logout()
149 user.is_superuser = False
150 user.save()
152 # User without Permissions
153 self.assertTrue(c.login(username=user.username, password='secret2'))
154 self.assertFalse(user.is_superuser)
155 session = c.session
156 session['os_list'] = os_list
157 session.save()
158 response = c.post(url, data)
159 self.assertEqual(403, response.status_code)
160 self.assertTrue(response.context['message'])
161 self.assertEqual('text/html; charset=utf-8', response['content-type'])
162 self.assertTemplateUsed(response, '403.html')
163 c.logout()
165 # User with Modify Permissions
166 user.grant('modify', vm)
167 self.assertTrue(c.login(username=user.username, password='secret2'))
168 self.assertFalse(user.is_superuser)
169 session = c.session
170 session['os_list'] = os_list
171 session.save()
172 response = c.post(url, data)
173 self.assertEqual(302, response.status_code)
174 self.assertEqual('text/html; charset=utf-8', response['content-type'])
175 user.revoke_all(vm)
176 c.logout()
178 # User with Admin Permissions
179 user.grant('admin', vm)
180 self.assertTrue(c.login(username=user.username, password='secret2'))
181 self.assertFalse(user.is_superuser)
182 session = c.session
183 session['os_list'] = os_list
184 session.save()
185 response = c.post(url, data)
186 self.assertEqual(302, response.status_code)
187 self.assertEqual('text/html; charset=utf-8', response['content-type'])
188 user.revoke_all(vm)
189 c.logout()
191 def test_view_modify_confirm(self):
193 Test confirm page for modifying an instance
195 vm = globals()['vm']
196 args = (cluster.slug, vm.hostname)
197 url = '/cluster/%s/%s/edit/confirm' % args
199 user = User(id=52, username='modifier')
200 user.set_password('secret2')
201 user.save()
203 vm.owner = user.get_profile()
204 vm.save()
206 os_list = cluster_os_list(cluster)
207 edit_form = dict(vcpus=2,
208 acpi=True,
209 disk_cache='default',
210 initrd_path='',
211 kernel_args='ro',
212 kvm_flag='',
213 mem_path='',
214 migration_downtime=30,
215 security_domain='',
216 security_model='none',
217 usb_mouse='',
218 use_chroot=False,
219 use_localtime=False,
220 vnc_bind_addres='0.0.0.0',
221 vnc_tls=False,
222 vnc_x509_path='',
223 vnc_x509_verify=False,
224 memory=512,
225 os_name='image+debian-osgeo',
226 disk_type='paravirtual',
227 boot_order='disk',
228 nic_type='paravirtual',
229 nic_link='br0',
230 nic_mac='aa:bb:00:00:33:d2',
231 root_path='/dev/vda1',
232 kernel_path='/boot/vmlinuz-2.32.6-27-generic',
233 serial_console=True,
234 cdrom_image_path='')
236 ## SESSION VARIABLES
237 # Make sure session variables are set
238 user.is_superuser = True
239 user.save()
240 self.assertTrue(c.login(username=user.username, password='secret2'))
241 session = c.session
242 # edit_form
243 response = c.get(url)
244 self.assertEqual(400, response.status_code)
245 session['edit_form'] = edit_form
246 session.save()
247 response = c.get(url)
248 self.assertEqual(200, response.status_code)
249 self.assertEqual('text/html; charset=utf-8', response['content-type'])
250 self.assertTemplateUsed(response, 'virtual_machine/edit_confirm.html')
252 #session['os_list'] = os_list
253 #session.save()
254 user.revoke_all(vm)
255 user.is_superuser = False
256 user.save()
257 c.logout()
259 ## GET
260 # Anonymous User
261 response = c.get(url)
262 self.assertEqual(302, response.status_code)
264 # User with Modify Permissions
265 user.grant('modify', vm)
266 self.assertFalse(user.is_superuser)
267 self.assertTrue(c.login(username=user.username, password='secret2'))
268 session = c.session
269 session['edit_form'] = edit_form
270 session['os_list'] = os_list
271 session.save()
272 response = c.get(url)
273 self.assertEqual(200, response.status_code)
274 self.assertEqual('text/html; charset=utf-8', response['content-type'])
275 self.assertTemplateUsed(response, 'virtual_machine/edit_confirm.html')
276 user.revoke_all(vm)
277 c.logout()
279 # User with Admin Permissions
280 user.grant('admin', vm)
281 self.assertFalse(user.is_superuser)
282 self.assertTrue(c.login(username=user.username, password='secret2'))
283 session = c.session
284 session['edit_form'] = edit_form
285 session['os_list'] = os_list
286 session.save()
287 response = c.get(url)
288 self.assertEqual(200, response.status_code)
289 self.assertEqual('text/html; charset=utf-8', response['content-type'])
290 self.assertTemplateUsed(response, 'virtual_machine/edit_confirm.html')
291 user.revoke_all(vm)
292 c.logout()
294 # Superuser
295 user.is_superuser = True
296 user.save()
297 self.assertTrue(c.login(username=user.username, password='secret2'))
298 session = c.session
299 session['edit_form'] = edit_form
300 session['os_list'] = os_list
301 session.save()
302 response = c.get(url)
303 self.assertEqual(200, response.status_code)
304 self.assertEqual('text/html; charset=utf-8', response['content-type'])
305 self.assertTemplateUsed(response, 'virtual_machine/edit_confirm.html')
306 c.logout()
307 user.is_superuser = False
308 user.save()
310 ## POST
311 data = {'rapi_dict':json.dumps(edit_form)}
312 # Anonymous User
313 response = c.post(url, data)
314 self.assertEqual(302, response.status_code)
316 for i in ('cancel', 'edit', 'save', 'reboot'):
317 data[i] = True
319 # Superuser
320 user.is_superuser = True
321 user.save()
322 self.assertTrue(c.login(username=user.username, password='secret2'))
323 session = c.session
324 session['edit_form'] = edit_form
325 session['os_list'] = os_list
326 session.save()
327 self.assertTrue(user.is_superuser)
328 response = c.post(url, data)
329 self.assertEqual(302, response.status_code)
330 self.assertEqual('text/html; charset=utf-8', response['content-type'])
331 c.logout()
332 user.is_superuser = False
333 user.save()
335 # User without Permissions
336 self.assertTrue(c.login(username=user.username, password='secret2'))
337 self.assertFalse(user.is_superuser)
338 response = c.post(url, data)
339 self.assertEqual(403, response.status_code)
340 self.assertTrue(response.context['message'])
341 self.assertEqual('text/html; charset=utf-8', response['content-type'])
342 self.assertTemplateUsed(response, '403.html')
343 c.logout()
345 # User with Modify Permissions
346 user.grant('modify', vm)
347 user.grant('power', vm)
348 self.assertTrue(c.login(username=user.username, password='secret2'))
349 session = c.session
350 session['edit_form'] = edit_form
351 session['os_list'] = os_list
352 session.save()
353 self.assertFalse(user.is_superuser)
354 response = c.post(url, data)
355 self.assertEqual(302, response.status_code)
356 self.assertEqual('text/html; charset=utf-8', response['content-type'])
357 user.revoke_all(vm)
358 c.logout()
360 # User with Admin Permissions
361 user.grant('admin', vm)
362 self.assertTrue(c.login(username=user.username, password='secret2'))
363 session = c.session
364 session['edit_form'] = edit_form
365 session['os_list'] = os_list
366 session.save()
367 self.assertFalse(user.is_superuser)
368 response = c.post(url, data)
369 self.assertEqual(302, response.status_code)
370 self.assertEqual('text/html; charset=utf-8', response['content-type'])
371 user.revoke_all(vm)
372 c.logout()
374 del data[i]
376 def test_view_modify_quota_over(self):
377 vm = globals()['vm']
378 args = (cluster.slug, vm.hostname)
379 url = '/cluster/%s/%s/edit' % args
381 user = User(id=52, username='modifier')
382 user.set_password('secret2')
383 user.save()
384 user.grant('modify', vm)
385 profile = user.get_profile()
386 vm.owner = profile
387 vm.save()
389 cluster.set_quota(profile, dict(ram=1000, disk=2000, virtual_cpus=10))
391 ## POST
392 os_list = cluster_os_list(cluster)
393 data = dict(vcpus=2000,
394 acpi=True,
395 disk_cache='default',
396 initrd_path='',
397 kernel_args='ro',
398 kvm_flag='',
399 mem_path='',
400 migration_downtime=30,
401 security_domain='',
402 security_model='none',
403 usb_mouse='',
404 use_chroot=False,
405 use_localtime=False,
406 vnc_bind_addres='0.0.0.0',
407 vnc_tls=False,
408 vnc_x509_path='',
409 vnc_x509_verify=False,
410 memory=512,
411 os='image+debian-osgeo',
412 disk_type='paravirtual',
413 boot_order='disk',
414 nic_type='paravirtual',
415 nic_link='br0',
416 nic_mac='aa:bb:00:00:33:d2',
417 root_path='/dev/vda1',
418 kernel_path='/boot/vmlinuz-2.32.6-27-generic',
419 serial_console=True,
420 cdrom_image_path='')
422 user.grant('modify', vm)
423 self.assertTrue(c.login(username=user.username, password='secret2'))
424 self.assertFalse(user.is_superuser)
425 session = c.session
426 session['os_list'] = os_list
427 session.save()
428 response = c.post(url, data)
429 self.assertEqual(200, response.status_code) # 302 if success (BAD)
430 self.assertTemplateUsed(response, 'virtual_machine/edit.html')
431 user.revoke_all(vm)
432 c.logout()
434 def test_view_modify_confirm_quota_over(self):
435 vm = globals()['vm']
436 args = (cluster.slug, vm.hostname)
437 url = '/cluster/%s/%s/edit/confirm' % args
439 user = User(id=52, username='modifier')
440 user.set_password('secret2')
441 user.save()
442 user.grant('modify', vm)
443 profile = user.get_profile()
444 vm.owner = profile
445 vm.save()
447 cluster.set_quota(profile, dict(ram=1000, disk=2000, virtual_cpus=10))
449 os_list = cluster_os_list(cluster)
450 edit_form = dict(vcpus=2000,
451 acpi=True,
452 disk_cache='default',
453 initrd_path='',
454 kernel_args='ro',
455 kvm_flag='',
456 mem_path='',
457 migration_downtime=30,
458 security_domain='',
459 security_model='none',
460 usb_mouse='',
461 use_chroot=False,
462 use_localtime=False,
463 vnc_bind_addres='0.0.0.0',
464 vnc_tls=False,
465 vnc_x509_path='',
466 vnc_x509_verify=False,
467 memory=512,
468 os='image+debian-osgeo',
469 disk_type='paravirtual',
470 boot_order='disk',
471 nic_type='paravirtual',
472 nic_link='br0',
473 nic_mac='aa:bb:00:00:33:d2',
474 root_path='/dev/vda1',
475 kernel_path='/boot/vmlinuz-2.32.6-27-generic',
476 serial_console=True,
477 cdrom_image_path='')
478 data = {'rapi_dict':json.dumps(edit_form), 'save':True}
480 # regular user
481 self.assertTrue(c.login(username=user.username, password='secret2'))
482 session = c.session
483 session['edit_form'] = edit_form
484 session['os_list'] = os_list
485 session.save()
486 response = c.post(url, data)
487 self.assertEqual(200, response.status_code)
488 self.assertEqual('text/html; charset=utf-8', response['content-type'])
489 self.assertTemplateUsed(response, 'virtual_machine/edit_confirm.html')
490 c.logout()
493 class TestVirtualMachineDeleteViews(TestVirtualMachineViewsBase):
494 context = globals()
496 def test_view_delete(self):
498 Tests view for deleting virtual machines
500 url = '/cluster/%s/%s/delete'
501 args = (cluster.slug, vm.hostname)
503 # anonymous user
504 response = c.get(url % args, follow=True)
505 self.assertEqual(200, response.status_code)
506 self.assertTemplateUsed(response, 'registration/login.html')
508 # unauthorized user
509 self.assert_(c.login(username=user.username, password='secret'))
510 response = c.post(url % args)
511 self.assertEqual(403, response.status_code)
513 # invalid vm
514 response = c.get(url % (cluster.slug, "DoesNotExist"))
515 self.assertEqual(404, response.status_code)
517 # authorized GET (vm remove permissions)
518 user.grant('remove', vm)
519 response = c.get(url % args)
520 self.assertEqual(200, response.status_code)
521 self.assertEqual('text/html; charset=utf-8', response['content-type'])
522 self.assertTemplateUsed(response, 'virtual_machine/delete.html')
523 self.assert_(VirtualMachine.objects.filter(id=vm.id).exists())
524 user.revoke_all(vm)
526 # authorized GET (vm admin permissions)
527 user.grant('admin', vm)
528 response = c.get(url % args)
529 self.assertEqual(200, response.status_code)
530 self.assertEqual('text/html; charset=utf-8', response['content-type'])
531 self.assertTemplateUsed(response, 'virtual_machine/delete.html')
532 self.assert_(VirtualMachine.objects.filter(id=vm.id).exists())
533 user.revoke_all(cluster)
535 # authorized GET (cluster admin permissions)
536 user.grant('admin', cluster)
537 response = c.get(url % args)
538 self.assertEqual(200, response.status_code)
539 self.assertEqual('text/html; charset=utf-8', response['content-type'])
540 self.assertTemplateUsed(response, 'virtual_machine/delete.html')
541 self.assert_(VirtualMachine.objects.filter(id=vm.id).exists())
542 user.revoke_all(cluster)
544 # authorized GET (superuser)
545 user.is_superuser = True
546 user.save()
547 response = c.get(url % args)
548 self.assertEqual(200, response.status_code)
549 self.assertEqual('text/html; charset=utf-8', response['content-type'])
550 self.assertTemplateUsed(response, 'virtual_machine/delete.html')
551 self.assert_(VirtualMachine.objects.filter(id=vm.id).exists())
553 #authorized POST (superuser)
554 user1.grant('power', vm)
555 vm.rapi.GetJobStatus.response = JOB_RUNNING
556 response = c.post(url % args, follow=True)
557 self.assertEqual(200, response.status_code)
558 self.assertEqual('text/html; charset=utf-8', response['content-type'])
559 self.assertTemplateUsed(response, 'virtual_machine/delete_status.html')
560 self.assert_(VirtualMachine.objects.filter(id=vm.id).exists())
561 pending_delete, job_id = VirtualMachine.objects.filter(id=vm.id).values('pending_delete','last_job_id')[0]
562 self.assert_(pending_delete)
563 self.assert_(job_id)
564 user.is_superuser = False
565 user.save()
566 vm.save()
568 #authorized POST (cluster admin)
569 user.grant('admin', cluster)
570 user1.grant('power', vm)
571 response = c.post(url % args, follow=True)
572 self.assertEqual(200, response.status_code)
573 self.assertEqual('text/html; charset=utf-8', response['content-type'])
574 self.assertTemplateUsed(response, 'virtual_machine/delete_status.html')
575 self.assert_(VirtualMachine.objects.filter(id=vm.id).exists())
576 pending_delete, job_id = VirtualMachine.objects.filter(id=vm.id).values('pending_delete','last_job_id')[0]
577 self.assert_(pending_delete)
578 self.assert_(job_id)
579 user.revoke_all(cluster)
581 #authorized POST (vm admin)
582 vm.save()
583 user.grant('admin', vm)
584 user1.grant('power', vm)
585 response = c.post(url % args, follow=True)
586 self.assertEqual(200, response.status_code)
587 self.assertEqual('text/html; charset=utf-8', response['content-type'])
588 self.assertTemplateUsed(response, 'virtual_machine/delete_status.html')
589 self.assert_(VirtualMachine.objects.filter(id=vm.id).exists())
590 pending_delete, job_id = VirtualMachine.objects.filter(id=vm.id).values('pending_delete','last_job_id')[0]
591 self.assert_(pending_delete)
592 self.assert_(job_id)
593 vm.save()
594 user.revoke_all(vm)
596 #authorized POST (cluster admin)
597 vm.save()
598 user.grant('remove', vm)
599 user1.grant('power', vm)
600 response = c.post(url % args, follow=True)
601 self.assertEqual(200, response.status_code)
602 self.assertEqual('text/html; charset=utf-8', response['content-type'])
603 self.assertTemplateUsed(response, 'virtual_machine/delete_status.html')
604 self.assert_(VirtualMachine.objects.filter(id=vm.id).exists())
605 pending_delete, job_id = VirtualMachine.objects.filter(id=vm.id).values('pending_delete','last_job_id')[0]
606 self.assert_(pending_delete)
607 self.assert_(job_id)
608 vm.save()
609 user.revoke_all(vm)
612 class TestVirtualMachineReinstallViews(TestVirtualMachineViewsBase):
614 context = globals()
616 def test_view_reinstall(self):
618 Tests view for reinstalling virtual machines
620 url = '/cluster/%s/%s/reinstall'
621 args = (cluster.slug, vm.hostname)
623 # anonymous user
624 response = c.get(url % args, follow=True)
625 self.assertEqual(200, response.status_code)
626 self.assertTemplateUsed(response, 'registration/login.html')
628 # unauthorized user
629 self.assert_(c.login(username=user.username, password='secret'))
630 response = c.post(url % args)
631 self.assertEqual(403, response.status_code)
633 # invalid vm
634 response = c.get(url % (cluster.slug, "DoesNotExist"))
635 self.assertEqual(404, response.status_code)
637 # authorized GET (vm remove permissions)
638 user.grant('remove', vm)
639 response = c.get(url % args)
640 self.assertEqual(200, response.status_code)
641 self.assertEqual('text/html; charset=utf-8', response['content-type'])
642 self.assertTemplateUsed(response, 'virtual_machine/reinstall.html')
643 self.assert_(VirtualMachine.objects.filter(id=vm.id).exists())
644 user.revoke_all(vm)
646 # authorized GET (vm admin permissions)
647 user.grant('admin', vm)
648 response = c.get(url % args)
649 self.assertEqual(200, response.status_code)
650 self.assertEqual('text/html; charset=utf-8', response['content-type'])
651 self.assertTemplateUsed(response, 'virtual_machine/reinstall.html')
652 self.assert_(VirtualMachine.objects.filter(id=vm.id).exists())
653 user.revoke_all(cluster)
655 # authorized GET (cluster admin permissions)
656 user.grant('admin', cluster)
657 response = c.get(url % args)
658 self.assertEqual(200, response.status_code)
659 self.assertEqual('text/html; charset=utf-8', response['content-type'])
660 self.assertTemplateUsed(response, 'virtual_machine/reinstall.html')
661 self.assert_(VirtualMachine.objects.filter(id=vm.id).exists())
662 user.revoke_all(cluster)
664 # authorized GET (superuser)
665 user.is_superuser = True
666 user.save()
667 response = c.get(url % args)
668 self.assertEqual(200, response.status_code)
669 self.assertEqual('text/html; charset=utf-8', response['content-type'])
670 self.assertTemplateUsed(response, 'virtual_machine/reinstall.html')
671 self.assert_(VirtualMachine.objects.filter(id=vm.id).exists())
673 #authorized POST (superuser)
674 response = c.post(url % args)
675 self.assertEqual(302, response.status_code)
676 user.is_superuser = False
677 user.save()
678 vm.save()
680 #authorized POST (cluster admin)
681 user.grant('admin', cluster)
682 response = c.post(url % args)
683 self.assertEqual(302, response.status_code)
684 user.revoke_all(cluster)
686 #authorized POST (vm admin)
687 vm.save()
688 user.grant('admin', vm)
689 response = c.post(url % args)
690 self.assertEqual(302, response.status_code)
691 vm.save()
692 user.revoke_all(vm)
694 #authorized POST (cluster admin)
695 vm.save()
696 user.grant('remove', vm)
697 response = c.post(url % args)
698 self.assertEqual(302, response.status_code)
699 vm.save()
700 user.revoke_all(vm)
703 class TestVirtualMachineRenameViews(TestVirtualMachineViewsBase):
705 context = globals()
707 def test_view_rename_get(self):
709 VM rename GET requests should have the standard responses.
712 url = "/cluster/%s/%s/rename/"
713 args = (cluster.slug, vm.hostname)
714 template = 'virtual_machine/rename.html'
715 users =[superuser, cluster_admin, vm_admin, vm_modify]
716 denied = [cluster_migrate]
718 # test GET requests
719 self.assert_standard_fails(url, args)
720 self.assert_200(url, args, users, template=template)
721 self.assert_403(url, args, denied)
723 def test_view_rename_post(self):
725 VM rename POST requests should have the standard responses.
728 url = "/cluster/%s/%s/rename/"
729 args = (cluster.slug, vm.hostname)
730 template_success = 'virtual_machine/detail.html'
731 users = [superuser, cluster_admin, vm_admin, vm_modify]
732 denied = [cluster_migrate]
733 data = {'hostname':'foo.arg.different', 'ip_check':False, 'name_check':False}
735 #noinspection PyUnusedLocal
736 def tests(user, response):
737 updated_vm = VirtualMachine.objects.get(pk=vm.pk)
738 self.assertEqual('foo.arg.different', updated_vm.hostname)
739 vm.save()
741 self.assert_standard_fails(url, args, data, method='post')
742 self.assert_200(url, args, users, template_success, data=data, follow=True, method="post", tests=tests)
743 self.assert_403(url, args, denied, data=data, method="post")
745 def test_view_rename_form(self):
747 Tests that form validation is working properly
750 url = "/cluster/%s/%s/rename/"
751 args = (cluster.slug, vm.hostname)
752 template = 'virtual_machine/rename.html'
753 data = {'hostname':'foo.arg.different', 'ip_check':False, 'name_check':False}
754 errors = ({'hostname':vm.hostname},)
756 #noinspection PyUnusedLocal
757 def tests(user, response):
758 updated_vm = VirtualMachine.objects.get(pk=vm.pk)
759 self.assertEqual(vm.hostname, updated_vm.hostname)
761 self.assert_view_missing_fields(url, args, data, fields=['hostname'], template=template, tests=tests)
762 self.assert_view_values(url, args, data, errors, template, tests=tests)
765 class TestVirtualMachineReparentViews(TestVirtualMachineViewsBase):
767 context = globals()
769 def test_view_rename_get(self):
771 VM reparent GET requests should have the standard responses.
774 url = "/cluster/%s/%s/reparent/"
775 args = (cluster.slug, vm.hostname)
776 template = 'virtual_machine/reparent.html'
777 users =[superuser, cluster_admin]
778 denied = [vm_admin, vm_modify, cluster_migrate]
780 # test GET requests
781 self.assert_standard_fails(url, args)
782 self.assert_200(url, args, users, template=template)
783 self.assert_403(url, args, denied)
785 def test_view_rename_post(self):
787 VM reparent POST requests should have the standard responses.
790 vm.owner = vm_admin.get_profile()
791 vm.save()
793 url = "/cluster/%s/%s/reparent/"
794 args = (cluster.slug, vm.hostname)
795 template_success = 'virtual_machine/detail.html'
796 users = [superuser, cluster_admin]
797 denied = [vm_admin, vm_modify, cluster_migrate]
798 data = {'owner':vm_modify.get_profile().pk}
800 #noinspection PyUnusedLocal
801 def tests(user, response):
802 updated_vm = VirtualMachine.objects.get(pk=vm.pk)
803 self.assertEqual(vm_modify.get_profile().clusteruser_ptr, updated_vm.owner)
805 self.assert_standard_fails(url, args, data, method='post')
806 self.assert_200(url, args, users, template_success, data=data, follow=True, method="post", tests=tests)
807 self.assert_403(url, args, denied, data=data, method="post")