| 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 |
POLICY_REQUEST_ARGS = { |
| 9 |
"policy_id": str(uuid.uuid1()), |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
def make_request_body(request_args: t.Mapping[str, t.Any]) -> t.Dict[str, t.Any]: |
| 14 |
request_body = {} |
| 15 |
|
| 16 |
for field in [ |
| 17 |
"authentication_assurance_timeout", |
| 18 |
"required_mfa", |
| 19 |
"display_name", |
| 20 |
"description", |
| 21 |
"domain_constraints_include", |
| 22 |
"domain_constraints_exclude", |
| 23 |
]: |
| 24 |
if field in request_args: |
| 25 |
request_body[field] = request_args[field] |
| 26 |
|
| 27 |
if "project_id" in request_args: |
| 28 |
request_body["project_id"] = str(request_args["project_id"]) |
| 29 |
|
| 30 |
return request_body |
| 31 |
|
| 32 |
|
| 33 |
def make_response_body(request_args: t.Mapping[str, t.Any]) -> t.Dict[str, t.Any]: |
| 34 |
return { |
| 35 |
"project_id": str(request_args.get("project_id", uuid.uuid1())), |
| 36 |
"high_assurance": request_args.get("high_assurance", True), |
| 37 |
"required_mfa": request_args.get("required_mfa", False), |
| 38 |
"authentication_assurance_timeout": request_args.get( |
| 39 |
"authentication_assurance_timeout", 25 |
| 40 |
), |
| 41 |
"display_name": request_args.get( |
| 42 |
"display_name", str(uuid.uuid4()).replace("-", "") |
| 43 |
), |
| 44 |
"description": request_args.get( |
| 45 |
"description", str(uuid.uuid4()).replace("-", "") |
| 46 |
), |
| 47 |
"domain_constraints_include": request_args.get("domain_constraints_include"), |
| 48 |
"domain_constraints_exclude": request_args.get("domain_constraints_exclude"), |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
def register_response( |
| 53 |
args: t.Mapping[str, t.Any], |
| 54 |
) -> RegisteredResponse: |
| 55 |
request_args = {**POLICY_REQUEST_ARGS, **args} |
| 56 |
request_body = make_request_body(request_args) |
| 57 |
response_body = make_response_body(request_args) |
| 58 |
|
| 59 |
return RegisteredResponse( |
| 60 |
service="auth", |
| 61 |
method="PUT", |
| 62 |
path=f"/v2/api/policies/{request_args['policy_id']}", |
| 63 |
json={"policy": response_body}, |
| 64 |
metadata={ |
| 65 |
# Test functions use 'args' to form request |
| 66 |
"args": request_args, |
| 67 |
# Test functions use 'response' to verify response |
| 68 |
"response": response_body, |
| 69 |
}, |
| 70 |
match=[json_params_matcher({"policy": request_body})], |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
RESPONSES = ResponseSet( |
| 75 |
default=register_response({}), |
| 76 |
project_id_str=register_response({"project_id": str(uuid.uuid1())}), |
| 77 |
project_id_uuid=register_response({"project_id": uuid.uuid1()}), |
| 78 |
authentication_assurance_timeout=register_response( |
| 79 |
{"authentication_assurance_timeout": 9100} |
| 80 |
), |
| 81 |
required_mfa=register_response({"required_mfa": True}), |
| 82 |
not_required_mfa=register_response({"required_mfa": False}), |
| 83 |
display_name=register_response( |
| 84 |
{"display_name": str(uuid.uuid4()).replace("-", "")} |
| 85 |
), |
| 86 |
description=register_response({"description": str(uuid.uuid4()).replace("-", "")}), |
| 87 |
no_domain_constrants_include=register_response( |
| 88 |
{"domain_constraints_include": None} |
| 89 |
), |
| 90 |
empty_domain_constrants_include=register_response( |
| 91 |
{"domain_constraints_include": []} |
| 92 |
), |
| 93 |
domain_constrants_include=register_response( |
| 94 |
{"domain_constraints_include": ["globus.org", "uchicago.edu"]} |
| 95 |
), |
| 96 |
no_domain_constrants_exclude=register_response( |
| 97 |
{"domain_constraints_exclude": None} |
| 98 |
), |
| 99 |
empty_domain_constrants_exclude=register_response( |
| 100 |
{"domain_constraints_exclude": []} |
| 101 |
), |
| 102 |
domain_constrants_exclude=register_response( |
| 103 |
{"domain_constraints_exclude": ["globus.org", "uchicago.edu"]} |
| 104 |
), |
| 105 |
) |