1
0

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