fix(portal): Fix nil error for address_description (#5346)

We try to parse `address_description` as a link on the resources/show
page, but it can be nil.
This commit is contained in:
Jamil
2024-06-12 18:54:48 -07:00
committed by GitHub
parent 489a14a0ed
commit e2d5ae5cac
2 changed files with 60 additions and 13 deletions

View File

@@ -121,20 +121,14 @@ defmodule Web.Resources.Show do
Address Description
</:label>
<:value>
<a
href={
if String.starts_with?(@resource.address_description, ["http", "ftp", "//"]) do
@resource.address_description
else
"//" <> @resource.address_description
end
}
target="_blank"
class={link_style()}
>
<%= if http_link?(@resource.address_description) do %>
<.link class={link_style()} navigate={@resource.address_description} target="_blank">
<%= @resource.address_description %>
<.icon name="hero-arrow-top-right-on-square" class="mb-3 w-3 h-3" />
</.link>
<% else %>
<%= @resource.address_description %>
<.icon name="hero-arrow-top-right-on-square" class="mb-3 w-3 h-3" />
</a>
<% end %>
</:value>
</.vertical_table_row>
<.vertical_table_row>
@@ -340,4 +334,16 @@ defmodule Web.Resources.Show do
{:noreply, push_navigate(socket, to: ~p"/#{socket.assigns.account}/resources")}
end
end
defp http_link?(nil), do: false
defp http_link?(address_description) do
uri = URI.parse(address_description)
if not is_nil(uri.scheme) and not is_nil(uri.host) and String.starts_with?(uri.scheme, "http") do
true
else
false
end
end
end

View File

@@ -118,12 +118,53 @@ defmodule Web.Live.Resources.ShowTest do
assert table["name"] =~ resource.name
assert table["address"] =~ resource.address
assert table["created"] =~ actor.name
assert table["address description"] =~ resource.address_description
for filter <- resource.filters do
assert String.downcase(table["traffic restriction"]) =~ Atom.to_string(filter.protocol)
end
end
test "omits address_description row if null", %{
account: account,
identity: identity,
conn: conn
} do
resource = Fixtures.Resources.create_resource(account: account, address_description: nil)
{:ok, lv, _html} =
conn
|> authorize_conn(identity)
|> live(~p"/#{account}/resources/#{resource}")
table =
lv
|> element("#resource")
|> render()
|> vertical_table_to_map()
assert table["address description"] == ""
end
test "renders link for address_descriptions that look like links", %{
account: account,
identity: identity,
conn: conn
} do
resource =
Fixtures.Resources.create_resource(
account: account,
address_description: "http://example.com"
)
{:ok, _lv, html} =
conn
|> authorize_conn(identity)
|> live(~p"/#{account}/resources/#{resource}")
assert Floki.find(html, "a[href='https://example.com']")
end
test "renders traffic filters on show page even when traffic filters disabled", %{
account: account,
identity: identity,