Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.2KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.WebFinger.WebFingerControllerTest do
  5. use Pleroma.Web.ConnCase
  6. import ExUnit.CaptureLog
  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. setup_all do: clear_config([:instance, :federating], true)
  14. test "GET host-meta" do
  15. response =
  16. build_conn()
  17. |> get("/.well-known/host-meta")
  18. assert response.status == 200
  19. assert response.resp_body ==
  20. ~s(<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" template="#{
  21. Pleroma.Web.Endpoint.url()
  22. }/.well-known/webfinger?resource={uri}" type="application/xrd+xml" /></XRD>)
  23. end
  24. test "Webfinger JRD" do
  25. user =
  26. insert(:user,
  27. ap_id: "https://hyrule.world/users/zelda",
  28. also_known_as: ["https://mushroom.kingdom/users/toad"]
  29. )
  30. response =
  31. build_conn()
  32. |> put_req_header("accept", "application/jrd+json")
  33. |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
  34. |> json_response(200)
  35. assert response["subject"] == "acct:#{user.nickname}@localhost"
  36. assert response["aliases"] == [
  37. "https://hyrule.world/users/zelda",
  38. "https://mushroom.kingdom/users/toad"
  39. ]
  40. end
  41. test "it returns 404 when user isn't found (JSON)" do
  42. result =
  43. build_conn()
  44. |> put_req_header("accept", "application/jrd+json")
  45. |> get("/.well-known/webfinger?resource=acct:jimm@localhost")
  46. |> json_response(404)
  47. assert result == "Couldn't find user"
  48. end
  49. test "Webfinger XML" do
  50. user =
  51. insert(:user,
  52. ap_id: "https://hyrule.world/users/zelda",
  53. also_known_as: ["https://mushroom.kingdom/users/toad"]
  54. )
  55. response =
  56. build_conn()
  57. |> put_req_header("accept", "application/xrd+xml")
  58. |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
  59. |> response(200)
  60. assert response =~ "<Alias>https://hyrule.world/users/zelda</Alias>"
  61. assert response =~ "<Alias>https://mushroom.kingdom/users/toad</Alias>"
  62. end
  63. test "it returns 404 when user isn't found (XML)" do
  64. result =
  65. build_conn()
  66. |> put_req_header("accept", "application/xrd+xml")
  67. |> get("/.well-known/webfinger?resource=acct:jimm@localhost")
  68. |> response(404)
  69. assert result == "Couldn't find user"
  70. end
  71. test "Sends a 404 when invalid format" do
  72. user = insert(:user)
  73. assert capture_log(fn ->
  74. assert_raise Phoenix.NotAcceptableError, fn ->
  75. build_conn()
  76. |> put_req_header("accept", "text/html")
  77. |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
  78. end
  79. end) =~ "no supported media type in accept header"
  80. end
  81. test "Sends a 400 when resource param is missing" do
  82. response =
  83. build_conn()
  84. |> put_req_header("accept", "application/xrd+xml,application/jrd+json")
  85. |> get("/.well-known/webfinger")
  86. assert response(response, 400)
  87. end
  88. end