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

Commit

Permalink
unit tests for /room/aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Feb 18, 2020
1 parent 45972cd commit 5f46fb9
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 13 deletions.
70 changes: 69 additions & 1 deletion tests/rest/client/v1/test_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
import synapse.rest.admin
from synapse.api.constants import EventContentFields, EventTypes, Membership
from synapse.handlers.pagination import PurgeStatus
from synapse.rest.client.v1 import login, profile, room
from synapse.rest.client.v1 import directory, login, profile, room
from synapse.rest.client.v2_alpha import account
from synapse.types import JsonDict, RoomAlias
from synapse.util.stringutils import random_string

from tests import unittest
Expand Down Expand Up @@ -1726,3 +1727,70 @@ def test_erased_sender(self):
self.assertEqual(len(events_after), 2, events_after)
self.assertDictEqual(events_after[0].get("content"), {}, events_after[0])
self.assertEqual(events_after[1].get("content"), {}, events_after[1])


class DirectoryTestCase(unittest.HomeserverTestCase):

servlets = [
synapse.rest.admin.register_servlets_for_client_rest_resource,
directory.register_servlets,
login.register_servlets,
room.register_servlets,
]

def prepare(self, reactor, clock, homeserver):
self.room_owner = self.register_user("room_owner", "test")
self.room_owner_tok = self.login("room_owner", "test")

self.room_id = self.helper.create_room_as(
self.room_owner, tok=self.room_owner_tok
)

def test_no_aliases(self):
res = self._get_aliases(self.room_owner_tok)
self.assertEqual(res["aliases"], [])

def test_not_in_room(self):
self.register_user("user", "test")
user_tok = self.login("user", "test")
res = self._get_aliases(user_tok, expected_code=403)
self.assertEqual(res["errcode"], "M_FORBIDDEN")

def test_with_aliases(self):
alias1 = self._random_alias()
alias2 = self._random_alias()

self._set_alias_via_directory(alias1)
self._set_alias_via_directory(alias2)

res = self._get_aliases(self.room_owner_tok)
self.assertEqual(set(res["aliases"]), {alias1, alias2})

def _get_aliases(self, access_token: str, expected_code: int = 200) -> JsonDict:
"""Calls the endpoint under test. returns the json response object."""
request, channel = self.make_request(
"GET",
"/_matrix/client/r0/rooms/%s/aliases" % (self.room_id,),
access_token=access_token,
)
self.render(request)
self.assertEqual(channel.code, expected_code, channel.result)
res = channel.json_body
self.assertIsInstance(res, dict)
if expected_code == 200:
self.assertIsInstance(res["aliases"], list)
return res

def _random_alias(self) -> str:
return RoomAlias(random_string(5), self.hs.hostname).to_string()

def _set_alias_via_directory(self, alias: str, expected_code: int = 200):
url = "/_matrix/client/r0/directory/room/" + alias
data = {"room_id": self.room_id}
request_data = json.dumps(data)

request, channel = self.make_request(
"PUT", url, request_data, access_token=self.room_owner_tok
)
self.render(request)
self.assertEqual(channel.code, expected_code, channel.result)
29 changes: 17 additions & 12 deletions tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import inspect
import logging
import time
from typing import Optional, Tuple, TypeVar, Union

from mock import Mock

Expand All @@ -42,7 +43,13 @@
from synapse.types import Requester, UserID, create_requester
from synapse.util.ratelimitutils import FederationRateLimiter

from tests.server import get_clock, make_request, render, setup_test_homeserver
from tests.server import (
FakeChannel,
get_clock,
make_request,
render,
setup_test_homeserver,
)
from tests.test_utils.logging_setup import setup_logging
from tests.utils import default_config, setupdb

Expand Down Expand Up @@ -334,14 +341,13 @@ def prepare(self, reactor, clock, homeserver):

def make_request(
self,
method,
path,
content=b"",
access_token=None,
request=SynapseRequest,
shorthand=True,
federation_auth_origin=None,
):
method: Union[bytes, str],
path: Union[bytes, str],
content: Union[bytes, dict] = b"",
access_token: Optional[str] = None,
shorthand: bool = True,
federation_auth_origin: str = None,
) -> Tuple[SynapseRequest, FakeChannel]:
"""
Create a SynapseRequest at the path using the method and containing the
given content.
Expand Down Expand Up @@ -369,9 +375,8 @@ def make_request(
path,
content,
access_token,
request,
shorthand,
federation_auth_origin,
shorthand=shorthand,
federation_auth_origin=federation_auth_origin,
)

def render(self, request):
Expand Down

0 comments on commit 5f46fb9

Please sign in to comment.