Debian Codegraph @

src/globus_sdk/testing/data/auth/create_child_client.py

raw
1 import typing as t
2 import uuid
3
4 from responses.matchers import json_params_matcher
5
6 from globus_sdk.testing.models import RegisteredResponse, ResponseSet
7
8 _COMMON_RESPONSE_RECORD: dict[str, t.Any] = {
9 "fqdns": [],
10 "links": {"privacy_policy": None, "terms_and_conditions": None},
11 "parent_client": None,
12 "preselect_idp": None,
13 "prompt_for_named_grant": True,
14 "redirect_uris": [],
15 "required_idp": None,
16 "scopes": [],
17 "userinfo_from_effective_identity": True,
18 }
19
20 PUBLIC_CLIENT_RESPONSE_RECORD: dict[str, t.Any] = {
21 "grant_types": ["authorization_code", "refresh_token"],
22 **_COMMON_RESPONSE_RECORD,
23 }
24
25 PRIVATE_CLIENT_RESPONSE_RECORD: dict[str, t.Any] = {
26 "grant_types": [
27 "authorization_code",
28 "client_credentials",
29 "refresh_token",
30 "urn:globus:auth:grant_type:dependent_token",
31 ],
32 **_COMMON_RESPONSE_RECORD,
33 }
34
35 PUBLIC_CLIENT_REQUEST_ARGS: dict[str, t.Any] = {
36 "name": "FOO",
37 "visibility": "public",
38 }
39
40
41 def register_response(
42 args: t.Mapping[str, t.Any],
43 ) -> RegisteredResponse:
44 request_args = {
45 **PUBLIC_CLIENT_REQUEST_ARGS,
46 **args,
47 }
48 # Default to public_client=True
49 if "public_client" not in args and "client_type" not in args:
50 request_args["public_client"] = True
51
52 # The request body follows almost directly from the request args. Some name of args
53 # to create_client() have differenlty named fields.
54 request_body: t.Dict[str, t.Any] = {}
55 for arg_name in request_args:
56 if arg_name == "terms_and_conditions" or arg_name == "privacy_policy":
57 request_body["links"] = {
58 arg_name: request_args[arg_name],
59 **request_body.get("links", {}),
60 }
61 else:
62 request_body[arg_name] = request_args[arg_name]
63
64 client_response_record = {}
65 if (
66 request_args.get("public_client", False)
67 or request_args.get("client_type") == "public_installed_client"
68 ):
69 client_response_record = {
70 **PUBLIC_CLIENT_RESPONSE_RECORD,
71 **request_body,
72 }
73 if "client_type" not in client_response_record:
74 client_response_record["client_type"] = "public_installed_client"
75 else:
76 client_response_record = {
77 **PRIVATE_CLIENT_RESPONSE_RECORD,
78 **request_body,
79 }
80 if "client_type" not in client_response_record:
81 client_response_record["client_type"] = (
82 "hybrid_confidential_client_resource_server"
83 )
84
85 return RegisteredResponse(
86 service="auth",
87 method="POST",
88 path="/v2/api/clients",
89 json={"client": client_response_record},
90 metadata={
91 # Test functions use 'args' to form request
92 "args": request_args,
93 # Test functions use 'response' to verify response
94 "response": client_response_record,
95 },
96 match=[json_params_matcher({"client": request_body})],
97 )
98
99
100 RESPONSES = ResponseSet(
101 default=register_response({}),
102 name=register_response({"name": str(uuid.uuid4()).replace("-", "")}),
103 public_client=register_response({"public_client": True}),
104 private_client=register_response({"public_client": False}),
105 publicly_visible=register_response({"visibility": "public"}),
106 not_publicly_visible=register_response({"visibility": "private"}),
107 redirect_uris=register_response({"redirect_uris": ["https://foo.com"]}),
108 links=register_response(
109 {
110 "terms_and_conditions": "https://foo.org",
111 "privacy_policy": "https://boo.org",
112 }
113 ),
114 required_idp=register_response({"required_idp": str(uuid.uuid1())}),
115 preselect_idp=register_response({"preselect_idp": str(uuid.uuid1())}),
116 client_type_confidential_client=register_response(
117 {"client_type": "confidential_client"}
118 ),
119 client_type_public_installed_client=register_response(
120 {"client_type": "public_installed_client"}
121 ),
122 client_type_client_identity=register_response({"client_type": "client_identity"}),
123 client_type_resource_server=register_response({"client_type": "resource_server"}),
124 client_type_globus_connect_server=register_response(
125 {"client_type": "globus_connect_server"}
126 ),
127 client_type_hybrid_confidential_client_resource_server=register_response(
128 {"client_type": "hybrid_confidential_client_resource_server"}
129 ),
130 client_type_public_webapp_client=register_response(
131 {"client_type": "public_webapp_client"}
132 ),
133 )