From ba661642916af9813d7c59586dae345f85c71001 Mon Sep 17 00:00:00 2001 From: Johanna England Date: Mon, 13 Nov 2023 11:52:42 +0100 Subject: [PATCH 1/2] Fix (de)activating alert profiles Avoid deleting elements of the dict that is iterated over --- python/nav/web/alertprofiles/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/nav/web/alertprofiles/views.py b/python/nav/web/alertprofiles/views.py index e0ee1f60fb..228e7b7385 100644 --- a/python/nav/web/alertprofiles/views.py +++ b/python/nav/web/alertprofiles/views.py @@ -313,7 +313,7 @@ def profile_save(request): def profile_remove(request): """Removes a profile""" post = request.POST.copy() - for data in post: + for data in request.POST: if data.find("=") != -1: attr, value = data.split("=") del post[data] From c08f762399df69995b646a1a666acdf53230e3c9 Mon Sep 17 00:00:00 2001 From: Johanna England Date: Mon, 13 Nov 2023 11:52:59 +0100 Subject: [PATCH 2/2] Add tests for (de)activating alert profiles --- tests/integration/web/alertprofiles_test.py | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/integration/web/alertprofiles_test.py b/tests/integration/web/alertprofiles_test.py index 77491a16c5..2127904ce6 100644 --- a/tests/integration/web/alertprofiles_test.py +++ b/tests/integration/web/alertprofiles_test.py @@ -125,6 +125,23 @@ def test_alertprofiles_activate_profile(db, client, dummy_profile): assert preference.active_profile == dummy_profile +def test_alertprofiles_activate_profile_with_info_in_key(db, client, dummy_profile): + # remarkably, activation/deactivation of profiles belong in the remove view! + url = reverse('alertprofiles-profile-remove') + response = client.post( + url, + follow=True, + data={ + f'activate={dummy_profile.id}': ["Activate"], + }, + ) + assert response.status_code == 200 + assert "Active profile set" in smart_str(response.content) + assert dummy_profile.name in smart_str(response.content) + preference = AlertPreference.objects.get(account=dummy_profile.account) + assert preference.active_profile == dummy_profile + + def test_alertprofiles_deactivate_profile(db, client, activated_dummy_profile): # remarkably, activation/deactivation of profiles belong in the remove view! url = reverse('alertprofiles-profile-remove') @@ -143,6 +160,26 @@ def test_alertprofiles_deactivate_profile(db, client, activated_dummy_profile): assert preference.active_profile is None +def test_alertprofiles_deactivate_profile_with_info_in_key( + db, client, activated_dummy_profile +): + # remarkably, activation/deactivation of profiles belong in the remove view! + url = reverse('alertprofiles-profile-remove') + response = client.post( + url, + follow=True, + data={ + f'deactivate={activated_dummy_profile.id}': ["Deactivate"], + }, + ) + assert response.status_code == 200 + print(type(response.content)) + assert "was deactivated" in smart_str(response.content) + assert activated_dummy_profile.name in smart_str(response.content) + preference = AlertPreference.objects.get(account=activated_dummy_profile.account) + assert preference.active_profile is None + + def test_alertprofiles_add_private_filter_should_succeed(client): """Tests that an admin can POST a new private filter""" url = reverse("alertprofiles-filters-save")