From 1fad874deeb5da022c3cdbebefe80abdcfddd47b Mon Sep 17 00:00:00 2001 From: Arik Fraimovich Date: Mon, 12 Jun 2017 09:47:08 +0300 Subject: [PATCH] Change: redirect to / when org not found --- redash/authentication/__init__.py | 6 +++--- redash/authentication/org_resolving.py | 2 +- tests/test_authentication.py | 18 +++++++++++++++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/redash/authentication/__init__.py b/redash/authentication/__init__.py index ddbbedc8e..1d75296f9 100644 --- a/redash/authentication/__init__.py +++ b/redash/authentication/__init__.py @@ -16,7 +16,9 @@ logger = logging.getLogger('authentication') def get_login_url(external=False, next="/"): - if settings.MULTI_ORG: + if settings.MULTI_ORG and current_org == None: + login_url = '/' + elif settings.MULTI_ORG: login_url = url_for('redash.login', org_slug=current_org.slug, next=next, _external=external) else: login_url = url_for('redash.login', next=next, _external=external) @@ -155,5 +157,3 @@ def setup_authentication(app): else: logger.warning("Unknown authentication type ({}). Using default (HMAC).".format(settings.AUTH_TYPE)) login_manager.request_loader(hmac_load_user_from_request) - - diff --git a/redash/authentication/org_resolving.py b/redash/authentication/org_resolving.py index a79d4945a..52f7309cd 100644 --- a/redash/authentication/org_resolving.py +++ b/redash/authentication/org_resolving.py @@ -7,7 +7,7 @@ single Organization in your installation. import logging -from flask import request, g +from flask import g, request from werkzeug.local import LocalProxy from redash.models import Organization diff --git a/tests/test_authentication.py b/tests/test_authentication.py index c06d6e78e..f527e94c3 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -2,12 +2,14 @@ import time from flask import request from mock import patch -from redash import models +from tests import BaseTestCase + +from redash import models, settings from redash.authentication import (api_key_load_user_from_request, - hmac_load_user_from_request, sign) + get_login_url, hmac_load_user_from_request, + sign) from redash.authentication.google_oauth import (create_and_login_user, verify_profile) -from tests import BaseTestCase class TestApiKeyAuthentication(BaseTestCase): @@ -169,3 +171,13 @@ class TestVerifyProfile(BaseTestCase): self.factory.create_user(email='arik@example.com') self.factory.org.settings[models.Organization.SETTING_GOOGLE_APPS_DOMAINS] = ['example.org'] self.assertTrue(verify_profile(self.factory.org, profile)) + + +class TestGetLoginUrl(BaseTestCase): + def test_when_multi_org_enabled_and_org_exists(self): + with self.app.test_request_context('/{}/'.format(self.factory.org.slug)): + self.assertEqual(get_login_url(next=None), '/{}/login'.format(self.factory.org.slug)) + + def test_when_multi_org_enabled_and_org_doesnt_exist(self): + with self.app.test_request_context('/{}_notexists/'.format(self.factory.org.slug)): + self.assertEqual(get_login_url(next=None), '/')