Skip to content

Commit

Permalink
Add tests to include validation of CIDR addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Sep 5, 2023
1 parent e79c4c2 commit 8929cf3
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion tests/integration/web/alertprofiles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand All @@ -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
):
Expand All @@ -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
Expand Down

0 comments on commit 8929cf3

Please sign in to comment.