Add protocol implementation for IPPort struct and tests (#1478)

Closes #1477
This commit is contained in:
Andrew Dryga
2023-03-01 11:44:30 -06:00
committed by GitHub
parent fd769f14b3
commit 8a3e8bbdf0
5 changed files with 60 additions and 4 deletions

View File

@@ -61,10 +61,9 @@ defmodule FzHttp.Config.Resolver do
defp fetch_process_value(pid, key) do
with {:dictionary, pdict} <- :erlang.process_info(pid, :dictionary),
{^key, value} <- List.keyfind(pdict, key, 0) do
value
{:ok, value}
else
_other ->
:error
_other -> :error
end
end

View File

@@ -5,3 +5,11 @@ end
defimpl Phoenix.HTML.Safe, for: Postgrex.INET do
def to_iodata(%Postgrex.INET{} = inet), do: FzHttp.Types.INET.to_string(inet)
end
defimpl String.Chars, for: FzHttp.Types.IPPort do
def to_string(%FzHttp.Types.IPPort{} = ip_port), do: FzHttp.Types.IPPort.to_string(ip_port)
end
defimpl Phoenix.HTML.Safe, for: FzHttp.Types.IPPort do
def to_iodata(%FzHttp.Types.IPPort{} = ip_port), do: FzHttp.Types.IPPort.to_string(ip_port)
end

View File

@@ -325,6 +325,28 @@ defmodule FzHttpWeb.Acceptance.AdminTest do
%Postgrex.INET{address: {0, 0, 0, 0, 0, 0, 0, 0}, netmask: 0}
]
end
feature "can use IP with a port in default client endpoint and host in DNS", %{
session: session
} do
session
|> visit(~p"/settings/client_defaults")
|> assert_el(Query.text("Client Defaults", count: 2))
|> fill_in(Query.fillable_field("configuration[default_client_dns]"),
with: "dns.example.com"
)
|> fill_in(Query.fillable_field("configuration[default_client_endpoint]"),
with: "1.2.3.4:8123"
)
|> click(Query.button("Save"))
# XXX: We need to show a flash that settings are saved
|> visit(~p"/settings/client_defaults")
|> assert_el(Query.text("Client Defaults", count: 2))
assert configuration = FzHttp.Config.fetch_db_config!()
assert configuration.default_client_endpoint == "1.2.3.4:8123"
assert configuration.default_client_dns == ["dns.example.com"]
end
end
describe "customization" do

View File

@@ -31,7 +31,7 @@ defmodule FzHttpWeb.SettingLive.ClientDefaultsTest do
path = ~p"/settings/client_defaults"
{:ok, view, html} = live(conn, path)
%{html: html, view: view}
%{conn: conn, html: html, view: view}
end
test "renders current configuration", %{html: html} do
@@ -92,6 +92,32 @@ defmodule FzHttpWeb.SettingLive.ClientDefaultsTest do
"""
end
test "blocks overridden default client endpoint" do
FzHttp.Config.put_system_env_override(:default_client_endpoint, "1.2.3.4:1234")
{_admin_user, conn} = admin_conn(%{})
{:ok, view, _html} = live(conn, ~p"/settings/client_defaults")
test_view =
view
|> element("#client_defaults_form_component")
|> render()
assert Floki.find(test_view, "#client_defaults_form_component_default_client_endpoint") ==
[
{"input",
[
{"class", "input "},
{"disabled", "disabled"},
{"id", "client_defaults_form_component_default_client_endpoint"},
{"name", "configuration[default_client_endpoint]"},
{"placeholder", "firezone.example.com"},
{"type", "text"},
{"value", "Set in environment variable DEFAULT_CLIENT_ENDPOINT: 1.2.3.4:1234"}
], []}
]
end
test "updates default client persistent_keepalive", %{view: view} do
test_view =
view

View File

@@ -27,6 +27,7 @@ defmodule FzHttpWeb.ConnCase do
import Phoenix.ConnTest
import Phoenix.LiveViewTest
import FzHttp.TestHelpers
import FzHttpWeb.ConnCase
# The default endpoint for testing
@endpoint FzHttpWeb.Endpoint