Exemplo n.º 1
0
def test_get_args(json_response, text_response, malformed_response):
    err = exc.GlobusAPIError(json_response.r)
    assert err._get_args() == [
        json_response.method,
        json_response.url,
        None,
        "400",
        "Json Error",
        "json error message",
    ]
    err = exc.GlobusAPIError(text_response.r)
    assert err._get_args() == [
        text_response.method,
        text_response.url,
        None,
        "401",
        "Error",
        "error message",
    ]
    err = exc.GlobusAPIError(malformed_response.r)
    assert err._get_args() == [
        text_response.method,
        text_response.url,
        None,
        "403",
        "Error",
        "{",
    ]
Exemplo n.º 2
0
def test_info_is_falsey_on_non_dict_json():
    res = _mk_json_response(["foo", "bar"], 400)
    err = exc.GlobusAPIError(res.r)
    assert bool(err.info.consent_required) is False
    assert bool(err.info.authorization_parameters) is False
    assert str(
        err.info) == "AuthorizationParameterInfo(:)|ConsentRequiredInfo(:)"
Exemplo n.º 3
0
def test_consent_required_info():
    res = _mk_json_response(
        {
            "code": "ConsentRequired",
            "required_scopes": ["foo", "bar"]
        }, 401)
    err = exc.GlobusAPIError(res.r)
    assert bool(err.info.consent_required) is True
    assert err.info.consent_required.required_scopes == ["foo", "bar"]
    assert str(err.info.consent_required) == (
        "ConsentRequiredInfo(required_scopes=['foo', 'bar'])")

    # if the code is right but the scope list is missing, it should be falsey
    res = _mk_json_response({"code": "ConsentRequired"}, 401)
    err = exc.GlobusAPIError(res.r)
    assert bool(err.info.consent_required) is False
    assert err.info.consent_required.required_scopes is None
    assert str(err.info.consent_required) == "ConsentRequiredInfo(:)"
Exemplo n.º 4
0
def test_get_args_on_non_dict_json():
    res = _mk_json_response(["foo", "bar"], 400)
    err = exc.GlobusAPIError(res.r)
    assert err._get_args() == [
        res.method,
        res.url,
        None,
        "400",
        "Error",
        '["foo", "bar"]',
    ]
Exemplo n.º 5
0
def test_get_args_on_unknown_json():
    res = _mk_json_response({"foo": "bar"}, 400)
    err = exc.GlobusAPIError(res.r)
    assert err._get_args() == [
        res.method,
        res.url,
        None,
        "400",
        "Error",
        '{"foo": "bar"}',
    ]
Exemplo n.º 6
0
def test_http_reason_exposure(status, expect_reason):
    res = _mk_response(
        {"errors": [{
            "message": "json error message",
            "code": "Json Error"
        }]},
        status,
        data_transform=json.dumps,
        headers={"Content-Type": "application/json"},
    )
    err = exc.GlobusAPIError(res.r)
    assert err.http_reason == expect_reason
Exemplo n.º 7
0
def test_error_repr_has_expected_info(http_method, http_status, authz_scheme,
                                      request_url, error_code, error_message):
    http_reason = {
        404: "Not Found",
        500: "Server Error",
        501: "Not Implemented"
    }.get(http_status)

    body = {"otherfield": "otherdata"}
    if error_code is not None:
        body["code"] = error_code
    if error_message is not None:
        body["message"] = error_message

    headers = {"Content-Type": "application/json", "Spam": "Eggs"}
    if authz_scheme is not None:
        headers["Authorization"] = f"{authz_scheme} TOKENINFO"

    # build the response -> error -> error repr
    res = _mk_response(
        body,
        http_status,
        method=http_method,
        data_transform=json.dumps,
        url=request_url,
        headers=headers,
    )
    err = exc.GlobusAPIError(res.r)
    stringified = repr(err)

    # check using substring -- do not check exact format
    assert http_method in stringified
    assert request_url in stringified
    if authz_scheme in exc.GlobusAPIError.RECOGNIZED_AUTHZ_SCHEMES:
        assert authz_scheme in stringified
    # confirm that actual tokens don't get into the repr, regardless of authz scheme
    assert "TOKENINFO" not in stringified
    assert str(http_status) in stringified
    if error_code is not None:
        assert error_code in stringified
    else:
        assert "'Error'" in stringified
    if error_message is None:
        assert "otherdata" in stringified
    else:
        assert "otherdata" not in stringified
        if error_message:
            assert error_message in stringified
        else:
            assert http_reason in stringified
Exemplo n.º 8
0
def test_http_header_exposure():
    res = _mk_response(
        {"errors": [{
            "message": "json error message",
            "code": "Json Error"
        }]},
        400,
        data_transform=json.dumps,
        headers={
            "Content-Type": "application/json",
            "Spam": "Eggs"
        },
    )
    err = exc.GlobusAPIError(res.r)
    assert err.headers["spam"] == "Eggs"
    assert err.headers["Content-Type"] == "application/json"
Exemplo n.º 9
0
    def get_goauth_token(self):
        """
        Note that these tokens have a long lifetime and should be
        saved and re-used.

        :rtype: string
        """
        self.logger.debug("NexusClient.get_goauth_token() called")
        if not isinstance(self.authorizer, BasicAuthorizer):
            raise exc.GlobusError('get_goauth_token() requires basic auth')
        r = self.get('/goauth/token?grant_type=client_credentials')
        try:
            tok = r['access_token']
            self.logger.debug("NexusClient.get_goauth_token() success")
            return tok
        except KeyError:
            self.logger.warn(
                ("NexusClient.get_goauth_token() failed somehow, raising an "
                 "exception now"))
            raise exc.GlobusAPIError(r)
Exemplo n.º 10
0
def test_authz_params_info():
    res = _mk_json_response(
        {"authorization_parameters": {
            "session_message": "foo"
        }}, 401)
    err = exc.GlobusAPIError(res.r)
    assert bool(err.info.authorization_parameters) is True
    assert err.info.authorization_parameters.session_message == "foo"
    assert err.info.authorization_parameters.session_required_identities is None
    assert err.info.authorization_parameters.session_required_single_domain is None
    _strmatch_any_order(
        str(err.info.authorization_parameters),
        "AuthorizationParameterInfo(",
        [
            "session_message=foo",
            "session_required_identities=None",
            "session_required_single_domain=None",
        ],
        ")",
    )

    res = _mk_json_response(
        {
            "authorization_parameters": {
                "session_required_identities": ["foo", "bar"]
            }
        },
        401,
    )
    err = exc.GlobusAPIError(res.r)
    assert bool(err.info.authorization_parameters) is True
    assert err.info.authorization_parameters.session_message is None
    assert err.info.authorization_parameters.session_required_identities == [
        "foo",
        "bar",
    ]
    assert err.info.authorization_parameters.session_required_single_domain is None
    _strmatch_any_order(
        str(err.info.authorization_parameters),
        "AuthorizationParameterInfo(",
        [
            "session_message=None",
            "session_required_identities=['foo', 'bar']",
            "session_required_single_domain=None",
        ],
        ")",
    )

    res = _mk_json_response(
        {
            "authorization_parameters": {
                "session_required_single_domain": ["foo", "bar"]
            }
        },
        401,
    )
    err = exc.GlobusAPIError(res.r)
    assert bool(err.info.authorization_parameters) is True
    assert err.info.authorization_parameters.session_message is None
    assert err.info.authorization_parameters.session_required_identities is None
    assert err.info.authorization_parameters.session_required_single_domain == [
        "foo",
        "bar",
    ]
    _strmatch_any_order(
        str(err.info.authorization_parameters),
        "AuthorizationParameterInfo(",
        [
            "session_message=None",
            "session_required_identities=None",
            "session_required_single_domain=['foo', 'bar']",
        ],
        ")",
    )
Exemplo n.º 11
0
def test_raw_text_works(json_response, text_response):
    err = exc.GlobusAPIError(json_response.r)
    assert err.raw_text == json.dumps(json_response.data)
    err = exc.GlobusAPIError(text_response.r)
    assert err.raw_text == text_response.data
Exemplo n.º 12
0
def test_raw_json_fail(text_response, malformed_response):
    err = exc.GlobusAPIError(text_response.r)
    assert err.raw_json is None

    err = exc.GlobusAPIError(malformed_response.r)
    assert err.raw_json is None
Exemplo n.º 13
0
def test_raw_json_works(json_response):
    err = exc.GlobusAPIError(json_response.r)
    assert err.raw_json == json_response.data