test_end_to_end_keys.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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()
  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(
  31. "user", "device", None
  32. )
  33. yield self.store.set_e2e_device_keys(
  34. "user", "device", now, json)
  35. res = yield self.store.get_e2e_device_keys((("user", "device"),))
  36. self.assertIn("user", res)
  37. self.assertIn("device", res["user"])
  38. dev = res["user"]["device"]
  39. self.assertDictContainsSubset({
  40. "keys": json,
  41. "device_display_name": None,
  42. }, dev)
  43. @defer.inlineCallbacks
  44. def test_get_key_with_device_name(self):
  45. now = 1470174257070
  46. json = {"key": "value"}
  47. yield self.store.set_e2e_device_keys(
  48. "user", "device", now, json)
  49. yield self.store.store_device(
  50. "user", "device", "display_name"
  51. )
  52. res = yield self.store.get_e2e_device_keys((("user", "device"),))
  53. self.assertIn("user", res)
  54. self.assertIn("device", res["user"])
  55. dev = res["user"]["device"]
  56. self.assertDictContainsSubset({
  57. "keys": json,
  58. "device_display_name": "display_name",
  59. }, dev)
  60. @defer.inlineCallbacks
  61. def test_multiple_devices(self):
  62. now = 1470174257070
  63. yield self.store.store_device(
  64. "user1", "device1", None
  65. )
  66. yield self.store.store_device(
  67. "user1", "device2", None
  68. )
  69. yield self.store.store_device(
  70. "user2", "device1", None
  71. )
  72. yield self.store.store_device(
  73. "user2", "device2", None
  74. )
  75. yield self.store.set_e2e_device_keys(
  76. "user1", "device1", now, 'json11')
  77. yield self.store.set_e2e_device_keys(
  78. "user1", "device2", now, 'json12')
  79. yield self.store.set_e2e_device_keys(
  80. "user2", "device1", now, 'json21')
  81. yield self.store.set_e2e_device_keys(
  82. "user2", "device2", now, 'json22')
  83. res = yield self.store.get_e2e_device_keys((("user1", "device1"),
  84. ("user2", "device2")))
  85. self.assertIn("user1", res)
  86. self.assertIn("device1", res["user1"])
  87. self.assertNotIn("device2", res["user1"])
  88. self.assertIn("user2", res)
  89. self.assertNotIn("device1", res["user2"])
  90. self.assertIn("device2", res["user2"])