Skip to content

Commit

Permalink
Add handling for non int components keys
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Nov 23, 2023
1 parent 2a378e6 commit 463e547
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
6 changes: 5 additions & 1 deletion python/nav/web/maintenance/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def infodict_by_state(task):

def get_component_keys(post):
remove = {}
errors = []
raw_component_keys = {
'service': post.getlist('service'),
'netbox': post.getlist('netbox'),
Expand Down Expand Up @@ -125,10 +126,13 @@ def get_component_keys(post):
for value in raw_component_keys[key]:
if not remove or value not in remove[key]:
if key in PRIMARY_KEY_INTEGER:
if not value.isdigit():
errors.append(key + ": argument needs to be a number")
continue
value = int(value)
if value not in component_keys[key]:
component_keys[key].append(value)
return component_keys
return component_keys, errors


def components_for_keys(component_keys):
Expand Down
16 changes: 8 additions & 8 deletions python/nav/web/maintenance/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,8 @@ def edit(request, task_id=None, start_time=None, **_):
task_form = MaintenanceTaskForm(initial=task_form_initial(task, start_time))

if request.method == 'POST':
try:
component_keys = get_component_keys(request.POST)
except ValueError:
component_keys = None
component_data = dict()
component_keys, component_keys_errors = get_component_keys(request.POST)

elif task:
component_keys = {
'service': [],
Expand All @@ -279,19 +276,22 @@ def edit(request, task_id=None, start_time=None, **_):
value = int(value)
component_keys[key].append(value)
else:
component_keys = get_component_keys(request.GET)
component_keys, component_keys_errors = get_component_keys(request.GET)

if component_keys:
component_data = components_for_keys(component_keys)
components = structure_component_data(component_data)
component_trail = task_component_trails(component_keys, components)

if component_keys_errors:
new_message(request, ",".join(component_keys_errors), Messages.ERROR)

if request.method == 'POST':
if 'save' in request.POST:
task_form = MaintenanceTaskForm(request.POST)
if not any(component_data.values()):
if component_keys and not any(component_data.values()):
new_message(request, "No components selected.", Messages.ERROR)
elif task_form.is_valid():
elif not component_keys_errors and task_form.is_valid():
start_time = task_form.cleaned_data['start_time']
end_time = task_form.cleaned_data['end_time']
no_end_time = task_form.cleaned_data['no_end_time']
Expand Down
10 changes: 9 additions & 1 deletion tests/integration/web/maintenance/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,19 @@ def test_with_non_int_netbox_key_should_fail(self, db, client):
)

assert response.status_code == 200
assert "No components selected." in smart_str(response.content)
assert "netbox: argument needs to be a number" in smart_str(response.content)
assert not MaintenanceTask.objects.filter(
description=data["description"]
).exists()

def test_with_non_int_netbox_key_in_url_should_fail(self, db, client):
url = reverse('maintenance-new') + '?netbox=foobar'

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

assert response.status_code == 200
assert "netbox: argument needs to be a number" 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 463e547

Please sign in to comment.