1
0

test_consent.py 3.6 KB

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