test_api.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. hostname="myserver",
  35. )
  36. def test_query_3pe_authenticates_token(self):
  37. """
  38. Tests that 3pe queries to the appservice are authenticated
  39. with the appservice's token.
  40. """
  41. SUCCESS_RESULT_USER = [
  42. {
  43. "protocol": PROTOCOL,
  44. "userid": "@a:user",
  45. "fields": {
  46. "more": "fields",
  47. },
  48. }
  49. ]
  50. SUCCESS_RESULT_LOCATION = [
  51. {
  52. "protocol": PROTOCOL,
  53. "alias": "#a:room",
  54. "fields": {
  55. "more": "fields",
  56. },
  57. }
  58. ]
  59. URL_USER = f"{URL}/_matrix/app/unstable/thirdparty/user/{PROTOCOL}"
  60. URL_LOCATION = f"{URL}/_matrix/app/unstable/thirdparty/location/{PROTOCOL}"
  61. self.request_url = None
  62. async def get_json(url: str, args: Mapping[Any, Any]) -> List[JsonDict]:
  63. if not args.get(b"access_token"):
  64. raise RuntimeError("Access token not provided")
  65. self.assertEqual(args.get(b"access_token"), TOKEN)
  66. self.request_url = url
  67. if url == URL_USER:
  68. return SUCCESS_RESULT_USER
  69. elif url == URL_LOCATION:
  70. return SUCCESS_RESULT_LOCATION
  71. else:
  72. raise RuntimeError(
  73. "URL provided was invalid. This should never be seen."
  74. )
  75. # We assign to a method, which mypy doesn't like.
  76. self.api.get_json = Mock(side_effect=get_json) # type: ignore[assignment]
  77. result = self.get_success(
  78. self.api.query_3pe(self.service, "user", PROTOCOL, {b"some": [b"field"]})
  79. )
  80. self.assertEqual(self.request_url, URL_USER)
  81. self.assertEqual(result, SUCCESS_RESULT_USER)
  82. result = self.get_success(
  83. self.api.query_3pe(
  84. self.service, "location", PROTOCOL, {b"some": [b"field"]}
  85. )
  86. )
  87. self.assertEqual(self.request_url, URL_LOCATION)
  88. self.assertEqual(result, SUCCESS_RESULT_LOCATION)