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