move to 2.5.5
[anni] / test / pleroma / web / web_finger_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.WebFingerTest do
6   use Pleroma.DataCase, async: true
7   alias Pleroma.Web.WebFinger
8   import Pleroma.Factory
9   import Tesla.Mock
10
11   setup do
12     mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
13     :ok
14   end
15
16   describe "host meta" do
17     test "returns a link to the xml lrdd" do
18       host_info = WebFinger.host_meta()
19
20       assert String.contains?(host_info, Pleroma.Web.Endpoint.url())
21     end
22   end
23
24   describe "incoming webfinger request" do
25     test "works for fqns" do
26       user = insert(:user)
27
28       {:ok, result} =
29         WebFinger.webfinger("#{user.nickname}@#{Pleroma.Web.Endpoint.host()}", "XML")
30
31       assert is_binary(result)
32     end
33
34     test "works for ap_ids" do
35       user = insert(:user)
36
37       {:ok, result} = WebFinger.webfinger(user.ap_id, "XML")
38       assert is_binary(result)
39     end
40   end
41
42   describe "fingering" do
43     test "returns error for nonsensical input" do
44       assert {:error, _} = WebFinger.finger("bliblablu")
45       assert {:error, _} = WebFinger.finger("pleroma.social")
46     end
47
48     test "returns error when there is no content-type header" do
49       Tesla.Mock.mock(fn
50         %{url: "https://social.heldscal.la/.well-known/host-meta"} ->
51           {:ok,
52            %Tesla.Env{
53              status: 200,
54              body: File.read!("test/fixtures/tesla_mock/social.heldscal.la_host_meta")
55            }}
56
57         %{
58           url:
59             "https://social.heldscal.la/.well-known/webfinger?resource=acct:invalid_content@social.heldscal.la"
60         } ->
61           {:ok, %Tesla.Env{status: 200, body: ""}}
62       end)
63
64       user = "invalid_content@social.heldscal.la"
65       assert {:error, {:content_type, nil}} = WebFinger.finger(user)
66     end
67
68     test "returns error when fails parse xml or json" do
69       user = "invalid_content@social.heldscal.la"
70       assert {:error, %Jason.DecodeError{}} = WebFinger.finger(user)
71     end
72
73     test "returns the ActivityPub actor URI for an ActivityPub user" do
74       user = "framasoft@framatube.org"
75
76       {:ok, _data} = WebFinger.finger(user)
77     end
78
79     test "returns the ActivityPub actor URI and subscribe address for an ActivityPub user with the ld+json mimetype" do
80       user = "kaniini@gerzilla.de"
81
82       {:ok, data} = WebFinger.finger(user)
83
84       assert data["ap_id"] == "https://gerzilla.de/channel/kaniini"
85       assert data["subscribe_address"] == "https://gerzilla.de/follow?f=&url={uri}"
86     end
87
88     test "it work for AP-only user" do
89       user = "kpherox@mstdn.jp"
90
91       {:ok, data} = WebFinger.finger(user)
92
93       assert data["magic_key"] == nil
94       assert data["salmon"] == nil
95
96       assert data["topic"] == nil
97       assert data["subject"] == "acct:kPherox@mstdn.jp"
98       assert data["ap_id"] == "https://mstdn.jp/users/kPherox"
99       assert data["subscribe_address"] == "https://mstdn.jp/authorize_interaction?acct={uri}"
100     end
101
102     test "it works for friendica" do
103       user = "lain@squeet.me"
104
105       {:ok, _data} = WebFinger.finger(user)
106     end
107
108     test "it gets the xrd endpoint" do
109       {:ok, template} = WebFinger.find_lrdd_template("social.heldscal.la")
110
111       assert template == "https://social.heldscal.la/.well-known/webfinger?resource={uri}"
112     end
113
114     test "it gets the xrd endpoint for hubzilla" do
115       {:ok, template} = WebFinger.find_lrdd_template("macgirvin.com")
116
117       assert template == "https://macgirvin.com/xrd/?uri={uri}"
118     end
119
120     test "it gets the xrd endpoint for statusnet" do
121       {:ok, template} = WebFinger.find_lrdd_template("status.alpicola.com")
122
123       assert template == "https://status.alpicola.com/main/xrd?uri={uri}"
124     end
125
126     test "it works with idna domains as nickname" do
127       nickname = "lain@" <> to_string(:idna.encode("zetsubou.みんな"))
128
129       {:ok, _data} = WebFinger.finger(nickname)
130     end
131
132     test "it works with idna domains as link" do
133       ap_id = "https://" <> to_string(:idna.encode("zetsubou.みんな")) <> "/users/lain"
134       {:ok, _data} = WebFinger.finger(ap_id)
135     end
136
137     test "respects json content-type" do
138       Tesla.Mock.mock(fn
139         %{
140           url:
141             "https://mastodon.social/.well-known/webfinger?resource=acct:emelie@mastodon.social"
142         } ->
143           {:ok,
144            %Tesla.Env{
145              status: 200,
146              body: File.read!("test/fixtures/tesla_mock/webfinger_emelie.json"),
147              headers: [{"content-type", "application/jrd+json"}]
148            }}
149
150         %{url: "https://mastodon.social/.well-known/host-meta"} ->
151           {:ok,
152            %Tesla.Env{
153              status: 200,
154              body: File.read!("test/fixtures/tesla_mock/mastodon.social_host_meta")
155            }}
156       end)
157
158       {:ok, _data} = WebFinger.finger("emelie@mastodon.social")
159     end
160
161     test "respects xml content-type" do
162       Tesla.Mock.mock(fn
163         %{
164           url: "https://pawoo.net/.well-known/webfinger?resource=acct:pekorino@pawoo.net"
165         } ->
166           {:ok,
167            %Tesla.Env{
168              status: 200,
169              body: File.read!("test/fixtures/tesla_mock/https___pawoo.net_users_pekorino.xml"),
170              headers: [{"content-type", "application/xrd+xml"}]
171            }}
172
173         %{url: "https://pawoo.net/.well-known/host-meta"} ->
174           {:ok,
175            %Tesla.Env{
176              status: 200,
177              body: File.read!("test/fixtures/tesla_mock/pawoo.net_host_meta")
178            }}
179       end)
180
181       {:ok, _data} = WebFinger.finger("pekorino@pawoo.net")
182     end
183
184     test "refuses to process XML remote entities" do
185       Tesla.Mock.mock(fn
186         %{
187           url: "https://pawoo.net/.well-known/webfinger?resource=acct:pekorino@pawoo.net"
188         } ->
189           {:ok,
190            %Tesla.Env{
191              status: 200,
192              body: File.read!("test/fixtures/xml_external_entities.xml"),
193              headers: [{"content-type", "application/xrd+xml"}]
194            }}
195
196         %{url: "https://pawoo.net/.well-known/host-meta"} ->
197           {:ok,
198            %Tesla.Env{
199              status: 200,
200              body: File.read!("test/fixtures/tesla_mock/pawoo.net_host_meta")
201            }}
202       end)
203
204       assert :error = WebFinger.finger("pekorino@pawoo.net")
205     end
206   end
207 end