Skip to content

Commit

Permalink
Show error message on trying to delete last dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Nov 25, 2024
1 parent 3c80b8b commit 7720f56
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python/nav/web/static/js/src/webfront.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ require([
window.location = '/';
});
request.fail(function (response) {
feedback.addFeedback(response, 'error');
feedback.addFeedback(response.responseText, 'error');
});
}
});
Expand Down
3 changes: 2 additions & 1 deletion python/nav/web/webfront/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from urllib.parse import quote as urlquote

from django.http import (
HttpResponseBadRequest,
HttpResponseForbidden,
HttpResponseRedirect,
HttpResponse,
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/web/webfront_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down

0 comments on commit 7720f56

Please sign in to comment.