diff --git a/engine/apps/auth_token/auth.py b/engine/apps/auth_token/auth.py index af1fc2c6b..85126ef64 100644 --- a/engine/apps/auth_token/auth.py +++ b/engine/apps/auth_token/auth.py @@ -361,7 +361,7 @@ def authenticate(self, request): 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 @@ def get_organization(self, request, auth): 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 diff --git a/engine/apps/auth_token/tests/test_grafana_auth.py b/engine/apps/auth_token/tests/test_grafana_auth.py index 3cb01727f..950e63e1f 100644 --- a/engine/apps/auth_token/tests/test_grafana_auth.py +++ b/engine/apps/auth_token/tests/test_grafana_auth.py @@ -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