def test_parse_without_message_object():
    """Make sure a JSON without message member does not cause a disaster"""

    sap_error = SAP.BusinessGatewayError('Programmer message',
                                         MockResponse(b'{"error": { "message" : "foo" } }'))

    assert str(sap_error) == 'Programmer message'
    assert not sap_error.errordetails
def test_parse_invalid_json():
    """Make sure an invalid JSON does not cause a disaster"""

    sap_error = SAP.BusinessGatewayError('Programmer message',
                                         MockResponse(b'random data'))

    assert str(sap_error) == 'Programmer message'
    assert not sap_error.errordetails
def test_parse_without_error():
    """Make sure a JSON without error member does not cause a disaster"""

    sap_error = SAP.BusinessGatewayError('Programmer message',
                                         MockResponse(b'{"random": "data"}'))

    assert str(sap_error) == 'Programmer message'
    assert not sap_error.errordetails
def test_parse_with_error(response_with_error):
    """Make sure a JSON without message member does not cause a disaster"""

    sap_error = SAP.BusinessGatewayError(
        'Programmer message',
        response_with_error)

    assert str(sap_error) == 'Gateway Error'
    assert not sap_error.errordetails
def test_parse_with_error_and_innererror(response_with_error_and_innererror):
    """Make sure we parse out data correctly"""

    sap_error = SAP.BusinessGatewayError(
        'Programmer error',
        response_with_error_and_innererror)

    assert str(sap_error) == 'Gateway Error'
    assert sap_error.errordetails
    assert 2 == len(sap_error.errordetails)
    assert sap_error.errordetails[0] == 'Inner Error 1'
    assert sap_error.errordetails[1] == 'Inner Error 2'
def test_add_btp_token_to_session_invalid_clientid():
    """Invalid clientid in key returns an HttpError"""

    invalid_key = MOCK_KEY.copy()
    invalid_key['uaa']['clientid'] = 'invalid-client-id'

    responses.add(
        responses.POST,
        MOCK_AUTH_URL + f'/oauth/token?grant_type=password&username={MOCK_BTP_USER}&password={MOCK_BTP_PASSWORD}',
        headers={'Content-type': 'application/json'},
        json={
            'error': 'unauthorized',
            'error_description': 'Bad credentials'
        },
        status=401)

    with pytest.raises(HttpError) as caught:
        SAP.add_btp_token_to_session(requests.Session(), invalid_key, MOCK_BTP_USER, MOCK_BTP_PASSWORD)

    assert caught.value.response.status_code == 401
    assert json.loads(caught.value.response.text)['error_description'] == 'Bad credentials'
def test_parse_errordetails_no_message():
    """Make sure a JSON where error details misses the member message
       does not cause a disaster
    """

    sap_error = SAP.BusinessGatewayError(
        'Programmer message',
        MockResponse(b'{"error" : {\n\
            "innererror": { "errordetails" : [ { "foo" : "bar" } ] } } }'))

    assert str(sap_error) == 'Programmer message'
    assert [''] == sap_error.errordetails
def test_parse_errordetails_no_object():
    """Make sure a JSON where error details are not objects
       does not cause a disaster
    """

    sap_error = SAP.BusinessGatewayError(
        'Programmer message',
        MockResponse(b'{"error" : {\n\
            "innererror": { "errordetails" : [ "foo", "bar" ] } } }'))

    assert str(sap_error) == 'Programmer message'
    assert not sap_error.errordetails
def test_parse_without_array_errordetails():
    """Make sure a JSON without array errordetails member
       does not cause a disaster
    """

    sap_error = SAP.BusinessGatewayError(
        'Programmer message',
        MockResponse(b'{"error" : {\n\
            "innererror": { "errordetails" : "value" } } }'))

    assert str(sap_error) == 'Programmer message'
    assert not sap_error.errordetails
def test_add_btp_token_to_session_invalid_user():
    """Invalid username returns an HttpError"""

    invalid_user = "******"

    responses.add(
        responses.POST,
        MOCK_AUTH_URL + f'/oauth/token?grant_type=password&username={invalid_user}&password={MOCK_BTP_PASSWORD}',
        headers={'Content-type': 'application/json'},
        json={
            'error': 'unauthorized',
            'error_description': {
                'error': 'invalid_grant',
                'error_description': 'User authentication failed.'
            }
        },
        status=401)

    with pytest.raises(HttpError) as caught:
        SAP.add_btp_token_to_session(requests.Session(), MOCK_KEY, invalid_user, MOCK_BTP_PASSWORD)

    assert caught.value.response.status_code == 401
    assert json.loads(caught.value.response.text)['error_description']['error'] == 'invalid_grant'
def test_add_btp_token_to_session_valid():
    """Valid username, password and key return a session with set token"""

    responses.add(
        responses.POST,
        MOCK_AUTH_URL + f'/oauth/token?grant_type=password&username={MOCK_BTP_USER}&password={MOCK_BTP_PASSWORD}',
        headers={'Content-type': 'application/json'},
        json={
            'access_token': 'valid_access_token',
            'token_type': 'bearer',
            'id_token': 'valid_id_token',
            'refresh_token': 'valid_refresh_token',
            'expires_in': 43199,
            'scope': 'openid uaa.user',
            'jti': 'valid_jti'
        },
        status=200)

    result = SAP.add_btp_token_to_session(requests.Session(), MOCK_KEY, MOCK_BTP_USER, MOCK_BTP_PASSWORD)
    assert result.headers['Authorization'] == 'Bearer valid_id_token'