test_presence.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 twisted.internet import defer
  16. from synapse.storage.presence import PresenceStore
  17. from synapse.types import UserID
  18. from tests import unittest
  19. from tests.utils import MockClock, setup_test_homeserver
  20. class PresenceStoreTestCase(unittest.TestCase):
  21. @defer.inlineCallbacks
  22. def setUp(self):
  23. hs = yield setup_test_homeserver(self.addCleanup, clock=MockClock())
  24. self.store = PresenceStore(None, 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. (
  32. yield self.store.get_presence_list(
  33. observer_localpart=self.u_apple.localpart
  34. )
  35. ),
  36. )
  37. self.assertEquals(
  38. [],
  39. (
  40. yield self.store.get_presence_list(
  41. observer_localpart=self.u_apple.localpart, accepted=True
  42. )
  43. ),
  44. )
  45. yield self.store.add_presence_list_pending(
  46. observer_localpart=self.u_apple.localpart,
  47. observed_userid=self.u_banana.to_string(),
  48. )
  49. self.assertEquals(
  50. [{"observed_user_id": "@banana:test", "accepted": 0}],
  51. (
  52. yield self.store.get_presence_list(
  53. observer_localpart=self.u_apple.localpart
  54. )
  55. ),
  56. )
  57. self.assertEquals(
  58. [],
  59. (
  60. yield self.store.get_presence_list(
  61. observer_localpart=self.u_apple.localpart, accepted=True
  62. )
  63. ),
  64. )
  65. yield self.store.set_presence_list_accepted(
  66. observer_localpart=self.u_apple.localpart,
  67. observed_userid=self.u_banana.to_string(),
  68. )
  69. self.assertEquals(
  70. [{"observed_user_id": "@banana:test", "accepted": 1}],
  71. (
  72. yield self.store.get_presence_list(
  73. observer_localpart=self.u_apple.localpart
  74. )
  75. ),
  76. )
  77. self.assertEquals(
  78. [{"observed_user_id": "@banana:test", "accepted": 1}],
  79. (
  80. yield self.store.get_presence_list(
  81. observer_localpart=self.u_apple.localpart, accepted=True
  82. )
  83. ),
  84. )
  85. yield self.store.del_presence_list(
  86. observer_localpart=self.u_apple.localpart,
  87. observed_userid=self.u_banana.to_string(),
  88. )
  89. self.assertEquals(
  90. [],
  91. (
  92. yield self.store.get_presence_list(
  93. observer_localpart=self.u_apple.localpart
  94. )
  95. ),
  96. )
  97. self.assertEquals(
  98. [],
  99. (
  100. yield self.store.get_presence_list(
  101. observer_localpart=self.u_apple.localpart, accepted=True
  102. )
  103. ),
  104. )