Add test to WebFinger controller

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-11-27 18:42:56 +01:00
parent da378633ac
commit 3ddd5ee485
2 changed files with 41 additions and 10 deletions

View file

@ -33,29 +33,32 @@ defmodule Mobilizon.Service.WebFinger do
regex = ~r/(acct:)?(?<name>\w+)@#{host}/ regex = ~r/(acct:)?(?<name>\w+)@#{host}/
with %{"name" => name} <- Regex.named_captures(regex, resource) do with %{"name" => name} <- Regex.named_captures(regex, resource) do
user = Actors.get_local_actor_by_name(name) actor = Actors.get_local_actor_by_name(name)
{:ok, represent_user(user, "JSON")} {:ok, represent_actor(actor, "JSON")}
else else
_e -> _e ->
with user when not is_nil(user) <- Actors.get_actor_by_url!(resource) do with actor when not is_nil(actor) <- Actors.get_actor_by_url!(resource) do
{:ok, represent_user(user, "JSON")} {:ok, represent_actor(actor, "JSON")}
else else
_e -> _e ->
{:error, "Couldn't find user"} {:error, "Couldn't find actor"}
end end
end end
end end
def represent_user(user, "JSON") do @spec represent_actor(Actor.t()) :: struct()
def represent_actor(actor), do: represent_actor(actor, "JSON")
def represent_actor(actor, "JSON") do
%{ %{
"subject" => "acct:#{user.preferred_username}@#{MobilizonWeb.Endpoint.host()}", "subject" => "acct:#{actor.preferred_username}@#{MobilizonWeb.Endpoint.host()}",
"aliases" => [user.url], "aliases" => [actor.url],
"links" => [ "links" => [
%{"rel" => "self", "type" => "application/activity+json", "href" => user.url}, %{"rel" => "self", "type" => "application/activity+json", "href" => actor.url},
%{ %{
"rel" => "https://webfinger.net/rel/profile-page/", "rel" => "https://webfinger.net/rel/profile-page/",
"type" => "text/html", "type" => "text/html",
"href" => user.url "href" => actor.url
} }
] ]
} }

View file

@ -0,0 +1,28 @@
defmodule MobilizonWeb.WebFingerTest do
use MobilizonWeb.ConnCase
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.WebFinger
import Mobilizon.Factory
test "GET /.well-known/host-meta", %{conn: conn} do
conn = get(conn, "/.well-known/host-meta")
assert response(conn, 200) ==
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><XRD xmlns=\"http://docs.oasis-open.org/ns/xri/xrd-1.0\"><Link rel=\"lrdd\" template=\"#{
MobilizonWeb.Endpoint.url()
}/.well-known/webfinger?resource={uri}\" type=\"application/xrd+xml\" /></XRD>"
assert {"content-type", "application/xrd+xml; charset=utf-8"} in conn.resp_headers
end
test "GET /.well-known/webfinger with local actor", %{conn: conn} do
%Actor{preferred_username: username} = actor = insert(:actor)
conn = get(conn, "/.well-known/webfinger?resource=acct:#{username}@localhost:4001")
assert json_response(conn, 200) == WebFinger.represent_actor(actor)
end
test "GET /.well-known/webfinger with no query", %{conn: conn} do
conn = get(conn, "/.well-known/webfinger")
assert response(conn, 400) == "No query provided"
end
end