From 0a00b7ff14890987f09112a2ae696c61001e6cf1 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Tue, 16 Feb 2021 16:32:34 -0600 Subject: Update black, and run auto formatting over the codebase (#9381) - Update black version to the latest - Run black auto formatting over the codebase - Run autoformatting according to [`docs/code_style.md `](https://github.com/matrix-org/synapse/blob/80d6dc9783aa80886a133756028984dbf8920168/docs/code_style.md) - Update `code_style.md` docs around installing black to use the correct version --- tests/storage/test_account_data.py | 4 +++- tests/storage/test_background_update.py | 4 +++- tests/storage/test_cleanup_extrems.py | 3 +-- tests/storage/test_client_ips.py | 24 +++++++++++++++++++++--- tests/storage/test_event_chain.py | 23 ++++++++++++++--------- tests/storage/test_event_federation.py | 17 ++++++++++++++--- tests/storage/test_event_push_actions.py | 4 +++- tests/storage/test_events.py | 6 ++---- tests/storage/test_id_generators.py | 30 ++++++++++++++++-------------- tests/storage/test_monthly_active_users.py | 2 +- tests/storage/test_redaction.py | 9 +++------ tests/storage/test_registration.py | 10 ++++++++-- 12 files changed, 89 insertions(+), 47 deletions(-) (limited to 'tests/storage') diff --git a/tests/storage/test_account_data.py b/tests/storage/test_account_data.py index 673e1fe3e3..38444e48e2 100644 --- a/tests/storage/test_account_data.py +++ b/tests/storage/test_account_data.py @@ -96,7 +96,9 @@ class IgnoredUsersTestCase(unittest.HomeserverTestCase): # No ignored_users key. self.get_success( self.store.add_account_data_for_user( - self.user, AccountDataTypes.IGNORED_USER_LIST, {}, + self.user, + AccountDataTypes.IGNORED_USER_LIST, + {}, ) ) diff --git a/tests/storage/test_background_update.py b/tests/storage/test_background_update.py index 02aae1c13d..1b4fae0bb5 100644 --- a/tests/storage/test_background_update.py +++ b/tests/storage/test_background_update.py @@ -67,7 +67,9 @@ class BackgroundUpdateTestCase(unittest.HomeserverTestCase): async def update(progress, count): self.assertEqual(progress, {"my_key": 2}) self.assertAlmostEqual( - count, target_background_update_duration_ms / duration_ms, places=0, + count, + target_background_update_duration_ms / duration_ms, + places=0, ) await self.updates._end_background_update("test_update") return count diff --git a/tests/storage/test_cleanup_extrems.py b/tests/storage/test_cleanup_extrems.py index c13a57dad1..7791138688 100644 --- a/tests/storage/test_cleanup_extrems.py +++ b/tests/storage/test_cleanup_extrems.py @@ -43,8 +43,7 @@ class CleanupExtremBackgroundUpdateStoreTestCase(HomeserverTestCase): self.room_id = info["room_id"] def run_background_update(self): - """Re run the background update to clean up the extremities. - """ + """Re run the background update to clean up the extremities.""" # Make sure we don't clash with in progress updates. self.assertTrue( self.store.db_pool.updates._all_done, "Background updates are still ongoing" diff --git a/tests/storage/test_client_ips.py b/tests/storage/test_client_ips.py index a69117c5a9..34e6526097 100644 --- a/tests/storage/test_client_ips.py +++ b/tests/storage/test_client_ips.py @@ -41,7 +41,13 @@ class ClientIpStoreTestCase(unittest.HomeserverTestCase): device_id = "MY_DEVICE" # Insert a user IP - self.get_success(self.store.store_device(user_id, device_id, "display name",)) + self.get_success( + self.store.store_device( + user_id, + device_id, + "display name", + ) + ) self.get_success( self.store.insert_client_ip( user_id, "access_token", "ip", "user_agent", device_id @@ -214,7 +220,13 @@ class ClientIpStoreTestCase(unittest.HomeserverTestCase): device_id = "MY_DEVICE" # Insert a user IP - self.get_success(self.store.store_device(user_id, device_id, "display name",)) + self.get_success( + self.store.store_device( + user_id, + device_id, + "display name", + ) + ) self.get_success( self.store.insert_client_ip( user_id, "access_token", "ip", "user_agent", device_id @@ -303,7 +315,13 @@ class ClientIpStoreTestCase(unittest.HomeserverTestCase): device_id = "MY_DEVICE" # Insert a user IP - self.get_success(self.store.store_device(user_id, device_id, "display name",)) + self.get_success( + self.store.store_device( + user_id, + device_id, + "display name", + ) + ) self.get_success( self.store.insert_client_ip( user_id, "access_token", "ip", "user_agent", device_id diff --git a/tests/storage/test_event_chain.py b/tests/storage/test_event_chain.py index 0c46ad595b..16daa66cc9 100644 --- a/tests/storage/test_event_chain.py +++ b/tests/storage/test_event_chain.py @@ -90,7 +90,8 @@ class EventChainStoreTestCase(HomeserverTestCase): "content": {"tag": "power"}, }, ).build( - prev_event_ids=[], auth_event_ids=[create.event_id, bob_join.event_id], + prev_event_ids=[], + auth_event_ids=[create.event_id, bob_join.event_id], ) ) @@ -226,7 +227,8 @@ class EventChainStoreTestCase(HomeserverTestCase): self.assertFalse( link_map.exists_path_from( - chain_map[create.event_id], chain_map[event.event_id], + chain_map[create.event_id], + chain_map[event.event_id], ), ) @@ -287,7 +289,8 @@ class EventChainStoreTestCase(HomeserverTestCase): "content": {"tag": "power"}, }, ).build( - prev_event_ids=[], auth_event_ids=[create.event_id, bob_join.event_id], + prev_event_ids=[], + auth_event_ids=[create.event_id, bob_join.event_id], ) ) @@ -373,7 +376,8 @@ class EventChainStoreTestCase(HomeserverTestCase): ) def persist( - self, events: List[EventBase], + self, + events: List[EventBase], ): """Persist the given events and check that the links generated match those given. @@ -394,7 +398,10 @@ class EventChainStoreTestCase(HomeserverTestCase): persist_events_store._persist_event_auth_chain_txn(txn, events) self.get_success( - persist_events_store.db_pool.runInteraction("_persist", _persist,) + persist_events_store.db_pool.runInteraction( + "_persist", + _persist, + ) ) def fetch_chains( @@ -447,8 +454,7 @@ class EventChainStoreTestCase(HomeserverTestCase): class LinkMapTestCase(unittest.TestCase): def test_simple(self): - """Basic tests for the LinkMap. - """ + """Basic tests for the LinkMap.""" link_map = _LinkMap() link_map.add_link((1, 1), (2, 1), new=False) @@ -490,8 +496,7 @@ class EventChainBackgroundUpdateTestCase(HomeserverTestCase): self.requester = create_requester(self.user_id) def _generate_room(self) -> Tuple[str, List[Set[str]]]: - """Insert a room without a chain cover index. - """ + """Insert a room without a chain cover index.""" room_id = self.helper.create_room_as(self.user_id, tok=self.token) # Mark the room as not having a chain cover index diff --git a/tests/storage/test_event_federation.py b/tests/storage/test_event_federation.py index 9d04a066d8..06000f81a6 100644 --- a/tests/storage/test_event_federation.py +++ b/tests/storage/test_event_federation.py @@ -215,7 +215,12 @@ class EventFederationWorkerStoreTestCase(tests.unittest.HomeserverTestCase): ], ) - self.get_success(self.store.db_pool.runInteraction("insert", insert_event,)) + self.get_success( + self.store.db_pool.runInteraction( + "insert", + insert_event, + ) + ) # Now actually test that various combinations give the right result: @@ -370,7 +375,8 @@ class EventFederationWorkerStoreTestCase(tests.unittest.HomeserverTestCase): ) self.hs.datastores.persist_events._persist_event_auth_chain_txn( - txn, [FakeEvent("b", room_id, auth_graph["b"])], + txn, + [FakeEvent("b", room_id, auth_graph["b"])], ) self.store.db_pool.simple_update_txn( @@ -380,7 +386,12 @@ class EventFederationWorkerStoreTestCase(tests.unittest.HomeserverTestCase): updatevalues={"has_auth_chain_index": True}, ) - self.get_success(self.store.db_pool.runInteraction("insert", insert_event,)) + self.get_success( + self.store.db_pool.runInteraction( + "insert", + insert_event, + ) + ) # Now actually test that various combinations give the right result: diff --git a/tests/storage/test_event_push_actions.py b/tests/storage/test_event_push_actions.py index c0595963dd..485f1ee033 100644 --- a/tests/storage/test_event_push_actions.py +++ b/tests/storage/test_event_push_actions.py @@ -84,7 +84,9 @@ class EventPushActionsStoreTestCase(tests.unittest.TestCase): yield defer.ensureDeferred( self.store.add_push_actions_to_staging( - event.event_id, {user_id: action}, False, + event.event_id, + {user_id: action}, + False, ) ) yield defer.ensureDeferred( diff --git a/tests/storage/test_events.py b/tests/storage/test_events.py index 71210ce606..ed898b8dbb 100644 --- a/tests/storage/test_events.py +++ b/tests/storage/test_events.py @@ -68,16 +68,14 @@ class ExtremPruneTestCase(HomeserverTestCase): self.assert_extremities([self.remote_event_1.event_id]) def persist_event(self, event, state=None): - """Persist the event, with optional state - """ + """Persist the event, with optional state""" context = self.get_success( self.state.compute_event_context(event, old_state=state) ) self.get_success(self.persistence.persist_event(event, context)) def assert_extremities(self, expected_extremities): - """Assert the current extremities for the room - """ + """Assert the current extremities for the room""" extremities = self.get_success( self.store.get_prev_events_for_room(self.room_id) ) diff --git a/tests/storage/test_id_generators.py b/tests/storage/test_id_generators.py index 3e2fd4da01..aad6bc907e 100644 --- a/tests/storage/test_id_generators.py +++ b/tests/storage/test_id_generators.py @@ -86,7 +86,11 @@ class MultiWriterIdGeneratorTestCase(HomeserverTestCase): def _insert(txn): txn.execute( - "INSERT INTO foobar VALUES (?, ?)", (stream_id, instance_name,), + "INSERT INTO foobar VALUES (?, ?)", + ( + stream_id, + instance_name, + ), ) txn.execute("SELECT setval('foobar_seq', ?)", (stream_id,)) txn.execute( @@ -138,8 +142,7 @@ class MultiWriterIdGeneratorTestCase(HomeserverTestCase): self.assertEqual(id_gen.get_current_token_for_writer("master"), 8) def test_out_of_order_finish(self): - """Test that IDs persisted out of order are correctly handled - """ + """Test that IDs persisted out of order are correctly handled""" # Prefill table with 7 rows written by 'master' self._insert_rows("master", 7) @@ -246,8 +249,7 @@ class MultiWriterIdGeneratorTestCase(HomeserverTestCase): self.assertEqual(second_id_gen.get_positions(), {"first": 8, "second": 9}) def test_get_next_txn(self): - """Test that the `get_next_txn` function works correctly. - """ + """Test that the `get_next_txn` function works correctly.""" # Prefill table with 7 rows written by 'master' self._insert_rows("master", 7) @@ -386,8 +388,7 @@ class MultiWriterIdGeneratorTestCase(HomeserverTestCase): self.assertEqual(id_gen_worker.get_positions(), {"master": 9}) def test_writer_config_change(self): - """Test that changing the writer config correctly works. - """ + """Test that changing the writer config correctly works.""" self._insert_row_with_id("first", 3) self._insert_row_with_id("second", 5) @@ -434,8 +435,7 @@ class MultiWriterIdGeneratorTestCase(HomeserverTestCase): self.assertEqual(id_gen_5.get_current_token_for_writer("third"), 6) def test_sequence_consistency(self): - """Test that we error out if the table and sequence diverges. - """ + """Test that we error out if the table and sequence diverges.""" # Prefill with some rows self._insert_row_with_id("master", 3) @@ -452,8 +452,7 @@ class MultiWriterIdGeneratorTestCase(HomeserverTestCase): class BackwardsMultiWriterIdGeneratorTestCase(HomeserverTestCase): - """Tests MultiWriterIdGenerator that produce *negative* stream IDs. - """ + """Tests MultiWriterIdGenerator that produce *negative* stream IDs.""" if not USE_POSTGRES_FOR_TESTS: skip = "Requires Postgres" @@ -494,12 +493,15 @@ class BackwardsMultiWriterIdGeneratorTestCase(HomeserverTestCase): return self.get_success(self.db_pool.runWithConnection(_create)) def _insert_row(self, instance_name: str, stream_id: int): - """Insert one row as the given instance with given stream_id. - """ + """Insert one row as the given instance with given stream_id.""" def _insert(txn): txn.execute( - "INSERT INTO foobar VALUES (?, ?)", (stream_id, instance_name,), + "INSERT INTO foobar VALUES (?, ?)", + ( + stream_id, + instance_name, + ), ) txn.execute( """ diff --git a/tests/storage/test_monthly_active_users.py b/tests/storage/test_monthly_active_users.py index 8d97b6d4cd..5858c7fcc4 100644 --- a/tests/storage/test_monthly_active_users.py +++ b/tests/storage/test_monthly_active_users.py @@ -198,7 +198,7 @@ class MonthlyActiveUsersTestCase(unittest.HomeserverTestCase): # value, although it gets stored on the config object as mau_limits. @override_config({"max_mau_value": 5, "mau_limit_reserved_threepids": gen_3pids(5)}) def test_reap_monthly_active_users_reserved_users(self): - """ Tests that reaping correctly handles reaping where reserved users are + """Tests that reaping correctly handles reaping where reserved users are present""" threepids = self.hs.config.mau_limits_reserved_threepids initial_users = len(threepids) diff --git a/tests/storage/test_redaction.py b/tests/storage/test_redaction.py index a6303bf0ee..b2a0e60856 100644 --- a/tests/storage/test_redaction.py +++ b/tests/storage/test_redaction.py @@ -299,8 +299,7 @@ class RedactionTestCase(unittest.HomeserverTestCase): ) def test_redact_censor(self): - """Test that a redacted event gets censored in the DB after a month - """ + """Test that a redacted event gets censored in the DB after a month""" self.get_success( self.inject_room_member(self.room1, self.u_alice, Membership.JOIN) @@ -370,8 +369,7 @@ class RedactionTestCase(unittest.HomeserverTestCase): self.assert_dict({"content": {}}, json.loads(event_json)) def test_redact_redaction(self): - """Tests that we can redact a redaction and can fetch it again. - """ + """Tests that we can redact a redaction and can fetch it again.""" self.get_success( self.inject_room_member(self.room1, self.u_alice, Membership.JOIN) @@ -404,8 +402,7 @@ class RedactionTestCase(unittest.HomeserverTestCase): ) def test_store_redacted_redaction(self): - """Tests that we can store a redacted redaction. - """ + """Tests that we can store a redacted redaction.""" self.get_success( self.inject_room_member(self.room1, self.u_alice, Membership.JOIN) diff --git a/tests/storage/test_registration.py b/tests/storage/test_registration.py index c8c7a90e5d..abbaed7cdc 100644 --- a/tests/storage/test_registration.py +++ b/tests/storage/test_registration.py @@ -145,7 +145,10 @@ class RegistrationStoreTestCase(unittest.TestCase): try: yield defer.ensureDeferred( self.store.validate_threepid_session( - "fake_sid", "fake_client_secret", "fake_token", 0, + "fake_sid", + "fake_client_secret", + "fake_token", + 0, ) ) except ThreepidValidationError as e: @@ -158,7 +161,10 @@ class RegistrationStoreTestCase(unittest.TestCase): try: yield defer.ensureDeferred( self.store.validate_threepid_session( - "fake_sid", "fake_client_secret", "fake_token", 0, + "fake_sid", + "fake_client_secret", + "fake_token", + 0, ) ) except ThreepidValidationError as e: -- cgit 1.5.1 From c8d9383cfb0985a99bf10756ea2dcb0b6329859d Mon Sep 17 00:00:00 2001 From: Dirk Klimpel <5740567+dklimpel@users.noreply.github.com> Date: Wed, 17 Feb 2021 21:19:23 +0100 Subject: Add the shadow-banning status to the display user admin API. (#9400) --- changelog.d/9400.feature | 1 + docs/admin_api/user_admin_api.rst | 9 ++++++--- synapse/storage/databases/main/__init__.py | 2 +- synapse/storage/databases/main/registration.py | 7 +++++-- tests/rest/admin/test_user.py | 2 ++ tests/storage/test_registration.py | 1 + 6 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 changelog.d/9400.feature (limited to 'tests/storage') diff --git a/changelog.d/9400.feature b/changelog.d/9400.feature new file mode 100644 index 0000000000..3067c3907b --- /dev/null +++ b/changelog.d/9400.feature @@ -0,0 +1 @@ +Add the shadow-banning status to the display user admin API. \ No newline at end of file diff --git a/docs/admin_api/user_admin_api.rst b/docs/admin_api/user_admin_api.rst index 1eb674939e..33dfbcfb49 100644 --- a/docs/admin_api/user_admin_api.rst +++ b/docs/admin_api/user_admin_api.rst @@ -29,8 +29,9 @@ It returns a JSON body like the following: } ], "avatar_url": "", - "admin": false, - "deactivated": false, + "admin": 0, + "deactivated": 0, + "shadow_banned": 0, "password_hash": "$2b$12$p9B4GkqYdRTPGD", "creation_ts": 1560432506, "appservice_id": null, @@ -150,6 +151,7 @@ A JSON body is returned with the following shape: "admin": 0, "user_type": null, "deactivated": 0, + "shadow_banned": 0, "displayname": "", "avatar_url": null }, { @@ -158,6 +160,7 @@ A JSON body is returned with the following shape: "admin": 1, "user_type": null, "deactivated": 0, + "shadow_banned": 0, "displayname": "", "avatar_url": "" } @@ -262,7 +265,7 @@ The following actions are performed when deactivating an user: - Reject all pending invites - Remove all account validity information related to the user -The following additional actions are performed during deactivation if``erase`` +The following additional actions are performed during deactivation if ``erase`` is set to ``true``: - Remove the user's display name diff --git a/synapse/storage/databases/main/__init__.py b/synapse/storage/databases/main/__init__.py index 5d0845588c..70b49854cf 100644 --- a/synapse/storage/databases/main/__init__.py +++ b/synapse/storage/databases/main/__init__.py @@ -340,7 +340,7 @@ class DataStore( count = txn.fetchone()[0] sql = ( - "SELECT name, user_type, is_guest, admin, deactivated, displayname, avatar_url " + "SELECT name, user_type, is_guest, admin, deactivated, shadow_banned, displayname, avatar_url " + sql_base + " ORDER BY u.name LIMIT ? OFFSET ?" ) diff --git a/synapse/storage/databases/main/registration.py b/synapse/storage/databases/main/registration.py index 07e219aaed..d5b5507815 100644 --- a/synapse/storage/databases/main/registration.py +++ b/synapse/storage/databases/main/registration.py @@ -113,6 +113,7 @@ class RegistrationWorkerStore(CacheInvalidationWorkerStore): "creation_ts", "user_type", "deactivated", + "shadow_banned", ], allow_none=True, desc="get_user_by_id", @@ -372,23 +373,25 @@ class RegistrationWorkerStore(CacheInvalidationWorkerStore): """ def set_shadow_banned_txn(txn): + user_id = user.to_string() self.db_pool.simple_update_one_txn( txn, table="users", - keyvalues={"name": user.to_string()}, + keyvalues={"name": user_id}, updatevalues={"shadow_banned": shadow_banned}, ) # In order for this to apply immediately, clear the cache for this user. tokens = self.db_pool.simple_select_onecol_txn( txn, table="access_tokens", - keyvalues={"user_id": user.to_string()}, + keyvalues={"user_id": user_id}, retcol="token", ) for token in tokens: self._invalidate_cache_and_stream( txn, self.get_user_by_access_token, (token,) ) + self._invalidate_cache_and_stream(txn, self.get_user_by_id, (user_id,)) await self.db_pool.runInteraction("set_shadow_banned", set_shadow_banned_txn) diff --git a/tests/rest/admin/test_user.py b/tests/rest/admin/test_user.py index ff75199c8e..ba26895391 100644 --- a/tests/rest/admin/test_user.py +++ b/tests/rest/admin/test_user.py @@ -769,6 +769,7 @@ class UsersListTestCase(unittest.HomeserverTestCase): self.assertIn("admin", u) self.assertIn("user_type", u) self.assertIn("deactivated", u) + self.assertIn("shadow_banned", u) self.assertIn("displayname", u) self.assertIn("avatar_url", u) @@ -1146,6 +1147,7 @@ class UserRestTestCase(unittest.HomeserverTestCase): self.assertEqual(False, channel.json_body["admin"]) self.assertEqual(False, channel.json_body["is_guest"]) self.assertEqual(False, channel.json_body["deactivated"]) + self.assertEqual(False, channel.json_body["shadow_banned"]) self.assertEqual("mxc://fibble/wibble", channel.json_body["avatar_url"]) @override_config( diff --git a/tests/storage/test_registration.py b/tests/storage/test_registration.py index abbaed7cdc..4eb41c46e8 100644 --- a/tests/storage/test_registration.py +++ b/tests/storage/test_registration.py @@ -52,6 +52,7 @@ class RegistrationStoreTestCase(unittest.TestCase): "creation_ts": 1000, "user_type": None, "deactivated": 0, + "shadow_banned": 0, }, (yield defer.ensureDeferred(self.store.get_user_by_id(self.user_id))), ) -- cgit 1.5.1