fd59ca892fdff4b078757ba207ae2ad3d0c9ddfa
[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("content-type", "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("content-type", "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 "returns 404 for a missing feed", %{conn: conn} do
89 conn =
90 conn
91 |> put_req_header("content-type", "application/atom+xml")
92 |> get(user_feed_path(conn, :feed, "nonexisting"))
93
94 assert response(conn, 404)
95 end
96 end
97
98 # Note: see ActivityPubControllerTest for JSON format tests
99 describe "feed_redirect" do
100 test "with html format, it redirects to user feed", %{conn: conn} do
101 note_activity = insert(:note_activity)
102 user = User.get_cached_by_ap_id(note_activity.data["actor"])
103
104 response =
105 conn
106 |> get("/users/#{user.nickname}")
107 |> response(200)
108
109 assert response ==
110 Fallback.RedirectController.redirector_with_meta(
111 conn,
112 %{user: user}
113 ).resp_body
114 end
115
116 test "with html format, it returns error when user is not found", %{conn: conn} do
117 response =
118 conn
119 |> get("/users/jimm")
120 |> json_response(404)
121
122 assert response == %{"error" => "Not found"}
123 end
124
125 test "with non-html / non-json format, it redirects to user feed in atom format", %{
126 conn: conn
127 } do
128 note_activity = insert(:note_activity)
129 user = User.get_cached_by_ap_id(note_activity.data["actor"])
130
131 conn =
132 conn
133 |> put_req_header("accept", "application/xml")
134 |> get("/users/#{user.nickname}")
135
136 assert conn.status == 302
137 assert redirected_to(conn) == "#{Pleroma.Web.base_url()}/users/#{user.nickname}/feed.atom"
138 end
139
140 test "with non-html / non-json format, it returns error when user is not found", %{conn: conn} do
141 response =
142 conn
143 |> put_req_header("accept", "application/xml")
144 |> get(user_feed_path(conn, :feed, "jimm"))
145 |> response(404)
146
147 assert response == ~S({"error":"Not found"})
148 end
149 end
150 end