tests WebFinger

This commit is contained in:
Maksim 2019-07-24 15:13:10 +00:00 committed by kaniini
parent b7fae304d3
commit 55341ac717
7 changed files with 141 additions and 24 deletions

View file

@ -0,0 +1,24 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Plugs.SetFormatPlug do
import Plug.Conn, only: [assign: 3, fetch_query_params: 1]
def init(_), do: nil
def call(conn, _) do
case get_format(conn) do
nil -> conn
format -> assign(conn, :format, format)
end
end
defp get_format(conn) do
conn.private[:phoenix_format] ||
case fetch_query_params(conn) do
%{query_params: %{"_format" => format}} -> format
_ -> nil
end
end
end

View file

@ -187,6 +187,7 @@ def find_lrdd_template(domain) do
end end
end end
@spec finger(String.t()) :: {:ok, map()} | {:error, any()}
def finger(account) do def finger(account) do
account = String.trim_leading(account, "@") account = String.trim_leading(account, "@")
@ -220,8 +221,6 @@ def finger(account) do
else else
with {:ok, doc} <- Jason.decode(body) do with {:ok, doc} <- Jason.decode(body) do
webfinger_from_json(doc) webfinger_from_json(doc)
else
{:error, e} -> e
end end
end end
else else

View file

@ -7,6 +7,7 @@ defmodule Pleroma.Web.WebFinger.WebFingerController do
alias Pleroma.Web.WebFinger alias Pleroma.Web.WebFinger
plug(Pleroma.Plugs.SetFormatPlug)
plug(Pleroma.Web.FederatingPlug) plug(Pleroma.Web.FederatingPlug)
def host_meta(conn, _params) do def host_meta(conn, _params) do
@ -17,30 +18,28 @@ def host_meta(conn, _params) do
|> send_resp(200, xml) |> send_resp(200, xml)
end end
def webfinger(conn, %{"resource" => resource}) do def webfinger(%{assigns: %{format: format}} = conn, %{"resource" => resource})
case get_format(conn) do when format in ["xml", "xrd+xml"] do
n when n in ["xml", "xrd+xml"] -> with {:ok, response} <- WebFinger.webfinger(resource, "XML") do
with {:ok, response} <- WebFinger.webfinger(resource, "XML") do conn
conn |> put_resp_content_type("application/xrd+xml")
|> put_resp_content_type("application/xrd+xml") |> send_resp(200, response)
|> send_resp(200, response) else
else _e -> send_resp(conn, 404, "Couldn't find user")
_e -> send_resp(conn, 404, "Couldn't find user")
end
n when n in ["json", "jrd+json"] ->
with {:ok, response} <- WebFinger.webfinger(resource, "JSON") do
json(conn, response)
else
_e -> send_resp(conn, 404, "Couldn't find user")
end
_ ->
send_resp(conn, 404, "Unsupported format")
end end
end end
def webfinger(conn, _params) do def webfinger(%{assigns: %{format: format}} = conn, %{"resource" => resource})
send_resp(conn, 400, "Bad Request") when format in ["json", "jrd+json"] do
with {:ok, response} <- WebFinger.webfinger(resource, "JSON") do
json(conn, response)
else
_e ->
conn
|> put_status(404)
|> json("Couldn't find user")
end
end end
def webfinger(conn, _params), do: send_resp(conn, 400, "Bad Request")
end end

View file

@ -0,0 +1,38 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Plugs.SetFormatPlugTest do
use ExUnit.Case, async: true
use Plug.Test
alias Pleroma.Plugs.SetFormatPlug
test "set format from params" do
conn =
:get
|> conn("/cofe?_format=json")
|> SetFormatPlug.call([])
assert %{format: "json"} == conn.assigns
end
test "set format from header" do
conn =
:get
|> conn("/cofe")
|> put_private(:phoenix_format, "xml")
|> SetFormatPlug.call([])
assert %{format: "xml"} == conn.assigns
end
test "doesn't set format" do
conn =
:get
|> conn("/cofe")
|> SetFormatPlug.call([])
refute conn.assigns[:format]
end
end

View file

@ -614,6 +614,15 @@ def get(
}} }}
end end
def get(
"https://social.heldscal.la/.well-known/webfinger?resource=invalid_content@social.heldscal.la",
_,
_,
Accept: "application/xrd+xml,application/jrd+json"
) do
{:ok, %Tesla.Env{status: 200, body: ""}}
end
def get("http://framatube.org/.well-known/host-meta", _, _, _) do def get("http://framatube.org/.well-known/host-meta", _, _, _) do
{:ok, {:ok,
%Tesla.Env{ %Tesla.Env{

View file

@ -19,6 +19,19 @@ defmodule Pleroma.Web.WebFinger.WebFingerControllerTest do
:ok :ok
end end
test "GET host-meta" do
response =
build_conn()
|> get("/.well-known/host-meta")
assert response.status == 200
assert response.resp_body ==
~s(<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" template="#{
Pleroma.Web.base_url()
}/.well-known/webfinger?resource={uri}" type="application/xrd+xml" /></XRD>)
end
test "Webfinger JRD" do test "Webfinger JRD" do
user = insert(:user) user = insert(:user)
@ -30,6 +43,16 @@ test "Webfinger JRD" do
assert json_response(response, 200)["subject"] == "acct:#{user.nickname}@localhost" assert json_response(response, 200)["subject"] == "acct:#{user.nickname}@localhost"
end end
test "it returns 404 when user isn't found (JSON)" do
result =
build_conn()
|> put_req_header("accept", "application/jrd+json")
|> get("/.well-known/webfinger?resource=acct:jimm@localhost")
|> json_response(404)
assert result == "Couldn't find user"
end
test "Webfinger XML" do test "Webfinger XML" do
user = insert(:user) user = insert(:user)
@ -41,6 +64,26 @@ test "Webfinger XML" do
assert response(response, 200) assert response(response, 200)
end end
test "it returns 404 when user isn't found (XML)" do
result =
build_conn()
|> put_req_header("accept", "application/xrd+xml")
|> get("/.well-known/webfinger?resource=acct:jimm@localhost")
|> response(404)
assert result == "Couldn't find user"
end
test "Sends a 404 when invalid format" do
user = insert(:user)
assert_raise Phoenix.NotAcceptableError, fn ->
build_conn()
|> put_req_header("accept", "text/html")
|> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
end
end
test "Sends a 400 when resource param is missing" do test "Sends a 400 when resource param is missing" do
response = response =
build_conn() build_conn()

View file

@ -40,6 +40,11 @@ test "works for ap_ids" do
end end
describe "fingering" do describe "fingering" do
test "returns error when fails parse xml or json" do
user = "invalid_content@social.heldscal.la"
assert {:error, %Jason.DecodeError{}} = WebFinger.finger(user)
end
test "returns the info for an OStatus user" do test "returns the info for an OStatus user" do
user = "shp@social.heldscal.la" user = "shp@social.heldscal.la"