Skip to content

Commit

Permalink
Add unit tests for generic oidc
Browse files Browse the repository at this point in the history
  - Fixed typo
  - Added unit tests cases
  • Loading branch information
palash247 committed Mar 26, 2024
1 parent 3853b3d commit 6649774
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions redash/authentication/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def get_oidc_config(url):
return None
return resp.json()

Check warning on line 32 in redash/authentication/oidc.py

View check run for this annotation

Codecov / codecov/patch

redash/authentication/oidc.py#L31-L32

Added lines #L31 - L32 were not covered by tests

oidc_config = get_oidc_config(settings.OIDC_COFIGURATION_URL)
oidc_config = get_oidc_config(settings.OIDC_ISSUER_URL)
oauth = OAuth(app)
oauth.register(

Check warning on line 36 in redash/authentication/oidc.py

View check run for this annotation

Codecov / codecov/patch

redash/authentication/oidc.py#L34-L36

Added lines #L34 - L36 were not covered by tests
name="oidc",
server_metadata_url=settings.OIDC_COFIGURATION_URL,
server_metadata_url=settings.OIDC_ISSUER_URL,
client_kwargs={
"scope": "openid email profile",
},
Expand Down
4 changes: 2 additions & 2 deletions redash/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@

OIDC_CLIENT_ID = os.environ.get("REDASH_OIDC_CLIENT_ID", "")
OIDC_CLIENT_SECRET = os.environ.get("REDASH_OIDC_CLIENT_SECRET", "")
OIDC_COFIGURATION_URL = os.environ.get("REDASH_OIDC_COFIGURATION_URL", "")
OIDC_ENABLED = bool(OIDC_CLIENT_ID and OIDC_CLIENT_SECRET and OIDC_COFIGURATION_URL)
OIDC_ISSUER_URL = os.environ.get("REDASH_OIDC_ISSUER_URL", "")
OIDC_ENABLED = bool(OIDC_CLIENT_ID and OIDC_CLIENT_SECRET and OIDC_ISSUER_URL)

# If Redash is behind a proxy it might sometimes receive a X-Forwarded-Proto of HTTP
# even if your actual Redash URL scheme is HTTPS. This will cause Flask to build
Expand Down
27 changes: 27 additions & 0 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
get_login_url,
hmac_load_user_from_request,
jwt_auth,
oidc,
org_settings,
sign,
)
Expand Down Expand Up @@ -177,6 +178,32 @@ def test_prefers_api_key_over_session_user_id(self):
self.assertEqual(rv.status_code, 200)


class TestCreateAndLoginUserOIDC(BaseTestCase):
def test_logins_valid_user(self):
user = self.factory.create_user(email="test@example.com")
with patch("redash.authentication.login_user") as login_user_mock:
oidc.create_and_login_user(self.factory.org, user.name, user.email)
login_user_mock.assert_called_once_with(user, remember=True)

def test_creates_vaild_new_user(self):
email = "test@example.com"
name = "Test User"

with patch("redash.authentication.login_user") as login_user_mock:
oidc.create_and_login_user(self.factory.org, name, email)

self.assertTrue(login_user_mock.called)
user = models.User.query.filter(models.User.email == email).one()
self.assertEqual(user.email, email)

def test_updates_user_name(self):
user = self.factory.create_user(email="test@example.com")

with patch("redash.authentication.login_user") as login_user_mock:
create_and_login_user(self.factory.org, "New Name", user.email)
login_user_mock.assert_called_once_with(user, remember=True)


class TestCreateAndLoginUser(BaseTestCase):
def test_logins_valid_user(self):
user = self.factory.create_user(email="test@example.com")
Expand Down

0 comments on commit 6649774

Please sign in to comment.