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

Commit 8332475

Browse files
authored
Allow specifying the application service-specific user_id parameter in the join test helper. (#11616)
1 parent 964f5b9 commit 8332475

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

changelog.d/11615.misc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Expose the registered device ID from the `register_appservice_user` test helper.
1+
Enhance user registration test helpers to make them more useful for tests involving Application Services and devices.

changelog.d/11616.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Enhance user registration test helpers to make them more useful for tests involving Application Services and devices.

tests/rest/client/utils.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
overload,
3232
)
3333
from unittest.mock import patch
34+
from urllib.parse import urlencode
3435

3536
import attr
3637
from typing_extensions import Literal
@@ -147,12 +148,20 @@ def invite(self, room=None, src=None, targ=None, expect_code=200, tok=None):
147148
expect_code=expect_code,
148149
)
149150

150-
def join(self, room=None, user=None, expect_code=200, tok=None):
151+
def join(
152+
self,
153+
room: str,
154+
user: Optional[str] = None,
155+
expect_code: int = 200,
156+
tok: Optional[str] = None,
157+
appservice_user_id: Optional[str] = None,
158+
) -> None:
151159
self.change_membership(
152160
room=room,
153161
src=user,
154162
targ=user,
155163
tok=tok,
164+
appservice_user_id=appservice_user_id,
156165
membership=Membership.JOIN,
157166
expect_code=expect_code,
158167
)
@@ -209,11 +218,12 @@ def ban(self, room: str, src: str, targ: str, **kwargs: object):
209218
def change_membership(
210219
self,
211220
room: str,
212-
src: str,
213-
targ: str,
221+
src: Optional[str],
222+
targ: Optional[str],
214223
membership: str,
215224
extra_data: Optional[dict] = None,
216225
tok: Optional[str] = None,
226+
appservice_user_id: Optional[str] = None,
217227
expect_code: int = 200,
218228
expect_errcode: Optional[str] = None,
219229
) -> None:
@@ -227,15 +237,26 @@ def change_membership(
227237
membership: The type of membership event
228238
extra_data: Extra information to include in the content of the event
229239
tok: The user access token to use
240+
appservice_user_id: The `user_id` URL parameter to pass.
241+
This allows driving an application service user
242+
using an application service access token in `tok`.
230243
expect_code: The expected HTTP response code
231244
expect_errcode: The expected Matrix error code
232245
"""
233246
temp_id = self.auth_user_id
234247
self.auth_user_id = src
235248

236-
path = "/_matrix/client/r0/rooms/%s/state/m.room.member/%s" % (room, targ)
249+
path = f"/_matrix/client/r0/rooms/{room}/state/m.room.member/{targ}"
250+
url_params: Dict[str, str] = {}
251+
237252
if tok:
238-
path = path + "?access_token=%s" % tok
253+
url_params["access_token"] = tok
254+
255+
if appservice_user_id:
256+
url_params["user_id"] = appservice_user_id
257+
258+
if url_params:
259+
path += "?" + urlencode(url_params)
239260

240261
data = {"membership": membership}
241262
data.update(extra_data or {})

0 commit comments

Comments
 (0)