oncall-engine/engine/apps/grafana_plugin/tests/test_ui_url_builder.py
Joey Orlando bfcc0b9f29
update URLs constructed by the backend to support IRM plugin (#5137)
# What this PR does

Introduces a new class,
`apps.grafana_plugin.ui_url_builder.UIURLBuilder`, which is responsible
for... building UI URLs (😄). The class mainly does two things:
- it will decide if the URL should point to `grafana-oncall-app` or
`grafana-irm-app` based on the value of
`organization.is_grafana_irm_enabled` (**NOTE**: this value isn't yet
being set + defaults to `False`; logic for setting this value will be
done in a subsequent PR)
- Adds `enum`s, `OnCallPage` and `IncidentPage` to DRYify hardcoded UI
URLs (in case we decide to change these slightly in the near future)

## 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] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
2024-10-09 08:55:10 -04:00

105 lines
3.4 KiB
Python

import pytest
from apps.grafana_plugin.ui_url_builder import UIURLBuilder
from common.constants.plugin_ids import PluginID
GRAFANA_URL = "http://example.com"
ALERT_GROUP_ID = "1234"
INTEGRATION_ID = "5678"
SCHEDULE_ID = "lasdfasdf"
PATH_EXTRA = "/extra?foo=bar"
@pytest.fixture
def org_setup(make_organization):
def _org_setup(is_grafana_irm_enabled=False):
return make_organization(grafana_url=GRAFANA_URL, is_grafana_irm_enabled=is_grafana_irm_enabled)
return _org_setup
@pytest.mark.parametrize(
"func,call_kwargs,expected_url",
[
# oncall pages
(
"home",
{"path_extra": PATH_EXTRA},
f"{GRAFANA_URL}/a/{PluginID.ONCALL}{PATH_EXTRA}",
),
(
"alert_groups",
{"path_extra": PATH_EXTRA},
f"{GRAFANA_URL}/a/{PluginID.ONCALL}/alert-groups{PATH_EXTRA}",
),
(
"alert_group_detail",
{"id": ALERT_GROUP_ID, "path_extra": PATH_EXTRA},
f"{GRAFANA_URL}/a/{PluginID.ONCALL}/alert-groups/{ALERT_GROUP_ID}{PATH_EXTRA}",
),
(
"integration_detail",
{"id": INTEGRATION_ID, "path_extra": PATH_EXTRA},
f"{GRAFANA_URL}/a/{PluginID.ONCALL}/integrations/{INTEGRATION_ID}{PATH_EXTRA}",
),
(
"schedules",
{"path_extra": PATH_EXTRA},
f"{GRAFANA_URL}/a/{PluginID.ONCALL}/schedules{PATH_EXTRA}",
),
(
"schedule_detail",
{"id": SCHEDULE_ID, "path_extra": PATH_EXTRA},
f"{GRAFANA_URL}/a/{PluginID.ONCALL}/schedules/{SCHEDULE_ID}{PATH_EXTRA}",
),
(
"users",
{"path_extra": PATH_EXTRA},
f"{GRAFANA_URL}/a/{PluginID.ONCALL}/users{PATH_EXTRA}",
),
(
"user_profile",
{"path_extra": PATH_EXTRA},
f"{GRAFANA_URL}/a/{PluginID.ONCALL}/users/me{PATH_EXTRA}",
),
(
"chatops",
{"path_extra": PATH_EXTRA},
f"{GRAFANA_URL}/a/{PluginID.ONCALL}/chat-ops{PATH_EXTRA}",
),
(
"settings",
{"path_extra": PATH_EXTRA},
f"{GRAFANA_URL}/a/{PluginID.ONCALL}/settings{PATH_EXTRA}",
),
# incident pages
(
"declare_incident",
{"path_extra": "?caption=abcd&url=asdf&title=test1234"},
f"{GRAFANA_URL}/a/{PluginID.INCIDENT}/incidents/declare?caption=abcd&url=asdf&title=test1234",
),
],
)
@pytest.mark.django_db
def test_build_page_urls(org_setup, func, call_kwargs, expected_url):
builder = UIURLBuilder(org_setup())
assert getattr(builder, func)(**call_kwargs) == expected_url
@pytest.mark.django_db
def test_build_url_overriden_base_url(org_setup):
overriden_base_url = "http://overriden.com"
builder = UIURLBuilder(org_setup(), base_url=overriden_base_url)
assert builder.chatops() == f"{overriden_base_url}/a/{PluginID.ONCALL}/chat-ops"
@pytest.mark.parametrize(
"is_grafana_irm_enabled,expected_url",
[
(True, f"{GRAFANA_URL}/a/{PluginID.IRM}/alert-groups/{ALERT_GROUP_ID}"),
(False, f"{GRAFANA_URL}/a/{PluginID.ONCALL}/alert-groups/{ALERT_GROUP_ID}"),
],
)
@pytest.mark.django_db
def test_build_url_works_for_irm_and_oncall_plugins(org_setup, is_grafana_irm_enabled, expected_url):
assert UIURLBuilder(org_setup(is_grafana_irm_enabled)).alert_group_detail(ALERT_GROUP_ID) == expected_url