Files
romm/backend/tests/utils/test_urls.py
Marcello Pichini 084a6404e2 fix(auth): return no public base URL when ROMM_BASE_URL is malformed (#5700)
get_public_base_url promises the URL only when it is shareable, else
None, but yarl.URL raised ValueError on a value such as "http://[invalid",
so invite and notification rendering failed instead of omitting the link.

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 10:05:03 -05:00

86 lines
2.2 KiB
Python

import pytest
from fastapi import Request
from main import app
from pytest_mock import MockerFixture
from models.rom import Rom, RomFile
from utils.urls import (
get_public_base_url,
is_shareable_host,
rom_content_url,
rom_id_from_content_url,
romfile_content_url,
)
REQUEST = Request(
{
"type": "http",
"scheme": "https",
"server": ("example.test", 443),
"headers": [],
"root_path": "",
"router": app.router,
}
)
def test_rom_content_url_encodes_fs_name() -> None:
rom = Rom(id=7, fs_name="Game #1?.zip")
assert (
rom_content_url(REQUEST, rom)
== "https://example.test/api/roms/7/content/Game%20%231%3F.zip"
)
def test_romfile_content_url_encodes_file_name() -> None:
file = RomFile(id=9, file_name="Disc 1/100%.bin")
assert (
romfile_content_url(REQUEST, file)
== "https://example.test/api/roms/9/files/content/Disc%201%2F100%25.bin"
)
@pytest.mark.parametrize(
("value", "rom_id"),
[
("https://example.test/romm/api/roms/12/content/Game%20%231.zip", 12),
("http://old.host/api/roms/3/content/Game #1?.zip", 3),
("https://example.test/api/roms/3/files/content/Game.zip", None),
("./Game.zip", None),
],
)
def test_rom_id_from_content_url(value: str, rom_id: int | None) -> None:
assert rom_id_from_content_url(value) == rom_id
@pytest.mark.parametrize(
"host, shareable",
[
("localhost", False),
("localhost.", False),
("LOCALHOST.", False),
("127.0.0.1", False),
("romm.example.com", True),
("romm.example.com.", True),
],
)
def test_is_shareable_host(host: str, shareable: bool) -> None:
assert is_shareable_host(host) is shareable
@pytest.mark.parametrize(
"base_url, expected",
[
("https://romm.example.com/", "https://romm.example.com"),
("http://localhost:8080", None),
("http://[invalid", None),
],
)
def test_get_public_base_url(
mocker: MockerFixture, base_url: str, expected: str | None
) -> None:
mocker.patch("utils.urls.config.ROMM_BASE_URL", base_url)
assert get_public_base_url() == expected