logo

pleroma

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

static_fe_controller_test.exs (5961B)


  1. defmodule Pleroma.Web.StaticFE.StaticFEControllerTest do
  2. use Pleroma.Web.ConnCase
  3. alias Pleroma.Activity
  4. alias Pleroma.Config
  5. alias Pleroma.Web.ActivityPub.Transmogrifier
  6. alias Pleroma.Web.ActivityPub.Utils
  7. alias Pleroma.Web.CommonAPI
  8. import Pleroma.Factory
  9. setup_all do: clear_config([:static_fe, :enabled], true)
  10. setup do: clear_config([:instance, :federating], true)
  11. setup %{conn: conn} do
  12. conn = put_req_header(conn, "accept", "text/html")
  13. user = insert(:user)
  14. %{conn: conn, user: user}
  15. end
  16. describe "user profile html" do
  17. test "just the profile as HTML", %{conn: conn, user: user} do
  18. conn = get(conn, "/users/#{user.nickname}")
  19. assert html_response(conn, 200) =~ user.nickname
  20. end
  21. test "404 when user not found", %{conn: conn} do
  22. conn = get(conn, "/users/limpopo")
  23. assert html_response(conn, 404) =~ "not found"
  24. end
  25. test "profile does not include private messages", %{conn: conn, user: user} do
  26. CommonAPI.post(user, %{status: "public"})
  27. CommonAPI.post(user, %{status: "private", visibility: "private"})
  28. conn = get(conn, "/users/#{user.nickname}")
  29. html = html_response(conn, 200)
  30. assert html =~ ">public<"
  31. refute html =~ ">private<"
  32. end
  33. test "pagination", %{conn: conn, user: user} do
  34. Enum.map(1..30, fn i -> CommonAPI.post(user, %{status: "test#{i}"}) end)
  35. conn = get(conn, "/users/#{user.nickname}")
  36. html = html_response(conn, 200)
  37. assert html =~ ">test30<"
  38. assert html =~ ">test11<"
  39. refute html =~ ">test10<"
  40. refute html =~ ">test1<"
  41. end
  42. test "pagination, page 2", %{conn: conn, user: user} do
  43. activities = Enum.map(1..30, fn i -> CommonAPI.post(user, %{status: "test#{i}"}) end)
  44. {:ok, a11} = Enum.at(activities, 11)
  45. conn = get(conn, "/users/#{user.nickname}?max_id=#{a11.id}")
  46. html = html_response(conn, 200)
  47. assert html =~ ">test1<"
  48. assert html =~ ">test10<"
  49. refute html =~ ">test20<"
  50. refute html =~ ">test29<"
  51. end
  52. test "it requires authentication if instance is NOT federating", %{conn: conn, user: user} do
  53. ensure_federating_or_authenticated(conn, "/users/#{user.nickname}", user)
  54. end
  55. end
  56. describe "notice html" do
  57. test "single notice page", %{conn: conn, user: user} do
  58. {:ok, activity} = CommonAPI.post(user, %{status: "testing a thing!"})
  59. conn = get(conn, "/notice/#{activity.id}")
  60. html = html_response(conn, 200)
  61. assert html =~ "<header>"
  62. assert html =~ user.nickname
  63. assert html =~ "testing a thing!"
  64. end
  65. test "redirects to json if requested", %{conn: conn, user: user} do
  66. {:ok, activity} = CommonAPI.post(user, %{status: "testing a thing!"})
  67. conn =
  68. conn
  69. |> put_req_header(
  70. "accept",
  71. "Accept: application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", text/html"
  72. )
  73. |> get("/notice/#{activity.id}")
  74. assert redirected_to(conn, 302) =~ activity.data["object"]
  75. end
  76. test "filters HTML tags", %{conn: conn} do
  77. user = insert(:user)
  78. {:ok, activity} = CommonAPI.post(user, %{status: "<script>alert('xss')</script>"})
  79. conn =
  80. conn
  81. |> put_req_header("accept", "text/html")
  82. |> get("/notice/#{activity.id}")
  83. html = html_response(conn, 200)
  84. assert html =~ ~s[&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;]
  85. end
  86. test "shows the whole thread", %{conn: conn, user: user} do
  87. {:ok, activity} = CommonAPI.post(user, %{status: "space: the final frontier"})
  88. CommonAPI.post(user, %{
  89. status: "these are the voyages or something",
  90. in_reply_to_status_id: activity.id
  91. })
  92. conn = get(conn, "/notice/#{activity.id}")
  93. html = html_response(conn, 200)
  94. assert html =~ "the final frontier"
  95. assert html =~ "voyages"
  96. end
  97. test "redirect by AP object ID", %{conn: conn, user: user} do
  98. {:ok, %Activity{data: %{"object" => object_url}}} =
  99. CommonAPI.post(user, %{status: "beam me up"})
  100. conn = get(conn, URI.parse(object_url).path)
  101. assert html_response(conn, 302) =~ "redirected"
  102. end
  103. test "redirect by activity ID", %{conn: conn, user: user} do
  104. {:ok, %Activity{data: %{"id" => id}}} =
  105. CommonAPI.post(user, %{status: "I'm a doctor, not a devops!"})
  106. conn = get(conn, URI.parse(id).path)
  107. assert html_response(conn, 302) =~ "redirected"
  108. end
  109. test "404 when notice not found", %{conn: conn} do
  110. conn = get(conn, "/notice/88c9c317")
  111. assert html_response(conn, 404) =~ "not found"
  112. end
  113. test "404 for private status", %{conn: conn, user: user} do
  114. {:ok, activity} = CommonAPI.post(user, %{status: "don't show me!", visibility: "private"})
  115. conn = get(conn, "/notice/#{activity.id}")
  116. assert html_response(conn, 404) =~ "not found"
  117. end
  118. test "302 for remote cached status", %{conn: conn, user: user} do
  119. message = %{
  120. "@context" => "https://www.w3.org/ns/activitystreams",
  121. "type" => "Create",
  122. "actor" => user.ap_id,
  123. "object" => %{
  124. "to" => user.follower_address,
  125. "cc" => "https://www.w3.org/ns/activitystreams#Public",
  126. "id" => Utils.generate_object_id(),
  127. "content" => "blah blah blah",
  128. "type" => "Note",
  129. "attributedTo" => user.ap_id
  130. }
  131. }
  132. assert {:ok, activity} = Transmogrifier.handle_incoming(message)
  133. conn = get(conn, "/notice/#{activity.id}")
  134. assert html_response(conn, 302) =~ "redirected"
  135. end
  136. test "it requires authentication if instance is NOT federating", %{conn: conn, user: user} do
  137. {:ok, activity} = CommonAPI.post(user, %{status: "testing a thing!"})
  138. ensure_federating_or_authenticated(conn, "/notice/#{activity.id}", user)
  139. end
  140. end
  141. end