Skip to content

Commit

Permalink
Add mondialrelay_fr vcr tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Sep 17, 2024
1 parent 605d0ee commit 9529a4a
Show file tree
Hide file tree
Showing 12 changed files with 3,008 additions and 34 deletions.
5 changes: 5 additions & 0 deletions roulier/carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def wrapper(self, carrier_type, action, data):
try:
input = hints["input"](**data)
except Exception as e:
if "auth" in data:
if "login" in data["auth"]:
data["auth"]["login"] = "xxx"
if "password" in data["auth"]:
data["auth"]["password"] = "xxx"
raise InvalidApiInput(f"Invalid input data {data!r}\n\n{e!s}") from e

try:
Expand Down
Empty file.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions roulier/carriers/mondialrelay_fr/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


from ....tests.conftest import * # noqa
72 changes: 72 additions & 0 deletions roulier/carriers/mondialrelay_fr/tests/test_mondialrelay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import pytest
from roulier import roulier
from ....helpers import merge


@pytest.fixture
def get_label_data(credentials, base_get_label_data):
return merge(
credentials["mondialrelay_fr"],
base_get_label_data,
{
"service": {
"product": "HOM",
"pickupMode": "REL",
"pickupSite": "AUTO",
"pickupCountry": "FR",
}
},
)


@pytest.fixture
def find_pickup_site_data(credentials, base_find_pickup_site_data):
return merge(
credentials["mondialrelay_fr"],
base_find_pickup_site_data,
)


@pytest.mark.vcr()
def test_mondialrelay_label(get_label_data):
rv = roulier.get("mondialrelay_fr", "get_label", get_label_data)
assert "parcels" in rv
assert "label" in rv["parcels"][0]
label = rv["parcels"][0]["label"]
assert label["name"] == "label_url"
assert label["type"] == "url"
assert label["data"].startswith("https://www.mondialrelay.com")


@pytest.mark.vcr()
def test_mondialrelay_label_return(get_label_data):
rv = roulier.get(
"mondialrelay_fr",
"get_label",
merge(
get_label_data,
{
"service": {
"product": "LCC",
},
},
),
)
assert "parcels" in rv
assert "label" in rv["parcels"][0]
label = rv["parcels"][0]["label"]
assert label["name"] == "label_url"
assert label["type"] == "url"
assert label["data"].startswith("https://www.mondialrelay.com")


@pytest.mark.vcr()
def test_mondialrelay_pickup_site(find_pickup_site_data):
rv = roulier.get("mondialrelay_fr", "find_pickup_site", find_pickup_site_data)
assert "sites" in rv
assert len(rv["sites"]) > 0
assert "name" in rv["sites"][0]
22 changes: 15 additions & 7 deletions roulier/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from datetime import date
import pytest
from collections import defaultdict

try:
from .credentials import CREDENTIALS
Expand All @@ -12,20 +12,28 @@

@pytest.fixture(scope="session")
def credentials():
return {
**CREDENTIALS,
"isTest": True,
}
for cred in CREDENTIALS.values():
if "auth" in cred:
cred["auth"]["isTest"] = True
return defaultdict(
lambda: {
"auth": {
"login": "login",
"password": "password",
"isTest": True,
}
},
CREDENTIALS,
)


@pytest.fixture
def base_get_label_data():
return {
"service": {
"shippingDate": date(2024, 1, 1),
"labelFormat": "PDF",
},
"parcels": [{"weight": 1.2, "comment": "Fake comment"}],
"parcels": [{"weight": 1.2, "reference": "Parcel 1"}],
"from_address": {
"name": "Akrétion",
"street1": "27 rue Henri Rolland",
Expand Down
8 changes: 5 additions & 3 deletions roulier/tests/credentials_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"""Copy this file to credentials.py and fill in your test credentials"""

CREDENTIALS = {
"mondialrelay_fr": { # These are public test credentials
"login": "BDTEST13",
"password": "PrivateK",
"mondialrelay_fr": {
"auth": { # These are public test credentials
"login": "BDTEST13",
"password": "PrivateK",
}
},
}
39 changes: 39 additions & 0 deletions roulier/tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from base64 import b64decode


def assert_data_type(b64_data, expected_type):
data = b64decode(b64_data)
if expected_type == "EPL": # No magic bytes for EPL
assert len(data) > 0, "EPL data should not be empty"
assert b"\r\n" in data
return

magic_bytes = {
"PDF": b"%PDF-",
"PNG": b"\x89PNG\r\n",
# https://gist.github.com/metafloor/773bc61480d1d05a976184d45099ef56
"ZPL": b"^XA",
}
if expected_type not in magic_bytes:
raise ValueError(f"Unsupported type: {expected_type}")

if expected_type == "ZPL":
if data[:3] == b"\xef\xbb\xbf":
# Handle ZPL data that starts with the byte order mark (BOM)
data = data[3:]
if not data.startswith(magic_bytes[expected_type]) and b"\n" in data:
# Try next line
data = data.split(b"\n", 1)[1]

assert data.startswith(magic_bytes[expected_type]), f"Invalid {expected_type} data"


def assert_pdf(b64_data):
assert_data_type(b64_data, "PDF")


def assert_zpl(b64_data):
assert_data_type(b64_data, "ZPL")
1 change: 0 additions & 1 deletion roulier/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def acquire(self, input: DummyIn) -> DummyOut:


def test_carrier_api_bad_input_signature():

class DummyOut(BaseModel):
pass

Expand Down
26 changes: 3 additions & 23 deletions roulier/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@ def test_suffix():


def test_walk_data():
assert walk_data(
{
"a": 1,
"b": {
"c": 3,
"d": {"a": 5, "f": 6},
},
}
) == {
assert walk_data({"a": 1, "b": {"c": 3, "d": {"a": 5, "f": 6},},}) == {
"a": 1,
"b": {
"c": 3,
Expand Down Expand Up @@ -73,12 +65,7 @@ def test_filter_empty():
"c": "",
}
) == {"a": 1}
assert filter_empty(
{
"a": 1,
"b": {"c": None, "d": {"a": 5, "f": ""}},
}
) == {
assert filter_empty({"a": 1, "b": {"c": None, "d": {"a": 5, "f": ""}},}) == {
"a": 1,
"b": {"d": {"a": 5}},
}
Expand All @@ -103,14 +90,7 @@ def test_merge():
assert merge({"a": None}, {"a": 1}) == {"a": 1}
assert merge({"a": ""}, {"a": 1}) == {"a": 1}
assert merge({"a": 1}, {"a": 2}, {"a": 3}) == {"a": 3}
assert merge(
{
"a": {"b": 1},
},
{
"a": {"c": 2},
},
) == {
assert merge({"a": {"b": 1},}, {"a": {"c": 2},},) == {
"a": {"b": 1, "c": 2},
}
assert merge({"a": {"b": 1}}, {"a": {"b": 2}}) == {"a": {"b": 2}}
Expand Down

0 comments on commit 9529a4a

Please sign in to comment.