|
| 1 | +# Copyright 2022 The Matrix.org Foundation C.I.C. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from synapse.api.errors import SynapseError |
| 15 | +from synapse.rest import admin |
| 16 | + |
| 17 | +from tests.unittest import HomeserverTestCase |
| 18 | + |
| 19 | + |
| 20 | +class ModuleApiTestCase(HomeserverTestCase): |
| 21 | + servlets = [ |
| 22 | + admin.register_servlets, |
| 23 | + ] |
| 24 | + |
| 25 | + def prepare(self, reactor, clock, homeserver) -> None: |
| 26 | + self._store = homeserver.get_datastores().main |
| 27 | + self._module_api = homeserver.get_module_api() |
| 28 | + self._account_data_mgr = self._module_api.account_data_manager |
| 29 | + |
| 30 | + self.user_id = self.register_user("kristina", "secret") |
| 31 | + |
| 32 | + def test_get_global(self) -> None: |
| 33 | + """ |
| 34 | + Tests that getting global account data through the module API works as |
| 35 | + expected, including getting `None` for unset account data. |
| 36 | + """ |
| 37 | + self.get_success( |
| 38 | + self._store.add_account_data_for_user( |
| 39 | + self.user_id, "test.data", {"wombat": True} |
| 40 | + ) |
| 41 | + ) |
| 42 | + |
| 43 | + # Getting existent account data works as expected. |
| 44 | + self.assertEqual( |
| 45 | + self.get_success( |
| 46 | + self._account_data_mgr.get_global(self.user_id, "test.data") |
| 47 | + ), |
| 48 | + {"wombat": True}, |
| 49 | + ) |
| 50 | + |
| 51 | + # Getting non-existent account data returns None. |
| 52 | + self.assertIsNone( |
| 53 | + self.get_success( |
| 54 | + self._account_data_mgr.get_global(self.user_id, "no.data.at.all") |
| 55 | + ) |
| 56 | + ) |
| 57 | + |
| 58 | + def test_get_global_validation(self) -> None: |
| 59 | + """ |
| 60 | + Tests that invalid or remote user IDs are treated as errors and raised as exceptions, |
| 61 | + whilst getting global account data for a user. |
| 62 | +
|
| 63 | + This is a design choice to try and communicate potential bugs to modules |
| 64 | + earlier on. |
| 65 | + """ |
| 66 | + with self.assertRaises(SynapseError): |
| 67 | + self.get_success_or_raise( |
| 68 | + self._account_data_mgr.get_global("this isn't a user id", "test.data") |
| 69 | + ) |
| 70 | + |
| 71 | + with self.assertRaises(ValueError): |
| 72 | + self.get_success_or_raise( |
| 73 | + self._account_data_mgr.get_global("@valid.but:remote", "test.data") |
| 74 | + ) |
| 75 | + |
| 76 | + def test_get_global_no_mutability(self) -> None: |
| 77 | + """ |
| 78 | + Tests that modules can't introduce bugs into Synapse by mutating the result |
| 79 | + of `get_global`. |
| 80 | + """ |
| 81 | + # First add some account data to set up the test. |
| 82 | + self.get_success( |
| 83 | + self._store.add_account_data_for_user( |
| 84 | + self.user_id, "test.data", {"wombat": True} |
| 85 | + ) |
| 86 | + ) |
| 87 | + |
| 88 | + # Now request that data and then mutate it (out of negligence or otherwise). |
| 89 | + the_data = self.get_success( |
| 90 | + self._account_data_mgr.get_global(self.user_id, "test.data") |
| 91 | + ) |
| 92 | + with self.assertRaises(TypeError): |
| 93 | + # This throws an exception because it's a frozen dict. |
| 94 | + the_data["wombat"] = False |
| 95 | + |
| 96 | + def test_put_global(self) -> None: |
| 97 | + """ |
| 98 | + Tests that written account data using `put_global` can be read out again later. |
| 99 | + """ |
| 100 | + |
| 101 | + self.get_success( |
| 102 | + self._module_api.account_data_manager.put_global( |
| 103 | + self.user_id, "test.data", {"wombat": True} |
| 104 | + ) |
| 105 | + ) |
| 106 | + |
| 107 | + # Request that account data from the normal store; check it's as we expect. |
| 108 | + self.assertEqual( |
| 109 | + self.get_success( |
| 110 | + self._store.get_global_account_data_by_type_for_user( |
| 111 | + self.user_id, "test.data" |
| 112 | + ) |
| 113 | + ), |
| 114 | + {"wombat": True}, |
| 115 | + ) |
| 116 | + |
| 117 | + def test_put_global_validation(self) -> None: |
| 118 | + """ |
| 119 | + Tests that a module can't write account data to user IDs that don't have |
| 120 | + actual users registered to them. |
| 121 | + Modules also must supply the correct types. |
| 122 | + """ |
| 123 | + |
| 124 | + with self.assertRaises(SynapseError): |
| 125 | + self.get_success_or_raise( |
| 126 | + self._account_data_mgr.put_global( |
| 127 | + "this isn't a user id", "test.data", {} |
| 128 | + ) |
| 129 | + ) |
| 130 | + |
| 131 | + with self.assertRaises(ValueError): |
| 132 | + self.get_success_or_raise( |
| 133 | + self._account_data_mgr.put_global("@valid.but:remote", "test.data", {}) |
| 134 | + ) |
| 135 | + |
| 136 | + with self.assertRaises(ValueError): |
| 137 | + self.get_success_or_raise( |
| 138 | + self._module_api.account_data_manager.put_global( |
| 139 | + "@notregistered:test", "test.data", {} |
| 140 | + ) |
| 141 | + ) |
| 142 | + |
| 143 | + with self.assertRaises(TypeError): |
| 144 | + # The account data type must be a string. |
| 145 | + self.get_success_or_raise( |
| 146 | + self._module_api.account_data_manager.put_global( |
| 147 | + self.user_id, 42, {} # type: ignore |
| 148 | + ) |
| 149 | + ) |
| 150 | + |
| 151 | + with self.assertRaises(TypeError): |
| 152 | + # The account data dict must be a dict. |
| 153 | + self.get_success_or_raise( |
| 154 | + self._module_api.account_data_manager.put_global( |
| 155 | + self.user_id, "test.data", 42 # type: ignore |
| 156 | + ) |
| 157 | + ) |
0 commit comments