Skip to content

Commit

Permalink
Add handling for non existent components
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Nov 23, 2023
1 parent 463e547 commit 9805e9f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 36 deletions.
90 changes: 56 additions & 34 deletions python/nav/web/maintenance/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,40 +137,62 @@ def get_component_keys(post):

def components_for_keys(component_keys):
component_data = {}
component_data['service'] = Service.objects.filter(
id__in=component_keys['service']
).values(
'id',
'handler',
'netbox__id',
'netbox__sysname',
'netbox__ip',
'netbox__room__id',
'netbox__room__description',
'netbox__room__location__id',
'netbox__room__location__description',
)
component_data['netbox'] = Netbox.objects.filter(
id__in=component_keys['netbox']
).values(
'id',
'sysname',
'ip',
'room__id',
'room__description',
'room__location__id',
'room__location__description',
)
component_data['room'] = Room.objects.filter(id__in=component_keys['room']).values(
'id', 'description', 'location__id', 'location__description'
)
component_data['location'] = Location.objects.filter(
id__in=component_keys['location']
).values('id', 'description')
component_data['netboxgroup'] = NetboxGroup.objects.filter(
id__in=component_keys['netboxgroup']
).values('id', 'description')
return component_data
component_data_errors = []
if component_keys['service']:
component_data['service'] = Service.objects.filter(

Check warning on line 142 in python/nav/web/maintenance/utils.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/maintenance/utils.py#L142

Added line #L142 was not covered by tests
id__in=component_keys['service']
).values(
'id',
'handler',
'netbox__id',
'netbox__sysname',
'netbox__ip',
'netbox__room__id',
'netbox__room__description',
'netbox__room__location__id',
'netbox__room__location__description',
)
if not component_data['service']:
component_data_errors.append(

Check warning on line 156 in python/nav/web/maintenance/utils.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/maintenance/utils.py#L155-L156

Added lines #L155 - L156 were not covered by tests
"service: no elements with the given pks found"
)
if component_keys['netbox']:
component_data['netbox'] = Netbox.objects.filter(
id__in=component_keys['netbox']
).values(
'id',
'sysname',
'ip',
'room__id',
'room__description',
'room__location__id',
'room__location__description',
)
if not component_data['netbox']:
component_data_errors.append("netbox: no elements with the given pks found")
if component_keys['room']:
component_data['room'] = Room.objects.filter(

Check warning on line 174 in python/nav/web/maintenance/utils.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/maintenance/utils.py#L174

Added line #L174 was not covered by tests
id__in=component_keys['room']
).values('id', 'description', 'location__id', 'location__description')
if not component_data['room']:
component_data_errors.append("room: no elements with the given pks found")

Check warning on line 178 in python/nav/web/maintenance/utils.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/maintenance/utils.py#L177-L178

Added lines #L177 - L178 were not covered by tests
if component_keys['location']:
component_data['location'] = Location.objects.filter(

Check warning on line 180 in python/nav/web/maintenance/utils.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/maintenance/utils.py#L180

Added line #L180 was not covered by tests
id__in=component_keys['location']
).values('id', 'description')
if not component_data['location']:
component_data_errors.append(

Check warning on line 184 in python/nav/web/maintenance/utils.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/maintenance/utils.py#L183-L184

Added lines #L183 - L184 were not covered by tests
"location: no elements with the given pks found"
)
if component_keys['netboxgroup']:
component_data['netboxgroup'] = NetboxGroup.objects.filter(

Check warning on line 188 in python/nav/web/maintenance/utils.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/maintenance/utils.py#L188

Added line #L188 was not covered by tests
id__in=component_keys['netboxgroup']
).values('id', 'description')
if not component_data['netboxgroup']:
component_data_errors.append(

Check warning on line 192 in python/nav/web/maintenance/utils.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/maintenance/utils.py#L191-L192

Added lines #L191 - L192 were not covered by tests
"netboxgroup: no elements with the given pks found"
)
return component_data, component_data_errors


def structure_component_data(component_data):
Expand Down
6 changes: 4 additions & 2 deletions python/nav/web/maintenance/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def view(request, task_id):
value = int(value)
component_keys[key].append(value)

component_data = components_for_keys(component_keys)
component_data, _ = components_for_keys(component_keys)
components = structure_component_data(component_data)
component_trail = task_component_trails(component_keys, components)

Expand Down Expand Up @@ -279,9 +279,11 @@ def edit(request, task_id=None, start_time=None, **_):
component_keys, component_keys_errors = get_component_keys(request.GET)

if component_keys:
component_data = components_for_keys(component_keys)
component_data, component_data_errors = components_for_keys(component_keys)
components = structure_component_data(component_data)
component_trail = task_component_trails(component_keys, components)
if component_data_errors:
new_message(request, ",".join(component_data_errors), Messages.ERROR)

if component_keys_errors:
new_message(request, ",".join(component_keys_errors), Messages.ERROR)
Expand Down
15 changes: 15 additions & 0 deletions tests/integration/web/maintenance/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from django.urls import reverse
from nav.compatibility import smart_str
from nav.models.manage import Netbox
from nav.models.msgmaint import MaintenanceTask


Expand Down Expand Up @@ -100,6 +101,20 @@ def test_with_non_int_netbox_key_in_url_should_fail(self, db, client):
assert response.status_code == 200
assert "netbox: argument needs to be a number" in smart_str(response.content)

def test_with_non_existent_netbox_key_in_url_should_fail(self, db, client):
last_netbox_id = getattr(Netbox.objects.last(), "pk", 0)
url = (
reverse('maintenance-new')
+ f'?netbox={last_netbox_id+1}&netbox={last_netbox_id+2}'
)

response = client.get(url, follow=True)

assert response.status_code == 200
assert "netbox: no elements with the given pks found" in smart_str(
response.content
)

def test_with_end_time_before_start_time_should_fail(self, db, client, localhost):
url = reverse('maintenance-new')
data = {
Expand Down

0 comments on commit 9805e9f

Please sign in to comment.