logo

pleroma

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

o_status_controller_test.exs (10265B)


  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.OStatus.OStatusControllerTest do
  5. use Pleroma.Web.ConnCase
  6. import Pleroma.Factory
  7. alias Pleroma.Object
  8. alias Pleroma.User
  9. alias Pleroma.Web.ActivityPub.ActivityPub
  10. alias Pleroma.Web.CommonAPI
  11. alias Pleroma.Web.Endpoint
  12. require Pleroma.Constants
  13. setup_all do
  14. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  15. :ok
  16. end
  17. setup do: clear_config([:static_fe, :enabled], false)
  18. describe "Mastodon compatibility routes" do
  19. setup %{conn: conn} do
  20. conn = put_req_header(conn, "accept", "text/html")
  21. {:ok, object} =
  22. %{
  23. "type" => "Note",
  24. "content" => "hey",
  25. "id" => Endpoint.url() <> "/users/raymoo/statuses/999999999",
  26. "actor" => Endpoint.url() <> "/users/raymoo",
  27. "to" => [Pleroma.Constants.as_public()]
  28. }
  29. |> Object.create()
  30. {:ok, activity, _} =
  31. %{
  32. "id" => object.data["id"] <> "/activity",
  33. "type" => "Create",
  34. "object" => object.data["id"],
  35. "actor" => object.data["actor"],
  36. "to" => object.data["to"]
  37. }
  38. |> ActivityPub.persist(local: true)
  39. %{conn: conn, activity: activity}
  40. end
  41. test "redirects to /notice/:id for html format", %{conn: conn, activity: activity} do
  42. conn = get(conn, "/users/raymoo/statuses/999999999")
  43. assert redirected_to(conn) == "/notice/#{activity.id}"
  44. end
  45. test "redirects to /notice/:id for html format for activity", %{
  46. conn: conn,
  47. activity: activity
  48. } do
  49. conn = get(conn, "/users/raymoo/statuses/999999999/activity")
  50. assert redirected_to(conn) == "/notice/#{activity.id}"
  51. end
  52. end
  53. # Note: see ActivityPubControllerTest for JSON format tests
  54. describe "GET /objects/:uuid (text/html)" do
  55. setup %{conn: conn} do
  56. conn = put_req_header(conn, "accept", "text/html")
  57. %{conn: conn}
  58. end
  59. test "redirects to /notice/id for html format", %{conn: conn} do
  60. note_activity = insert(:note_activity)
  61. object = Object.normalize(note_activity, fetch: false)
  62. [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
  63. url = "/objects/#{uuid}"
  64. conn = get(conn, url)
  65. assert redirected_to(conn) == "/notice/#{note_activity.id}"
  66. end
  67. test "404s on private objects", %{conn: conn} do
  68. note_activity = insert(:direct_note_activity)
  69. object = Object.normalize(note_activity, fetch: false)
  70. [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
  71. conn
  72. |> get("/objects/#{uuid}")
  73. |> response(404)
  74. end
  75. test "404s on non-existing objects", %{conn: conn} do
  76. conn
  77. |> get("/objects/123")
  78. |> response(404)
  79. end
  80. end
  81. # Note: see ActivityPubControllerTest for JSON format tests
  82. describe "GET /activities/:uuid (text/html)" do
  83. setup %{conn: conn} do
  84. conn = put_req_header(conn, "accept", "text/html")
  85. %{conn: conn}
  86. end
  87. test "redirects to /notice/id for html format", %{conn: conn} do
  88. note_activity = insert(:note_activity)
  89. [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
  90. conn = get(conn, "/activities/#{uuid}")
  91. assert redirected_to(conn) == "/notice/#{note_activity.id}"
  92. end
  93. test "404s on private activities", %{conn: conn} do
  94. note_activity = insert(:direct_note_activity)
  95. [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
  96. conn
  97. |> get("/activities/#{uuid}")
  98. |> response(404)
  99. end
  100. test "404s on nonexistent activities", %{conn: conn} do
  101. conn
  102. |> get("/activities/123")
  103. |> response(404)
  104. end
  105. end
  106. describe "GET notice/2" do
  107. test "redirects to a proper object URL when json requested and the object is local", %{
  108. conn: conn
  109. } do
  110. note_activity = insert(:note_activity)
  111. expected_redirect_url = Object.normalize(note_activity, fetch: false).data["id"]
  112. redirect_url =
  113. conn
  114. |> put_req_header("accept", "application/activity+json")
  115. |> get("/notice/#{note_activity.id}")
  116. |> redirected_to()
  117. assert redirect_url == expected_redirect_url
  118. end
  119. test "redirects to a proper object URL when json requested and the object is remote", %{
  120. conn: conn
  121. } do
  122. note_activity = insert(:note_activity, local: false)
  123. expected_redirect_url = Object.normalize(note_activity, fetch: false).data["id"]
  124. redirect_url =
  125. conn
  126. |> put_req_header("accept", "application/activity+json")
  127. |> get("/notice/#{note_activity.id}")
  128. |> redirected_to()
  129. assert redirect_url == expected_redirect_url
  130. end
  131. test "500s when actor not found", %{conn: conn} do
  132. note_activity = insert(:note_activity)
  133. user = User.get_cached_by_ap_id(note_activity.data["actor"])
  134. User.invalidate_cache(user)
  135. Pleroma.Repo.delete(user)
  136. conn =
  137. conn
  138. |> get("/notice/#{note_activity.id}")
  139. assert response(conn, 500) == ~S({"error":"Something went wrong"})
  140. end
  141. test "render html for redirect for html format", %{conn: conn} do
  142. note_activity = insert(:note_activity)
  143. resp =
  144. conn
  145. |> put_req_header("accept", "text/html")
  146. |> get("/notice/#{note_activity.id}")
  147. |> response(200)
  148. assert resp =~
  149. "<meta content=\"#{Pleroma.Web.Endpoint.url()}/notice/#{note_activity.id}\" property=\"og:url\">"
  150. user = insert(:user)
  151. {:ok, like_activity} = CommonAPI.favorite(user, note_activity.id)
  152. assert like_activity.data["type"] == "Like"
  153. resp =
  154. conn
  155. |> put_req_header("accept", "text/html")
  156. |> get("/notice/#{like_activity.id}")
  157. |> response(200)
  158. refute resp =~ ~r(<meta content="[^"]*" property="og:url")
  159. end
  160. test "404s a private notice", %{conn: conn} do
  161. note_activity = insert(:direct_note_activity)
  162. url = "/notice/#{note_activity.id}"
  163. conn =
  164. conn
  165. |> get(url)
  166. assert response(conn, 404)
  167. end
  168. test "404s a non-existing notice", %{conn: conn} do
  169. url = "/notice/123"
  170. conn =
  171. conn
  172. |> get(url)
  173. assert response(conn, 404)
  174. end
  175. test "does not require authentication on non-federating instances", %{
  176. conn: conn
  177. } do
  178. clear_config([:instance, :federating], false)
  179. note_activity = insert(:note_activity)
  180. conn
  181. |> put_req_header("accept", "text/html")
  182. |> get("/notice/#{note_activity.id}")
  183. |> response(200)
  184. end
  185. end
  186. describe "GET /notice/:id/embed_player" do
  187. setup do
  188. note_activity = insert(:note_activity)
  189. object = Pleroma.Object.normalize(note_activity, fetch: false)
  190. object_data =
  191. Map.put(object.data, "attachment", [
  192. %{
  193. "url" => [
  194. %{
  195. "href" =>
  196. "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
  197. "mediaType" => "video/mp4",
  198. "type" => "Link"
  199. }
  200. ]
  201. }
  202. ])
  203. object
  204. |> Ecto.Changeset.change(data: object_data)
  205. |> Pleroma.Repo.update()
  206. %{note_activity: note_activity}
  207. end
  208. test "renders embed player", %{conn: conn, note_activity: note_activity} do
  209. conn = get(conn, "/notice/#{note_activity.id}/embed_player")
  210. assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
  211. assert Plug.Conn.get_resp_header(
  212. conn,
  213. "content-security-policy"
  214. ) == [
  215. "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
  216. ]
  217. assert response(conn, 200) =~
  218. "<video controls loop><source src=\"https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4\" type=\"video/mp4\">Your browser does not support video/mp4 playback.</video>"
  219. end
  220. test "404s when activity isn't create", %{conn: conn} do
  221. note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
  222. assert conn
  223. |> get("/notice/#{note_activity.id}/embed_player")
  224. |> response(404)
  225. end
  226. test "404s when activity is direct message", %{conn: conn} do
  227. note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
  228. assert conn
  229. |> get("/notice/#{note_activity.id}/embed_player")
  230. |> response(404)
  231. end
  232. test "404s when attachment is empty", %{conn: conn} do
  233. note_activity = insert(:note_activity)
  234. object = Pleroma.Object.normalize(note_activity, fetch: false)
  235. object_data = Map.put(object.data, "attachment", [])
  236. object
  237. |> Ecto.Changeset.change(data: object_data)
  238. |> Pleroma.Repo.update()
  239. assert conn
  240. |> get("/notice/#{note_activity.id}/embed_player")
  241. |> response(404)
  242. end
  243. test "404s when attachment isn't audio or video", %{conn: conn} do
  244. note_activity = insert(:note_activity)
  245. object = Pleroma.Object.normalize(note_activity, fetch: false)
  246. object_data =
  247. Map.put(object.data, "attachment", [
  248. %{
  249. "url" => [
  250. %{
  251. "href" => "https://peertube.moe/static/webseed/480.jpg",
  252. "mediaType" => "image/jpg",
  253. "type" => "Link"
  254. }
  255. ]
  256. }
  257. ])
  258. object
  259. |> Ecto.Changeset.change(data: object_data)
  260. |> Pleroma.Repo.update()
  261. conn
  262. |> get("/notice/#{note_activity.id}/embed_player")
  263. |> response(404)
  264. end
  265. test "does not require authentication on non-federating instances", %{
  266. conn: conn,
  267. note_activity: note_activity
  268. } do
  269. clear_config([:instance, :federating], false)
  270. conn
  271. |> put_req_header("accept", "text/html")
  272. |> get("/notice/#{note_activity.id}/embed_player")
  273. |> response(200)
  274. end
  275. end
  276. end