0f973d5b6c2cf02269068583f769cdba7dff0016
[akkoma] / test / web / ostatus / ostatus_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.OStatus.OStatusControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Pleroma.Factory
9
10 alias Pleroma.Config
11 alias Pleroma.Object
12 alias Pleroma.User
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.Endpoint
16
17 require Pleroma.Constants
18
19 setup_all do
20 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 :ok
22 end
23
24 setup do: clear_config([:instance, :federating], true)
25
26 describe "Mastodon compatibility routes" do
27 setup %{conn: conn} do
28 conn = put_req_header(conn, "accept", "text/html")
29 %{conn: conn}
30 end
31
32 test "redirects to /notice/:id for html format", %{conn: conn} do
33 {:ok, object} =
34 %{
35 "type" => "Note",
36 "content" => "hey",
37 "id" => Endpoint.url() <> "/users/raymoo/statuses/999999999",
38 "actor" => Endpoint.url() <> "/users/raymoo",
39 "to" => [Pleroma.Constants.as_public()]
40 }
41 |> Object.create()
42
43 {:ok, activity, _} =
44 %{
45 "id" => object.data["id"] <> "/activity",
46 "type" => "Create",
47 "object" => object.data["id"],
48 "actor" => object.data["actor"],
49 "to" => object.data["to"]
50 }
51 |> ActivityPub.persist(local: true)
52
53 conn = get(conn, "/users/raymoo/statuses/999999999")
54 assert redirected_to(conn) == "/notice/#{activity.id}"
55 end
56 end
57
58 # Note: see ActivityPubControllerTest for JSON format tests
59 describe "GET /objects/:uuid (text/html)" do
60 setup %{conn: conn} do
61 conn = put_req_header(conn, "accept", "text/html")
62 %{conn: conn}
63 end
64
65 test "redirects to /notice/id for html format", %{conn: conn} do
66 note_activity = insert(:note_activity)
67 object = Object.normalize(note_activity)
68 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
69 url = "/objects/#{uuid}"
70
71 conn = get(conn, url)
72 assert redirected_to(conn) == "/notice/#{note_activity.id}"
73 end
74
75 test "404s on private objects", %{conn: conn} do
76 note_activity = insert(:direct_note_activity)
77 object = Object.normalize(note_activity)
78 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, object.data["id"]))
79
80 conn
81 |> get("/objects/#{uuid}")
82 |> response(404)
83 end
84
85 test "404s on non-existing objects", %{conn: conn} do
86 conn
87 |> get("/objects/123")
88 |> response(404)
89 end
90 end
91
92 # Note: see ActivityPubControllerTest for JSON format tests
93 describe "GET /activities/:uuid (text/html)" do
94 setup %{conn: conn} do
95 conn = put_req_header(conn, "accept", "text/html")
96 %{conn: conn}
97 end
98
99 test "redirects to /notice/id for html format", %{conn: conn} do
100 note_activity = insert(:note_activity)
101 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
102
103 conn = get(conn, "/activities/#{uuid}")
104 assert redirected_to(conn) == "/notice/#{note_activity.id}"
105 end
106
107 test "404s on private activities", %{conn: conn} do
108 note_activity = insert(:direct_note_activity)
109 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
110
111 conn
112 |> get("/activities/#{uuid}")
113 |> response(404)
114 end
115
116 test "404s on nonexistent activities", %{conn: conn} do
117 conn
118 |> get("/activities/123")
119 |> response(404)
120 end
121 end
122
123 describe "GET notice/2" do
124 test "redirects to a proper object URL when json requested and the object is local", %{
125 conn: conn
126 } do
127 note_activity = insert(:note_activity)
128 expected_redirect_url = Object.normalize(note_activity).data["id"]
129
130 redirect_url =
131 conn
132 |> put_req_header("accept", "application/activity+json")
133 |> get("/notice/#{note_activity.id}")
134 |> redirected_to()
135
136 assert redirect_url == expected_redirect_url
137 end
138
139 test "returns a 404 on remote notice when json requested", %{conn: conn} do
140 note_activity = insert(:note_activity, local: false)
141
142 conn
143 |> put_req_header("accept", "application/activity+json")
144 |> get("/notice/#{note_activity.id}")
145 |> response(404)
146 end
147
148 test "500s when actor not found", %{conn: conn} do
149 note_activity = insert(:note_activity)
150 user = User.get_cached_by_ap_id(note_activity.data["actor"])
151 User.invalidate_cache(user)
152 Pleroma.Repo.delete(user)
153
154 conn =
155 conn
156 |> get("/notice/#{note_activity.id}")
157
158 assert response(conn, 500) == ~S({"error":"Something went wrong"})
159 end
160
161 test "render html for redirect for html format", %{conn: conn} do
162 note_activity = insert(:note_activity)
163
164 resp =
165 conn
166 |> put_req_header("accept", "text/html")
167 |> get("/notice/#{note_activity.id}")
168 |> response(200)
169
170 assert resp =~
171 "<meta content=\"#{Pleroma.Web.base_url()}/notice/#{note_activity.id}\" property=\"og:url\">"
172
173 user = insert(:user)
174
175 {:ok, like_activity} = CommonAPI.favorite(user, note_activity.id)
176
177 assert like_activity.data["type"] == "Like"
178
179 resp =
180 conn
181 |> put_req_header("accept", "text/html")
182 |> get("/notice/#{like_activity.id}")
183 |> response(200)
184
185 assert resp =~ "<!--server-generated-meta-->"
186 end
187
188 test "404s a private notice", %{conn: conn} do
189 note_activity = insert(:direct_note_activity)
190 url = "/notice/#{note_activity.id}"
191
192 conn =
193 conn
194 |> get(url)
195
196 assert response(conn, 404)
197 end
198
199 test "404s a non-existing notice", %{conn: conn} do
200 url = "/notice/123"
201
202 conn =
203 conn
204 |> get(url)
205
206 assert response(conn, 404)
207 end
208
209 test "it requires authentication if instance is NOT federating", %{
210 conn: conn
211 } do
212 user = insert(:user)
213 note_activity = insert(:note_activity)
214
215 conn = put_req_header(conn, "accept", "text/html")
216
217 ensure_federating_or_authenticated(conn, "/notice/#{note_activity.id}", user)
218 end
219 end
220
221 describe "GET /notice/:id/embed_player" do
222 setup do
223 note_activity = insert(:note_activity)
224 object = Pleroma.Object.normalize(note_activity)
225
226 object_data =
227 Map.put(object.data, "attachment", [
228 %{
229 "url" => [
230 %{
231 "href" =>
232 "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4",
233 "mediaType" => "video/mp4",
234 "type" => "Link"
235 }
236 ]
237 }
238 ])
239
240 object
241 |> Ecto.Changeset.change(data: object_data)
242 |> Pleroma.Repo.update()
243
244 %{note_activity: note_activity}
245 end
246
247 test "renders embed player", %{conn: conn, note_activity: note_activity} do
248 conn = get(conn, "/notice/#{note_activity.id}/embed_player")
249
250 assert Plug.Conn.get_resp_header(conn, "x-frame-options") == ["ALLOW"]
251
252 assert Plug.Conn.get_resp_header(
253 conn,
254 "content-security-policy"
255 ) == [
256 "default-src 'none';style-src 'self' 'unsafe-inline';img-src 'self' data: https:; media-src 'self' https:;"
257 ]
258
259 assert response(conn, 200) =~
260 "<video controls loop><source src=\"https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4\" type=\"video/mp4\">Your browser does not support video/mp4 playback.</video>"
261 end
262
263 test "404s when activity isn't create", %{conn: conn} do
264 note_activity = insert(:note_activity, data_attrs: %{"type" => "Like"})
265
266 assert conn
267 |> get("/notice/#{note_activity.id}/embed_player")
268 |> response(404)
269 end
270
271 test "404s when activity is direct message", %{conn: conn} do
272 note_activity = insert(:note_activity, data_attrs: %{"directMessage" => true})
273
274 assert conn
275 |> get("/notice/#{note_activity.id}/embed_player")
276 |> response(404)
277 end
278
279 test "404s when attachment is empty", %{conn: conn} do
280 note_activity = insert(:note_activity)
281 object = Pleroma.Object.normalize(note_activity)
282 object_data = Map.put(object.data, "attachment", [])
283
284 object
285 |> Ecto.Changeset.change(data: object_data)
286 |> Pleroma.Repo.update()
287
288 assert conn
289 |> get("/notice/#{note_activity.id}/embed_player")
290 |> response(404)
291 end
292
293 test "404s when attachment isn't audio or video", %{conn: conn} do
294 note_activity = insert(:note_activity)
295 object = Pleroma.Object.normalize(note_activity)
296
297 object_data =
298 Map.put(object.data, "attachment", [
299 %{
300 "url" => [
301 %{
302 "href" => "https://peertube.moe/static/webseed/480.jpg",
303 "mediaType" => "image/jpg",
304 "type" => "Link"
305 }
306 ]
307 }
308 ])
309
310 object
311 |> Ecto.Changeset.change(data: object_data)
312 |> Pleroma.Repo.update()
313
314 conn
315 |> get("/notice/#{note_activity.id}/embed_player")
316 |> response(404)
317 end
318
319 test "it requires authentication if instance is NOT federating", %{
320 conn: conn,
321 note_activity: note_activity
322 } do
323 user = insert(:user)
324 conn = put_req_header(conn, "accept", "text/html")
325
326 ensure_federating_or_authenticated(conn, "/notice/#{note_activity.id}/embed_player", user)
327 end
328 end
329 end