Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return API URL as part of status #2791

Merged
merged 10 commits into from
Aug 22, 2023
Prev Previous commit
Next Next commit
Cleanup tests
  • Loading branch information
mderynck committed Aug 15, 2023
commit ebeef7970f9e441f8dd427a35e744577239a4b61
7 changes: 0 additions & 7 deletions engine/apps/auth_token/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,6 @@ def _get_user(request: Request, organization: Organization) -> User:
except User.DoesNotExist:
return None

@classmethod
def is_user_from_request_present_in_organization(cls, request: Request, organization: Organization) -> bool:
try:
return cls._get_user(request, organization) is not None
except exceptions.AuthenticationFailed:
return False


class PluginAuthentication(BasePluginAuthentication):
@staticmethod
Expand Down
55 changes: 30 additions & 25 deletions engine/apps/grafana_plugin/tests/test_status.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
from unittest.mock import patch

import pytest
from django.test import override_settings
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient

from apps.user_management.models import Organization

GRAFANA_TOKEN = "TESTTOKEN"
GRAFANA_URL = "hello.com"
LICENSE = "asdfasdf"
VERSION = "asdfasdfasdf"
BASE_URL = "http://asdasdqweqweqw.com/oncall"
GRAFANA_CONTEXT_DATA = {"IsAnonymous": False}
SETTINGS = {"LICENSE": LICENSE, "VERSION": VERSION}
SETTINGS = {"LICENSE": LICENSE, "VERSION": VERSION, "BASE_URL": BASE_URL}


def _check_status_response(auth_headers, client):
response = client.post(reverse("grafana-plugin:status"), format="json", **auth_headers)
response_data = response.data
assert response.status_code == status.HTTP_200_OK
assert response_data["token_ok"] is True
assert response_data["is_installed"] is True
assert response_data["allow_signup"] is True
assert response_data["is_user_anonymous"] is False
assert response_data["license"] == LICENSE
assert response_data["version"] == VERSION
assert response_data["api_url"] == BASE_URL


@pytest.mark.django_db
@override_settings(**SETTINGS)
@patch("apps.grafana_plugin.views.status.GrafanaAPIClient")
def test_token_ok_is_based_on_grafana_api_check_token_response(
mocked_grafana_api_client, make_organization_and_user_with_plugin_token, make_user_auth_headers
make_organization_and_user_with_plugin_token, make_user_auth_headers
):
mocked_grafana_api_client.return_value.check_token.return_value = (None, {"connected": True})

organization, user, token = make_organization_and_user_with_plugin_token()
organization.grafana_url = GRAFANA_URL
organization.save(update_fields=["grafana_url"])
organization.api_token_status = Organization.API_TOKEN_STATUS_OK
organization.save(update_fields=["grafana_url", "api_token_status"])

client = APIClient()
url = reverse("grafana-plugin:status")
response = client.post(url)
assert response.status_code == status.HTTP_403_FORBIDDEN

auth_headers = make_user_auth_headers(
user, token, grafana_token=GRAFANA_TOKEN, grafana_context_data=GRAFANA_CONTEXT_DATA
)
response = client.get(reverse("grafana-plugin:status"), format="json", **auth_headers)
response_data = response.data

assert response.status_code == status.HTTP_200_OK
assert response_data["token_ok"] is True
assert response_data["is_installed"] is True
assert response_data["allow_signup"] is True
assert response_data["is_user_anonymous"] is False
assert response_data["license"] == LICENSE
assert response_data["version"] == VERSION

assert mocked_grafana_api_client.called_once_with(api_url=GRAFANA_URL, api_token=GRAFANA_TOKEN)
assert mocked_grafana_api_client.return_value.check_token.called_once_with()
_check_status_response(auth_headers, client)


@pytest.mark.django_db
Expand All @@ -65,22 +69,23 @@ def test_allow_signup(make_organization_and_user_with_plugin_token, make_user_au


@pytest.mark.django_db
@override_settings(**SETTINGS)
def test_status_mobile_app_auth_token(
make_organization_and_user_with_mobile_app_auth_token,
make_user_auth_headers,
):
organization, user, auth_token = make_organization_and_user_with_mobile_app_auth_token()
organization.grafana_url = GRAFANA_URL
organization.api_token_status = Organization.API_TOKEN_STATUS_OK
organization.save(update_fields=["grafana_url", "api_token_status"])

client = APIClient()
url = reverse("grafana-plugin:status")

response = client.post(url)
assert response.status_code == status.HTTP_403_FORBIDDEN

auth_headers = make_user_auth_headers(
user,
auth_token,
)

response = client.post(url, format="json", **auth_headers)
assert response.status_code == status.HTTP_200_OK
_check_status_response(auth_headers, client)
10 changes: 3 additions & 7 deletions engine/apps/grafana_plugin/views/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ def post(self, request: Request) -> Response:
name="allow_plugin_organization_signup", defaults={"boolean_value": True}
)[0].boolean_value

# Check if current user is in OnCall database
user_is_present_in_org = BasePluginAuthentication.is_user_from_request_present_in_organization(
request, organization
)
# If user is not present in OnCall database, set token_ok to False, which will trigger reinstall
if not user_is_present_in_org:
if not request.user:
token_ok = False
organization.api_token_status = Organization.API_TOKEN_STATUS_PENDING
organization.save(update_fields=["api_token_status"])
Expand All @@ -69,7 +65,7 @@ def post(self, request: Request) -> Response:
"is_installed": is_installed,
"token_ok": token_ok,
"allow_signup": allow_signup,
"is_user_anonymous": self.grafana_context.get("IsAnonymous", False),
"is_user_anonymous": self.grafana_context.get("IsAnonymous", request.user is None),
"license": settings.LICENSE,
"version": settings.VERSION,
"recaptcha_site_key": settings.RECAPTCHA_V3_SITE_KEY,
Expand Down Expand Up @@ -102,7 +98,7 @@ def get(self, _request: Request) -> Response:
"is_installed": is_installed,
"token_ok": token_ok,
"allow_signup": allow_signup,
"is_user_anonymous": self.grafana_context.get("IsAnonymous", False),
"is_user_anonymous": self.grafana_context.get("IsAnonymous", _request.user is None),
"license": settings.LICENSE,
"version": settings.VERSION,
}
Expand Down
5 changes: 1 addition & 4 deletions engine/apps/grafana_plugin/views/sync.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ def post(self, request: Request) -> Response:
if organization.api_token_status == Organization.API_TOKEN_STATUS_OK:
is_installed = True

user_is_present_in_org = BasePluginAuthentication.is_user_from_request_present_in_organization(
request, organization
)
if not user_is_present_in_org:
if not request.auth.user:
organization.api_token_status = Organization.API_TOKEN_STATUS_PENDING
organization.save(update_fields=["api_token_status"])

Expand Down