Skip to content

Commit

Permalink
Reduce verbosity in responses tests
Browse files Browse the repository at this point in the history
It was easy to make those tests a bit easier to read, so I did. Also
removed a switch based on the Python version, because we only support
Python 3 anyway.
  • Loading branch information
maiksprenger committed Oct 1, 2015
1 parent cc46c8e commit ec6192a
Showing 1 changed file with 15 additions and 50 deletions.
65 changes: 15 additions & 50 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

from django.test import TestCase

import six

from adyen.facade import Facade
from adyen.gateway import MissingFieldException, InvalidTransactionException
from adyen.models import AdyenTransaction
from adyen.scaffold import Scaffold

from tests.test_notifications import AUTHORISED_PAYMENT_PARAMS_POST


AUTHORISED_PAYMENT_PARAMS_GET = {
'authResult': 'AUTHORISED',
'merchantReference': 'WVubjVRFOTPBsLNy33zqliF-vmc:109:00000109',
Expand Down Expand Up @@ -75,89 +74,55 @@ def setUp(self):
self.request = request

def test_is_valid_ip_address(self):
# Valid IPv4 and IPv6 addresses
valid_addresses = ['127.0.0.1', '192.168.12.34', '2001:0db8:85a3:0000:0000:8a2e:0370:7334']
for address in valid_addresses:
assert Facade._is_valid_ip_address(address)

# The empty string is not a valid IP address.
ip_address = ''
self.assertFalse(Facade._is_valid_ip_address(ip_address))

# A kinda random string is not a valid IP address.
ip_address = 'TOTORO'
self.assertFalse(Facade._is_valid_ip_address(ip_address))

# These are valid IP addresses.
ip_address = '127.0.0.1'
self.assertTrue(Facade._is_valid_ip_address(ip_address))
ip_address = '192.168.12.34'
self.assertTrue(Facade._is_valid_ip_address(ip_address))

# This one is out of the valid ranges.
ip_address = '192.168.12.345'
self.assertFalse(Facade._is_valid_ip_address(ip_address))

# This one is a valid IPv6 address.
ip_address = '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
self.assertTrue(Facade._is_valid_ip_address(ip_address))

# This one is an invalid IPv6 address.
ip_address = '2001::0234:C1ab::A0:aabc:003F'
self.assertFalse(Facade._is_valid_ip_address(ip_address))
# Empty string, noise, IPv4 out of range, invalid IPv6-lookalike
invalid_addresses = ['', 'TOTORO', '192.168.12.345', '2001::0234:C1ab::A0:aabc:003F']
for address in invalid_addresses:
assert not Facade._is_valid_ip_address(address)

def test_get_origin_ip_address(self):
"""
Make sure that the `_get_origin_ip_address()` method works with all
the possible meaningful combinations of default and custom HTTP header
names.
"""
facade = Facade()

# With no specified ADYEN_IP_ADDRESS_HTTP_HEADER setting,
# ensure we fetch the origin IP address in the REMOTE_ADDR
# HTTP header.
ip_address = facade._get_origin_ip_address(self.request)
self.assertEqual(ip_address, '127.0.0.1')
if six.PY3:
self.assertEqual(type(ip_address), str)
assert '127.0.0.1' == Facade()._get_origin_ip_address(self.request)

# Check the return value is None if we have nothing
# in the `REMOTE_ADDR` header.
self.request.META.update({'REMOTE_ADDR': ''})
ip_address = facade._get_origin_ip_address(self.request)
self.assertIsNone(ip_address)
assert Facade()._get_origin_ip_address(self.request) is None

# Check the return value is None if we have no `REMOTE_ADDR`
# header at all.
del self.request.META['REMOTE_ADDR']
ip_address = facade._get_origin_ip_address(self.request)
self.assertIsNone(ip_address)
assert Facade()._get_origin_ip_address(self.request) is None

with self.settings(ADYEN_IP_ADDRESS_HTTP_HEADER=TEST_IP_ADDRESS_HTTP_HEADER):
facade = Facade() # Recreate the instance to update the Adyen config.

# Now we add the `HTTP_X_FORWARDED_FOR` header and
# ensure it is used instead.
self.request.META.update({
'REMOTE_ADDR': '127.0.0.1',
'HTTP_X_FORWARDED_FOR': '93.16.93.168'
})
ip_address = facade._get_origin_ip_address(self.request)
self.assertEqual(ip_address, '93.16.93.168')
if six.PY3:
self.assertEqual(type(ip_address), str)
assert '93.16.93.168' == Facade()._get_origin_ip_address(self.request)

# Even if the default header is missing.
del self.request.META['REMOTE_ADDR']
ip_address = facade._get_origin_ip_address(self.request)
self.assertEqual(ip_address, '93.16.93.168')
if six.PY3:
self.assertEqual(type(ip_address), str)
assert '93.16.93.168' == Facade()._get_origin_ip_address(self.request)

# And finally back to `None` if we have neither header.
del self.request.META['HTTP_X_FORWARDED_FOR']
ip_address = facade._get_origin_ip_address(self.request)
self.assertIsNone(ip_address)
assert Facade()._get_origin_ip_address(self.request) is None

def test_handle_authorised_payment(self):

self.request.GET = deepcopy(AUTHORISED_PAYMENT_PARAMS_GET)
success, status, details = Scaffold().handle_payment_feedback(self.request)

Expand Down

3 comments on commit ec6192a

@aaugustin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a public library. If we keep compatibility with Python 2, perhaps more people will find it useful.

@maiksprenger
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a stab at running the tests with Python 2, and failed. Just tried again a bit more vigorously, and while another version of freezegun gets us past the install stage, the code at the moment isn't Python 2 compatible (e.g. super() calls).

@aaugustin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I must have told @metamatik it wasn't needed back then. I just assumed it was compatible because of this reference to six. Leave it there.

Please sign in to comment.