test_models.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. import unittest as stdlib_unittest
  15. from typing import TYPE_CHECKING
  16. from typing_extensions import Literal
  17. from synapse._pydantic_compat import HAS_PYDANTIC_V2
  18. from synapse.rest.client.models import EmailRequestTokenBody
  19. if TYPE_CHECKING or HAS_PYDANTIC_V2:
  20. from pydantic.v1 import BaseModel, ValidationError
  21. else:
  22. from pydantic import BaseModel, ValidationError
  23. class ThreepidMediumEnumTestCase(stdlib_unittest.TestCase):
  24. class Model(BaseModel):
  25. medium: Literal["email", "msisdn"]
  26. def test_accepts_valid_medium_string(self) -> None:
  27. """Sanity check that Pydantic behaves sensibly with an enum-of-str
  28. This is arguably more of a test of a class that inherits from str and Enum
  29. simultaneously.
  30. """
  31. model = self.Model.parse_obj({"medium": "email"})
  32. self.assertEqual(model.medium, "email")
  33. def test_rejects_invalid_medium_value(self) -> None:
  34. with self.assertRaises(ValidationError):
  35. self.Model.parse_obj({"medium": "interpretive_dance"})
  36. def test_rejects_invalid_medium_type(self) -> None:
  37. with self.assertRaises(ValidationError):
  38. self.Model.parse_obj({"medium": 123})
  39. class EmailRequestTokenBodyTestCase(stdlib_unittest.TestCase):
  40. base_request = {
  41. "client_secret": "hunter2",
  42. "email": "alice@wonderland.com",
  43. "send_attempt": 1,
  44. }
  45. def test_token_required_if_id_server_provided(self) -> None:
  46. with self.assertRaises(ValidationError):
  47. EmailRequestTokenBody.parse_obj(
  48. {
  49. **self.base_request,
  50. "id_server": "identity.wonderland.com",
  51. }
  52. )
  53. with self.assertRaises(ValidationError):
  54. EmailRequestTokenBody.parse_obj(
  55. {
  56. **self.base_request,
  57. "id_server": "identity.wonderland.com",
  58. "id_access_token": None,
  59. }
  60. )
  61. def test_token_typechecked_when_id_server_provided(self) -> None:
  62. with self.assertRaises(ValidationError):
  63. EmailRequestTokenBody.parse_obj(
  64. {
  65. **self.base_request,
  66. "id_server": "identity.wonderland.com",
  67. "id_access_token": 1337,
  68. }
  69. )