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

Consistently exclude from user_directory, round 2 #10960

Merged
merged 12 commits into from
Oct 4, 2021
17 changes: 5 additions & 12 deletions tests/rest/client/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,13 +1064,6 @@ class AppserviceLoginRestServletTestCase(unittest.HomeserverTestCase):
register.register_servlets,
]

def register_as_user(self, username):
self.make_request(
b"POST",
"/_matrix/client/r0/register?access_token=%s" % (self.service.token,),
{"username": username},
)

def make_homeserver(self, reactor, clock):
self.hs = self.setup_test_homeserver()

Expand Down Expand Up @@ -1107,7 +1100,7 @@ def make_homeserver(self, reactor, clock):

def test_login_appservice_user(self):
"""Test that an appservice user can use /login"""
self.register_as_user(AS_USER)
self.register_appservice_user(AS_USER, self.service.token)

params = {
"type": login.LoginRestServlet.APPSERVICE_TYPE,
Expand All @@ -1121,7 +1114,7 @@ def test_login_appservice_user(self):

def test_login_appservice_user_bot(self):
"""Test that the appservice bot can use /login"""
self.register_as_user(AS_USER)
self.register_appservice_user(AS_USER, self.service.token)

params = {
"type": login.LoginRestServlet.APPSERVICE_TYPE,
Expand All @@ -1135,7 +1128,7 @@ def test_login_appservice_user_bot(self):

def test_login_appservice_wrong_user(self):
"""Test that non-as users cannot login with the as token"""
self.register_as_user(AS_USER)
self.register_appservice_user(AS_USER, self.service.token)

params = {
"type": login.LoginRestServlet.APPSERVICE_TYPE,
Expand All @@ -1149,7 +1142,7 @@ def test_login_appservice_wrong_user(self):

def test_login_appservice_wrong_as(self):
"""Test that as users cannot login with wrong as token"""
self.register_as_user(AS_USER)
self.register_appservice_user(AS_USER, self.service.token)

params = {
"type": login.LoginRestServlet.APPSERVICE_TYPE,
Expand All @@ -1165,7 +1158,7 @@ def test_login_appservice_no_token(self):
"""Test that users must provide a token when using the appservice
login method
"""
self.register_as_user(AS_USER)
self.register_appservice_user(AS_USER, self.service.token)

params = {
"type": login.LoginRestServlet.APPSERVICE_TYPE,
Expand Down
20 changes: 20 additions & 0 deletions tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import time
from typing import Callable, Dict, Iterable, Optional, Tuple, Type, TypeVar, Union
from unittest.mock import Mock, patch
from urllib.parse import quote

from canonicaljson import json

Expand Down Expand Up @@ -596,6 +597,25 @@ def register_user(
user_id = channel.json_body["user_id"]
return user_id

def register_appservice_user(
self,
username: str,
appservice_token: str,
) -> str:
"""Register an appservice user. Requires the client-facing registration API be registered.

Returns the MXID of the new user."""
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
urlencoded_token = quote(appservice_token)
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
channel = self.make_request(
"POST",
f"/_matrix/client/r0/register?access_token={urlencoded_token}",
{
"username": username,
"type": "m.login.application_service",
},
)
return channel.json_body["user_id"]

def login(
self,
username,
Expand Down