oncall-engine/engine/apps/public_api/tests/test_alerts.py
Michael Derynck 6b40f95033 World, meet OnCall!
Co-authored-by: Eve832 <eve.meelan@grafana.com>
    Co-authored-by: Francisco Montes de Oca <nevermind89x@gmail.com>
    Co-authored-by: Ildar Iskhakov <ildar.iskhakov@grafana.com>
    Co-authored-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>
    Co-authored-by: Julia <ferril.darkdiver@gmail.com>
    Co-authored-by: maskin25 <kengurek@gmail.com>
    Co-authored-by: Matias Bordese <mbordese@gmail.com>
    Co-authored-by: Matvey Kukuy <motakuk@gmail.com>
    Co-authored-by: Michael Derynck <michael.derynck@grafana.com>
    Co-authored-by: Richard Hartmann <richih@richih.org>
    Co-authored-by: Robby Milo <robbymilo@fastmail.com>
    Co-authored-by: Timur Olzhabayev <timur.olzhabayev@grafana.com>
    Co-authored-by: Vadim Stepanov <vadimkerr@gmail.com>
    Co-authored-by: Yulia Shanyrova <yulia.shanyrova@grafana.com>
2022-06-03 08:09:47 -06:00

172 lines
5.8 KiB
Python

import pytest
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient
alert_raw_request_data = {
"evalMatches": [
{"value": 100, "metric": "High value", "tags": None},
{"value": 200, "metric": "Higher Value", "tags": None},
],
"message": "Someone is testing the alert notification within grafana.",
"ruleId": 0,
"ruleName": "Test notification",
"ruleUrl": "http://localhost:3000/",
"state": "alerting",
"title": "[Alerting] Test notification",
}
@pytest.fixture()
def alert_public_api_setup(
make_organization,
make_alert_receive_channel,
make_channel_filter,
):
organization = make_organization()
alert_receive_channel = make_alert_receive_channel(organization)
default_channel_filter = make_channel_filter(alert_receive_channel, is_default=True)
return organization, alert_receive_channel, default_channel_filter
@pytest.mark.django_db
def test_get_list_alerts(
alert_public_api_setup,
make_user_for_organization,
make_public_api_token,
make_alert_group,
make_alert,
):
# https://api-docs.amixr.io/#list-alerts
organization, alert_receive_channel, default_channel_filter = alert_public_api_setup
alert_group = make_alert_group(alert_receive_channel)
alert = make_alert(alert_group, alert_raw_request_data)
admin = make_user_for_organization(organization)
_, token = make_public_api_token(admin, organization)
client = APIClient()
url = reverse("api-public:alerts-list")
response = client.get(url, HTTP_AUTHORIZATION=f"{token}")
expected_response = {
"count": 1,
"next": None,
"previous": None,
"results": [
{
"id": alert.public_primary_key,
"alert_group_id": alert_group.public_primary_key,
"created_at": alert.created_at.isoformat().replace("+00:00", "Z"),
"payload": {
"state": "alerting",
"title": "[Alerting] Test notification",
"ruleId": 0,
"message": "Someone is testing the alert notification within grafana.",
"ruleUrl": "http://localhost:3000/",
"ruleName": "Test notification",
"evalMatches": [
{"tags": None, "value": 100, "metric": "High value"},
{"tags": None, "value": 200, "metric": "Higher Value"},
],
},
}
],
}
assert response.status_code == status.HTTP_200_OK
assert response.json() == expected_response
@pytest.mark.django_db
def test_get_list_alerts_filter_by_incident(
alert_public_api_setup,
make_user_for_organization,
make_public_api_token,
make_alert_group,
make_alert,
):
# https://api-docs.amixr.io/#list-alerts
organization, alert_receive_channel, default_channel_filter = alert_public_api_setup
alert_group = make_alert_group(alert_receive_channel)
make_alert(alert_group, alert_raw_request_data)
admin = make_user_for_organization(organization)
_, token = make_public_api_token(admin, organization)
client = APIClient()
url = reverse("api-public:alerts-list")
response = client.get(
url + f"?alert_group_id={alert_group.public_primary_key}", format="json", HTTP_AUTHORIZATION=f"{token}"
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["count"] == 1
@pytest.mark.django_db
def test_get_list_alerts_filter_by_non_existing_incident(
alert_public_api_setup,
make_user_for_organization,
make_public_api_token,
make_alert_group,
make_alert,
):
organization, alert_receive_channel, default_channel_filter = alert_public_api_setup
alert_group = make_alert_group(alert_receive_channel)
make_alert(alert_group, alert_raw_request_data)
admin = make_user_for_organization(organization)
_, token = make_public_api_token(admin, organization)
client = APIClient()
url = reverse("api-public:alerts-list")
response = client.get(url + "?alert_group_id=invalid_alert_group_id", format="json", HTTP_AUTHORIZATION=f"{token}")
assert response.status_code == status.HTTP_200_OK
assert response.json()["count"] == 0
@pytest.mark.django_db
def test_alerts_search(
alert_public_api_setup,
make_user_for_organization,
make_public_api_token,
make_alert_group,
make_alert,
):
organization, alert_receive_channel, default_channel_filter = alert_public_api_setup
alert_group = make_alert_group(alert_receive_channel)
make_alert(alert_group, alert_raw_request_data)
admin = make_user_for_organization(organization)
_, token = make_public_api_token(admin, organization)
client = APIClient()
url = reverse("api-public:alerts-list")
response = client.get(url + "?search=evalMatches", format="json", HTTP_AUTHORIZATION=f"{token}")
assert response.status_code == status.HTTP_200_OK
assert response.json()["count"] == 1
@pytest.mark.django_db
def test_alerts_search_with_no_results(
alert_public_api_setup,
make_user_for_organization,
make_public_api_token,
make_alert_group,
make_alert,
):
organization, alert_receive_channel, default_channel_filter = alert_public_api_setup
alert_group = make_alert_group(alert_receive_channel)
make_alert(alert_group, alert_raw_request_data)
admin = make_user_for_organization(organization)
_, token = make_public_api_token(admin, organization)
client = APIClient()
url = reverse("api-public:alerts-list")
response = client.get(url + "?search=impossible payload", format="json", HTTP_AUTHORIZATION=f"{token}")
assert response.status_code == status.HTTP_200_OK
assert response.json()["count"] == 0