logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

web_finger_controller_test.exs (2746B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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.base_url()
  22. }/.well-known/webfinger?resource={uri}" type="application/xrd+xml" /></XRD>)
  23. end
  24. test "Webfinger JRD" do
  25. user = insert(:user)
  26. response =
  27. build_conn()
  28. |> put_req_header("accept", "application/jrd+json")
  29. |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
  30. assert json_response(response, 200)["subject"] == "acct:#{user.nickname}@localhost"
  31. end
  32. test "it returns 404 when user isn't found (JSON)" do
  33. result =
  34. build_conn()
  35. |> put_req_header("accept", "application/jrd+json")
  36. |> get("/.well-known/webfinger?resource=acct:jimm@localhost")
  37. |> json_response(404)
  38. assert result == "Couldn't find user"
  39. end
  40. test "Webfinger XML" do
  41. user = insert(:user)
  42. response =
  43. build_conn()
  44. |> put_req_header("accept", "application/xrd+xml")
  45. |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
  46. assert response(response, 200)
  47. end
  48. test "it returns 404 when user isn't found (XML)" do
  49. result =
  50. build_conn()
  51. |> put_req_header("accept", "application/xrd+xml")
  52. |> get("/.well-known/webfinger?resource=acct:jimm@localhost")
  53. |> response(404)
  54. assert result == "Couldn't find user"
  55. end
  56. test "Sends a 404 when invalid format" do
  57. user = insert(:user)
  58. assert capture_log(fn ->
  59. assert_raise Phoenix.NotAcceptableError, fn ->
  60. build_conn()
  61. |> put_req_header("accept", "text/html")
  62. |> get("/.well-known/webfinger?resource=acct:#{user.nickname}@localhost")
  63. end
  64. end) =~ "no supported media type in accept header"
  65. end
  66. test "Sends a 400 when resource param is missing" do
  67. response =
  68. build_conn()
  69. |> put_req_header("accept", "application/xrd+xml,application/jrd+json")
  70. |> get("/.well-known/webfinger")
  71. assert response(response, 400)
  72. end
  73. end