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