936a9a55369adbd79e25e02248aeab6ee8a7860b
[pharos-tools.git] / dashboard / src / booking / tests / test_quick_booking.py
1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
3 #
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 ##############################################################################
9
10 import datetime
11
12 from django.test import TestCase, Client
13
14 from booking.models import Booking
15 from dashboard.testing_utils import (
16     instantiate_host,
17     instantiate_user,
18     instantiate_userprofile,
19     instantiate_lab,
20     instantiate_installer,
21     instantiate_image,
22     instantiate_scenario,
23     instantiate_os,
24     make_hostprofile_set,
25     instantiate_opnfvrole,
26     instantiate_publicnet,
27 )
28 # from dashboard import test_utils
29
30
31 class QuickBookingValidFormTestCase(TestCase):
32     @classmethod
33     def setUpTestData(cls):
34         cls.loginuser = instantiate_user(False, username="newtestuser", password="testpassword")
35         instantiate_userprofile(cls.loginuser, True)
36
37         lab_user = instantiate_user(True)
38         cls.lab = instantiate_lab(lab_user)
39
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)
48
49         cls.lab_selected = 'lab_' + str(cls.lab.lab_user.id) + '_selected'
50         cls.host_selected = 'host_' + str(cls.host_profile.id) + '_selected'
51
52         cls.post_data = cls.build_post_data()
53
54         cls.client = Client()
55
56     @classmethod
57     def build_post_data(cls):
58         post_data = {}
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)
69         return post_data
70
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)
75         return response
76
77     def setUp(self):
78         self.client.login(
79             username=self.loginuser.username, password="testpassword")
80
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))
88
89         resourcebundle = booking.resource
90         configbundle = booking.config_bundle
91
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')
96
97         return True
98
99     def test_with_too_long_length(self):
100         response = self.post({'length': '22'})
101
102         self.assertEqual(response.status_code, 200)
103         self.assertIsNone(Booking.objects.first())
104
105     def test_with_negative_length(self):
106         response = self.post({'length': '-1'})
107
108         self.assertEqual(response.status_code, 200)
109         self.assertIsNone(Booking.objects.first())
110
111     def test_with_invalid_installer(self):
112         response = self.post({'installer': str(self.installer.id + 100)})
113
114         self.assertEqual(response.status_code, 200)
115         self.assertIsNone(Booking.objects.first())
116
117     def test_with_invalid_scenario(self):
118         response = self.post({'scenario': str(self.scenario.id + 100)})
119
120         self.assertEqual(response.status_code, 200)
121         self.assertIsNone(Booking.objects.first())
122
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"}]}'})
125
126         self.assertEqual(response.status_code, 200)
127         self.assertIsNone(Booking.objects.first())
128
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"}]}'})
131
132         self.assertEqual(response.status_code, 200)
133         self.assertIsNone(Booking.objects.first())
134
135     def test_with_invalid_empty_filter_field(self):
136         response = self.post({'filter_field': ''})
137
138         self.assertEqual(response.status_code, 200)
139         self.assertIsNone(Booking.objects.first())
140
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
143
144         self.assertEqual(response.status_code, 200)
145         booking = Booking.objects.first()
146         self.assertIsNotNone(booking)
147         self.assertTrue(self.is_valid_booking(booking))
148
149     def test_with_valid_form(self):
150         response = self.post()
151
152         self.assertEqual(response.status_code, 200)
153         booking = Booking.objects.first()
154         self.assertIsNotNone(booking)
155         self.assertTrue(self.is_valid_booking(booking))