move to 2.5.5
[anni] / test / pleroma / web / feed / tag_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Feed.TagControllerTest do
6   use Pleroma.Web.ConnCase
7
8   import Pleroma.Factory
9   import SweetXml
10
11   alias Pleroma.Object
12   alias Pleroma.Web.CommonAPI
13   alias Pleroma.Web.Feed.FeedView
14
15   setup do: clear_config([:feed])
16
17   test "gets a feed (ATOM)", %{conn: conn} do
18     clear_config(
19       [:feed, :post_title],
20       %{max_length: 25, omission: "..."}
21     )
22
23     user = insert(:user)
24     {:ok, activity1} = CommonAPI.post(user, %{status: "yeah #PleromaArt"})
25
26     object = Object.normalize(activity1, fetch: false)
27
28     object_data =
29       Map.put(object.data, "attachment", [
30         %{
31           "url" => [
32             %{
33               "href" =>
34                 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
35               "mediaType" => "video/mp4",
36               "type" => "Link"
37             }
38           ]
39         }
40       ])
41
42     object
43     |> Ecto.Changeset.change(data: object_data)
44     |> Pleroma.Repo.update()
45
46     {:ok, activity2} = CommonAPI.post(user, %{status: "42 This is :moominmamma #PleromaArt"})
47
48     {:ok, _activity3} = CommonAPI.post(user, %{status: "This is :moominmamma"})
49
50     response =
51       conn
52       |> put_req_header("accept", "application/atom+xml")
53       |> get(tag_feed_path(conn, :feed, "pleromaart.atom"))
54       |> response(200)
55
56     xml = parse(response)
57
58     assert xpath(xml, ~x"//feed/title/text()") == '#pleromaart'
59
60     assert xpath(xml, ~x"//feed/entry/title/text()"l) == [
61              '42 This is :moominmamm...',
62              'yeah #PleromaArt'
63            ]
64
65     assert xpath(xml, ~x"//feed/entry/author/name/text()"ls) == [user.nickname, user.nickname]
66
67     conn =
68       conn
69       |> put_req_header("accept", "application/atom+xml")
70       |> get("/tags/pleromaart.atom", %{"max_id" => activity2.id})
71
72     assert get_resp_header(conn, "content-type") == ["application/atom+xml; charset=utf-8"]
73     resp = response(conn, 200)
74     xml = parse(resp)
75
76     assert xpath(xml, ~x"//feed/title/text()") == '#pleromaart'
77
78     assert xpath(xml, ~x"//feed/entry/title/text()"l) == [
79              'yeah #PleromaArt'
80            ]
81   end
82
83   test "gets a feed (RSS)", %{conn: conn} do
84     clear_config(
85       [:feed, :post_title],
86       %{max_length: 25, omission: "..."}
87     )
88
89     user = insert(:user)
90     {:ok, activity1} = CommonAPI.post(user, %{status: "yeah #PleromaArt"})
91
92     object = Object.normalize(activity1, fetch: false)
93
94     object_data =
95       Map.put(object.data, "attachment", [
96         %{
97           "url" => [
98             %{
99               "href" =>
100                 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
101               "mediaType" => "video/mp4",
102               "type" => "Link"
103             }
104           ]
105         }
106       ])
107
108     object
109     |> Ecto.Changeset.change(data: object_data)
110     |> Pleroma.Repo.update()
111
112     {:ok, activity2} = CommonAPI.post(user, %{status: "42 This is :moominmamma #PleromaArt"})
113
114     {:ok, _activity3} = CommonAPI.post(user, %{status: "This is :moominmamma"})
115
116     response =
117       conn
118       |> put_req_header("accept", "application/rss+xml")
119       |> get(tag_feed_path(conn, :feed, "pleromaart.rss"))
120       |> response(200)
121
122     xml = parse(response)
123     assert xpath(xml, ~x"//channel/title/text()") == '#pleromaart'
124
125     assert xpath(xml, ~x"//channel/description/text()"s) ==
126              "These are public toots tagged with #pleromaart. You can interact with them if you have an account anywhere in the fediverse."
127
128     assert xpath(xml, ~x"//channel/link/text()") ==
129              '#{Pleroma.Web.Endpoint.url()}/tags/pleromaart.rss'
130
131     assert xpath(xml, ~x"//channel/webfeeds:logo/text()") ==
132              '#{Pleroma.Web.Endpoint.url()}/static/logo.svg'
133
134     assert xpath(xml, ~x"//channel/item/title/text()"l) == [
135              '42 This is :moominmamm...',
136              'yeah #PleromaArt'
137            ]
138
139     assert xpath(xml, ~x"//channel/item/pubDate/text()"sl) == [
140              FeedView.to_rfc2822(activity2.data["published"]),
141              FeedView.to_rfc2822(activity1.data["published"])
142            ]
143
144     assert xpath(xml, ~x"//channel/item/enclosure/@url"sl) == [
145              "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4"
146            ]
147
148     obj1 = Object.normalize(activity1, fetch: false)
149     obj2 = Object.normalize(activity2, fetch: false)
150
151     assert xpath(xml, ~x"//channel/item/description/text()"sl) == [
152              HtmlEntities.decode(FeedView.activity_content(obj2.data)),
153              HtmlEntities.decode(FeedView.activity_content(obj1.data))
154            ]
155
156     response =
157       conn
158       |> put_req_header("accept", "application/rss+xml")
159       |> get(tag_feed_path(conn, :feed, "pleromaart.rss"))
160       |> response(200)
161
162     xml = parse(response)
163     assert xpath(xml, ~x"//channel/title/text()") == '#pleromaart'
164
165     assert xpath(xml, ~x"//channel/description/text()"s) ==
166              "These are public toots tagged with #pleromaart. You can interact with them if you have an account anywhere in the fediverse."
167
168     conn =
169       conn
170       |> put_req_header("accept", "application/rss+xml")
171       |> get("/tags/pleromaart.rss", %{"max_id" => activity2.id})
172
173     assert get_resp_header(conn, "content-type") == ["application/rss+xml; charset=utf-8"]
174     resp = response(conn, 200)
175     xml = parse(resp)
176
177     assert xpath(xml, ~x"//channel/title/text()") == '#pleromaart'
178
179     assert xpath(xml, ~x"//channel/item/title/text()"l) == [
180              'yeah #PleromaArt'
181            ]
182   end
183
184   describe "private instance" do
185     setup do: clear_config([:instance, :public], false)
186
187     test "returns 404 for tags feed", %{conn: conn} do
188       conn
189       |> put_req_header("accept", "application/rss+xml")
190       |> get(tag_feed_path(conn, :feed, "pleromaart.rss"))
191       |> response(404)
192     end
193   end
194 end