Fix tests
[akkoma] / test / web / feed / feed_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Feed.FeedControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Pleroma.Factory
9
10 alias Pleroma.Object
11 alias Pleroma.User
12
13 test "gets a feed", %{conn: conn} do
14 activity = insert(:note_activity)
15
16 note =
17 insert(:note,
18 data: %{
19 "attachment" => [
20 %{
21 "url" => [%{"mediaType" => "image/png", "href" => "https://pleroma.gov/image.png"}]
22 }
23 ],
24 "inReplyTo" => activity.data["id"]
25 }
26 )
27
28 note_activity = insert(:note_activity, note: note)
29 object = Object.normalize(note_activity)
30 user = User.get_cached_by_ap_id(note_activity.data["actor"])
31
32 conn =
33 conn
34 |> put_req_header("content-type", "application/atom+xml")
35 |> get("/users/#{user.nickname}/feed.atom")
36
37 assert response(conn, 200) =~ object.data["content"]
38 end
39
40 test "returns 404 for a missing feed", %{conn: conn} do
41 conn =
42 conn
43 |> put_req_header("content-type", "application/atom+xml")
44 |> get("/users/nonexisting/feed.atom")
45
46 assert response(conn, 404)
47 end
48
49 describe "feed_redirect" do
50 test "undefined format. it redirects to feed", %{conn: conn} do
51 note_activity = insert(:note_activity)
52 user = User.get_cached_by_ap_id(note_activity.data["actor"])
53
54 response =
55 conn
56 |> put_req_header("accept", "application/xml")
57 |> get("/users/#{user.nickname}")
58 |> response(302)
59
60 assert response ==
61 "<html><body>You are being <a href=\"#{Pleroma.Web.base_url()}/users/#{
62 user.nickname
63 }/feed.atom\">redirected</a>.</body></html>"
64 end
65
66 test "undefined format. it returns error when user not found", %{conn: conn} do
67 response =
68 conn
69 |> put_req_header("accept", "application/xml")
70 |> get("/users/jimm")
71 |> response(404)
72
73 assert response == ~S({"error":"Not found"})
74 end
75
76 test "activity+json format. it redirects on actual feed of user", %{conn: conn} do
77 note_activity = insert(:note_activity)
78 user = User.get_cached_by_ap_id(note_activity.data["actor"])
79
80 response =
81 conn
82 |> put_req_header("accept", "application/activity+json")
83 |> get("/users/#{user.nickname}")
84 |> json_response(200)
85
86 assert response["endpoints"] == %{
87 "oauthAuthorizationEndpoint" => "#{Pleroma.Web.base_url()}/oauth/authorize",
88 "oauthRegistrationEndpoint" => "#{Pleroma.Web.base_url()}/api/v1/apps",
89 "oauthTokenEndpoint" => "#{Pleroma.Web.base_url()}/oauth/token",
90 "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox",
91 "uploadMedia" => "#{Pleroma.Web.base_url()}/api/ap/upload_media"
92 }
93
94 assert response["@context"] == [
95 "https://www.w3.org/ns/activitystreams",
96 "http://localhost:4001/schemas/litepub-0.1.jsonld",
97 %{"@language" => "und"}
98 ]
99
100 assert Map.take(response, [
101 "followers",
102 "following",
103 "id",
104 "inbox",
105 "manuallyApprovesFollowers",
106 "name",
107 "outbox",
108 "preferredUsername",
109 "summary",
110 "tag",
111 "type",
112 "url"
113 ]) == %{
114 "followers" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/followers",
115 "following" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/following",
116 "id" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}",
117 "inbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/inbox",
118 "manuallyApprovesFollowers" => false,
119 "name" => user.name,
120 "outbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/outbox",
121 "preferredUsername" => user.nickname,
122 "summary" => user.bio,
123 "tag" => [],
124 "type" => "Person",
125 "url" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
126 }
127 end
128
129 test "activity+json format. it returns error whe use not found", %{conn: conn} do
130 response =
131 conn
132 |> put_req_header("accept", "application/activity+json")
133 |> get("/users/jimm")
134 |> json_response(404)
135
136 assert response == "Not found"
137 end
138
139 test "json format. it redirects on actual feed of user", %{conn: conn} do
140 note_activity = insert(:note_activity)
141 user = User.get_cached_by_ap_id(note_activity.data["actor"])
142
143 response =
144 conn
145 |> put_req_header("accept", "application/json")
146 |> get("/users/#{user.nickname}")
147 |> json_response(200)
148
149 assert response["endpoints"] == %{
150 "oauthAuthorizationEndpoint" => "#{Pleroma.Web.base_url()}/oauth/authorize",
151 "oauthRegistrationEndpoint" => "#{Pleroma.Web.base_url()}/api/v1/apps",
152 "oauthTokenEndpoint" => "#{Pleroma.Web.base_url()}/oauth/token",
153 "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox",
154 "uploadMedia" => "#{Pleroma.Web.base_url()}/api/ap/upload_media"
155 }
156
157 assert response["@context"] == [
158 "https://www.w3.org/ns/activitystreams",
159 "http://localhost:4001/schemas/litepub-0.1.jsonld",
160 %{"@language" => "und"}
161 ]
162
163 assert Map.take(response, [
164 "followers",
165 "following",
166 "id",
167 "inbox",
168 "manuallyApprovesFollowers",
169 "name",
170 "outbox",
171 "preferredUsername",
172 "summary",
173 "tag",
174 "type",
175 "url"
176 ]) == %{
177 "followers" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/followers",
178 "following" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/following",
179 "id" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}",
180 "inbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/inbox",
181 "manuallyApprovesFollowers" => false,
182 "name" => user.name,
183 "outbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/outbox",
184 "preferredUsername" => user.nickname,
185 "summary" => user.bio,
186 "tag" => [],
187 "type" => "Person",
188 "url" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}"
189 }
190 end
191
192 test "json format. it returns error whe use not found", %{conn: conn} do
193 response =
194 conn
195 |> put_req_header("accept", "application/json")
196 |> get("/users/jimm")
197 |> json_response(404)
198
199 assert response == "Not found"
200 end
201
202 test "html format. it redirects on actual feed of user", %{conn: conn} do
203 note_activity = insert(:note_activity)
204 user = User.get_cached_by_ap_id(note_activity.data["actor"])
205
206 response =
207 conn
208 |> get("/users/#{user.nickname}")
209 |> response(200)
210
211 assert response ==
212 Fallback.RedirectController.redirector_with_meta(
213 conn,
214 %{user: user}
215 ).resp_body
216 end
217
218 test "html format. it returns error when user not found", %{conn: conn} do
219 response =
220 conn
221 |> get("/users/jimm")
222 |> json_response(404)
223
224 assert response == %{"error" => "Not found"}
225 end
226 end
227 end