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