diff --git a/python/nav/web/static/js/src/webfront.js b/python/nav/web/static/js/src/webfront.js index 6099c3af05..ae36bb173a 100644 --- a/python/nav/web/static/js/src/webfront.js +++ b/python/nav/web/static/js/src/webfront.js @@ -289,7 +289,7 @@ require([ window.location = '/'; }); request.fail(function (response) { - feedback.addFeedback(response, 'error'); + feedback.addFeedback(response.responseText, 'error'); }); } }); diff --git a/python/nav/web/webfront/views.py b/python/nav/web/webfront/views.py index c98f0e7732..85bdb88be5 100644 --- a/python/nav/web/webfront/views.py +++ b/python/nav/web/webfront/views.py @@ -24,6 +24,7 @@ from urllib.parse import quote as urlquote from django.http import ( + HttpResponseBadRequest, HttpResponseForbidden, HttpResponseRedirect, HttpResponse, @@ -428,7 +429,7 @@ def delete_dashboard(request, did): """Delete this dashboard and all widgets on it""" is_last = AccountDashboard.objects.filter(account=request.account).count() == 1 if is_last: - return HttpResponse('Can not delete last dashboard', status=400) + return HttpResponseBadRequest('Cannot delete last dashboard') dash = get_object_or_404(AccountDashboard, pk=did, account=request.account) dash.delete() diff --git a/tests/integration/web/webfront_test.py b/tests/integration/web/webfront_test.py index 49e7e45b41..c4118ae017 100644 --- a/tests/integration/web/webfront_test.py +++ b/tests/integration/web/webfront_test.py @@ -68,6 +68,20 @@ def test_set_default_dashboard_with_multiple_previous_defaults_should_succeed( ) +def test_delete_last_dashboard_should_fail(db, client, admin_account): + """Tests that the last dashboard cannot be deleted""" + dashboard = AccountDashboard.objects.get( + is_default=True, + account=admin_account, + ) + url = reverse("delete-dashboard", args=(dashboard.pk,)) + response = client.post(url, follow=True) + + assert response.status_code == 400 + assert "Cannot delete last dashboard" in smart_str(response.content) + assert AccountDashboard.objects.filter(id=dashboard.id).exists() + + def test_when_logging_in_it_should_change_the_session_id( db, client, admin_username, admin_password ):