test_api.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, Sequence, Union
  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) -> None:
  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) -> None:
  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(
  62. url: str,
  63. args: Mapping[Any, Any],
  64. headers: Mapping[Union[str, bytes], Sequence[Union[str, bytes]]],
  65. ) -> List[JsonDict]:
  66. # Ensure the access token is passed as both a header and query arg.
  67. if not headers.get("Authorization") or not args.get(b"access_token"):
  68. raise RuntimeError("Access token not provided")
  69. self.assertEqual(headers.get("Authorization"), [f"Bearer {TOKEN}"])
  70. self.assertEqual(args.get(b"access_token"), TOKEN)
  71. self.request_url = url
  72. if url == URL_USER:
  73. return SUCCESS_RESULT_USER
  74. elif url == URL_LOCATION:
  75. return SUCCESS_RESULT_LOCATION
  76. else:
  77. raise RuntimeError(
  78. "URL provided was invalid. This should never be seen."
  79. )
  80. # We assign to a method, which mypy doesn't like.
  81. self.api.get_json = Mock(side_effect=get_json) # type: ignore[assignment]
  82. result = self.get_success(
  83. self.api.query_3pe(self.service, "user", PROTOCOL, {b"some": [b"field"]})
  84. )
  85. self.assertEqual(self.request_url, URL_USER)
  86. self.assertEqual(result, SUCCESS_RESULT_USER)
  87. result = self.get_success(
  88. self.api.query_3pe(
  89. self.service, "location", PROTOCOL, {b"some": [b"field"]}
  90. )
  91. )
  92. self.assertEqual(self.request_url, URL_LOCATION)
  93. self.assertEqual(result, SUCCESS_RESULT_LOCATION)