Skip to content

Commit

Permalink
πŸ”‘ Add authenticated property to context (#917)
Browse files Browse the repository at this point in the history
* βž• 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]>
  • Loading branch information
3 people authored Mar 7, 2025
1 parent 48bb130 commit 4bc8b6b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tiled/_tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,19 @@ def test_password_auth(enter_username_password, config):
from_context(context)
# Reuse token from cache.
client = from_context(context)
# Check user authentication status string
assert "authenticated as 'alice'" in repr(client.context)
# Check authenticated property exists
assert "authenticated" in dir(client.context)
# Check authenticated property is True
assert client.context.authenticated
client.logout()
# Check authentication status string
assert "unauthenticated" in repr(client.context)
# Check authenticated property still exists
assert "authenticated" in dir(client.context)
# Check authenticated property is False
assert not client.context.authenticated

# Log in as Bob.
with enter_username_password("bob", "secret2"):
Expand Down Expand Up @@ -354,6 +364,10 @@ def test_api_key_activity(enter_username_password, config):
context.api_key = key_info["secret"]
assert "authenticated as 'alice'" in repr(context)
assert "with API key" in repr(context)
# Check authenticated property exists
assert "authenticated" in dir(context)
# Check authenticated property is True
assert context.authenticated
assert key_info["secret"][:8] in repr(context)
assert key_info["secret"][8:] not in repr(context)

Expand All @@ -376,6 +390,10 @@ def test_api_key_activity(enter_username_password, config):
context.api_key = None
with pytest.raises(RuntimeError):
context.which_api_key()
# Check authenticated property still exists
assert "authenticated" in dir(context)
# Check authenticated property is False
assert not context.authenticated
# Set the API key.
context.api_key = secret
# Now this works again.
Expand Down
22 changes: 22 additions & 0 deletions tiled/client/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,28 @@ def configure_auth(self, tokens, remember_me=True):
auth.sync_set_token("refresh_token", tokens["refresh_token"])
self.http_client.auth = auth

@property
def authenticated(self) -> bool:
"""
Boolean indicated whether session is authenticated (true) or anonymous (false)
Examples
--------
An anonymous session at first, after login, is authenticated.
>>> client.context.authenticated
False
>>> client.login()
Username: USERNAME
Password: <input is hidden>
>>> client.context.authenticated
True
"""
# Confirm the state of properties that authentication consists of
return (self.api_key is not None) or (self.http_client.auth is not None)

def _token_directory(self):
# e.g. ~/.config/tiled/tokens/{host:port}
# with the templated element URL-encoded so it is a valid filename.
Expand Down

0 comments on commit 4bc8b6b

Please sign in to comment.