test_api.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 typing import Any, List, Mapping
  15. from unittest.mock import Mock
  16. from twisted.test.proto_helpers import MemoryReactor
  17. from synapse.appservice import ApplicationService
  18. from synapse.server import HomeServer
  19. from synapse.types import JsonDict
  20. from synapse.util import Clock
  21. from tests import unittest
  22. PROTOCOL = "myproto"
  23. TOKEN = "myastoken"
  24. URL = "http://mytestservice"
  25. class ApplicationServiceApiTestCase(unittest.HomeserverTestCase):
  26. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer):
  27. self.api = hs.get_application_service_api()
  28. self.service = ApplicationService(
  29. id="unique_identifier",
  30. sender="@as:test",
  31. url=URL,
  32. token="unused",
  33. hs_token=TOKEN,
  34. )
  35. def test_query_3pe_authenticates_token(self):
  36. """
  37. Tests that 3pe queries to the appservice are authenticated
  38. with the appservice's token.
  39. """
  40. SUCCESS_RESULT_USER = [
  41. {
  42. "protocol": PROTOCOL,
  43. "userid": "@a:user",
  44. "fields": {
  45. "more": "fields",
  46. },
  47. }
  48. ]
  49. SUCCESS_RESULT_LOCATION = [
  50. {
  51. "protocol": PROTOCOL,
  52. "alias": "#a:room",
  53. "fields": {
  54. "more": "fields",
  55. },
  56. }
  57. ]
  58. URL_USER = f"{URL}/_matrix/app/unstable/thirdparty/user/{PROTOCOL}"
  59. URL_LOCATION = f"{URL}/_matrix/app/unstable/thirdparty/location/{PROTOCOL}"
  60. self.request_url = None
  61. async def get_json(url: str, args: Mapping[Any, Any]) -> List[JsonDict]:
  62. if not args.get(b"access_token"):
  63. raise RuntimeError("Access token not provided")
  64. self.assertEqual(args.get(b"access_token"), TOKEN)
  65. self.request_url = url
  66. if url == URL_USER:
  67. return SUCCESS_RESULT_USER
  68. elif url == URL_LOCATION:
  69. return SUCCESS_RESULT_LOCATION
  70. else:
  71. raise RuntimeError(
  72. "URL provided was invalid. This should never be seen."
  73. )
  74. # We assign to a method, which mypy doesn't like.
  75. self.api.get_json = Mock(side_effect=get_json) # type: ignore[assignment]
  76. result = self.get_success(
  77. self.api.query_3pe(self.service, "user", PROTOCOL, {b"some": [b"field"]})
  78. )
  79. self.assertEqual(self.request_url, URL_USER)
  80. self.assertEqual(result, SUCCESS_RESULT_USER)
  81. result = self.get_success(
  82. self.api.query_3pe(
  83. self.service, "location", PROTOCOL, {b"some": [b"field"]}
  84. )
  85. )
  86. self.assertEqual(self.request_url, URL_LOCATION)
  87. self.assertEqual(result, SUCCESS_RESULT_LOCATION)