test_presence.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-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 tests import unittest
  16. from twisted.internet import defer
  17. from synapse.storage.presence import PresenceStore
  18. from synapse.types import UserID
  19. from tests.utils import setup_test_homeserver, MockClock
  20. class PresenceStoreTestCase(unittest.TestCase):
  21. @defer.inlineCallbacks
  22. def setUp(self):
  23. hs = yield setup_test_homeserver(clock=MockClock())
  24. self.store = PresenceStore(hs)
  25. self.u_apple = UserID.from_string("@apple:test")
  26. self.u_banana = UserID.from_string("@banana:test")
  27. @defer.inlineCallbacks
  28. def test_presence_list(self):
  29. self.assertEquals(
  30. [],
  31. (yield self.store.get_presence_list(
  32. observer_localpart=self.u_apple.localpart,
  33. ))
  34. )
  35. self.assertEquals(
  36. [],
  37. (yield self.store.get_presence_list(
  38. observer_localpart=self.u_apple.localpart,
  39. accepted=True,
  40. ))
  41. )
  42. yield self.store.add_presence_list_pending(
  43. observer_localpart=self.u_apple.localpart,
  44. observed_userid=self.u_banana.to_string(),
  45. )
  46. self.assertEquals(
  47. [{"observed_user_id": "@banana:test", "accepted": 0}],
  48. (yield self.store.get_presence_list(
  49. observer_localpart=self.u_apple.localpart,
  50. ))
  51. )
  52. self.assertEquals(
  53. [],
  54. (yield self.store.get_presence_list(
  55. observer_localpart=self.u_apple.localpart,
  56. accepted=True,
  57. ))
  58. )
  59. yield self.store.set_presence_list_accepted(
  60. observer_localpart=self.u_apple.localpart,
  61. observed_userid=self.u_banana.to_string(),
  62. )
  63. self.assertEquals(
  64. [{"observed_user_id": "@banana:test", "accepted": 1}],
  65. (yield self.store.get_presence_list(
  66. observer_localpart=self.u_apple.localpart,
  67. ))
  68. )
  69. self.assertEquals(
  70. [{"observed_user_id": "@banana:test", "accepted": 1}],
  71. (yield self.store.get_presence_list(
  72. observer_localpart=self.u_apple.localpart,
  73. accepted=True,
  74. ))
  75. )
  76. yield self.store.del_presence_list(
  77. observer_localpart=self.u_apple.localpart,
  78. observed_userid=self.u_banana.to_string(),
  79. )
  80. self.assertEquals(
  81. [],
  82. (yield self.store.get_presence_list(
  83. observer_localpart=self.u_apple.localpart,
  84. ))
  85. )
  86. self.assertEquals(
  87. [],
  88. (yield self.store.get_presence_list(
  89. observer_localpart=self.u_apple.localpart,
  90. accepted=True,
  91. ))
  92. )