Skip to content

Commit

Permalink
fix IP unauthorised by DPD
Browse files Browse the repository at this point in the history
  • Loading branch information
DylannCordel committed Jan 21, 2021
1 parent 6ef275b commit bba49ad
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
2 changes: 2 additions & 0 deletions roulier/carriers/dpd_fr/tests/credential_demo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" fake credentials
duplicate as credential.py with real values for real tests
Specific to DPD FR: You IP must be allowed to use the test server...
"""

credentials = {
Expand All @@ -9,4 +10,5 @@
"agencyId": 77,
"customerCountry": 250,
"isTest": True,
"my_IP_is_allowed": False,
}
2 changes: 2 additions & 0 deletions roulier/carriers/dpd_fr/tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@
"email": "[email protected]",
},
}

MY_IP_IS_ALLOWED = credentials.get("my_IP_is_allowed", False)
67 changes: 48 additions & 19 deletions roulier/carriers/dpd_fr/tests/test_dpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,28 @@
from roulier import roulier
from roulier.exception import InvalidApiInput, CarrierError

from .data import DATA
from .data import DATA, MY_IP_IS_ALLOWED

logger = logging.getLogger(__name__)


EXCEPTION_MESSAGE = "Failed call with parameters %s"


def _test_ip_allowed(REQUIRE_ALLOWED=True):
def canceled_test():
if REQUIRE_ALLOWED:
msg = "%s canceled because your IP is not allowed by DPD"
else:
msg = "%s canceled because your IP is allowed by DPD"
logger.info(msg)

def decorator(test):
return test if MY_IP_IS_ALLOWED == REQUIRE_ALLOWED else canceled_test

return decorator


def test_methods():
assert (
"dpd_fr" in roulier.get_carriers_action_available().keys()
Expand All @@ -29,54 +43,69 @@ def test_methods():

def test_label_basic_checks():
vals = copy.deepcopy(DATA)
vals["service"]["product"] = "whatisitproduct"

vals["service"]["product"] = "whatisitproduct"
with assert_raises(InvalidApiInput, {"service": [{"product": "unallowed"}]}):
roulier.get("dpd_fr", "get_label", vals)

del vals["service"]["product"]

vals["parcels"][0].pop("weight")
with assert_raises(InvalidApiInput, {"parcels": [{0: [{"weight": "float"}]}]}):
roulier.get("dpd_fr", "get_label", vals)
vals["parcels"][0]["weight"] = DATA["parcels"][0]["weight"]

vals["to_address"].pop("country")
with assert_raises(InvalidApiInput, {"to_address": [{"country": "empty"}]}):
roulier.get("dpd_fr", "get_label", vals)

# no address
del vals["to_address"]
with assert_raises(InvalidApiInput, {"to_address": "empty"}):
roulier.get("dpd_fr", "get_label", vals)


@_test_ip_allowed(False)
def test_dpd_invalid_ip():
vals = copy.deepcopy(DATA)
with assert_raises(CarrierError, [{"id": "IpPermissionDenied"}]):
roulier.get("dpd_fr", "get_label", vals)


@_test_ip_allowed(True)
def test_dpd_classic():
vals = copy.deepcopy(DATA)
result = roulier.get("dpd_fr", "get_label", vals)
assert_result(vals, result, 1, 1)


@_test_ip_allowed(True)
def test_common_failed_get_label():
vals = copy.deepcopy(DATA)
# Weight
orig_weight = vals["parcels"][0].pop("weight")
with assert_raises(InvalidApiInput, {"parcels": [{0: [{"weight": "float"}]}]}):
roulier.get("dpd_fr", "get_label", vals)

vals["parcels"][0]["weight"] = 999.99
with assert_raises(CarrierError, "Invalid weight"):
with assert_raises(CarrierError, [{"id": "InvalidWeight"}]):
roulier.get("dpd_fr", "get_label", vals)

vals["parcels"][0]["weight"] = 0
with assert_raises(CarrierError, "Invalid weight"):
with assert_raises(CarrierError, [{"id": "InvalidWeight"}]):
roulier.get("dpd_fr", "get_label", vals)
vals["parcels"][0]["weight"] = orig_weight
vals["parcels"][0]["weight"] = DATA["parcels"][0]["weight"]

# Country
vals["to_address"].pop("country")
with assert_raises(InvalidApiInput, {"to_address": [{"country": "empty"}]}):
roulier.get("dpd_fr", "get_label", vals)

vals["to_address"]["country"] = "ZZ"
with assert_raises(CarrierError, [{"id": "InvalidCountryPrefix"}]):
roulier.get("dpd_fr", "get_label", vals)

# no address
del vals["to_address"]
with assert_raises(InvalidApiInput, {"to_address": "empty"}):
roulier.get("dpd_fr", "get_label", vals)


@_test_ip_allowed(True)
def test_auth():
vals = copy.deepcopy(DATA)
vals["auth"]["login"] = "test"
with assert_raises(CarrierError, [{"id": "PermissionDenied"}]):
roulier.get("dpd_fr", "get_label", vals)


@_test_ip_allowed(True)
def test_relai():
vals = copy.deepcopy(DATA)
vals["service"]["product"] = "DPD_Relais"
Expand Down

0 comments on commit bba49ad

Please sign in to comment.