chore: update service account token auth organization setup check (#5354)

Ignore setup organization response (for now, since it can return a 400
when a sync is/was recently in progress) and base response on
organization being available or not instead.
This commit is contained in:
Matias Bordese 2024-12-11 11:50:49 -03:00 committed by GitHub
parent 59f531e967
commit ec874440ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 6 deletions

View file

@ -361,7 +361,7 @@ class GrafanaServiceAccountAuthentication(BaseAuthentication):
organization = self.get_organization(request, auth)
if not organization:
raise exceptions.AuthenticationFailed("Invalid organization.")
raise exceptions.AuthenticationFailed("Organization not found.")
if organization.is_moved:
raise OrganizationMovedException(organization)
if organization.deleted_at:
@ -374,9 +374,10 @@ class GrafanaServiceAccountAuthentication(BaseAuthentication):
if grafana_url:
organization = Organization.objects.filter(grafana_url=grafana_url).first()
if not organization:
success = setup_organization(grafana_url, auth)
if not success:
raise exceptions.AuthenticationFailed("Invalid Grafana URL.")
# trigger a request to sync the organization
# (ignore response since we can get a 400 if sync was already triggered;
# if organization exists, we are good)
setup_organization(grafana_url, auth)
organization = Organization.objects.filter(grafana_url=grafana_url).first()
return organization

View file

@ -93,7 +93,7 @@ def test_grafana_authentication_missing_org():
with pytest.raises(exceptions.AuthenticationFailed) as exc:
GrafanaServiceAccountAuthentication().authenticate(request)
assert exc.value.detail == "Invalid organization."
assert exc.value.detail == "Organization not found."
@pytest.mark.django_db
@ -112,7 +112,7 @@ def test_grafana_authentication_invalid_grafana_url():
with pytest.raises(exceptions.AuthenticationFailed) as exc:
GrafanaServiceAccountAuthentication().authenticate(request)
assert exc.value.detail == "Invalid Grafana URL."
assert exc.value.detail == "Organization not found."
@pytest.mark.django_db