Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Allow specifying the application service-specific user_id parameter in the join test helper. #11616

Merged
merged 7 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/11616.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow specifying the application service-specific `user_id` parameter in the `join` test helper.
clokep marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 25 additions & 5 deletions tests/rest/client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
overload,
)
from unittest.mock import patch
from urllib.parse import urlencode

import attr
from typing_extensions import Literal
Expand Down Expand Up @@ -147,12 +148,20 @@ def invite(self, room=None, src=None, targ=None, expect_code=200, tok=None):
expect_code=expect_code,
)

def join(self, room=None, user=None, expect_code=200, tok=None):
def join(
self,
room: str,
user: Optional[str] = None,
expect_code: int = 200,
tok: Optional[str] = None,
appservice_user_id: Optional[str] = None,
) -> None:
self.change_membership(
room=room,
src=user,
targ=user,
tok=tok,
appservice_user_id=appservice_user_id,
membership=Membership.JOIN,
expect_code=expect_code,
)
Expand Down Expand Up @@ -199,11 +208,12 @@ def leave(self, room=None, user=None, expect_code=200, tok=None):
def change_membership(
self,
room: str,
src: str,
targ: str,
src: Optional[str],
targ: Optional[str],
membership: str,
extra_data: Optional[dict] = None,
tok: Optional[str] = None,
appservice_user_id: Optional[str] = None,
expect_code: int = 200,
expect_errcode: Optional[str] = None,
) -> None:
Expand All @@ -217,15 +227,25 @@ def change_membership(
membership: The type of membership event
extra_data: Extra information to include in the content of the event
tok: The user access token to use
appservice_user_id: The `user_id` URL parameter to pass.
This allows driving an application service user
using an application service access token in `tok`.
expect_code: The expected HTTP response code
expect_errcode: The expected Matrix error code
"""
temp_id = self.auth_user_id
self.auth_user_id = src

path = "/_matrix/client/r0/rooms/%s/state/m.room.member/%s" % (room, targ)
path = f"/_matrix/client/r0/rooms/{room}/state/m.room.member/{targ}"
url_params: Dict[str, str] = {}

if tok:
path = path + "?access_token=%s" % tok
url_params["access_token"] = tok

if appservice_user_id:
url_params["user_id"] = appservice_user_id

path += "?" + urlencode(url_params)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably doesn't matter much, but if we want to be perfectly consistent with past behavior we could wrap this in an if url_params.


data = {"membership": membership}
data.update(extra_data or {})
Expand Down