Skip to content

Commit

Permalink
Add tests for adding maintenance tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Nov 24, 2023
1 parent 15f41f1 commit dba746a
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions tests/integration/web/maintenance/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
# License along with NAV. If not, see <http://www.gnu.org/licenses/>.
#

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):
Expand All @@ -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()

0 comments on commit dba746a

Please sign in to comment.