logo

pleroma

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

ostatus_controller_test.exs (9956B)


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