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