Skip to content

Commit 4bc8b6b

Browse files
πŸ”‘ Add authenticated property to context (#917)
* βž• append `authenticated` property to context as needed. Not yet visible to the client. * πŸ–₯️ Compute state of `authenticated` property * πŸ“ Create tests that check for `authenticated` * 🏷️ Add `bool` type to `authenticated` definition Co-authored-by: Dan Allan <[email protected]> * ✍️ Add Docstring to explain property function * Docstring must start with one-liner followed by blank line * Use Examples section and clear variable names --------- Co-authored-by: Dan Allan <[email protected]> Co-authored-by: Dan Allan <[email protected]>
1 parent 48bb130 commit 4bc8b6b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

β€Žtiled/_tests/test_authentication.pyβ€Ž

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,19 @@ def test_password_auth(enter_username_password, config):
7373
from_context(context)
7474
# Reuse token from cache.
7575
client = from_context(context)
76+
# Check user authentication status string
7677
assert "authenticated as 'alice'" in repr(client.context)
78+
# Check authenticated property exists
79+
assert "authenticated" in dir(client.context)
80+
# Check authenticated property is True
81+
assert client.context.authenticated
7782
client.logout()
83+
# Check authentication status string
7884
assert "unauthenticated" in repr(client.context)
85+
# Check authenticated property still exists
86+
assert "authenticated" in dir(client.context)
87+
# Check authenticated property is False
88+
assert not client.context.authenticated
7989

8090
# Log in as Bob.
8191
with enter_username_password("bob", "secret2"):
@@ -354,6 +364,10 @@ def test_api_key_activity(enter_username_password, config):
354364
context.api_key = key_info["secret"]
355365
assert "authenticated as 'alice'" in repr(context)
356366
assert "with API key" in repr(context)
367+
# Check authenticated property exists
368+
assert "authenticated" in dir(context)
369+
# Check authenticated property is True
370+
assert context.authenticated
357371
assert key_info["secret"][:8] in repr(context)
358372
assert key_info["secret"][8:] not in repr(context)
359373

@@ -376,6 +390,10 @@ def test_api_key_activity(enter_username_password, config):
376390
context.api_key = None
377391
with pytest.raises(RuntimeError):
378392
context.which_api_key()
393+
# Check authenticated property still exists
394+
assert "authenticated" in dir(context)
395+
# Check authenticated property is False
396+
assert not context.authenticated
379397
# Set the API key.
380398
context.api_key = secret
381399
# Now this works again.

β€Žtiled/client/context.pyβ€Ž

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,28 @@ def configure_auth(self, tokens, remember_me=True):
661661
auth.sync_set_token("refresh_token", tokens["refresh_token"])
662662
self.http_client.auth = auth
663663

664+
@property
665+
def authenticated(self) -> bool:
666+
"""
667+
Boolean indicated whether session is authenticated (true) or anonymous (false)
668+
669+
Examples
670+
--------
671+
672+
An anonymous session at first, after login, is authenticated.
673+
674+
>>> client.context.authenticated
675+
False
676+
>>> client.login()
677+
Username: USERNAME
678+
Password: <input is hidden>
679+
>>> client.context.authenticated
680+
True
681+
682+
"""
683+
# Confirm the state of properties that authentication consists of
684+
return (self.api_key is not None) or (self.http_client.auth is not None)
685+
664686
def _token_directory(self):
665687
# e.g. ~/.config/tiled/tokens/{host:port}
666688
# with the templated element URL-encoded so it is a valid filename.

0 commit comments

Comments
Β (0)