oncall-engine/engine/apps/grafana_plugin/tests/test_install.py
Yulya Artyukhina 381520ee13
Get rid of installation token + add a bunch of tests (#624)
* Get rid of installation token (for OSS installations)

This is done by being required to supply the grafana API URL as an
environment variable on the backend. Additionally, optionally an OnCall
API URL environment variable can be passed in to the frontend (this basically
allows completely skipping the need to configure anything).
- deduplicated a lot of the sync logic on the frontend + made
error message more useful and consistent
- Split PluginConfigPage component into several subcomponents
(making it easier to test each individual component)
- Moved RootWithLoader (from plugin/GrafanaPluginRootPage) into its own
subcomponent (making it easier to test)
- Added tests for pre-existing components that were touched:
  - PluginConfigPage component (and its new subcomponents)
  - state/plugin and state/rootBaseStore functions
  - apps.grafana_plugin django app

Helm changes:
- add GRAFANA_API_URL to oncall.env
- some yaml autoformatting changes
- remove reference to python manage.py issue_invite_for_the_frontend --override

Co-authored-by: Joey Orlando <joseph.t.orlando@gmail.com>
2022-11-21 16:26:00 +01:00

27 lines
1,008 B
Python

from unittest.mock import patch
import pytest
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient
GRAFANA_TOKEN = "TESTTOKEN"
@patch("apps.grafana_plugin.views.install.sync_organization", return_value=None)
@pytest.mark.django_db
def test_it_triggers_an_organization_sync_and_saves_the_grafana_token(
mocked_sync_organization, make_organization_and_user_with_plugin_token, make_user_auth_headers
):
organization, user, token = make_organization_and_user_with_plugin_token()
client = APIClient()
auth_headers = make_user_auth_headers(user, token, grafana_token=GRAFANA_TOKEN)
response = client.post(reverse("grafana-plugin:install"), format="json", **auth_headers)
assert response.status_code == status.HTTP_204_NO_CONTENT
assert mocked_sync_organization.called_once_with(organization)
# make sure api token is saved on the org
organization.refresh_from_db()
assert organization.api_token == GRAFANA_TOKEN