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