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

Convert storage test cases to HomeserverTestCase. #9736

Merged
merged 11 commits into from
Apr 6, 2021
1 change: 1 addition & 0 deletions changelog.d/9736.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert various testcases to `HomeserverTestCase`.
80 changes: 32 additions & 48 deletions tests/storage/test_devices.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2016 OpenMarket Ltd
# Copyright 2016-2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,32 +13,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from twisted.internet import defer

import synapse.api.errors

import tests.unittest
import tests.utils


class DeviceStoreTestCase(tests.unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.store = None # type: synapse.storage.DataStore
from tests.unittest import HomeserverTestCase

@defer.inlineCallbacks
def setUp(self):
hs = yield tests.utils.setup_test_homeserver(self.addCleanup)

class DeviceStoreTestCase(HomeserverTestCase):
def prepare(self, reactor, clock, hs):
self.store = hs.get_datastore()

@defer.inlineCallbacks
def test_store_new_device(self):
yield defer.ensureDeferred(
self.get_success(
self.store.store_device("user_id", "device_id", "display_name")
)

res = yield defer.ensureDeferred(self.store.get_device("user_id", "device_id"))
res = self.get_success(self.store.get_device("user_id", "device_id"))
self.assertDictContainsSubset(
{
"user_id": "user_id",
Expand All @@ -48,19 +37,18 @@ def test_store_new_device(self):
res,
)

@defer.inlineCallbacks
def test_get_devices_by_user(self):
yield defer.ensureDeferred(
self.get_success(
self.store.store_device("user_id", "device1", "display_name 1")
)
yield defer.ensureDeferred(
self.get_success(
self.store.store_device("user_id", "device2", "display_name 2")
)
yield defer.ensureDeferred(
self.get_success(
self.store.store_device("user_id2", "device3", "display_name 3")
)

res = yield defer.ensureDeferred(self.store.get_devices_by_user("user_id"))
res = self.get_success(self.store.get_devices_by_user("user_id"))
self.assertEqual(2, len(res.keys()))
self.assertDictContainsSubset(
{
Expand All @@ -79,43 +67,41 @@ def test_get_devices_by_user(self):
res["device2"],
)

@defer.inlineCallbacks
def test_count_devices_by_users(self):
yield defer.ensureDeferred(
self.get_success(
self.store.store_device("user_id", "device1", "display_name 1")
)
yield defer.ensureDeferred(
self.get_success(
self.store.store_device("user_id", "device2", "display_name 2")
)
yield defer.ensureDeferred(
self.get_success(
self.store.store_device("user_id2", "device3", "display_name 3")
)

res = yield defer.ensureDeferred(self.store.count_devices_by_users())
res = self.get_success(self.store.count_devices_by_users())
self.assertEqual(0, res)

res = yield defer.ensureDeferred(self.store.count_devices_by_users(["unknown"]))
res = self.get_success(self.store.count_devices_by_users(["unknown"]))
self.assertEqual(0, res)

res = yield defer.ensureDeferred(self.store.count_devices_by_users(["user_id"]))
res = self.get_success(self.store.count_devices_by_users(["user_id"]))
self.assertEqual(2, res)

res = yield defer.ensureDeferred(
res = self.get_success(
self.store.count_devices_by_users(["user_id", "user_id2"])
)
self.assertEqual(3, res)

@defer.inlineCallbacks
def test_get_device_updates_by_remote(self):
device_ids = ["device_id1", "device_id2"]

# Add two device updates with a single stream_id
yield defer.ensureDeferred(
self.get_success(
self.store.add_device_change_to_streams("user_id", device_ids, ["somehost"])
)

# Get all device updates ever meant for this remote
now_stream_id, device_updates = yield defer.ensureDeferred(
now_stream_id, device_updates = self.get_success(
self.store.get_device_updates_by_remote("somehost", -1, limit=100)
)

Expand All @@ -131,37 +117,35 @@ def _check_devices_in_updates(self, expected_device_ids, device_updates):
}
self.assertEqual(received_device_ids, set(expected_device_ids))

@defer.inlineCallbacks
def test_update_device(self):
yield defer.ensureDeferred(
self.get_success(
self.store.store_device("user_id", "device_id", "display_name 1")
)

res = yield defer.ensureDeferred(self.store.get_device("user_id", "device_id"))
res = self.get_success(self.store.get_device("user_id", "device_id"))
self.assertEqual("display_name 1", res["display_name"])

# do a no-op first
yield defer.ensureDeferred(self.store.update_device("user_id", "device_id"))
res = yield defer.ensureDeferred(self.store.get_device("user_id", "device_id"))
self.get_success(self.store.update_device("user_id", "device_id"))
res = self.get_success(self.store.get_device("user_id", "device_id"))
self.assertEqual("display_name 1", res["display_name"])

# do the update
yield defer.ensureDeferred(
self.get_success(
self.store.update_device(
"user_id", "device_id", new_display_name="display_name 2"
)
)

# check it worked
res = yield defer.ensureDeferred(self.store.get_device("user_id", "device_id"))
res = self.get_success(self.store.get_device("user_id", "device_id"))
self.assertEqual("display_name 2", res["display_name"])

@defer.inlineCallbacks
def test_update_unknown_device(self):
with self.assertRaises(synapse.api.errors.StoreError) as cm:
yield defer.ensureDeferred(
self.store.update_device(
"user_id", "unknown_device_id", new_display_name="display_name 2"
)
)
self.assertEqual(404, cm.exception.code)
exc = self.get_failure(
self.store.update_device(
"user_id", "unknown_device_id", new_display_name="display_name 2"
),
synapse.api.errors.StoreError,
)
self.assertEqual(404, exc.value.code)
44 changes: 11 additions & 33 deletions tests/storage/test_directory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2014-2016 OpenMarket Ltd
# Copyright 2014-2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,74 +13,52 @@
# See the License for the specific language governing permissions and
# limitations under the License.


from twisted.internet import defer

from synapse.types import RoomAlias, RoomID

from tests import unittest
from tests.utils import setup_test_homeserver
from tests.unittest import HomeserverTestCase


class DirectoryStoreTestCase(unittest.TestCase):
@defer.inlineCallbacks
def setUp(self):
hs = yield setup_test_homeserver(self.addCleanup)

class DirectoryStoreTestCase(HomeserverTestCase):
def prepare(self, reactor, clock, hs):
self.store = hs.get_datastore()

self.room = RoomID.from_string("!abcde:test")
self.alias = RoomAlias.from_string("#my-room:test")

@defer.inlineCallbacks
def test_room_to_alias(self):
yield defer.ensureDeferred(
self.get_success(
self.store.create_room_alias_association(
room_alias=self.alias, room_id=self.room.to_string(), servers=["test"]
)
)

self.assertEquals(
["#my-room:test"],
(
yield defer.ensureDeferred(
self.store.get_aliases_for_room(self.room.to_string())
)
),
(self.get_success(self.store.get_aliases_for_room(self.room.to_string()))),
)

@defer.inlineCallbacks
def test_alias_to_room(self):
yield defer.ensureDeferred(
self.get_success(
self.store.create_room_alias_association(
room_alias=self.alias, room_id=self.room.to_string(), servers=["test"]
)
)

self.assertObjectHasAttributes(
{"room_id": self.room.to_string(), "servers": ["test"]},
(
yield defer.ensureDeferred(
self.store.get_association_from_room_alias(self.alias)
)
),
(self.get_success(self.store.get_association_from_room_alias(self.alias))),
)

@defer.inlineCallbacks
def test_delete_alias(self):
yield defer.ensureDeferred(
self.get_success(
self.store.create_room_alias_association(
room_alias=self.alias, room_id=self.room.to_string(), servers=["test"]
)
)

room_id = yield defer.ensureDeferred(self.store.delete_room_alias(self.alias))
room_id = self.get_success(self.store.delete_room_alias(self.alias))
self.assertEqual(self.room.to_string(), room_id)

self.assertIsNone(
(
yield defer.ensureDeferred(
self.store.get_association_from_room_alias(self.alias)
)
)
(self.get_success(self.store.get_association_from_room_alias(self.alias)))
)
59 changes: 22 additions & 37 deletions tests/storage/test_end_to_end_keys.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2016 OpenMarket Ltd
# Copyright 2016-2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,69 +13,55 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from twisted.internet import defer
from tests.unittest import HomeserverTestCase

import tests.unittest
import tests.utils


class EndToEndKeyStoreTestCase(tests.unittest.TestCase):
@defer.inlineCallbacks
def setUp(self):
hs = yield tests.utils.setup_test_homeserver(self.addCleanup)
class EndToEndKeyStoreTestCase(HomeserverTestCase):
def prepare(self, reactor, clock, hs):
self.store = hs.get_datastore()

@defer.inlineCallbacks
def test_key_without_device_name(self):
now = 1470174257070
json = {"key": "value"}

yield defer.ensureDeferred(self.store.store_device("user", "device", None))
self.get_success(self.store.store_device("user", "device", None))

yield defer.ensureDeferred(
self.store.set_e2e_device_keys("user", "device", now, json)
)
self.get_success(self.store.set_e2e_device_keys("user", "device", now, json))

res = yield defer.ensureDeferred(
res = self.get_success(
self.store.get_e2e_device_keys_for_cs_api((("user", "device"),))
)
self.assertIn("user", res)
self.assertIn("device", res["user"])
dev = res["user"]["device"]
self.assertDictContainsSubset(json, dev)

@defer.inlineCallbacks
def test_reupload_key(self):
now = 1470174257070
json = {"key": "value"}

yield defer.ensureDeferred(self.store.store_device("user", "device", None))
self.get_success(self.store.store_device("user", "device", None))

changed = yield defer.ensureDeferred(
changed = self.get_success(
self.store.set_e2e_device_keys("user", "device", now, json)
)
self.assertTrue(changed)

# If we try to upload the same key then we should be told nothing
# changed
changed = yield defer.ensureDeferred(
changed = self.get_success(
self.store.set_e2e_device_keys("user", "device", now, json)
)
self.assertFalse(changed)

@defer.inlineCallbacks
def test_get_key_with_device_name(self):
now = 1470174257070
json = {"key": "value"}

yield defer.ensureDeferred(
self.store.set_e2e_device_keys("user", "device", now, json)
)
yield defer.ensureDeferred(
self.store.store_device("user", "device", "display_name")
)
self.get_success(self.store.set_e2e_device_keys("user", "device", now, json))
self.get_success(self.store.store_device("user", "device", "display_name"))

res = yield defer.ensureDeferred(
res = self.get_success(
self.store.get_e2e_device_keys_for_cs_api((("user", "device"),))
)
self.assertIn("user", res)
Expand All @@ -85,29 +71,28 @@ def test_get_key_with_device_name(self):
{"key": "value", "unsigned": {"device_display_name": "display_name"}}, dev
)

@defer.inlineCallbacks
def test_multiple_devices(self):
now = 1470174257070

yield defer.ensureDeferred(self.store.store_device("user1", "device1", None))
yield defer.ensureDeferred(self.store.store_device("user1", "device2", None))
yield defer.ensureDeferred(self.store.store_device("user2", "device1", None))
yield defer.ensureDeferred(self.store.store_device("user2", "device2", None))
self.get_success(self.store.store_device("user1", "device1", None))
self.get_success(self.store.store_device("user1", "device2", None))
self.get_success(self.store.store_device("user2", "device1", None))
self.get_success(self.store.store_device("user2", "device2", None))

yield defer.ensureDeferred(
self.get_success(
self.store.set_e2e_device_keys("user1", "device1", now, {"key": "json11"})
)
yield defer.ensureDeferred(
self.get_success(
self.store.set_e2e_device_keys("user1", "device2", now, {"key": "json12"})
)
yield defer.ensureDeferred(
self.get_success(
self.store.set_e2e_device_keys("user2", "device1", now, {"key": "json21"})
)
yield defer.ensureDeferred(
self.get_success(
self.store.set_e2e_device_keys("user2", "device2", now, {"key": "json22"})
)

res = yield defer.ensureDeferred(
res = self.get_success(
self.store.get_e2e_device_keys_for_cs_api(
(("user1", "device1"), ("user2", "device2"))
)
Expand Down
Loading