/
/
/
1"""Tests for the webserver publish address selection."""
2
3from music_assistant.controllers.webserver.controller import _get_publish_addresses
4from music_assistant.helpers.util import format_ip_for_url
5
6ALL_ADDRESSES = ("192.168.1.10", "10.0.0.5", "fd00::10", "fd00::20")
7
8
9def test_wildcard_bind_adds_other_ip_family() -> None:
10 """A wildcard bind publishes the publish IP plus the first address of the other family."""
11 assert _get_publish_addresses("0.0.0.0", "192.168.1.10", ALL_ADDRESSES) == [
12 "192.168.1.10",
13 "fd00::10",
14 ]
15 assert _get_publish_addresses(None, "192.168.1.10", ALL_ADDRESSES) == [
16 "192.168.1.10",
17 "fd00::10",
18 ]
19 assert _get_publish_addresses("::", "fd00::10", ALL_ADDRESSES) == [
20 "fd00::10",
21 "192.168.1.10",
22 ]
23
24
25def test_specific_bind_publishes_only_that_address() -> None:
26 """Binding to one specific address publishes only that address."""
27 assert _get_publish_addresses("fd00::10", "fd00::10", ALL_ADDRESSES) == ["fd00::10"]
28 assert _get_publish_addresses("192.168.1.10", "192.168.1.10", ALL_ADDRESSES) == ["192.168.1.10"]
29
30
31def test_ipv4_only_host() -> None:
32 """An IPv4-only host publishes just its IPv4 address on a wildcard bind."""
33 v4_addresses = ("192.168.1.10", "10.0.0.5")
34 assert _get_publish_addresses("0.0.0.0", "192.168.1.10", v4_addresses) == ["192.168.1.10"]
35 assert _get_publish_addresses(None, "192.168.1.10", v4_addresses) == ["192.168.1.10"]
36
37
38def test_ipv6_only_host() -> None:
39 """An IPv6-only host publishes its IPv6 address, for both wildcard and specific binds."""
40 v6_addresses = ("fd00::10", "fd00::20")
41 assert _get_publish_addresses("::", "fd00::10", v6_addresses) == ["fd00::10"]
42 assert _get_publish_addresses(None, "fd00::10", v6_addresses) == ["fd00::10"]
43 assert _get_publish_addresses("fd00::10", "fd00::10", v6_addresses) == ["fd00::10"]
44
45
46def test_ipv6_publish_ip_yields_bracketed_base_url() -> None:
47 """An IPv6 publish IP must produce a bracketed host in the (auto) base URL."""
48 publish_ip = _get_publish_addresses("::", "fd00::10", ("fd00::10",))[0]
49 assert f"http://{format_ip_for_url(publish_ip)}:8095" == "http://[fd00::10]:8095"
50