| 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 |
"scope_id": str(uuid.uuid1()), |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
def make_request_body(request_args: t.Mapping[str, t.Any]) -> t.Dict[str, t.Any]: |
| 15 |
request_body = {} |
| 16 |
|
| 17 |
for field in [ |
| 18 |
"name", |
| 19 |
"description", |
| 20 |
"scope_suffix", |
| 21 |
"advertised", |
| 22 |
"allows_refresh_token", |
| 23 |
"required_domains", |
| 24 |
]: |
| 25 |
if field in request_args and request_args[field] is not None: |
| 26 |
request_body[field] = request_args[field] |
| 27 |
|
| 28 |
if "dependent_scopes" in request_args: |
| 29 |
request_body["dependent_scopes"] = request_args["dependent_scopes"] |
| 30 |
|
| 31 |
return request_body |
| 32 |
|
| 33 |
|
| 34 |
def make_response_body(request_args: t.Mapping[str, t.Any]) -> t.Dict[str, t.Any]: |
| 35 |
client_id = str(uuid.uuid1()) |
| 36 |
scope_suffix = request_args.get("scope_suffix", str(uuid.uuid4()).replace("-", "")) |
| 37 |
|
| 38 |
return { |
| 39 |
"scope_string": f"https://auth.globus.org/scopes/{client_id}/{scope_suffix}", |
| 40 |
"allows_refresh_token": request_args.get("allows_refresh_token", True), |
| 41 |
"id": request_args["scope_id"], |
| 42 |
"advertised": request_args.get("advertised", False), |
| 43 |
"required_domains": request_args.get("required_domains", []), |
| 44 |
"name": request_args.get("name", str(uuid.uuid4()).replace("-", "")), |
| 45 |
"description": request_args.get( |
| 46 |
"description", str(uuid.uuid4()).replace("-", "") |
| 47 |
), |
| 48 |
"client": str(request_args.get("client_id", uuid.uuid1())), |
| 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="PUT", |
| 70 |
path=f"/v2/api/scopes/{request_args['scope_id']}", |
| 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 |
name=register_response({"name": str(uuid.uuid4()).replace("-", "")}), |
| 85 |
description=register_response({"description": str(uuid.uuid4()).replace("-", "")}), |
| 86 |
scope_suffix=register_response( |
| 87 |
{"scope_suffix": str(uuid.uuid4()).replace("-", "")} |
| 88 |
), |
| 89 |
no_required_domains=register_response({"required_domains": []}), |
| 90 |
required_domains=register_response( |
| 91 |
{"required_domains": ["globus.org", "uchicago.edu"]} |
| 92 |
), |
| 93 |
no_dependent_scopes=register_response({"dependent_scopes": []}), |
| 94 |
dependent_scopes=register_response( |
| 95 |
{ |
| 96 |
"dependent_scopes": [ |
| 97 |
DependentScopeSpec(str(uuid.uuid1()), True, True), |
| 98 |
DependentScopeSpec(uuid.uuid1(), False, False), |
| 99 |
], |
| 100 |
} |
| 101 |
), |
| 102 |
advertised=register_response({"advertised": True}), |
| 103 |
not_advertised=register_response({"advertised": False}), |
| 104 |
allows_refresh_token=register_response({"allows_refresh_token": True}), |
| 105 |
disallows_refresh_token=register_response({"allows_refresh_token": False}), |
| 106 |
) |