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