| 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 |
"project_id": str(uuid.uuid1()), |
| 10 |
"display_name": "Policy of Foo", |
| 11 |
"description": "Controls access to Foo", |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
def make_request_body(request_args: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]: |
| 16 |
request_body = request_args.copy() |
| 17 |
request_body["project_id"] = str(request_args["project_id"]) |
| 18 |
|
| 19 |
for domain_constraints in [ |
| 20 |
"domain_constraints_include", |
| 21 |
"domain_constraints_exclude", |
| 22 |
]: |
| 23 |
if domain_constraints in request_args: |
| 24 |
request_body[domain_constraints] = request_args[domain_constraints] |
| 25 |
|
| 26 |
return request_body |
| 27 |
|
| 28 |
|
| 29 |
def make_response_body(request_args: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]: |
| 30 |
response_body = request_args.copy() |
| 31 |
response_body["project_id"] = str(request_args["project_id"]) |
| 32 |
response_body["id"] = str(uuid.uuid1()) |
| 33 |
|
| 34 |
for domain_constraints in [ |
| 35 |
"domain_constraints_include", |
| 36 |
"domain_constraints_exclude", |
| 37 |
]: |
| 38 |
if domain_constraints in request_args: |
| 39 |
response_body[domain_constraints] = request_args.get(domain_constraints) |
| 40 |
|
| 41 |
return response_body |
| 42 |
|
| 43 |
|
| 44 |
def register_response( |
| 45 |
args: t.Mapping[str, t.Any], |
| 46 |
match: t.Any = None, |
| 47 |
) -> RegisteredResponse: |
| 48 |
request_args = {**POLICY_REQUEST_ARGS, **args} |
| 49 |
request_body = make_request_body(request_args) |
| 50 |
response_body = make_response_body(request_args) |
| 51 |
|
| 52 |
return RegisteredResponse( |
| 53 |
service="auth", |
| 54 |
method="POST", |
| 55 |
path="/v2/api/policies", |
| 56 |
json={"policy": response_body}, |
| 57 |
metadata={ |
| 58 |
# Test functions use 'args' to form request |
| 59 |
"args": request_args, |
| 60 |
# Test functions use 'response' to verify response |
| 61 |
"response": response_body, |
| 62 |
}, |
| 63 |
match=( |
| 64 |
[json_params_matcher({"policy": request_body})] if match is None else match |
| 65 |
), |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
RESPONSES = ResponseSet( |
| 70 |
default=register_response({}, match=[]), |
| 71 |
project_id_str=register_response({"project_id": str(uuid.uuid1())}), |
| 72 |
project_id_uuid=register_response({"project_id": uuid.uuid1()}), |
| 73 |
high_assurance=register_response( |
| 74 |
{ |
| 75 |
"high_assurance": True, |
| 76 |
"authentication_assurance_timeout": 35, |
| 77 |
"required_mfa": False, |
| 78 |
} |
| 79 |
), |
| 80 |
not_high_assurance=register_response( |
| 81 |
{"high_assurance": False, "required_mfa": False} |
| 82 |
), |
| 83 |
required_mfa=register_response( |
| 84 |
{ |
| 85 |
"high_assurance": True, |
| 86 |
"authentication_assurance_timeout": 45, |
| 87 |
"required_mfa": True, |
| 88 |
} |
| 89 |
), |
| 90 |
authentication_assurance_timeout=register_response( |
| 91 |
{"authentication_assurance_timeout": 23} |
| 92 |
), |
| 93 |
display_name=register_response( |
| 94 |
{"display_name": str(uuid.uuid4()).replace("-", "")} |
| 95 |
), |
| 96 |
description=register_response({"description": str(uuid.uuid4()).replace("-", "")}), |
| 97 |
domain_constraints_include=register_response( |
| 98 |
{ |
| 99 |
"domain_constraints_include": ["globus.org", "uchicago.edu"], |
| 100 |
}, |
| 101 |
), |
| 102 |
empty_domain_constraints_include=register_response( |
| 103 |
{ |
| 104 |
"domain_constraints_include": [], |
| 105 |
}, |
| 106 |
), |
| 107 |
no_domain_constraints_include=register_response( |
| 108 |
{ |
| 109 |
"domain_constraints_include": None, |
| 110 |
}, |
| 111 |
), |
| 112 |
domain_constraints_exclude=register_response( |
| 113 |
{ |
| 114 |
"domain_constraints_exclude": ["globus.org", "uchicago.edu"], |
| 115 |
}, |
| 116 |
), |
| 117 |
empty_domain_constraints_exclude=register_response( |
| 118 |
{ |
| 119 |
"domain_constraints_exclude": [], |
| 120 |
}, |
| 121 |
), |
| 122 |
no_domain_constraints_exclude=register_response( |
| 123 |
{ |
| 124 |
"domain_constraints_exclude": None, |
| 125 |
}, |
| 126 |
), |
| 127 |
) |