logo

pleroma

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

user_controller_test.exs (7559B)


  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.Feed.UserControllerTest do
  5. use Pleroma.Web.ConnCase
  6. import Pleroma.Factory
  7. import SweetXml
  8. alias Pleroma.Config
  9. alias Pleroma.Object
  10. alias Pleroma.User
  11. alias Pleroma.Web.CommonAPI
  12. setup do: clear_config([:instance, :federating], true)
  13. describe "feed" do
  14. setup do: clear_config([:feed])
  15. test "gets an atom feed", %{conn: conn} do
  16. Config.put(
  17. [:feed, :post_title],
  18. %{max_length: 10, omission: "..."}
  19. )
  20. activity = insert(:note_activity)
  21. note =
  22. insert(:note,
  23. data: %{
  24. "content" => "This is :moominmamma: note ",
  25. "attachment" => [
  26. %{
  27. "url" => [
  28. %{"mediaType" => "image/png", "href" => "https://pleroma.gov/image.png"}
  29. ]
  30. }
  31. ],
  32. "inReplyTo" => activity.data["id"]
  33. }
  34. )
  35. note_activity = insert(:note_activity, note: note)
  36. user = User.get_cached_by_ap_id(note_activity.data["actor"])
  37. note2 =
  38. insert(:note,
  39. user: user,
  40. data: %{
  41. "content" => "42 This is :moominmamma: note ",
  42. "inReplyTo" => activity.data["id"]
  43. }
  44. )
  45. note_activity2 = insert(:note_activity, note: note2)
  46. object = Object.normalize(note_activity)
  47. resp =
  48. conn
  49. |> put_req_header("accept", "application/atom+xml")
  50. |> get(user_feed_path(conn, :feed, user.nickname))
  51. |> response(200)
  52. activity_titles =
  53. resp
  54. |> SweetXml.parse()
  55. |> SweetXml.xpath(~x"//entry/title/text()"l)
  56. assert activity_titles == ['42 This...', 'This is...']
  57. assert resp =~ object.data["content"]
  58. resp =
  59. conn
  60. |> put_req_header("accept", "application/atom+xml")
  61. |> get("/users/#{user.nickname}/feed", %{"max_id" => note_activity2.id})
  62. |> response(200)
  63. activity_titles =
  64. resp
  65. |> SweetXml.parse()
  66. |> SweetXml.xpath(~x"//entry/title/text()"l)
  67. assert activity_titles == ['This is...']
  68. end
  69. test "gets a rss feed", %{conn: conn} do
  70. Pleroma.Config.put(
  71. [:feed, :post_title],
  72. %{max_length: 10, omission: "..."}
  73. )
  74. activity = insert(:note_activity)
  75. note =
  76. insert(:note,
  77. data: %{
  78. "content" => "This is :moominmamma: note ",
  79. "attachment" => [
  80. %{
  81. "url" => [
  82. %{"mediaType" => "image/png", "href" => "https://pleroma.gov/image.png"}
  83. ]
  84. }
  85. ],
  86. "inReplyTo" => activity.data["id"]
  87. }
  88. )
  89. note_activity = insert(:note_activity, note: note)
  90. user = User.get_cached_by_ap_id(note_activity.data["actor"])
  91. note2 =
  92. insert(:note,
  93. user: user,
  94. data: %{
  95. "content" => "42 This is :moominmamma: note ",
  96. "inReplyTo" => activity.data["id"]
  97. }
  98. )
  99. note_activity2 = insert(:note_activity, note: note2)
  100. object = Object.normalize(note_activity)
  101. resp =
  102. conn
  103. |> put_req_header("accept", "application/rss+xml")
  104. |> get("/users/#{user.nickname}/feed.rss")
  105. |> response(200)
  106. activity_titles =
  107. resp
  108. |> SweetXml.parse()
  109. |> SweetXml.xpath(~x"//item/title/text()"l)
  110. assert activity_titles == ['42 This...', 'This is...']
  111. assert resp =~ object.data["content"]
  112. resp =
  113. conn
  114. |> put_req_header("accept", "application/rss+xml")
  115. |> get("/users/#{user.nickname}/feed.rss", %{"max_id" => note_activity2.id})
  116. |> response(200)
  117. activity_titles =
  118. resp
  119. |> SweetXml.parse()
  120. |> SweetXml.xpath(~x"//item/title/text()"l)
  121. assert activity_titles == ['This is...']
  122. end
  123. test "returns 404 for a missing feed", %{conn: conn} do
  124. conn =
  125. conn
  126. |> put_req_header("accept", "application/atom+xml")
  127. |> get(user_feed_path(conn, :feed, "nonexisting"))
  128. assert response(conn, 404)
  129. end
  130. test "returns feed with public and unlisted activities", %{conn: conn} do
  131. user = insert(:user)
  132. {:ok, _} = CommonAPI.post(user, %{status: "public", visibility: "public"})
  133. {:ok, _} = CommonAPI.post(user, %{status: "direct", visibility: "direct"})
  134. {:ok, _} = CommonAPI.post(user, %{status: "unlisted", visibility: "unlisted"})
  135. {:ok, _} = CommonAPI.post(user, %{status: "private", visibility: "private"})
  136. resp =
  137. conn
  138. |> put_req_header("accept", "application/atom+xml")
  139. |> get(user_feed_path(conn, :feed, user.nickname))
  140. |> response(200)
  141. activity_titles =
  142. resp
  143. |> SweetXml.parse()
  144. |> SweetXml.xpath(~x"//entry/title/text()"l)
  145. |> Enum.sort()
  146. assert activity_titles == ['public', 'unlisted']
  147. end
  148. test "returns 404 when the user is remote", %{conn: conn} do
  149. user = insert(:user, local: false)
  150. {:ok, _} = CommonAPI.post(user, %{status: "test"})
  151. assert conn
  152. |> put_req_header("accept", "application/atom+xml")
  153. |> get(user_feed_path(conn, :feed, user.nickname))
  154. |> response(404)
  155. end
  156. end
  157. # Note: see ActivityPubControllerTest for JSON format tests
  158. describe "feed_redirect" do
  159. test "with html format, it redirects to user feed", %{conn: conn} do
  160. note_activity = insert(:note_activity)
  161. user = User.get_cached_by_ap_id(note_activity.data["actor"])
  162. response =
  163. conn
  164. |> get("/users/#{user.nickname}")
  165. |> response(200)
  166. assert response ==
  167. Fallback.RedirectController.redirector_with_meta(
  168. conn,
  169. %{user: user}
  170. ).resp_body
  171. end
  172. test "with html format, it returns error when user is not found", %{conn: conn} do
  173. response =
  174. conn
  175. |> get("/users/jimm")
  176. |> json_response(404)
  177. assert response == %{"error" => "Not found"}
  178. end
  179. test "with non-html / non-json format, it redirects to user feed in atom format", %{
  180. conn: conn
  181. } do
  182. note_activity = insert(:note_activity)
  183. user = User.get_cached_by_ap_id(note_activity.data["actor"])
  184. conn =
  185. conn
  186. |> put_req_header("accept", "application/xml")
  187. |> get("/users/#{user.nickname}")
  188. assert conn.status == 302
  189. assert redirected_to(conn) == "#{Pleroma.Web.base_url()}/users/#{user.nickname}/feed.atom"
  190. end
  191. test "with non-html / non-json format, it returns error when user is not found", %{conn: conn} do
  192. response =
  193. conn
  194. |> put_req_header("accept", "application/xml")
  195. |> get(user_feed_path(conn, :feed, "jimm"))
  196. |> response(404)
  197. assert response == ~S({"error":"Not found"})
  198. end
  199. end
  200. describe "private instance" do
  201. setup do: clear_config([:instance, :public])
  202. test "returns 404 for user feed", %{conn: conn} do
  203. Config.put([:instance, :public], false)
  204. user = insert(:user)
  205. {:ok, _} = CommonAPI.post(user, %{status: "test"})
  206. assert conn
  207. |> put_req_header("accept", "application/atom+xml")
  208. |> get(user_feed_path(conn, :feed, user.nickname))
  209. |> response(404)
  210. end
  211. end
  212. end