Skip to content

Commit

Permalink
Bugfix: make WhoamiResponse alter async_client state appropriately. (#…
Browse files Browse the repository at this point in the history
…422)

* Bugfix: make WhoamiResponse alter async_client state appropriately.

* Comply with v1.0 and v1.1 of spec.

* Test fixes for python3.8/3.9
  • Loading branch information
PaarthShah committed Jul 17, 2023
1 parent 03fddfb commit 4dc00bf
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion nio/client/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3252,7 +3252,7 @@ async def upload_filter(

return await self._send(UploadFilterResponse, method, path, data)

async def whoami(self):
async def whoami(self) -> Union[WhoamiResponse, WhoamiError]:
if self.access_token is None:
raise ValueError("No access_token is set.")

Expand Down
8 changes: 8 additions & 0 deletions nio/client/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
ShareGroupSessionResponse,
SyncResponse,
ToDeviceResponse,
WhoamiResponse,
)
from ..rooms import MatrixInvitedRoom, MatrixRoom

Expand Down Expand Up @@ -1022,6 +1023,11 @@ def _handle_presence_response(self, response: PresenceGetResponse):
)
self.rooms[room_id].users[response.user_id].status_msg = response.status_msg

def _handle_whoami_response(self, response: WhoamiResponse):
self.user_id = response.user_id
self.device_id = response.device_id or self.device_id
# self.is_guest = response.is_guest

def receive_response(
self, response: Response
) -> Union[None, Coroutine[Any, Any, None]]:
Expand Down Expand Up @@ -1073,6 +1079,8 @@ def receive_response(
pass
elif isinstance(response, PresenceGetResponse):
self._handle_presence_response(response)
elif isinstance(response, WhoamiResponse):
self._handle_whoami_response(response)
elif isinstance(response, ErrorResponse):
if response.soft_logout:
self.access_token = ""
Expand Down
8 changes: 7 additions & 1 deletion nio/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1912,14 +1912,20 @@ class WhoamiError(ErrorResponse):
@dataclass
class WhoamiResponse(Response):
user_id: str = field()
device_id: Optional[str] = field()
is_guest: Optional[bool] = field()

@classmethod
@verify(Schemas.whoami, WhoamiError)
def from_dict(
cls,
parsed_dict: Dict[Any, Any],
) -> Union["WhoamiResponse", WhoamiError]:
return cls(parsed_dict["user_id"])
return cls(
parsed_dict["user_id"],
parsed_dict.get("device_id"),
parsed_dict.get("is_guest", False),
)


@dataclass
Expand Down
6 changes: 5 additions & 1 deletion nio/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,11 @@ class Schemas:

whoami = {
"type": "object",
"user_id": "string",
"properties": {
"user_id": {"type": "string", "format": "user_id"},
"device_id": {"type": "string"},
"is_guest": {"type": "boolean"},
},
"required": ["user_id"],
}

Expand Down
19 changes: 19 additions & 0 deletions tests/async_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ def get_avatar_response(avatar_url):
def room_resolve_alias_response(self):
return {"room_id": TEST_ROOM_ID, "servers": ["example.org", "matrix.org"]}

@property
def whoami_response(self):
return self._load_response("tests/data/whoami_response.json")

async def test_mxc_to_http(self, async_client):
mxc = "mxc://privacytools.io/123foo"
url_path = "/_matrix/media/r0/download/privacytools.io/123foo"
Expand Down Expand Up @@ -604,6 +608,21 @@ async def test_login_raw_with_none_dict(self, async_client, aioresponse):
await async_client.close()
assert not async_client.client_session

async def test_whoami(self, async_client, aioresponse):
async_client.restore_login(
user_id="unknown",
device_id="unknown",
access_token="abc123",
)
aioresponse.get(
"https://example.org/_matrix/client/r0/account/whoami?access_token=abc123",
status=200,
payload=self.whoami_response,
)
await async_client.whoami()
assert async_client.user_id != "unknown"
assert async_client.device_id != "unknown"

async def test_logout(self, async_client, aioresponse):
aioresponse.post(
"https://example.org/_matrix/client/r0/login",
Expand Down
5 changes: 5 additions & 0 deletions tests/data/whoami_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"user_id": "@cheeky_monkey:matrix.org",
"is_guest": false,
"device_id": "GHTYAJCE"
}

0 comments on commit 4dc00bf

Please sign in to comment.