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

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed Mar 9, 2022
1 parent 42d6651 commit 53f9d01
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions tests/handlers/test_appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,90 @@ def _register_application_service(
return appservice


class ApplicationServicesHandlerDeviceListsTestCase(unittest.HomeserverTestCase):
"""
Tests that the ApplicationServicesHandler sends device list updates to application
services correctly.
"""

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

def prepare(self, reactor, clock, hs):
# Mock the ApplicationServiceScheduler's _TransactionController's send method so that
# we can track any outgoing ephemeral events
self.send_mock = simple_async_mock()
hs.get_application_service_handler().scheduler.txn_ctrl.send = self.send_mock

# Mock out application services, and allow defining our own in tests
self._services: List[ApplicationService] = []
self.hs.get_datastores().main.get_app_services = Mock(
return_value=self._services
)

@unittest.INFO
@unittest.override_config({"experimental_features": {"msc3202_device_lists": True}})
def test_application_services_receive_device_list_changes(self):
"""
Tests that an application service receives notice of changed device
lists for a user, when a user changes their device lists.
"""
# TODO: Why has @exclusive_as_user logging in above not already triggered
# a device list update? from_key for this appservice is 0.
# Oh, perhaps because we're registering it here instead of before the
# above users are created...
# Yeah, we should create a new TestCase and register the appservice before
# we make the users.
# Create an appservice that is interested in "local_user"
appservice = ApplicationService(
token=random_string(10),
hostname="example.com",
id=random_string(10),
sender="@as:example.com",
rate_limited=False,
namespaces={
ApplicationService.NS_USERS: [
{
"regex": "@local_user:.+",
"exclusive": False,
}
],
},
supports_ephemeral=True,
msc3202_transaction_extensions=True,
)

# Register the application service
self._services.append(appservice)

# Register a user on the homeserver
self.local_user = self.register_user("local_user", "password")
self.local_user_token = self.login("local_user", "password")

# Our application service should have received a device list update with
# "local_user" in the "changed" list
self.send_mock.assert_called_once()
(
service,
_events,
_ephemeral,
_to_device_messages,
_otks,
_fbks,
device_list_summary,
) = self.send_mock.call_args[0]

self.assertEqual(service, appservice)
self.assertIn(self.local_user, device_list_summary.changed)
# The list of "left" users should be empty
self.assertFalse(device_list_summary.left)


class ApplicationServicesHandlerOtkCountsTestCase(unittest.HomeserverTestCase):
# Argument indices for pulling out arguments from a `send_mock`.
ARG_OTK_COUNTS = 4
Expand Down

0 comments on commit 53f9d01

Please sign in to comment.