Skip to content

Commit 9529a4a

Browse files
committed
Add mondialrelay_fr vcr tests
1 parent 605d0ee commit 9529a4a

12 files changed

+3008
-34
lines changed

roulier/carrier.py

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ def wrapper(self, carrier_type, action, data):
7575
try:
7676
input = hints["input"](**data)
7777
except Exception as e:
78+
if "auth" in data:
79+
if "login" in data["auth"]:
80+
data["auth"]["login"] = "xxx"
81+
if "password" in data["auth"]:
82+
data["auth"]["password"] = "xxx"
7883
raise InvalidApiInput(f"Invalid input data {data!r}\n\n{e!s}") from e
7984

8085
try:

roulier/carriers/mondialrelay_fr/tests/__init__.py

Whitespace-only changes.

roulier/carriers/mondialrelay_fr/tests/cassettes/test_mondialrelay/test_mondialrelay_label.yaml

+908
Large diffs are not rendered by default.

roulier/carriers/mondialrelay_fr/tests/cassettes/test_mondialrelay/test_mondialrelay_label_return.yaml

+908
Large diffs are not rendered by default.

roulier/carriers/mondialrelay_fr/tests/cassettes/test_mondialrelay/test_mondialrelay_pickup_site.yaml

+1,047
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright 2024 Akretion (http://www.akretion.com).
2+
# @author Florian Mounier <florian.mounier@akretion.com>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
5+
6+
from ....tests.conftest import * # noqa
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2024 Akretion (http://www.akretion.com).
2+
# @author Florian Mounier <florian.mounier@akretion.com>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
5+
import pytest
6+
from roulier import roulier
7+
from ....helpers import merge
8+
9+
10+
@pytest.fixture
11+
def get_label_data(credentials, base_get_label_data):
12+
return merge(
13+
credentials["mondialrelay_fr"],
14+
base_get_label_data,
15+
{
16+
"service": {
17+
"product": "HOM",
18+
"pickupMode": "REL",
19+
"pickupSite": "AUTO",
20+
"pickupCountry": "FR",
21+
}
22+
},
23+
)
24+
25+
26+
@pytest.fixture
27+
def find_pickup_site_data(credentials, base_find_pickup_site_data):
28+
return merge(
29+
credentials["mondialrelay_fr"],
30+
base_find_pickup_site_data,
31+
)
32+
33+
34+
@pytest.mark.vcr()
35+
def test_mondialrelay_label(get_label_data):
36+
rv = roulier.get("mondialrelay_fr", "get_label", get_label_data)
37+
assert "parcels" in rv
38+
assert "label" in rv["parcels"][0]
39+
label = rv["parcels"][0]["label"]
40+
assert label["name"] == "label_url"
41+
assert label["type"] == "url"
42+
assert label["data"].startswith("https://www.mondialrelay.com")
43+
44+
45+
@pytest.mark.vcr()
46+
def test_mondialrelay_label_return(get_label_data):
47+
rv = roulier.get(
48+
"mondialrelay_fr",
49+
"get_label",
50+
merge(
51+
get_label_data,
52+
{
53+
"service": {
54+
"product": "LCC",
55+
},
56+
},
57+
),
58+
)
59+
assert "parcels" in rv
60+
assert "label" in rv["parcels"][0]
61+
label = rv["parcels"][0]["label"]
62+
assert label["name"] == "label_url"
63+
assert label["type"] == "url"
64+
assert label["data"].startswith("https://www.mondialrelay.com")
65+
66+
67+
@pytest.mark.vcr()
68+
def test_mondialrelay_pickup_site(find_pickup_site_data):
69+
rv = roulier.get("mondialrelay_fr", "find_pickup_site", find_pickup_site_data)
70+
assert "sites" in rv
71+
assert len(rv["sites"]) > 0
72+
assert "name" in rv["sites"][0]

roulier/tests/conftest.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Copyright 2024 Akretion (http://www.akretion.com).
22
# @author Florian Mounier <florian.mounier@akretion.com>
33
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4-
from datetime import date
54
import pytest
5+
from collections import defaultdict
66

77
try:
88
from .credentials import CREDENTIALS
@@ -12,20 +12,28 @@
1212

1313
@pytest.fixture(scope="session")
1414
def credentials():
15-
return {
16-
**CREDENTIALS,
17-
"isTest": True,
18-
}
15+
for cred in CREDENTIALS.values():
16+
if "auth" in cred:
17+
cred["auth"]["isTest"] = True
18+
return defaultdict(
19+
lambda: {
20+
"auth": {
21+
"login": "login",
22+
"password": "password",
23+
"isTest": True,
24+
}
25+
},
26+
CREDENTIALS,
27+
)
1928

2029

2130
@pytest.fixture
2231
def base_get_label_data():
2332
return {
2433
"service": {
25-
"shippingDate": date(2024, 1, 1),
2634
"labelFormat": "PDF",
2735
},
28-
"parcels": [{"weight": 1.2, "comment": "Fake comment"}],
36+
"parcels": [{"weight": 1.2, "reference": "Parcel 1"}],
2937
"from_address": {
3038
"name": "Akrétion",
3139
"street1": "27 rue Henri Rolland",

roulier/tests/credentials_demo.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
"""Copy this file to credentials.py and fill in your test credentials"""
66

77
CREDENTIALS = {
8-
"mondialrelay_fr": { # These are public test credentials
9-
"login": "BDTEST13",
10-
"password": "PrivateK",
8+
"mondialrelay_fr": {
9+
"auth": { # These are public test credentials
10+
"login": "BDTEST13",
11+
"password": "PrivateK",
12+
}
1113
},
1214
}

roulier/tests/helpers.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2024 Akretion (http://www.akretion.com).
2+
# @author Florian Mounier <florian.mounier@akretion.com>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
from base64 import b64decode
5+
6+
7+
def assert_data_type(b64_data, expected_type):
8+
data = b64decode(b64_data)
9+
if expected_type == "EPL": # No magic bytes for EPL
10+
assert len(data) > 0, "EPL data should not be empty"
11+
assert b"\r\n" in data
12+
return
13+
14+
magic_bytes = {
15+
"PDF": b"%PDF-",
16+
"PNG": b"\x89PNG\r\n",
17+
# https://gist.github.com/metafloor/773bc61480d1d05a976184d45099ef56
18+
"ZPL": b"^XA",
19+
}
20+
if expected_type not in magic_bytes:
21+
raise ValueError(f"Unsupported type: {expected_type}")
22+
23+
if expected_type == "ZPL":
24+
if data[:3] == b"\xef\xbb\xbf":
25+
# Handle ZPL data that starts with the byte order mark (BOM)
26+
data = data[3:]
27+
if not data.startswith(magic_bytes[expected_type]) and b"\n" in data:
28+
# Try next line
29+
data = data.split(b"\n", 1)[1]
30+
31+
assert data.startswith(magic_bytes[expected_type]), f"Invalid {expected_type} data"
32+
33+
34+
def assert_pdf(b64_data):
35+
assert_data_type(b64_data, "PDF")
36+
37+
38+
def assert_zpl(b64_data):
39+
assert_data_type(b64_data, "ZPL")

roulier/tests/test_api.py

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def acquire(self, input: DummyIn) -> DummyOut:
8080

8181

8282
def test_carrier_api_bad_input_signature():
83-
8483
class DummyOut(BaseModel):
8584
pass
8685

roulier/tests/test_helpers.py

+3-23
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,7 @@ def test_suffix():
2222

2323

2424
def test_walk_data():
25-
assert walk_data(
26-
{
27-
"a": 1,
28-
"b": {
29-
"c": 3,
30-
"d": {"a": 5, "f": 6},
31-
},
32-
}
33-
) == {
25+
assert walk_data({"a": 1, "b": {"c": 3, "d": {"a": 5, "f": 6},},}) == {
3426
"a": 1,
3527
"b": {
3628
"c": 3,
@@ -73,12 +65,7 @@ def test_filter_empty():
7365
"c": "",
7466
}
7567
) == {"a": 1}
76-
assert filter_empty(
77-
{
78-
"a": 1,
79-
"b": {"c": None, "d": {"a": 5, "f": ""}},
80-
}
81-
) == {
68+
assert filter_empty({"a": 1, "b": {"c": None, "d": {"a": 5, "f": ""}},}) == {
8269
"a": 1,
8370
"b": {"d": {"a": 5}},
8471
}
@@ -103,14 +90,7 @@ def test_merge():
10390
assert merge({"a": None}, {"a": 1}) == {"a": 1}
10491
assert merge({"a": ""}, {"a": 1}) == {"a": 1}
10592
assert merge({"a": 1}, {"a": 2}, {"a": 3}) == {"a": 3}
106-
assert merge(
107-
{
108-
"a": {"b": 1},
109-
},
110-
{
111-
"a": {"c": 2},
112-
},
113-
) == {
93+
assert merge({"a": {"b": 1},}, {"a": {"c": 2},},) == {
11494
"a": {"b": 1, "c": 2},
11595
}
11696
assert merge({"a": {"b": 1}}, {"a": {"b": 2}}) == {"a": {"b": 2}}

0 commit comments

Comments
 (0)