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>
221 lines
6.7 KiB
Python
221 lines
6.7 KiB
Python
import pytest
|
|
from django.urls import reverse
|
|
from rest_framework import status
|
|
from rest_framework.test import APIClient
|
|
|
|
from apps.alerts.models import ResolutionNote
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_get_resolution_note(
|
|
make_organization_and_user_with_token,
|
|
make_alert_receive_channel,
|
|
make_alert_group,
|
|
make_resolution_note,
|
|
):
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
client = APIClient()
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
|
|
resolution_note = make_resolution_note(
|
|
alert_group=alert_group,
|
|
source=ResolutionNote.Source.WEB,
|
|
author=user,
|
|
)
|
|
|
|
url = reverse("api-public:resolution_notes-detail", kwargs={"pk": resolution_note.public_primary_key})
|
|
|
|
response = client.get(url, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
result = {
|
|
"id": resolution_note.public_primary_key,
|
|
"alert_group_id": alert_group.public_primary_key,
|
|
"author": user.public_primary_key,
|
|
"source": resolution_note.get_source_display(),
|
|
"created_at": response.data["created_at"],
|
|
"text": resolution_note.text,
|
|
}
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert response.data == result
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_create_resolution_note(make_organization_and_user_with_token, make_alert_receive_channel, make_alert_group):
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
client = APIClient()
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
|
|
url = reverse("api-public:resolution_notes-list")
|
|
|
|
data = {
|
|
"alert_group_id": alert_group.public_primary_key,
|
|
"text": "Test Resolution Note Message",
|
|
}
|
|
|
|
response = client.post(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
resolution_note = ResolutionNote.objects.get(public_primary_key=response.data["id"])
|
|
|
|
result = {
|
|
"id": resolution_note.public_primary_key,
|
|
"alert_group_id": alert_group.public_primary_key,
|
|
"author": user.public_primary_key,
|
|
"source": resolution_note.get_source_display(),
|
|
"created_at": response.data["created_at"],
|
|
"text": data["text"],
|
|
}
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
|
assert response.data == result
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_create_resolution_note_invalid_text(
|
|
make_organization_and_user_with_token,
|
|
make_alert_receive_channel,
|
|
make_alert_group,
|
|
):
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
client = APIClient()
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
|
|
url = reverse("api-public:resolution_notes-list")
|
|
|
|
data = {
|
|
"alert_group_id": alert_group.public_primary_key,
|
|
"text": "",
|
|
}
|
|
|
|
response = client.post(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
assert response.data["text"][0] == "This field may not be blank."
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_update_resolution_note(
|
|
make_organization_and_user_with_token,
|
|
make_alert_receive_channel,
|
|
make_alert_group,
|
|
make_resolution_note,
|
|
):
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
client = APIClient()
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
|
|
resolution_note = make_resolution_note(
|
|
alert_group=alert_group,
|
|
source=ResolutionNote.Source.WEB,
|
|
author=user,
|
|
)
|
|
|
|
url = reverse("api-public:resolution_notes-detail", kwargs={"pk": resolution_note.public_primary_key})
|
|
|
|
data = {
|
|
"text": "Test Resolution Note Message",
|
|
}
|
|
|
|
assert resolution_note.text != data["text"]
|
|
|
|
response = client.put(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
result = {
|
|
"id": resolution_note.public_primary_key,
|
|
"alert_group_id": alert_group.public_primary_key,
|
|
"author": user.public_primary_key,
|
|
"source": resolution_note.get_source_display(),
|
|
"created_at": response.data["created_at"],
|
|
"text": data["text"],
|
|
}
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
resolution_note.refresh_from_db()
|
|
assert resolution_note.text == result["text"]
|
|
assert response.data == result
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_update_resolution_note_invalid_source(
|
|
make_organization_and_user_with_token,
|
|
make_alert_receive_channel,
|
|
make_alert_group,
|
|
make_resolution_note,
|
|
):
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
client = APIClient()
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
|
|
resolution_note = make_resolution_note(
|
|
alert_group=alert_group,
|
|
source=ResolutionNote.Source.SLACK,
|
|
author=user,
|
|
)
|
|
|
|
url = reverse("api-public:resolution_notes-detail", kwargs={"pk": resolution_note.public_primary_key})
|
|
|
|
data = {
|
|
"text": "Test Resolution Note Message",
|
|
}
|
|
|
|
assert resolution_note.message_text != data["text"]
|
|
|
|
response = client.put(url, data=data, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
|
resolution_note.refresh_from_db()
|
|
assert resolution_note.message_text != data["text"]
|
|
assert response.data["detail"] == "Cannot update message with this source type"
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_delete_resolution_note(
|
|
make_organization_and_user_with_token,
|
|
make_alert_receive_channel,
|
|
make_alert_group,
|
|
make_resolution_note,
|
|
):
|
|
|
|
organization, user, token = make_organization_and_user_with_token()
|
|
client = APIClient()
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
|
|
resolution_note = make_resolution_note(
|
|
alert_group=alert_group,
|
|
source=ResolutionNote.Source.WEB,
|
|
author=user,
|
|
)
|
|
|
|
url = reverse("api-public:resolution_notes-detail", kwargs={"pk": resolution_note.public_primary_key})
|
|
|
|
assert resolution_note.deleted_at is None
|
|
|
|
response = client.delete(url, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
assert response.status_code == status.HTTP_204_NO_CONTENT
|
|
|
|
resolution_note.refresh_from_db()
|
|
|
|
assert resolution_note.deleted_at is not None
|
|
|
|
response = client.get(url, format="json", HTTP_AUTHORIZATION=f"{token}")
|
|
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
assert response.data["detail"] == "Not found."
|