114 lines
4.1 KiB
Python
114 lines
4.1 KiB
Python
from pathlib import Path
|
|
|
|
from app.core.config import settings
|
|
from app.modules.shipmondo.backend.service import (
|
|
build_shipmondo_payload,
|
|
extract_label_base64,
|
|
extract_price,
|
|
extract_tracking_number,
|
|
)
|
|
from app.modules.shipmondo.models.schemas import ShipmondoBookingCreate
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def test_shipmondo_schema_normalizes_country_and_services():
|
|
payload = ShipmondoBookingCreate.model_validate(
|
|
{
|
|
"case_id": 88,
|
|
"product_code": "GLSDK_HD",
|
|
"service_codes": ["email_nt", "EMAIL_NT", "sms_nt"],
|
|
"address": {
|
|
"recipient_name": "Lene Jensen",
|
|
"address_line1": "Vindegade 112",
|
|
"postal_code": "5000",
|
|
"city": "Odense C",
|
|
"country_code": "dk",
|
|
},
|
|
"parcels": [{"weight_kg": 2, "description": "Router"}],
|
|
}
|
|
)
|
|
|
|
assert payload.address.country_code == "DK"
|
|
assert payload.service_codes == ["EMAIL_NT", "SMS_NT"]
|
|
|
|
|
|
def test_build_shipmondo_payload_uses_grams_and_required_parties(monkeypatch):
|
|
monkeypatch.setattr(settings, "SHIPMONDO_SENDER_NAME", "BMC Networks")
|
|
monkeypatch.setattr(settings, "SHIPMONDO_SENDER_ADDRESS1", "Testvej 1")
|
|
monkeypatch.setattr(settings, "SHIPMONDO_SENDER_POSTAL_CODE", "4600")
|
|
monkeypatch.setattr(settings, "SHIPMONDO_SENDER_CITY", "Køge")
|
|
monkeypatch.setattr(settings, "SHIPMONDO_SENDER_COUNTRY_CODE", "DK")
|
|
monkeypatch.setattr(settings, "SHIPMONDO_SENDER_ATTENTION", "")
|
|
monkeypatch.setattr(settings, "SHIPMONDO_SENDER_ADDRESS2", "")
|
|
monkeypatch.setattr(settings, "SHIPMONDO_SENDER_EMAIL", "fragt@example.test")
|
|
monkeypatch.setattr(settings, "SHIPMONDO_SENDER_PHONE", "70112233")
|
|
|
|
result = build_shipmondo_payload(
|
|
{
|
|
"booking_ref": "SMD-20260819-ABC12345",
|
|
"case_id": 88,
|
|
"product_code": "GLSDK_HD",
|
|
"service_codes": ["EMAIL_NT", "SMS_NT"],
|
|
"own_agreement": False,
|
|
"automatic_select_service_point": True,
|
|
"recipient_name": "Lene Jensen",
|
|
"company_name": "Kunde ApS",
|
|
"address_line1": "Vindegade 112",
|
|
"address_line2": None,
|
|
"postal_code": "5000",
|
|
"city": "Odense C",
|
|
"country_code": "DK",
|
|
"email": "lene@example.test",
|
|
"phone": "50607080",
|
|
"parcels": [
|
|
{
|
|
"weight_kg": 2.25,
|
|
"length_cm": 20,
|
|
"width_cm": 30,
|
|
"height_cm": 40,
|
|
"description": "Router",
|
|
}
|
|
],
|
|
}
|
|
)
|
|
|
|
assert result["product_code"] == "GLSDK_HD"
|
|
assert result["service_codes"] == "EMAIL_NT,SMS_NT"
|
|
assert result["automatic_select_service_point"] is True
|
|
assert result["label_format"] == "10x19_pdf"
|
|
assert result["parties"][0]["type"] == "sender"
|
|
assert result["parties"][1]["type"] == "receiver"
|
|
assert result["parties"][1]["attention"] == "Lene Jensen"
|
|
assert result["parcels"][0]["weight"] == 2250
|
|
assert result["parcels"][0]["internal_reference"] == "SMD-20260819-ABC12345-1"
|
|
|
|
|
|
def test_shipmondo_response_extractors_cover_real_response_shape():
|
|
response = {
|
|
"id": 123456,
|
|
"price": "50.00",
|
|
"parcels": [{"pkg_no": "012345678910", "label_base64": "UERG"}],
|
|
}
|
|
|
|
assert extract_tracking_number(response) == "012345678910"
|
|
assert extract_price(response) == (50.0, "DKK")
|
|
assert extract_label_base64(response) == "UERG"
|
|
|
|
|
|
def test_case_shipping_ui_supports_both_providers():
|
|
template = (ROOT / "app/modules/sag/templates/detail_v3.html").read_text(encoding="utf-8")
|
|
|
|
assert 'value="fedex"' in template
|
|
assert 'value="shipmondo"' in template
|
|
assert "/api/v1/shipmondo/products" in template
|
|
assert "shippingSelectedBooking.provider" in template
|
|
assert "Promise.allSettled" in template
|
|
|
|
|
|
def test_shipmondo_configuration_is_safe_by_default():
|
|
assert settings.SHIPMONDO_API_BASE_URL.endswith("/api/public/v3")
|
|
assert isinstance(settings.SHIPMONDO_READ_ONLY, bool)
|
|
assert isinstance(settings.SHIPMONDO_DRY_RUN, bool)
|