Skip to content

Commit

Permalink
Merge pull request #745 from danielballan/context-repr
Browse files Browse the repository at this point in the history
Show auth state in Context repr. Nothing substantial that I would change.
  • Loading branch information
Kezzsim authored May 21, 2024
2 parents 6ca698c + 4c377c8 commit 0383cf6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Write the date in place of the "Unreleased" in the case a new version is release
that the data in it can be read.
- Added `tiled.client.sync` with a utility for copying nodes between two
Tiled instances.
- Show authentication state in `Context` repr.

### Changed

Expand Down
7 changes: 7 additions & 0 deletions tiled/_tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ def test_password_auth(enter_password, config):
from_context(context, username="alice")
# Reuse token from cache.
client = from_context(context, username="alice")
assert "authenticated as 'alice'" in repr(client.context)
client.logout()
assert "unauthenticated" in repr(client.context)

# Log in as Bob.
with enter_password("secret2"):
client = from_context(context, username="bob")
assert "authenticated as 'bob'" in repr(client.context)
client.logout()

# Bob's password should not work for Alice.
Expand Down Expand Up @@ -333,6 +336,10 @@ def test_api_key_activity(enter_password, config):
context.logout()
assert key_info["latest_activity"] is None # never used
context.api_key = key_info["secret"]
assert "authenticated as 'alice'" in repr(context)
assert "with API key" in repr(context)
assert key_info["secret"][:8] in repr(context)
assert key_info["secret"][8:] not in repr(context)

# Use the key for a couple requests and see that latest_activity becomes set and then increases.
client = from_context(context)
Expand Down
19 changes: 19 additions & 0 deletions tiled/client/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ def __init__(
self.api_key = api_key # property setter sets Authorization header
self.admin = Admin(self) # accessor for admin-related requests

def __repr__(self):
auth_info = []
if (self.api_key is None) and (self.http_client.auth is None):
auth_info.append("(unauthenticated)")
else:
auth_info.append("authenticated")
if self.server_info["authentication"].get("links"):
whoami = self.whoami()
auth_info.append("as")
auth_info.append(
",".join(f"'{identity['id']}'" for identity in whoami["identities"])
)
if self.api_key is not None:
auth_info.append(
f"with API key '{self.api_key[:min(len(self.api_key)//2, 8)]}...'"
)
auth_repr = " ".join(auth_info)
return f"<{type(self).__name__} {auth_repr}>"

def __enter__(self):
return self

Expand Down

0 comments on commit 0383cf6

Please sign in to comment.