1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.WebFinger.WebFingerControllerTest do
6 use Pleroma.Web.ConnCase
8 import ExUnit.CaptureLog
13 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 setup_all do: clear_config([:instance, :federating], true)
19 test "GET host-meta" do
22 |> get("/.well-known/host-meta")
24 assert response.status == 200
28 |> Floki.parse_document!(html_parser: Floki.HTMLParser.Mochiweb, attributes_as_maps: true)
31 ~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.Endpoint.url()}/.well-known/webfinger?resource={uri}" type="application/xrd+xml" /></XRD>)
32 |> Floki.parse_document!(html_parser: Floki.HTMLParser.Mochiweb, attributes_as_maps: true)
34 assert match?(^response_xml, expected_xml)
37 test "Webfinger JRD" do
40 ap_id: "https://hyrule.world/users/zelda",
41 also_known_as: ["https://mushroom.kingdom/users/toad"]
46 |> put_req_header("accept", "application/jrd+json")
47 |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
50 assert response["subject"] == "acct:#{user.nickname}@localhost"
52 assert response["aliases"] == [
53 "https://hyrule.world/users/zelda",
54 "https://mushroom.kingdom/users/toad"
58 test "reach user on tld, while pleroma is running on subdomain" do
59 clear_config([Pleroma.Web.Endpoint, :url, :host], "sub.example.com")
61 clear_config([Pleroma.Web.WebFinger, :domain], "example.com")
63 user = insert(:user, ap_id: "https://sub.example.com/users/bobby", nickname: "bobby")
67 |> put_req_header("accept", "application/jrd+json")
68 |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@example.com")
71 assert response["subject"] == "acct:#{user.nickname}@example.com"
72 assert response["aliases"] == ["https://sub.example.com/users/#{user.nickname}"]
75 test "it returns 404 when user isn't found (JSON)" do
78 |> put_req_header("accept", "application/jrd+json")
79 |> get("/.well-known/webfinger?resource=acct:jimm@localhost")
82 assert result == "Couldn't find user"
85 test "Webfinger XML" do
88 ap_id: "https://hyrule.world/users/zelda",
89 also_known_as: ["https://mushroom.kingdom/users/toad"]
94 |> put_req_header("accept", "application/xrd+xml")
95 |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
98 assert response =~ "<Alias>https://hyrule.world/users/zelda</Alias>"
99 assert response =~ "<Alias>https://mushroom.kingdom/users/toad</Alias>"
102 test "it returns 404 when user isn't found (XML)" do
105 |> put_req_header("accept", "application/xrd+xml")
106 |> get("/.well-known/webfinger?resource=acct:jimm@localhost")
109 assert result == "Couldn't find user"
112 test "Sends a 404 when invalid format" do
115 assert capture_log(fn ->
116 assert_raise Phoenix.NotAcceptableError, fn ->
118 |> put_req_header("accept", "text/html")
119 |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
121 end) =~ "no supported media type in accept header"
124 test "Sends a 400 when resource param is missing" do
127 |> put_req_header("accept", "application/xrd+xml,application/jrd+json")
128 |> get("/.well-known/webfinger")
130 assert response(response, 400)