Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show error message on trying to delete last dashboard #3232

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading