summaryrefslogtreecommitdiffstats
path: root/src/booking/tests/test_quick_booking.py
blob: 5ba1744c301a599b1d315e070279c87842643ce8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
##############################################################################
# Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import datetime
import json

from django.test import TestCase, Client

from booking.models import Booking
from dashboard.testing_utils import (
    make_host,
    make_user,
    make_user_profile,
    make_lab,
    make_installer,
    make_image,
    make_scenario,
    make_os,
    make_complete_host_profile,
    make_opnfv_role,
    make_public_net,
)


class QuickBookingValidFormTestCase(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.user = make_user(False, username="newtestuser")
        cls.user.set_password("testpassword")
        cls.user.save()
        make_user_profile(cls.user, True)

        lab_user = make_user(True)
        cls.lab = make_lab(lab_user)

        cls.host_profile = make_complete_host_profile(cls.lab)
        cls.scenario = make_scenario()
        cls.installer = make_installer([cls.scenario])
        os = make_os([cls.installer])
        cls.image = make_image(cls.lab, 1, cls.user, os, cls.host_profile)
        cls.host = make_host(cls.host_profile, cls.lab)
        cls.role = make_opnfv_role()
        cls.pubnet = make_public_net(10, cls.lab)

        cls.post_data = cls.build_post_data()
        cls.client = Client()

    @classmethod
    def build_post_data(cls):
        return {
            'filter_field': json.dumps({
                "host": {
                    "host_" + str(cls.host_profile.id): {
                        "selected": True,
                        "id": cls.host_profile.id
                    }
                },
                "lab": {
                    "lab_" + str(cls.lab.lab_user.id): {
                        "selected": True,
                        "id": cls.lab.lab_user.id
                    }
                }
            }),
            'purpose': 'my_purpose',
            'project': 'my_project',
            'length': '3',
            'ignore_this': 1,
            'users': '',
            'hostname': 'my_host',
            'image': str(cls.image.id),
            'installer': str(cls.installer.id),
            'scenario': str(cls.scenario.id)
        }

    def post(self, changed_fields={}):
        payload = self.post_data.copy()
        payload.update(changed_fields)
        response = self.client.post('/booking/quick/', payload)
        return response

    def setUp(self):
        self.client.login(username="newtestuser", password="testpassword")

    def assertValidBooking(self, booking):
        self.assertEqual(booking.owner, self.user)
        self.assertEqual(booking.purpose, 'my_purpose')
        self.assertEqual(booking.project, 'my_project')
        delta = booking.end - booking.start
        delta -= datetime.timedelta(days=3)
        self.assertLess(delta, datetime.timedelta(minutes=1))

        resource_bundle = booking.resource
        config_bundle = booking.config_bundle

        opnfv_config = config_bundle.opnfv_config.first()
        self.assertEqual(self.installer, opnfv_config.installer)
        self.assertEqual(self.scenario, opnfv_config.scenario)

        host = resource_bundle.hosts.first()
        self.assertEqual(host.profile, self.host_profile)
        self.assertEqual(host.template.resource.name, 'my_host')

    def test_with_too_long_length(self):
        response = self.post({'length': '22'})

        self.assertEqual(response.status_code, 200)
        self.assertIsNone(Booking.objects.first())

    def test_with_negative_length(self):
        response = self.post({'length': '-1'})

        self.assertEqual(response.status_code, 200)
        self.assertIsNone(Booking.objects.first())

    def test_with_invalid_installer(self):
        response = self.post({'installer': str(self.installer.id + 100)})

        self.assertEqual(response.status_code, 200)
        self.assertIsNone(Booking.objects.first())

    def test_with_invalid_scenario(self):
        response = self.post({'scenario': str(self.scenario.id + 100)})

        self.assertEqual(response.status_code, 200)
        self.assertIsNone(Booking.objects.first())

    def test_with_invalid_host_id(self):
        response = self.post({'filter_field': json.dumps({
            "host": {
                "host_" + str(self.host_profile.id + 100): {
                    "selected": True,
                    "id": self.host_profile.id + 100
                }
            },
            "lab": {
                "lab_" + str(self.lab.lab_user.id): {
                    "selected": True,
                    "id": self.lab.lab_user.id
                }
            }
        })})

        self.assertEqual(response.status_code, 200)
        self.assertIsNone(Booking.objects.first())

    def test_with_invalid_lab_id(self):
        response = self.post({'filter_field': '{"hosts":[{"host_' + str(self.host_profile.id) + '":"true"}], "labs": [{"lab_' + str(self.lab.lab_user.id + 100) + '":"true"}]}'})
        response = self.post({'filter_field': json.dumps({
            "host": {
                "host_" + str(self.host_profile.id): {
                    "selected": True,
                    "id": self.host_profile.id
                }
            },
            "lab": {
                "lab_" + str(self.lab.lab_user.id + 100): {
                    "selected": True,
                    "id": self.lab.lab_user.id + 100
                }
            }
        })})

        self.assertEqual(response.status_code, 200)
        self.assertIsNone(Booking.objects.first())

    def test_with_invalid_empty_filter_field(self):
        response = self.post({'filter_field': ''})

        self.assertEqual(response.status_code, 200)
        self.assertIsNone(Booking.objects.first())

    def test_with_garbage_users_field(self):  # expected behavior: treat as though field is empty if it has garbage data
        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

        self.assertEqual(response.status_code, 200)
        booking = Booking.objects.first()
        self.assertIsNone(booking)

    def test_with_valid_form(self):
        response = self.post()

        self.assertEqual(response.status_code, 302)  # success should redirect
        booking = Booking.objects.first()
        self.assertIsNotNone(booking)
        self.assertValidBooking(booking)