Skip to content

Commit

Permalink
make sure to use changed preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
BerndSchuller committed Mar 20, 2024
1 parent c2a31e2 commit 43b5ec2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ pyunicore.egg-info/
.coverage
build/
/.pytest_cache/
.env
.vscode
26 changes: 20 additions & 6 deletions pyunicore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ def __init__(
self.verify = verify
self.use_security_sessions = use_security_sessions
self.last_session_id = None
self.preferences = None
self._preferences = None
self.timeout = timeout
self.settings_changed = True

def _clone(self):
"""create a copy of this transport"""
tr = Transport(self.credential)
tr.preferences = self.preferences
tr._preferences = self._preferences
tr.use_security_sessions = self.use_security_sessions
tr.last_session_id = self.last_session_id
tr.timeout = self.timeout
Expand All @@ -120,15 +121,25 @@ def _headers(self, kwargs):
if self.use_security_sessions and self.last_session_id is not None:
headers["X-UNICORE-SecuritySession"] = self.last_session_id

if self.preferences is not None:
headers["X-UNICORE-User-Preferences"] = self.preferences
if self._preferences is not None:
headers["X-UNICORE-User-Preferences"] = self._preferences

if "headers" in kwargs:
headers.update(kwargs["headers"])
del kwargs["headers"]

return headers

@property
def preferences(self):
return self._preferences

@preferences.setter
def preferences(self, value):
self._preferences = value
self.last_session_id = None
self.settings_changed = True

def check_error(self, res):
"""checks for error and extracts any error info sent by the server"""
if 400 <= res.status_code < 600:
Expand Down Expand Up @@ -163,6 +174,7 @@ def run_method(self, method, **args):
self.check_error(res)
if self.use_security_sessions:
self.last_session_id = res.headers.get("X-UNICORE-SecuritySession", None)
self.settings_changed = False
return res

def get(self, to_json=True, **kwargs):
Expand Down Expand Up @@ -221,8 +233,10 @@ def __init__(self, security, resource_url, cache_time=_DEFAULT_CACHE_TIME):
def properties(self):
"""get resource properties (these are cached for cache_time seconds)"""
now = datetime.now()
if self.cache_time <= 0 or (
timedelta(seconds=self.cache_time) < now - self._last_retrieved
if (
self.transport.settings_changed
or self.cache_time <= 0
or (timedelta(seconds=self.cache_time) < now - self._last_retrieved)
):
self._last_properties = self.transport.get(url=self.resource_url)
self._last_retrieved = now
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ def test_username_auth(self):
print(json.dumps(client.access_info(), indent=2))
self.assertEqual("user", client.properties["client"]["role"]["selected"])

def test_transport_settings(self):
print("*** test_transport_settings")
client = self.get_client()
ai = client.access_info()
print(json.dumps(ai, indent=2))
grps = ai["xlogin"]["availableGroups"]
for grp in grps:
client.transport.preferences = f"group:{grp}"
ai = client.access_info()
print("Selected group:", ai["xlogin"]["group"])
self.assertEqual(grp, ai["xlogin"]["group"])

def test_anonymous_info(self):
print("*** test_anonymous_info")
cred = uc_credentials.Anonymous()
Expand Down

0 comments on commit 43b5ec2

Please sign in to comment.