From 8929cf3a53fd4625eb2dacaedd0f6d94cc5d5ddb Mon Sep 17 00:00:00 2001 From: Johanna England Date: Tue, 5 Sep 2023 11:46:02 +0200 Subject: [PATCH] Add tests to include validation of CIDR addresses --- tests/integration/web/alertprofiles_test.py | 48 ++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/tests/integration/web/alertprofiles_test.py b/tests/integration/web/alertprofiles_test.py index 4d10fddb56..1cb5a6a493 100644 --- a/tests/integration/web/alertprofiles_test.py +++ b/tests/integration/web/alertprofiles_test.py @@ -212,6 +212,29 @@ def test_alertprofiles_add_expression_with_valid_ipv6_address_should_succeed( assert f"Added expression to filter {dummy_filter}" in smart_str(response.content) +def test_alertprofiles_add_expression_with_valid_cidr_address_should_succeed( + client, dummy_filter +): + """Tests that an expression with a valid CIDR address can be added""" + url = reverse("alertprofiles-filters-saveexpression") + ip_match_field = MatchField.objects.get(data_type=MatchField.IP) + data = { + "filter": dummy_filter.pk, + "match_field": ip_match_field.pk, + "operator": Operator.EQUALS, + "value": "129.241.190.0/24", + } + response = client.post(url, data=data, follow=True) + assert response.status_code == 200 + assert Expression.objects.filter( + filter=dummy_filter, + match_field=ip_match_field, + operator=Operator.EQUALS, + value=data["value"], + ).exists() + assert f"Added expression to filter {dummy_filter}" in smart_str(response.content) + + def test_alertprofiles_add_expression_with_non_valid_ip_address_should_fail( client, dummy_filter ): @@ -235,6 +258,29 @@ def test_alertprofiles_add_expression_with_non_valid_ip_address_should_fail( assert f"Invalid IP address: {data['value']}" in smart_str(response.content) +def test_alertprofiles_add_expression_with_non_valid_cidr_address_should_fail( + client, dummy_filter +): + """Tests that an expression with a not valid CIDR address cannot be added""" + ip_match_field = MatchField.objects.get(data_type=MatchField.IP) + url = reverse("alertprofiles-filters-saveexpression") + data = { + "filter": dummy_filter.pk, + "match_field": ip_match_field.pk, + "operator": Operator.EQUALS, + "value": "10.0.2.1/28", + } + response = client.post(url, data=data, follow=True) + assert response.status_code == 200 + assert not Expression.objects.filter( + filter=dummy_filter, + match_field=ip_match_field, + operator=Operator.EQUALS, + value=data["value"], + ).exists() + assert f"Invalid IP address: {data['value']}" in smart_str(response.content) + + def test_alertprofiles_add_expression_with_multiple_valid_ip_addresses_should_succeed( client, dummy_filter ): @@ -245,7 +291,7 @@ def test_alertprofiles_add_expression_with_multiple_valid_ip_addresses_should_su "filter": dummy_filter.pk, "match_field": ip_match_field.pk, "operator": Operator.IN, - "value": "172.0.0.1 2001:db8:3333:4444:5555:6666:7777:8888", + "value": "172.0.0.1 2001:db8:3333:4444:5555:6666:7777:8888 129.241.190.0/24", } response = client.post(url, data=data, follow=True) assert response.status_code == 200