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