️(frontend) backport phone number formatting to the backend

Use the Python port of Google's phone number library on the backend
instead of the JS one on the client. Saves 140Kb of unnecessary JS
and avoids a complex dynamic import that would have been required to
optimize loading.

The transformation is static: the phone number lives in the Django
settings, so the backend can format it once and pass the result to
the client. This avoids every client loading the 140Kb library and
re-computing the same information.

Moreover, within a single client, the transformation was previously
re-computed several times during a webapp lifecycle.
This commit is contained in:
lebaudantoine
2026-05-26 12:05:53 +02:00
committed by aleb_the_flash
parent 8984d863df
commit 7390673bfc
11 changed files with 242 additions and 48 deletions
@@ -0,0 +1,58 @@
"""
Test utils.build_telephony_config
"""
import logging
from core.utils import build_telephony_config
def test_build_telephony_config_disabled(settings):
"""Returns {"enabled": False} when telephony is disabled."""
settings.ROOM_TELEPHONY_ENABLED = False
config = build_telephony_config()
assert config == {"enabled": False}
def test_build_telephony_config_enabled_with_valid_number(settings):
"""Returns full config with country and international number when telephony is enabled."""
settings.ROOM_TELEPHONY_ENABLED = True
settings.ROOM_TELEPHONY_PHONE_NUMBER = "0123456789"
settings.ROOM_TELEPHONY_DEFAULT_COUNTRY = "FR"
config = build_telephony_config()
assert config == {
"enabled": True,
"default_country": "FR",
"international_phone_number": "+33 1 23 45 67 89",
}
def test_build_telephony_config_enabled_with_invalid_number(settings):
"""Returns {"enabled": False} when phone number cannot be parsed."""
settings.ROOM_TELEPHONY_ENABLED = True
settings.ROOM_TELEPHONY_PHONE_NUMBER = "not-a-number"
settings.ROOM_TELEPHONY_DEFAULT_COUNTRY = "FR"
config = build_telephony_config()
assert config == {"enabled": False}
def test_build_telephony_config_enabled_with_missing_number(settings):
"""Returns {"enabled": False} when phone number is not configured."""
settings.ROOM_TELEPHONY_ENABLED = True
settings.ROOM_TELEPHONY_PHONE_NUMBER = ""
settings.ROOM_TELEPHONY_DEFAULT_COUNTRY = "FR"
config = build_telephony_config()
assert config == {"enabled": False}
def test_build_telephony_config_enabled_with_missing_number_warns(settings, caplog):
"""Logs a warning when telephony is enabled but phone number is not configured."""
settings.ROOM_TELEPHONY_ENABLED = True
settings.ROOM_TELEPHONY_PHONE_NUMBER = ""
settings.ROOM_TELEPHONY_DEFAULT_COUNTRY = "FR"
with caplog.at_level(logging.WARNING):
build_telephony_config()
assert "ROOM_TELEPHONY_PHONE_NUMBER" in caplog.text
@@ -0,0 +1,102 @@
"""
Test utils._format_telephony_phone_number
"""
import logging
import pytest
from core.utils import _format_telephony_phone_number
@pytest.fixture(autouse=True)
def clear_lru_cache():
"""Clear the lru_cache before each test to ensure isolation."""
_format_telephony_phone_number.cache_clear()
yield
_format_telephony_phone_number.cache_clear()
def test_format_telephony_phone_number_missing_raw_number():
"""Returns (None, None) when raw_number is empty."""
country, international = _format_telephony_phone_number("", "FR")
assert country is None
assert international is None
def test_format_telephony_phone_number_none_raw_number():
"""Returns (None, None) when raw_number is None."""
country, international = _format_telephony_phone_number(None, "FR")
assert country is None
assert international is None
def test_format_telephony_phone_number_missing_default_country():
"""Returns (None, None) when default_country is empty."""
country, international = _format_telephony_phone_number("+33123456789", "")
assert country is None
assert international is None
def test_format_telephony_phone_number_none_default_country():
"""Returns (None, None) when default_country is None."""
country, international = _format_telephony_phone_number("+33123456789", None)
assert country is None
assert international is None
def test_format_telephony_phone_number_both_missing():
"""Returns (None, None) when both inputs are missing."""
country, international = _format_telephony_phone_number(None, None)
assert country is None
assert international is None
def test_format_telephony_phone_number_invalid_number(caplog):
"""Returns (None, None) and logs a warning when the number cannot be parsed."""
with caplog.at_level(logging.WARNING):
country, international = _format_telephony_phone_number("not-a-number", "FR")
assert country is None
assert international is None
assert "not-a-number" in caplog.text
assert "FR" in caplog.text
def test_format_telephony_phone_number_valid_french_number():
"""Returns correct country and international format for a valid French number."""
country, international = _format_telephony_phone_number("0123456789", "FR")
assert country == "FR"
assert international == "+33 1 23 45 67 89"
def test_format_telephony_phone_number_valid_e164_number():
"""Returns correct result for an E.164-formatted number (no default country needed)."""
country, international = _format_telephony_phone_number("+33123456789", "US")
assert country == "FR"
assert international == "+33 1 23 45 67 89"
def test_format_telephony_phone_number_valid_us_number():
"""Returns correct country and international format for a valid US number."""
country, international = _format_telephony_phone_number("2025550123", "US")
assert country == "US"
assert international == "+1 202-555-0123"
def test_format_telephony_phone_number_valid_german_number():
"""Returns correct country and international format for a valid German number."""
country, international = _format_telephony_phone_number("03012345678", "DE")
assert country == "DE"
assert international == "+49 30 12345678"
def test_format_telephony_phone_number_lru_cache():
"""Results are cached: the same inputs return the same object."""
result1 = _format_telephony_phone_number("0123456789", "FR")
result2 = _format_telephony_phone_number("0123456789", "FR")
assert result1 is result2
# pylint: disable=no-value-for-parameter
cache_info = _format_telephony_phone_number.cache_info()
assert cache_info.hits >= 1