def test_state_is_not_submitted_for_non_us_country():
    with mock.patch('requests.post') as post:
        post.return_value = mock.MagicMock(status_code=200)
        gateway.authenticate(
            AMT, CURRENCY, delivery_state="Somerset", delivery_country='GB'
        )
        args, __ = post.call_args
        assert args[1]['DeliveryState'] == ""
def test_fields_are_cleaned_to_match_sagepay_formats():
    with mock.patch('requests.post') as post:
        post.return_value = mock.MagicMock(status_code=200)
        gateway.authenticate(
            AMT, CURRENCY, delivery_surname="Name?"
        )
        args, __ = post.call_args
        assert args[1]['DeliverySurname'] == "Name"
def test_state_is_not_submitted_for_non_us_country():
    with mock.patch('requests.post') as post:
        post.return_value = mock.MagicMock(status_code=200)
        gateway.authenticate(AMT,
                             CURRENCY,
                             delivery_state="Somerset",
                             delivery_country='GB')
        args, __ = post.call_args
        assert args[1]['DeliveryState'] == ""
def test_fields_are_truncated_to_fit_sagepay():
    with mock.patch('requests.post') as post:
        post.return_value = mock.MagicMock(content=responses.MALFORMED,
                                           status_code=200)
        gateway.authenticate(
            AMT,
            CURRENCY,
            delivery_city="This is too long for Sagepay as they only allow 40")
        args, __ = post.call_args
        assert len(args[1]['DeliveryCity']) == 40
def test_audit_model_is_called_with_request_params():
    patch_kwargs = {
        'target': 'oscar_sagepay.models.RequestResponse',
    }
    with mock.patch(**patch_kwargs) as rr:
        gateway.authenticate(AMT, CURRENCY, reference='x')
        ref, call_params = rr.new.call_args[0]

    assert ref == 'x'
    for key in ('VPSProtocol', 'Vendor', 'TxType'):
        assert key in call_params
def test_audit_model_is_called_with_request_params():
    patch_kwargs = {
        'target': 'oscar_sagepay.models.RequestResponse',
    }
    with mock.patch(**patch_kwargs) as rr:
        gateway.authenticate(AMT, CURRENCY, reference='x')
        ref, call_params = rr.new.call_args[0]

    assert ref == 'x'
    for key in ('VPSProtocol', 'Vendor', 'TxType'):
        assert key in call_params
def test_fields_are_truncated_to_fit_sagepay():
    with mock.patch('requests.post') as post:
        post.return_value = mock.MagicMock(
            content=responses.MALFORMED,
            status_code=200)
        gateway.authenticate(
            AMT, CURRENCY,
            delivery_city="This is too long for Sagepay as they only allow 40"
        )
        args, __ = post.call_args
        assert len(args[1]['DeliveryCity']) == 40
def test_fields_are_cleaned_to_match_sagepay_formats():
    with mock.patch('requests.post') as post:
        post.return_value = mock.MagicMock(status_code=200)
        gateway.authenticate(AMT, CURRENCY, delivery_surname="Name?")
        args, __ = post.call_args
        assert args[1]['DeliverySurname'] == "Name"
def test_exception_raised_for_non_200_response():
    with pytest.raises(Exception) as e:
        gateway.authenticate(AMT, CURRENCY)
    assert '500' in e.exconly()
def test_authenticate_returns_response_obj():
    response = gateway.authenticate(AMT, CURRENCY)
    assert isinstance(response, wrappers.Response)
def test_exception_raised_for_non_200_response():
    with pytest.raises(Exception) as e:
        gateway.authenticate(AMT, CURRENCY)
    assert '500' in e.exconly()
def test_authenticate_returns_response_obj():
    response = gateway.authenticate(AMT, CURRENCY)
    assert isinstance(response, wrappers.Response)