logo

pleroma

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

static_fe_controller_test.exs (6968B)


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