Skip to content

Commit

Permalink
Merge pull request #2784 from lunkwill42/bugfix/maintenance-edit-crash
Browse files Browse the repository at this point in the history
Initialize variables that are potentially referenced before assignment in maintenance `edit()` view
  • Loading branch information
lunkwill42 authored Dec 11, 2023
2 parents b51ca9d + 1de2f9a commit 6528958
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
4 changes: 1 addition & 3 deletions python/nav/web/maintenance/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,7 @@ def cancel(request, task_id):
def edit(request, task_id=None, start_time=None, **_):
account = get_account(request)
quickselect = QuickSelect(service=True)
component_trail = None
component_keys = None
task = None
component_trail = component_keys_errors = component_data = task = None

if task_id:
task = get_object_or_404(MaintenanceTask, pk=task_id)
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/web/maintenance/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
# more details. You should have received a copy of the GNU General Public
# License along with NAV. If not, see <http://www.gnu.org/licenses/>.
#
import datetime

import pytest
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 @@ -136,3 +139,27 @@ def test_with_end_time_before_start_time_should_fail(self, db, client, localhost
assert not MaintenanceTask.objects.filter(
description=data["description"]
).exists()


class TestEditMaintenanceTask:
def test_when_existing_task_is_requested_it_should_render_with_intact_description(
self, db, client, localhost, empty_maintenance_task
):
url = reverse("maintenance-edit", kwargs={"task_id": empty_maintenance_task.id})
response = client.get(url, follow=True)

assert response.status_code == 200
assert empty_maintenance_task.description in smart_str(response.content)


@pytest.fixture
def empty_maintenance_task(db):
now = datetime.datetime.now()
task = MaintenanceTask(
start_time=now,
end_time=now + datetime.timedelta(hours=1),
description="Temporary test fixture task",
)
task.save()
yield task
task.delete()

0 comments on commit 6528958

Please sign in to comment.