test_end_to_end_keys.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from twisted.internet import defer
  16. import tests.unittest
  17. import tests.utils
  18. class EndToEndKeyStoreTestCase(tests.unittest.TestCase):
  19. def __init__(self, *args, **kwargs):
  20. super(EndToEndKeyStoreTestCase, self).__init__(*args, **kwargs)
  21. self.store = None # type: synapse.storage.DataStore
  22. @defer.inlineCallbacks
  23. def setUp(self):
  24. hs = yield tests.utils.setup_test_homeserver(self.addCleanup)
  25. self.store = hs.get_datastore()
  26. @defer.inlineCallbacks
  27. def test_key_without_device_name(self):
  28. now = 1470174257070
  29. json = {"key": "value"}
  30. yield self.store.store_device("user", "device", None)
  31. yield self.store.set_e2e_device_keys("user", "device", now, json)
  32. res = yield self.store.get_e2e_device_keys((("user", "device"),))
  33. self.assertIn("user", res)
  34. self.assertIn("device", res["user"])
  35. dev = res["user"]["device"]
  36. self.assertDictContainsSubset({"keys": json, "device_display_name": None}, dev)
  37. @defer.inlineCallbacks
  38. def test_get_key_with_device_name(self):
  39. now = 1470174257070
  40. json = {"key": "value"}
  41. yield self.store.set_e2e_device_keys("user", "device", now, json)
  42. yield self.store.store_device("user", "device", "display_name")
  43. res = yield self.store.get_e2e_device_keys((("user", "device"),))
  44. self.assertIn("user", res)
  45. self.assertIn("device", res["user"])
  46. dev = res["user"]["device"]
  47. self.assertDictContainsSubset(
  48. {"keys": json, "device_display_name": "display_name"}, dev
  49. )
  50. @defer.inlineCallbacks
  51. def test_multiple_devices(self):
  52. now = 1470174257070
  53. yield self.store.store_device("user1", "device1", None)
  54. yield self.store.store_device("user1", "device2", None)
  55. yield self.store.store_device("user2", "device1", None)
  56. yield self.store.store_device("user2", "device2", None)
  57. yield self.store.set_e2e_device_keys("user1", "device1", now, 'json11')
  58. yield self.store.set_e2e_device_keys("user1", "device2", now, 'json12')
  59. yield self.store.set_e2e_device_keys("user2", "device1", now, 'json21')
  60. yield self.store.set_e2e_device_keys("user2", "device2", now, 'json22')
  61. res = yield self.store.get_e2e_device_keys(
  62. (("user1", "device1"), ("user2", "device2"))
  63. )
  64. self.assertIn("user1", res)
  65. self.assertIn("device1", res["user1"])
  66. self.assertNotIn("device2", res["user1"])
  67. self.assertIn("user2", res)
  68. self.assertNotIn("device1", res["user2"])
  69. self.assertIn("device2", res["user2"])