Deprecate Pleroma.Web.base_url/0
[akkoma] / test / pleroma / web / feed / tag_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 assert xpath(xml, ~x"//feed/entry/author/id/text()"ls) == [user.ap_id, user.ap_id]
67
68 conn =
69 conn
70 |> put_req_header("accept", "application/atom+xml")
71 |> get("/tags/pleromaart.atom", %{"max_id" => activity2.id})
72
73 assert get_resp_header(conn, "content-type") == ["application/atom+xml; charset=utf-8"]
74 resp = response(conn, 200)
75 xml = parse(resp)
76
77 assert xpath(xml, ~x"//feed/title/text()") == '#pleromaart'
78
79 assert xpath(xml, ~x"//feed/entry/title/text()"l) == [
80 'yeah #PleromaArt'
81 ]
82 end
83
84 test "gets a feed (RSS)", %{conn: conn} do
85 clear_config(
86 [:feed, :post_title],
87 %{max_length: 25, omission: "..."}
88 )
89
90 user = insert(:user)
91 {:ok, activity1} = CommonAPI.post(user, %{status: "yeah #PleromaArt"})
92
93 object = Object.normalize(activity1, fetch: false)
94
95 object_data =
96 Map.put(object.data, "attachment", [
97 %{
98 "url" => [
99 %{
100 "href" =>
101 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
102 "mediaType" => "video/mp4",
103 "type" => "Link"
104 }
105 ]
106 }
107 ])
108
109 object
110 |> Ecto.Changeset.change(data: object_data)
111 |> Pleroma.Repo.update()
112
113 {:ok, activity2} = CommonAPI.post(user, %{status: "42 This is :moominmamma #PleromaArt"})
114
115 {:ok, _activity3} = CommonAPI.post(user, %{status: "This is :moominmamma"})
116
117 response =
118 conn
119 |> put_req_header("accept", "application/rss+xml")
120 |> get(tag_feed_path(conn, :feed, "pleromaart.rss"))
121 |> response(200)
122
123 xml = parse(response)
124 assert xpath(xml, ~x"//channel/title/text()") == '#pleromaart'
125
126 assert xpath(xml, ~x"//channel/description/text()"s) ==
127 "These are public toots tagged with #pleromaart. You can interact with them if you have an account anywhere in the fediverse."
128
129 assert xpath(xml, ~x"//channel/link/text()") ==
130 '#{Pleroma.Web.Endpoint.url()}/tags/pleromaart.rss'
131
132 assert xpath(xml, ~x"//channel/webfeeds:logo/text()") ==
133 '#{Pleroma.Web.Endpoint.url()}/static/logo.svg'
134
135 assert xpath(xml, ~x"//channel/item/title/text()"l) == [
136 '42 This is :moominmamm...',
137 'yeah #PleromaArt'
138 ]
139
140 assert xpath(xml, ~x"//channel/item/pubDate/text()"sl) == [
141 FeedView.pub_date(activity2.data["published"]),
142 FeedView.pub_date(activity1.data["published"])
143 ]
144
145 assert xpath(xml, ~x"//channel/item/enclosure/@url"sl) == [
146 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4"
147 ]
148
149 obj1 = Object.normalize(activity1, fetch: false)
150 obj2 = Object.normalize(activity2, fetch: false)
151
152 assert xpath(xml, ~x"//channel/item/description/text()"sl) == [
153 HtmlEntities.decode(FeedView.activity_content(obj2.data)),
154 HtmlEntities.decode(FeedView.activity_content(obj1.data))
155 ]
156
157 response =
158 conn
159 |> put_req_header("accept", "application/rss+xml")
160 |> get(tag_feed_path(conn, :feed, "pleromaart.rss"))
161 |> response(200)
162
163 xml = parse(response)
164 assert xpath(xml, ~x"//channel/title/text()") == '#pleromaart'
165
166 assert xpath(xml, ~x"//channel/description/text()"s) ==
167 "These are public toots tagged with #pleromaart. You can interact with them if you have an account anywhere in the fediverse."
168
169 conn =
170 conn
171 |> put_req_header("accept", "application/rss+xml")
172 |> get("/tags/pleromaart.rss", %{"max_id" => activity2.id})
173
174 assert get_resp_header(conn, "content-type") == ["application/rss+xml; charset=utf-8"]
175 resp = response(conn, 200)
176 xml = parse(resp)
177
178 assert xpath(xml, ~x"//channel/title/text()") == '#pleromaart'
179
180 assert xpath(xml, ~x"//channel/item/title/text()"l) == [
181 'yeah #PleromaArt'
182 ]
183 end
184
185 describe "private instance" do
186 setup do: clear_config([:instance, :public], false)
187
188 test "returns 404 for tags feed", %{conn: conn} do
189 conn
190 |> put_req_header("accept", "application/rss+xml")
191 |> get(tag_feed_path(conn, :feed, "pleromaart.rss"))
192 |> response(404)
193 end
194 end
195 end