From f7b53b6fa390f1c3c86183ab67d074744f20e56a Mon Sep 17 00:00:00 2001
From: Johanna England <johanna.england@sikt.no>
Date: Fri, 10 Nov 2023 10:08:07 +0100
Subject: [PATCH 1/2] Fix related name when deleting filter group

---
 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 228e7b7385..bf764c071c 100644
--- a/python/nav/web/alertprofiles/views.py
+++ b/python/nav/web/alertprofiles/views.py
@@ -1929,7 +1929,7 @@ def filter_group_remove(request):
             time_periods = TimePeriod.objects.filter(
                 alert_subscriptions__in=subscriptions
             )
-            profiles = AlertProfile.objects.filter(timeperiod__in=time_periods)
+            profiles = AlertProfile.objects.filter(time_periods__in=time_periods)
             warnings = []
 
             try:

From 804a40d4bdfe0ab9a6e1eb5ea6dbe380d7427f48 Mon Sep 17 00:00:00 2001
From: Johanna England <johanna.england@sikt.no>
Date: Fri, 10 Nov 2023 12:38:40 +0100
Subject: [PATCH 2/2] Add test for deleting filter group

---
 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 2127904ce6..2417876cbe 100644
--- a/tests/integration/web/alertprofiles_test.py
+++ b/tests/integration/web/alertprofiles_test.py
@@ -16,6 +16,7 @@
     AlertSender,
     Expression,
     Filter,
+    FilterGroup,
     MatchField,
     Operator,
 )
@@ -531,6 +532,35 @@ def test_alertprofiles_add_invalid_phone_number_should_fail(client):
     assert "Not a valid phone number." in smart_str(response.content)
 
 
+def test_alertprofiles_confirm_remove_filter_group(db, client, dummy_filter_group):
+    url = reverse('alertprofiles-filter_groups-remove')
+    response = client.post(
+        url,
+        follow=True,
+        data={
+            'confirm': '1',
+            'element': [dummy_filter_group.id],
+        },
+    )
+    assert response.status_code == 200
+    assert not FilterGroup.objects.filter(pk=dummy_filter_group.pk).exists()
+
+
+def test_alertprofiles_remove_filter_group(db, client, dummy_filter_group):
+    url = reverse('alertprofiles-filter_groups-remove')
+    response = client.post(
+        url,
+        follow=True,
+        data={
+            'filter_group': [dummy_filter_group.id],
+        },
+    )
+    assert response.status_code == 200
+    assert "Confirm deletion" in smart_str(response.content)
+    assert dummy_filter_group.name in smart_str(response.content)
+    assert FilterGroup.objects.filter(pk=dummy_filter_group.pk).count() == 1
+
+
 #
 # fixtures and helpers
 #
@@ -558,3 +588,10 @@ def dummy_filter():
     filtr = Filter(name="dummy", owner=Account.objects.get(id=Account.ADMIN_ACCOUNT))
     filtr.save()
     return filtr
+
+
+@pytest.fixture(scope="function")
+def dummy_filter_group(admin_account):
+    filter_group = FilterGroup(name="dummy_group", owner=admin_account)
+    filter_group.save()
+    return filter_group