blob: 102b35f9cc7498aca39015eb1e2269dd450b046a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
from copy import deepcopy
from typing import Any, Dict
import yaml
from pydantic import ValidationError
from synapse.config.oidc2 import OIDCProviderModel
from tests.unittest import TestCase
SAMPLE_CONFIG = yaml.safe_load(
"""
idp_id: apple
idp_name: Apple
idp_icon: "mxc://matrix.org/blahblahblah"
idp_brand: "apple"
issuer: "https://appleid.apple.com"
client_id: "org.matrix.synapse.sso.service"
client_secret_jwt_key:
key: DUMMY_PRIVATE_KEY
jwt_header:
alg: ES256
kid: potato123
jwt_payload:
iss: issuer456
client_auth_method: "client_secret_post"
scopes: ["name", "email", "openid"]
authorization_endpoint: https://appleid.apple.com/auth/authorize?response_mode=form_post
user_mapping_provider:
config:
email_template: "{{ user.email }}"
localpart_template: "{{ user.email|localpart_from_email }}"
confirm_localpart: true
"""
)
class PydanticOIDCTestCase(TestCase):
# Each test gets a dummy config it can change as it sees fit
config: Dict[str, Any]
def setUp(self) -> None:
self.config = deepcopy(SAMPLE_CONFIG)
def test_idp_id(self) -> None:
"""Demonstrate that Pydantic validates idp_id correctly."""
OIDCProviderModel.parse_obj(self.config)
# Enforce that idp_id is required.
with self.assertRaises(ValidationError):
del self.config["idp_id"]
OIDCProviderModel.parse_obj(self.config)
# Enforce that idp_id is a string.
with self.assertRaises(ValidationError) as e:
self.config["idp_id"] = 123
OIDCProviderModel.parse_obj(self.config)
print(e.exception)
with self.assertRaises(ValidationError):
self.config["idp_id"] = None
OIDCProviderModel.parse_obj(self.config)
# Enforce a length between 1 and 250.
with self.assertRaises(ValidationError):
self.config["idp_id"] = ""
OIDCProviderModel.parse_obj(self.config)
with self.assertRaises(ValidationError):
self.config["idp_id"] = "a" * 251
OIDCProviderModel.parse_obj(self.config)
# Enforce the character set
with self.assertRaises(ValidationError):
self.config["idp_id"] = "$"
OIDCProviderModel.parse_obj(self.config)
# What happens with a really long string of prohibited characters?
with self.assertRaises(ValidationError) as e:
self.config["idp_id"] = "$" * 500
OIDCProviderModel.parse_obj(self.config)
print(e.exception)
|