From ec7b8f1f85861cf97679230c64c7bcb6df1981ee Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Wed, 18 Dec 2024 16:49:54 -0300 Subject: [PATCH] Return a throttled response if org is being synced for the first time --- engine/apps/auth_token/auth.py | 3 +++ engine/apps/auth_token/tests/test_grafana_auth.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/engine/apps/auth_token/auth.py b/engine/apps/auth_token/auth.py index 36ea8d82c..f96e5ef6f 100644 --- a/engine/apps/auth_token/auth.py +++ b/engine/apps/auth_token/auth.py @@ -381,6 +381,9 @@ def get_organization(self, request, auth): # if organization exists, we are good) setup_organization(url, auth) organization = Organization.objects.filter(grafana_url=url).first() + if organization is None: + # sync may still be in progress, client should retry + raise exceptions.Throttled(detail="Organization being synced, please retry.") return organization if settings.LICENSE == settings.CLOUD_LICENSE_NAME: diff --git a/engine/apps/auth_token/tests/test_grafana_auth.py b/engine/apps/auth_token/tests/test_grafana_auth.py index 8da611a0f..228316454 100644 --- a/engine/apps/auth_token/tests/test_grafana_auth.py +++ b/engine/apps/auth_token/tests/test_grafana_auth.py @@ -110,9 +110,9 @@ def test_grafana_authentication_no_org_grafana_url(): request_sync_url = f"{grafana_url}/api/plugins/{PluginID.ONCALL}/resources/plugin/sync?wait=true&force=true" httpretty.register_uri(httpretty.POST, request_sync_url, status=404) - with pytest.raises(exceptions.AuthenticationFailed) as exc: + with pytest.raises(exceptions.Throttled) as exc: GrafanaServiceAccountAuthentication().authenticate(request) - assert exc.value.detail == "Organization not found." + assert exc.value.detail == "Organization being synced, please retry." @pytest.mark.parametrize("grafana_url", ["null;", "foo", ""])