1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
12 from django.test import TestCase, Client
14 from booking.models import Booking
15 from dashboard.testing_utils import (
18 instantiate_userprofile,
20 instantiate_installer,
25 instantiate_opnfvrole,
26 instantiate_publicnet,
28 # from dashboard import test_utils
31 class QuickBookingValidFormTestCase(TestCase):
33 def setUpTestData(cls):
34 cls.loginuser = instantiate_user(False, username="newtestuser", password="testpassword")
35 instantiate_userprofile(cls.loginuser, True)
37 lab_user = instantiate_user(True)
38 cls.lab = instantiate_lab(lab_user)
40 cls.host_profile = make_hostprofile_set(cls.lab)
41 cls.scenario = instantiate_scenario()
42 cls.installer = instantiate_installer([cls.scenario])
43 os = instantiate_os([cls.installer])
44 cls.image = instantiate_image(cls.lab, 1, cls.loginuser, os, cls.host_profile)
45 cls.host = instantiate_host(cls.host_profile, cls.lab)
46 cls.role = instantiate_opnfvrole()
47 cls.pubnet = instantiate_publicnet(10, cls.lab)
49 cls.lab_selected = 'lab_' + str(cls.lab.lab_user.id) + '_selected'
50 cls.host_selected = 'host_' + str(cls.host_profile.id) + '_selected'
52 cls.post_data = cls.build_post_data()
57 def build_post_data(cls):
59 post_data['filter_field'] = '{"hosts":[{"host_' + str(cls.host_profile.id) + '":"true"}], "labs": [{"lab_' + str(cls.lab.lab_user.id) + '":"true"}]}'
60 post_data['purpose'] = 'purposefieldcontentstring'
61 post_data['project'] = 'projectfieldcontentstring'
62 post_data['length'] = '3'
63 post_data['ignore_this'] = 1
64 post_data['users'] = ''
65 post_data['hostname'] = 'hostnamefieldcontentstring'
66 post_data['image'] = str(cls.image.id)
67 post_data['installer'] = str(cls.installer.id)
68 post_data['scenario'] = str(cls.scenario.id)
71 def post(self, changed_fields={}):
72 payload = self.post_data.copy()
73 payload.update(changed_fields)
74 response = self.client.post('/booking/quick/', payload)
79 username=self.loginuser.username, password="testpassword")
81 def is_valid_booking(self, booking):
82 self.assertEqual(booking.owner, self.loginuser)
83 self.assertEqual(booking.purpose, 'purposefieldcontentstring')
84 self.assertEqual(booking.project, 'projectfieldcontentstring')
85 delta = booking.end - booking.start
86 delta -= datetime.timedelta(days=3)
87 self.assertLess(delta, datetime.timedelta(minutes=1))
89 resourcebundle = booking.resource
90 configbundle = booking.config_bundle
92 self.assertEqual(self.installer, configbundle.opnfv_config.first().installer)
93 self.assertEqual(self.scenario, configbundle.opnfv_config.first().scenario)
94 self.assertEqual(resourcebundle.template.getHosts()[0].profile, self.host_profile)
95 self.assertEqual(resourcebundle.template.getHosts()[0].resource.name, 'hostnamefieldcontentstring')
99 def test_with_too_long_length(self):
100 response = self.post({'length': '22'})
102 self.assertEqual(response.status_code, 200)
103 self.assertIsNone(Booking.objects.first())
105 def test_with_negative_length(self):
106 response = self.post({'length': '-1'})
108 self.assertEqual(response.status_code, 200)
109 self.assertIsNone(Booking.objects.first())
111 def test_with_invalid_installer(self):
112 response = self.post({'installer': str(self.installer.id + 100)})
114 self.assertEqual(response.status_code, 200)
115 self.assertIsNone(Booking.objects.first())
117 def test_with_invalid_scenario(self):
118 response = self.post({'scenario': str(self.scenario.id + 100)})
120 self.assertEqual(response.status_code, 200)
121 self.assertIsNone(Booking.objects.first())
123 def test_with_invalid_host_id(self):
124 response = self.post({'filter_field': '{"hosts":[{"host_' + str(self.host_profile.id + 100) + '":"true"}], "labs": [{"lab_' + str(self.lab.lab_user.id) + '":"true"}]}'})
126 self.assertEqual(response.status_code, 200)
127 self.assertIsNone(Booking.objects.first())
129 def test_with_invalid_lab_id(self):
130 response = self.post({'filter_field': '{"hosts":[{"host_' + str(self.host_profile.id) + '":"true"}], "labs": [{"lab_' + str(self.lab.lab_user.id + 100) + '":"true"}]}'})
132 self.assertEqual(response.status_code, 200)
133 self.assertIsNone(Booking.objects.first())
135 def test_with_invalid_empty_filter_field(self):
136 response = self.post({'filter_field': ''})
138 self.assertEqual(response.status_code, 200)
139 self.assertIsNone(Booking.objects.first())
141 def test_with_garbage_users_field(self): # expected behavior: treat as though field is empty if it has garbage data
142 response = self.post({'users': 'X�]QP�槰DP�+m���h�U�_�yJA:.rDi��QN|.��C��n�P��F!��D�����5ȅj�9�LV��'}) # output from /dev/urandom
144 self.assertEqual(response.status_code, 200)
145 booking = Booking.objects.first()
146 self.assertIsNotNone(booking)
147 self.assertTrue(self.is_valid_booking(booking))
149 def test_with_valid_form(self):
150 response = self.post()
152 self.assertEqual(response.status_code, 200)
153 booking = Booking.objects.first()
154 self.assertIsNotNone(booking)
155 self.assertTrue(self.is_valid_booking(booking))