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