1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Feed.UserControllerTest do
6 use Pleroma.Web.ConnCase
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.Feed.FeedView
16 setup do: clear_config([:static_fe, :enabled], false)
19 setup do: clear_config([:feed])
24 %{max_length: 15, omission: "..."}
27 activity = insert(:note_activity)
32 "content" => "This & this is :moominmamma: note ",
33 "source" => "This & this is :moominmamma: note ",
37 %{"mediaType" => "image/png", "href" => "https://pleroma.gov/image.png"}
41 "inReplyTo" => activity.data["id"],
42 "context" => "2hu & as",
43 "summary" => "2hu & as"
47 note_activity = insert(:note_activity, note: note)
48 user = User.get_cached_by_ap_id(note_activity.data["actor"])
54 "content" => "42 & This is :moominmamma: note ",
55 "inReplyTo" => activity.data["id"]
59 note_activity2 = insert(:note_activity, note: note2)
65 "content" => "This note tests whether HTML entities are truncated properly",
66 "summary" => "Won't, didn't fail",
67 "inReplyTo" => note_activity2.id
71 _note_activity3 = insert(:note_activity, note: note3)
72 object = Object.normalize(note_activity, fetch: false)
74 encoded_title = FeedView.activity_title(note3.data)
76 [user: user, object: object, max_id: note_activity2.id, encoded_title: encoded_title]
79 test "gets an atom feed", %{conn: conn, user: user, object: object, max_id: max_id} do
82 |> put_req_header("accept", "application/atom+xml")
83 |> get(user_feed_path(conn, :feed, user.nickname))
89 |> SweetXml.xpath(~x"//entry/title/text()"l)
91 assert activity_titles == ['Won\'t, didn\'...', '2hu', '2hu & as']
92 assert resp =~ FeedView.escape(object.data["content"])
93 assert resp =~ FeedView.escape(object.data["summary"])
94 assert resp =~ FeedView.escape(object.data["context"])
98 |> put_req_header("accept", "application/atom+xml")
99 |> get("/users/#{user.nickname}/feed", %{"max_id" => max_id})
105 |> SweetXml.xpath(~x"//entry/title/text()"l)
107 assert activity_titles == ['2hu & as']
110 test "gets a rss feed", %{conn: conn, user: user, object: object, max_id: max_id} do
113 |> put_req_header("accept", "application/rss+xml")
114 |> get("/users/#{user.nickname}/feed.rss")
120 |> SweetXml.xpath(~x"//item/title/text()"l)
122 assert activity_titles == ['Won\'t, didn\'...', '2hu', '2hu & as']
123 assert resp =~ FeedView.escape(object.data["content"])
124 assert resp =~ FeedView.escape(object.data["summary"])
125 assert resp =~ FeedView.escape(object.data["context"])
129 |> put_req_header("accept", "application/rss+xml")
130 |> get("/users/#{user.nickname}/feed.rss", %{"max_id" => max_id})
136 |> SweetXml.xpath(~x"//item/title/text()"l)
138 assert activity_titles == ['2hu & as']
141 test "returns 404 for a missing feed", %{conn: conn} do
144 |> put_req_header("accept", "application/atom+xml")
145 |> get(user_feed_path(conn, :feed, "nonexisting"))
147 assert response(conn, 404)
150 test "returns feed with public and unlisted activities", %{conn: conn} do
153 {:ok, _} = CommonAPI.post(user, %{status: "public", visibility: "public"})
154 {:ok, _} = CommonAPI.post(user, %{status: "direct", visibility: "direct"})
155 {:ok, _} = CommonAPI.post(user, %{status: "unlisted", visibility: "unlisted"})
156 {:ok, _} = CommonAPI.post(user, %{status: "private", visibility: "private"})
160 |> put_req_header("accept", "application/atom+xml")
161 |> get(user_feed_path(conn, :feed, user.nickname))
167 |> SweetXml.xpath(~x"//entry/title/text()"l)
170 assert activity_titles == ['public', 'unlisted']
173 test "returns 404 when the user is remote", %{conn: conn} do
174 user = insert(:user, local: false)
176 {:ok, _} = CommonAPI.post(user, %{status: "test"})
179 |> put_req_header("accept", "application/atom+xml")
180 |> get(user_feed_path(conn, :feed, user.nickname))
184 test "does not require authentication on non-federating instances", %{conn: conn} do
185 clear_config([:instance, :federating], false)
189 |> put_req_header("accept", "application/rss+xml")
190 |> get("/users/#{user.nickname}/feed.rss")
194 test "does not mangle HTML entities midway", %{
198 encoded_title: encoded_title
202 |> put_req_header("accept", "application/atom+xml")
203 |> get(user_feed_path(conn, :feed, user.nickname))
209 |> SweetXml.xpath(~x"//entry/title/text()"l)
211 assert activity_titles == ['Won\'t, didn\'...', '2hu', '2hu & as']
212 assert resp =~ FeedView.escape(object.data["content"])
213 assert resp =~ FeedView.escape(object.data["summary"])
214 assert resp =~ FeedView.escape(object.data["context"])
215 assert resp =~ encoded_title
219 # Note: see ActivityPubControllerTest for JSON format tests
220 describe "feed_redirect" do
221 test "with html format, it redirects to user feed", %{conn: conn} do
222 note_activity = insert(:note_activity)
223 user = User.get_cached_by_ap_id(note_activity.data["actor"])
227 |> get("/users/#{user.nickname}")
231 Pleroma.Web.Fallback.RedirectController.redirector_with_meta(
237 test "with html format, it falls back to frontend when user is remote", %{conn: conn} do
238 user = insert(:user, local: false)
240 {:ok, _} = CommonAPI.post(user, %{status: "test"})
244 |> get("/users/#{user.nickname}")
247 assert response =~ "</html>"
250 test "with html format, it falls back to frontend when user is not found", %{conn: conn} do
253 |> get("/users/jimm")
256 assert response =~ "</html>"
259 test "with non-html / non-json format, it redirects to user feed in atom format", %{
262 note_activity = insert(:note_activity)
263 user = User.get_cached_by_ap_id(note_activity.data["actor"])
267 |> put_req_header("accept", "application/xml")
268 |> get("/users/#{user.nickname}")
270 assert conn.status == 302
272 assert redirected_to(conn) ==
273 "#{Pleroma.Web.Endpoint.url()}/users/#{user.nickname}/feed.atom"
276 test "with non-html / non-json format, it returns error when user is not found", %{conn: conn} do
279 |> put_req_header("accept", "application/xml")
280 |> get(user_feed_path(conn, :feed, "jimm"))
283 assert response == ~S({"error":"Not found"})
287 describe "private instance" do
288 setup do: clear_config([:instance, :public])
290 test "returns 404 for user feed", %{conn: conn} do
291 clear_config([:instance, :public], false)
294 {:ok, _} = CommonAPI.post(user, %{status: "test"})
297 |> put_req_header("accept", "application/atom+xml")
298 |> get(user_feed_path(conn, :feed, user.nickname))