1
0

test_consent.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright 2018 New Vector Ltd
  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. import os
  15. from twisted.test.proto_helpers import MemoryReactor
  16. import synapse.rest.admin
  17. from synapse.rest.client import login, room, sync
  18. from synapse.server import HomeServer
  19. from synapse.util import Clock
  20. from tests import unittest
  21. class ConsentNoticesTests(unittest.HomeserverTestCase):
  22. servlets = [
  23. sync.register_servlets,
  24. synapse.rest.admin.register_servlets_for_client_rest_resource,
  25. login.register_servlets,
  26. room.register_servlets,
  27. ]
  28. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  29. tmpdir = self.mktemp()
  30. os.mkdir(tmpdir)
  31. self.consent_notice_message = "consent %(consent_uri)s"
  32. config = self.default_config()
  33. config["user_consent"] = {
  34. "version": "1",
  35. "template_dir": tmpdir,
  36. "server_notice_content": {
  37. "msgtype": "m.text",
  38. "body": self.consent_notice_message,
  39. },
  40. }
  41. config["public_baseurl"] = "https://example.com/"
  42. config["form_secret"] = "123abc"
  43. config["server_notices"] = {
  44. "system_mxid_localpart": "notices",
  45. "system_mxid_display_name": "test display name",
  46. "system_mxid_avatar_url": None,
  47. "room_name": "Server Notices",
  48. }
  49. return self.setup_test_homeserver(config=config)
  50. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  51. self.user_id = self.register_user("bob", "abc123")
  52. self.access_token = self.login("bob", "abc123")
  53. def test_get_sync_message(self) -> None:
  54. """
  55. When user consent server notices are enabled, a sync will cause a notice
  56. to fire (in a room which the user is invited to). The notice contains
  57. the notice URL + an authentication code.
  58. """
  59. # Initial sync, to get the user consent room invite
  60. channel = self.make_request(
  61. "GET", "/_matrix/client/r0/sync", access_token=self.access_token
  62. )
  63. self.assertEqual(channel.code, 200)
  64. # Get the Room ID to join
  65. room_id = list(channel.json_body["rooms"]["invite"].keys())[0]
  66. # Join the room
  67. channel = self.make_request(
  68. "POST",
  69. "/_matrix/client/r0/rooms/" + room_id + "/join",
  70. access_token=self.access_token,
  71. )
  72. self.assertEqual(channel.code, 200)
  73. # Sync again, to get the message in the room
  74. channel = self.make_request(
  75. "GET", "/_matrix/client/r0/sync", access_token=self.access_token
  76. )
  77. self.assertEqual(channel.code, 200)
  78. # Get the message
  79. room = channel.json_body["rooms"]["join"][room_id]
  80. messages = [
  81. x for x in room["timeline"]["events"] if x["type"] == "m.room.message"
  82. ]
  83. # One message, with the consent URL
  84. self.assertEqual(len(messages), 1)
  85. self.assertTrue(
  86. messages[0]["content"]["body"].startswith(
  87. "consent https://example.com/_matrix/consent"
  88. )
  89. )