test_receipts.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 twisted.test.proto_helpers import MemoryReactor
  15. import synapse.rest.admin
  16. from synapse.rest.client import login, receipts, register
  17. from synapse.server import HomeServer
  18. from synapse.util import Clock
  19. from tests import unittest
  20. class ReceiptsTestCase(unittest.HomeserverTestCase):
  21. servlets = [
  22. login.register_servlets,
  23. register.register_servlets,
  24. receipts.register_servlets,
  25. synapse.rest.admin.register_servlets,
  26. ]
  27. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  28. self.owner = self.register_user("owner", "pass")
  29. self.owner_tok = self.login("owner", "pass")
  30. def test_send_receipt(self) -> None:
  31. channel = self.make_request(
  32. "POST",
  33. "/rooms/!abc:beep/receipt/m.read/$def",
  34. content={},
  35. access_token=self.owner_tok,
  36. )
  37. self.assertEqual(channel.code, 200, channel.result)
  38. def test_send_receipt_invalid_room_id(self) -> None:
  39. channel = self.make_request(
  40. "POST",
  41. "/rooms/not-a-room-id/receipt/m.read/$def",
  42. content={},
  43. access_token=self.owner_tok,
  44. )
  45. self.assertEqual(channel.code, 400, channel.result)
  46. self.assertEqual(
  47. channel.json_body["error"], "A valid room ID and event ID must be specified"
  48. )
  49. def test_send_receipt_invalid_event_id(self) -> None:
  50. channel = self.make_request(
  51. "POST",
  52. "/rooms/!abc:beep/receipt/m.read/not-an-event-id",
  53. content={},
  54. access_token=self.owner_tok,
  55. )
  56. self.assertEqual(channel.code, 400, channel.result)
  57. self.assertEqual(
  58. channel.json_body["error"], "A valid room ID and event ID must be specified"
  59. )
  60. def test_send_receipt_invalid_receipt_type(self) -> None:
  61. channel = self.make_request(
  62. "POST",
  63. "/rooms/!abc:beep/receipt/invalid-receipt-type/$def",
  64. content={},
  65. access_token=self.owner_tok,
  66. )
  67. self.assertEqual(channel.code, 400, channel.result)