1
0

test_consent.py 3.4 KB

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