From dba746a41d31c2ad6c40128f1d294a51e16295f5 Mon Sep 17 00:00:00 2001 From: Johanna England Date: Fri, 24 Nov 2023 10:01:31 +0100 Subject: [PATCH] Add tests for adding maintenance tasks --- .../integration/web/maintenance/views_test.py | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/tests/integration/web/maintenance/views_test.py b/tests/integration/web/maintenance/views_test.py index 78b1dc91ea..8109fcaae9 100644 --- a/tests/integration/web/maintenance/views_test.py +++ b/tests/integration/web/maintenance/views_test.py @@ -14,6 +14,11 @@ # License along with NAV. If not, see . # +from django.urls import reverse +from nav.compatibility import smart_str +from nav.models.manage import Netbox +from nav.models.msgmaint import MaintenanceTask + class TestMaintenanceCalendarView: def test_calendar_renders_when_no_arguments_given(self, client): @@ -23,3 +28,111 @@ def test_calendar_renders_when_no_arguments_given(self, client): def test_calendar_still_renders_when_invalid_arguments_given(self, client): response = client.get('/maintenance/?year=invalid&month=invalid', follow=True) assert response.status_code == 200 + + +class TestAddMaintenanceTask: + def test_valid_data_without_end_time_should_suceed(self, db, client, localhost): + url = reverse('maintenance-new') + data = { + "start_time": "2023-11-21 12:40", + "no_end_time": "on", + "description": "maintenance", + "netbox": localhost.pk, + "save": "Save+task", + } + + response = client.post( + url, + follow=True, + data=data, + ) + + assert response.status_code == 200 + assert f'Saved task {data["description"]}' in smart_str(response.content) + assert MaintenanceTask.objects.filter(description=data["description"]).exists() + + def test_valid_data_with_end_time_should_suceed(self, db, client, localhost): + url = reverse('maintenance-new') + data = { + "start_time": "2023-11-21 12:40", + "end_time": "2023-11-25 12:40", + "description": "maintenance", + "netbox": localhost.pk, + "save": "Save+task", + } + + response = client.post( + url, + follow=True, + data=data, + ) + + assert response.status_code == 200 + assert f'Saved task {data["description"]}' in smart_str(response.content) + assert MaintenanceTask.objects.filter(description=data["description"]).exists() + + def test_with_non_int_netbox_key_should_fail(self, db, client): + url = reverse('maintenance-new') + data = { + "start_time": "2023-11-21 12:40", + "no_end_time": "on", + "description": "maintenance", + "netbox": "137'", + "save": "Save+task", + } + + response = client.post( + url, + follow=True, + data=data, + ) + + assert response.status_code == 200 + 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_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 identifiers 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 = { + "start_time": "2023-11-22 14:35", + "end_time": "2023-11-08 14:35", + "description": "maintenance", + "netbox": localhost.pk, + "save": "Save+task", + } + + response = client.post( + url, + follow=True, + data=data, + ) + + assert response.status_code == 200 + assert "End time must be after start time" in smart_str(response.content) + assert not MaintenanceTask.objects.filter( + description=data["description"] + ).exists()