Update tests for routes in public api

This commit is contained in:
Julia 2022-07-07 16:34:01 +03:00
parent 9ba3f5a9bb
commit 3b53543e73
2 changed files with 181 additions and 21 deletions

View file

@ -3,6 +3,10 @@ from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient
from apps.base.tests.messaging_backend import TestOnlyBackend
TEST_MESSAGING_BACKEND_FIELD = TestOnlyBackend.backend_id.lower()
@pytest.mark.django_db
def test_get_list_integrations(
@ -31,7 +35,9 @@ def test_get_list_integrations(
"default_route": {
"escalation_chain_id": None,
"id": default_channel_filter.public_primary_key,
"slack": {"channel_id": None},
"slack": {"channel_id": None, "enabled": True},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": False},
},
"heartbeat": {
"link": f"{integration.integration_url}heartbeat/",
@ -159,7 +165,9 @@ def test_update_integration_template(
"default_route": {
"escalation_chain_id": None,
"id": default_channel_filter.public_primary_key,
"slack": {"channel_id": None},
"slack": {"channel_id": None, "enabled": True},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": False},
},
"heartbeat": {
"link": f"{integration.integration_url}heartbeat/",
@ -232,7 +240,9 @@ def test_update_resolve_signal_template(
"default_route": {
"escalation_chain_id": None,
"id": default_channel_filter.public_primary_key,
"slack": {"channel_id": None},
"slack": {"channel_id": None, "enabled": True},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": False},
},
"heartbeat": {
"link": f"{integration.integration_url}heartbeat/",
@ -337,7 +347,9 @@ def test_update_sms_template_with_empty_dict(
"default_route": {
"escalation_chain_id": None,
"id": default_channel_filter.public_primary_key,
"slack": {"channel_id": None},
"slack": {"channel_id": None, "enabled": True},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": False},
},
"heartbeat": {
"link": f"{integration.integration_url}heartbeat/",
@ -394,7 +406,9 @@ def test_update_integration_name(
"default_route": {
"escalation_chain_id": None,
"id": default_channel_filter.public_primary_key,
"slack": {"channel_id": None},
"slack": {"channel_id": None, "enabled": True},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": False},
},
"heartbeat": {
"link": f"{integration.integration_url}heartbeat/",
@ -454,7 +468,9 @@ def test_set_default_template(
"default_route": {
"escalation_chain_id": None,
"id": default_channel_filter.public_primary_key,
"slack": {"channel_id": None},
"slack": {"channel_id": None, "enabled": True},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": False},
},
"heartbeat": {
"link": f"{integration.integration_url}heartbeat/",

View file

@ -7,6 +7,7 @@ from apps.alerts.models import ChannelFilter
from apps.base.tests.messaging_backend import TestOnlyBackend
TEST_MESSAGING_BACKEND_FIELD = TestOnlyBackend.backend_id.lower()
TEST_MESSAGING_BACKEND_ID = "TESTBACKENDID"
@pytest.fixture()
@ -46,9 +47,9 @@ def test_get_route(
"routing_regex": channel_filter.filtering_term,
"position": channel_filter.order,
"is_the_last_route": channel_filter.is_default,
"slack": {"channel_id": channel_filter.slack_channel_id},
"telegram": {"id": None},
TEST_MESSAGING_BACKEND_FIELD: {"id": None},
"slack": {"channel_id": channel_filter.slack_channel_id, "enabled": True},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": False},
}
assert response.status_code == status.HTTP_200_OK
@ -78,9 +79,9 @@ def test_get_routes_list(
"routing_regex": channel_filter.filtering_term,
"position": channel_filter.order,
"is_the_last_route": channel_filter.is_default,
"slack": {"channel_id": channel_filter.slack_channel_id},
"telegram": {"id": None},
TEST_MESSAGING_BACKEND_FIELD: {"id": None},
"slack": {"channel_id": channel_filter.slack_channel_id, "enabled": True},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": False},
}
],
}
@ -114,9 +115,9 @@ def test_get_routes_filter_by_integration_id(
"routing_regex": channel_filter.filtering_term,
"position": channel_filter.order,
"is_the_last_route": channel_filter.is_default,
"slack": {"channel_id": channel_filter.slack_channel_id},
"telegram": {"id": None},
TEST_MESSAGING_BACKEND_FIELD: {"id": None},
"slack": {"channel_id": channel_filter.slack_channel_id, "enabled": True},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": False},
}
],
}
@ -148,9 +149,9 @@ def test_create_route(
"routing_regex": data_for_create["routing_regex"],
"position": 0,
"is_the_last_route": False,
"slack": {"channel_id": None},
"telegram": {"id": None},
TEST_MESSAGING_BACKEND_FIELD: {"id": None},
"slack": {"channel_id": None, "enabled": True},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": False},
}
assert response.status_code == status.HTTP_201_CREATED
@ -208,9 +209,9 @@ def test_update_route(
"routing_regex": data_to_update["routing_regex"],
"position": new_channel_filter.order,
"is_the_last_route": new_channel_filter.is_default,
"slack": {"channel_id": new_channel_filter.slack_channel_id},
"telegram": {"id": None},
TEST_MESSAGING_BACKEND_FIELD: {"id": None},
"slack": {"channel_id": new_channel_filter.slack_channel_id, "enabled": True},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": False},
}
assert response.status_code == status.HTTP_200_OK
@ -237,3 +238,146 @@ def test_delete_route(
assert response.status_code == status.HTTP_204_NO_CONTENT
with pytest.raises(ChannelFilter.DoesNotExist):
new_channel_filter.refresh_from_db()
@pytest.mark.django_db
def test_create_route_with_messaging_backend(
route_public_api_setup,
make_slack_team_identity,
make_slack_channel,
):
organization, _, token, alert_receive_channel, escalation_chain, _ = route_public_api_setup
slack_team_identity = make_slack_team_identity()
organization.slack_team_identity = slack_team_identity
organization.save(update_fields=["slack_team_identity"])
slack_id = "TEST_SLACK_ID"
slack_channel = make_slack_channel(slack_team_identity, slack_id=slack_id)
client = APIClient()
url = reverse("api-public:routes-list")
data_for_create = {
"integration_id": alert_receive_channel.public_primary_key,
"routing_regex": "testreg",
"escalation_chain_id": escalation_chain.public_primary_key,
"slack": {"channel_id": slack_channel.slack_id, "enabled": True},
"telegram": {"id": None, "enabled": True},
TEST_MESSAGING_BACKEND_FIELD: {"id": TEST_MESSAGING_BACKEND_ID, "enabled": True},
}
response = client.post(url, format="json", HTTP_AUTHORIZATION=token, data=data_for_create)
expected_response = {
"id": response.data["id"],
"integration_id": alert_receive_channel.public_primary_key,
"escalation_chain_id": escalation_chain.public_primary_key,
"routing_regex": data_for_create["routing_regex"],
"position": 0,
"is_the_last_route": False,
"slack": {"channel_id": slack_channel.slack_id, "enabled": True},
"telegram": {"id": None, "enabled": True},
TEST_MESSAGING_BACKEND_FIELD: {"id": TEST_MESSAGING_BACKEND_ID, "enabled": True},
}
assert response.status_code == status.HTTP_201_CREATED
assert response.json() == expected_response
@pytest.mark.django_db
def test_update_route_with_messaging_backend(
route_public_api_setup,
make_channel_filter,
make_slack_team_identity,
make_slack_channel,
):
organization, _, token, alert_receive_channel, escalation_chain, _ = route_public_api_setup
slack_team_identity = make_slack_team_identity()
organization.slack_team_identity = slack_team_identity
organization.save(update_fields=["slack_team_identity"])
slack_id = "TEST_SLACK_ID"
slack_channel = make_slack_channel(slack_team_identity, slack_id=slack_id)
new_channel_filter = make_channel_filter(
alert_receive_channel,
is_default=False,
filtering_term="testreg",
escalation_chain=escalation_chain,
)
client = APIClient()
url = reverse("api-public:routes-detail", kwargs={"pk": new_channel_filter.public_primary_key})
data_to_update = {
"slack": {"channel_id": slack_channel.slack_id, "enabled": False},
"telegram": {"id": None, "enabled": True},
TEST_MESSAGING_BACKEND_FIELD: {"id": TEST_MESSAGING_BACKEND_ID},
}
# check if route data is different
assert new_channel_filter.slack_channel_id != slack_channel.slack_id
assert new_channel_filter.notify_in_slack != data_to_update["slack"]["enabled"]
assert new_channel_filter.notify_in_telegram != data_to_update["telegram"]["enabled"]
assert new_channel_filter.notification_backends is None
response = client.put(url, format="json", HTTP_AUTHORIZATION=token, data=data_to_update)
expected_response = {
"id": response.data["id"],
"integration_id": alert_receive_channel.public_primary_key,
"escalation_chain_id": escalation_chain.public_primary_key,
"routing_regex": new_channel_filter.filtering_term,
"position": 0,
"is_the_last_route": False,
"slack": {"channel_id": slack_channel.slack_id, "enabled": False},
"telegram": {"id": None, "enabled": True},
TEST_MESSAGING_BACKEND_FIELD: {"id": TEST_MESSAGING_BACKEND_ID, "enabled": False},
}
assert response.status_code == status.HTTP_200_OK
assert response.json() == expected_response
new_channel_filter.refresh_from_db()
# check if route data is different was changed correctly
assert new_channel_filter.slack_channel_id == slack_channel.slack_id
assert new_channel_filter.notify_in_slack == data_to_update["slack"]["enabled"]
assert new_channel_filter.notify_in_telegram == data_to_update["telegram"]["enabled"]
assert new_channel_filter.notification_backends == {
TestOnlyBackend.backend_id: {"channel": TEST_MESSAGING_BACKEND_ID}
}
data_to_update = {
"slack": {"channel_id": None, "enabled": False},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": True},
}
response = client.put(url, format="json", HTTP_AUTHORIZATION=token, data=data_to_update)
expected_response = {
"id": response.data["id"],
"integration_id": alert_receive_channel.public_primary_key,
"escalation_chain_id": escalation_chain.public_primary_key,
"routing_regex": new_channel_filter.filtering_term,
"position": 0,
"is_the_last_route": False,
"slack": {"channel_id": None, "enabled": False},
"telegram": {"id": None, "enabled": False},
TEST_MESSAGING_BACKEND_FIELD: {"id": None, "enabled": True},
}
assert response.status_code == status.HTTP_200_OK
assert response.json() == expected_response
new_channel_filter.refresh_from_db()
# check if route data is different was changed correctly
assert new_channel_filter.slack_channel_id == data_to_update["slack"]["channel_id"]
assert new_channel_filter.notify_in_slack == data_to_update["slack"]["enabled"]
assert new_channel_filter.notify_in_telegram == data_to_update["telegram"]["enabled"]
assert new_channel_filter.notification_backends == {TestOnlyBackend.backend_id: {"channel": None, "enabled": True}}