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