3e52eb42b10269ae8cb934289a0425ffa3b068c0
[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("content-type", "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 end
72
73 test "returns 404 for a missing feed", %{conn: conn} do
74 conn =
75 conn
76 |> put_req_header("content-type", "application/atom+xml")
77 |> get(user_feed_path(conn, :feed, "nonexisting"))
78
79 assert response(conn, 404)
80 end
81 end
82
83 # Note: see ActivityPubControllerTest for JSON format tests
84 describe "feed_redirect" do
85 test "with html format, it redirects to user feed", %{conn: conn} do
86 note_activity = insert(:note_activity)
87 user = User.get_cached_by_ap_id(note_activity.data["actor"])
88
89 response =
90 conn
91 |> get("/users/#{user.nickname}")
92 |> response(200)
93
94 assert response ==
95 Fallback.RedirectController.redirector_with_meta(
96 conn,
97 %{user: user}
98 ).resp_body
99 end
100
101 test "with html format, it returns error when user is not found", %{conn: conn} do
102 response =
103 conn
104 |> get("/users/jimm")
105 |> json_response(404)
106
107 assert response == %{"error" => "Not found"}
108 end
109
110 test "with non-html / non-json format, it redirects to user feed in atom format", %{
111 conn: conn
112 } do
113 note_activity = insert(:note_activity)
114 user = User.get_cached_by_ap_id(note_activity.data["actor"])
115
116 conn =
117 conn
118 |> put_req_header("accept", "application/xml")
119 |> get("/users/#{user.nickname}")
120
121 assert conn.status == 302
122 assert redirected_to(conn) == "#{Pleroma.Web.base_url()}/users/#{user.nickname}/feed.atom"
123 end
124
125 test "with non-html / non-json format, it returns error when user is not found", %{conn: conn} do
126 response =
127 conn
128 |> put_req_header("accept", "application/xml")
129 |> get(user_feed_path(conn, :feed, "jimm"))
130 |> response(404)
131
132 assert response == ~S({"error":"Not found"})
133 end
134 end
135 end