logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

web_finger_test.exs (6345B)


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