2022-06-03 08:09:47 -06:00
|
|
|
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)
|
2023-06-29 14:52:30 +02:00
|
|
|
alert_1 = make_alert(alert_group, alert_raw_request_data)
|
|
|
|
|
alert_2 = make_alert(alert_group, alert_raw_request_data)
|
2022-06-03 08:09:47 -06:00
|
|
|
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 = {
|
2023-06-29 14:52:30 +02:00
|
|
|
"count": 2,
|
2022-06-03 08:09:47 -06:00
|
|
|
"next": None,
|
|
|
|
|
"previous": None,
|
|
|
|
|
"results": [
|
|
|
|
|
{
|
2023-06-29 14:52:30 +02:00
|
|
|
"id": alert_2.public_primary_key,
|
2022-06-03 08:09:47 -06:00
|
|
|
"alert_group_id": alert_group.public_primary_key,
|
2023-06-29 14:52:30 +02:00
|
|
|
"created_at": alert_2.created_at.isoformat().replace("+00:00", "Z"),
|
2022-06-03 08:09:47 -06:00
|
|
|
"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"},
|
|
|
|
|
],
|
|
|
|
|
},
|
2023-06-29 14:52:30 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": alert_1.public_primary_key,
|
|
|
|
|
"alert_group_id": alert_group.public_primary_key,
|
|
|
|
|
"created_at": alert_1.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"},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-06-03 08:09:47 -06:00
|
|
|
],
|
augment API response pagination attributes (#2471)
# What this PR does
This PR:
- adds a few attributes to paginated API responses
- removes channel filter "send demo alert" internal API endpoint + tests
(this endpoint was marked as deprecated + not consumed by the web UI)
With the new paginated API response schema, the web UI will no longer
need to:
- hardcode `ITEMS_PER_PAGE` for each table
- manually calculate total number of pages
(these two things ☝️ will be done in
https://github.com/grafana/oncall/issues/2476)
For `GET /api/internal/v1/alertgroups` the response will now look like
this:
```diff
{
"next": <url> | None,
"previous": <url> | None,
"results": [],
++ "page_size": <int>
}
```
For all other paginated API responses, the response will now look like:
```diff
{
"count": <int>,
"next": <url> | None,
"previous": <url> | None,
"results": [],
++ "page_size": <int>,
++ "current_page_number": <int>,
++ "total_pages": <int>
}
```
## TODO
- [x] update public API docs to include these new attributes
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-07-14 17:19:40 +02:00
|
|
|
"current_page_number": 1,
|
|
|
|
|
"page_size": 50,
|
|
|
|
|
"total_pages": 1,
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
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
|