Debian Codegraph @

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

raw
1 import typing as t
2 import uuid
3
4 from responses.matchers import json_params_matcher
5
6 from globus_sdk import DependentScopeSpec
7 from globus_sdk.testing.models import RegisteredResponse, ResponseSet
8
9 SCOPE_REQUEST_ARGS = {
10 "client_id": str(uuid.uuid1()),
11 "name": "Client manage scope",
12 "description": "Manage configuration of this client",
13 "scope_suffix": "manage",
14 }
15
16
17 def make_request_body(request_args: t.Mapping[str, t.Any]) -> t.Dict[str, t.Any]:
18 request_body = {
19 "name": request_args["name"],
20 "description": request_args["description"],
21 "scope_suffix": request_args["scope_suffix"],
22 }
23
24 if "advertised" in request_args:
25 request_body["advertised"] = request_args["advertised"]
26
27 if "allows_refresh_token" in request_args:
28 request_body["allows_refresh_token"] = request_args["allows_refresh_token"]
29
30 if "required_domains" in request_args:
31 request_body["required_domains"] = request_args["required_domains"]
32
33 if "dependent_scopes" in request_args:
34 request_body["dependent_scopes"] = request_args["dependent_scopes"]
35
36 return request_body
37
38
39 def make_response_body(request_args: t.Mapping[str, t.Any]) -> t.Dict[str, t.Any]:
40 return {
41 "scope_string": f"https://auth.globus.org/scopes/{request_args['client_id']}/{request_args['scope_suffix']}", # noqa: E501
42 "allows_refresh_token": request_args.get("allows_refresh_token", True),
43 "id": str(uuid.uuid1()),
44 "advertised": request_args.get("advertised", False),
45 "required_domains": request_args.get("required_domains", []),
46 "name": request_args["name"],
47 "description": request_args["description"],
48 "client": str(request_args["client_id"]),
49 "dependent_scopes": [
50 {
51 "scope": str(ds["scope"]),
52 "optional": ds["optional"],
53 "requires_refresh_token": ds["requires_refresh_token"],
54 }
55 for ds in request_args.get("dependent_scopes", [])
56 ],
57 }
58
59
60 def register_response(
61 args: t.Mapping[str, t.Any],
62 ) -> RegisteredResponse:
63 request_args = {**SCOPE_REQUEST_ARGS, **args}
64 request_body = make_request_body(request_args)
65 response_body = make_response_body(request_args)
66
67 return RegisteredResponse(
68 service="auth",
69 method="POST",
70 path=f"/v2/api/clients/{request_args['client_id']}/scopes",
71 json={"scope": response_body},
72 metadata={
73 # Test functions use 'args' to form request
74 "args": request_args,
75 # Test functions use 'response' to verify response
76 "response": response_body,
77 },
78 match=[json_params_matcher({"scope": request_body})],
79 )
80
81
82 RESPONSES = ResponseSet(
83 default=register_response({}),
84 client_id_str=register_response({"client_id": str(uuid.uuid1())}),
85 client_id_uuid=register_response({"client_id": uuid.uuid1()}),
86 name=register_response({"name": str(uuid.uuid4()).replace("-", "")}),
87 description=register_response({"description": str(uuid.uuid4()).replace("-", "")}),
88 scope_suffix=register_response(
89 {"scope_suffix": str(uuid.uuid4()).replace("-", "")}
90 ),
91 advertised=register_response({"advertised": True}),
92 not_advertised=register_response({"advertised": False}),
93 allows_refresh_token=register_response({"allows_refresh_token": True}),
94 disallows_refresh_token=register_response({"allows_refresh_token": False}),
95 no_required_domains=register_response({"required_domains": []}),
96 required_domains=register_response(
97 {"required_domains": ["globus.org", "uchicago.edu"]}
98 ),
99 no_dependent_scopes=register_response({"dependent_scopes": []}),
100 dependent_scopes=register_response(
101 {
102 "dependent_scopes": [
103 DependentScopeSpec(str(uuid.uuid1()), True, True),
104 DependentScopeSpec(uuid.uuid1(), False, False),
105 ],
106 }
107 ),
108 )