test_unsafe_locale.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 unittest.mock import MagicMock, patch
  15. from synapse.storage.database import make_conn
  16. from synapse.storage.engines._base import IncorrectDatabaseSetup
  17. from tests.unittest import HomeserverTestCase
  18. from tests.utils import USE_POSTGRES_FOR_TESTS
  19. class UnsafeLocaleTest(HomeserverTestCase):
  20. if not USE_POSTGRES_FOR_TESTS:
  21. skip = "Requires Postgres"
  22. @patch("synapse.storage.engines.postgres.PostgresEngine.get_db_locale")
  23. def test_unsafe_locale(self, mock_db_locale: MagicMock) -> None:
  24. mock_db_locale.return_value = ("B", "B")
  25. database = self.hs.get_datastores().databases[0]
  26. db_conn = make_conn(database._database_config, database.engine, "test_unsafe")
  27. with self.assertRaises(IncorrectDatabaseSetup):
  28. database.engine.check_database(db_conn)
  29. with self.assertRaises(IncorrectDatabaseSetup):
  30. database.engine.check_new_database(db_conn)
  31. db_conn.close()
  32. def test_safe_locale(self) -> None:
  33. database = self.hs.get_datastores().databases[0]
  34. db_conn = make_conn(database._database_config, database.engine, "test_unsafe")
  35. with db_conn.cursor() as txn:
  36. res = database.engine.get_db_locale(txn)
  37. self.assertEqual(res, ("C", "C"))
  38. db_conn.close()