| 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 |
"client_type": "public_installed_client", |
| 22 |
"grant_types": ["authorization_code", "refresh_token"], |
| 23 |
**_COMMON_RESPONSE_RECORD, |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
def register_response( |
| 28 |
args: t.Mapping[str, t.Any], |
| 29 |
) -> RegisteredResponse: |
| 30 |
# Some name of args to create_client() have differenlty named fields. |
| 31 |
body_fields: t.Dict[str, t.Any] = {} |
| 32 |
for arg_name in args: |
| 33 |
if arg_name == "terms_and_conditions" or arg_name == "privacy_policy": |
| 34 |
body_fields["links"] = { |
| 35 |
arg_name: args[arg_name], |
| 36 |
**body_fields.get("links", {}), |
| 37 |
} |
| 38 |
else: |
| 39 |
body_fields[arg_name] = args[arg_name] |
| 40 |
|
| 41 |
client_id = str(uuid.uuid1()) |
| 42 |
|
| 43 |
# Default to a public client response unless arg says otherwise |
| 44 |
client_response_record = { |
| 45 |
**PUBLIC_CLIENT_RESPONSE_RECORD, |
| 46 |
**body_fields, |
| 47 |
"id": client_id, |
| 48 |
} |
| 49 |
|
| 50 |
return RegisteredResponse( |
| 51 |
service="auth", |
| 52 |
method="PUT", |
| 53 |
path=f"/v2/api/clients/{client_id}", |
| 54 |
json={"client": client_response_record}, |
| 55 |
metadata={ |
| 56 |
# Test functions use 'args' to form request |
| 57 |
"args": {**args, "client_id": client_id}, |
| 58 |
# Test functions use 'response' to verify response |
| 59 |
"response": body_fields, |
| 60 |
}, |
| 61 |
match=[json_params_matcher({"client": body_fields})], |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
RESPONSES = ResponseSet( |
| 66 |
default=register_response({}), |
| 67 |
name=register_response({"name": str(uuid.uuid4()).replace("-", "")}), |
| 68 |
publicly_visible=register_response({"visibility": "public"}), |
| 69 |
not_publicly_visible=register_response({"visibility": "private"}), |
| 70 |
redirect_uris=register_response({"redirect_uris": ["https://foo.com"]}), |
| 71 |
links=register_response( |
| 72 |
{ |
| 73 |
"terms_and_conditions": "https://foo.org", |
| 74 |
"privacy_policy": "https://boo.org", |
| 75 |
} |
| 76 |
), |
| 77 |
required_idp=register_response({"required_idp": str(uuid.uuid1())}), |
| 78 |
preselect_idp=register_response({"preselect_idp": str(uuid.uuid1())}), |
| 79 |
) |