add test for ap_id link verification

This commit is contained in:
FloatingGhost 2022-12-29 17:09:39 +00:00
parent 96899f6095
commit 730d6dd49b
3 changed files with 43 additions and 10 deletions

View file

@ -2397,24 +2397,32 @@ defp is_url(uri) do
@spec maybe_validate_rel_me_field(Changeset.t(), User.t()) :: Changeset.t()
defp maybe_validate_rel_me_field(changeset, %User{ap_id: _ap_id} = struct) do
fields = get_change(changeset, :fields)
raw_fields = get_change(changeset, :raw_fields)
if is_nil(fields) do
changeset
else
validate_rel_me_field(changeset, fields, struct)
validate_rel_me_field(changeset, fields, raw_fields, struct)
end
end
defp maybe_validate_rel_me_field(changeset, _), do: changeset
@spec validate_rel_me_field(Changeset.t(), [Map.t()], User.t()) :: Changeset.t()
defp validate_rel_me_field(changeset, fields, %User{ap_id: ap_id}) do
@spec validate_rel_me_field(Changeset.t(), [Map.t()], [Map.t()], User.t()) :: Changeset.t()
defp validate_rel_me_field(changeset, fields, raw_fields, %User{ap_id: ap_id}) do
fields =
fields
|> Enum.map(fn %{"name" => name, "value" => value} ->
if is_url(value) do
with "me" <- RelMe.maybe_put_rel_me(value, [ap_id]) do
%{"name" => name, "value" => value, "verified_at" => DateTime.to_iso8601(DateTime.utc_now())}
|> Enum.with_index()
|> Enum.map(fn {%{"name" => name, "value" => value}, index} ->
raw_value = Enum.at(raw_fields, index)["value"]
if is_url(raw_value) do
with "me" <- RelMe.maybe_put_rel_me(raw_value, [ap_id]) do
%{
"name" => name,
"value" => value,
"verified_at" => DateTime.to_iso8601(DateTime.utc_now())
}
else
e ->
Logger.error("Could not check for rel=me, #{inspect(e)}")

View file

@ -38,15 +38,14 @@ defp parse_url(url) do
def maybe_put_rel_me("http" <> _ = target_page, profile_urls) when is_list(profile_urls) do
{:ok, rel_me_hrefs} = parse(target_page)
true = Enum.any?(rel_me_hrefs, fn x -> x in profile_urls end)
"me"
rescue
_ -> nil
e -> nil
end
def maybe_put_rel_me(_, _) do
def maybe_put_rel_me(a, b) do
nil
end
end

View file

@ -465,6 +465,32 @@ test "update fields", %{conn: conn} do
]
end
test "update fields with a link to content with rel=me", %{user: user, conn: conn} do
Tesla.Mock.mock(fn
%{url: "http://example.com/rel_me/ap_id"} ->
%Tesla.Env{
status: 200,
body: ~s[<html><head><link rel="me" href="#{user.ap_id}"></head></html>]
}
end)
field = %{name: "Website", value: "http://example.com/rel_me/ap_id"}
account_data =
conn
|> patch("/api/v1/accounts/update_credentials", %{fields_attributes: [field]})
|> json_response_and_validate_schema(200)
assert [%{
"name" => "Website",
"value" =>
~s[<a href="http://example.com/rel_me/ap_id" rel="ugc">http://example.com/rel_me/ap_id</a>],
"verified_at" => verified_at
}] = account_data["fields"]
{:ok, verified_at, _} = DateTime.from_iso8601(verified_at)
assert DateTime.diff(DateTime.utc_now(), verified_at) < 10
end
test "emojis in fields labels", %{conn: conn} do
fields = [
%{name: ":firefox:", value: "is best 2hu"},