Skip to content

Commit

Permalink
feat: add the possibility to switch realms
Browse files Browse the repository at this point in the history
This requires the token to be valid for the new realm
as well.
  • Loading branch information
derlin committed Mar 21, 2024
1 parent cfdc0b5 commit a6a775a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
5 changes: 5 additions & 0 deletions mantelo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def base_url(self):
def realm_name(self):
return self._store["base_url"].split("/realms/")[1]

@realm_name.setter
def realm_name(self, realm_name):
base_url = self._store["base_url"].split("/realms/")[0]
self._store["base_url"] = f"{base_url}/realms/{realm_name}"

@classmethod
def create(
cls,
Expand Down
33 changes: 26 additions & 7 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ def test_password_connection(with_custom_session):
assert constants.TEST_USER in resp


@pytest.mark.integration
def test_client_connection():
adm = KeycloakAdmin.from_service_account(
server_url=constants.TEST_SERVER_URL,
realm_name=constants.TEST_REALM,
client_id=constants.TEST_CLIENT_ID,
client_secret=constants.TEST_CLIENT_SECRET,
)

resp = [u["username"] for u in adm.users.get()]
assert constants.TEST_USER in resp


@pytest.mark.integration
def test_different_auth_realm(openid_connection_admin):
adm = KeycloakAdmin.from_credentials(
Expand All @@ -79,13 +92,19 @@ def test_different_auth_realm(openid_connection_admin):


@pytest.mark.integration
def test_client_connection():
adm = KeycloakAdmin.from_service_account(
server_url=constants.TEST_SERVER_URL,
realm_name=constants.TEST_REALM,
client_id=constants.TEST_CLIENT_ID,
client_secret=constants.TEST_CLIENT_SECRET,
)
def test_switch_realm(openid_connection_admin):
adm = KeycloakAdmin.create(connection=openid_connection_admin)
assert openid_connection_admin.realm_name == constants.MASTER_REALM
assert adm.realm_name == constants.MASTER_REALM

resp = [u["username"] for u in adm.users.get()]
assert constants.TEST_MASTER_USER in resp

# Switch to another realm
adm.realm_name = constants.TEST_REALM

assert adm.realm_name == constants.TEST_REALM
assert openid_connection_admin.realm_name == constants.MASTER_REALM

resp = [u["username"] for u in adm.users.get()]
assert constants.TEST_USER in resp
Expand Down

0 comments on commit a6a775a

Please sign in to comment.