1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
6 use Pleroma.Web.ConnCase
8 alias Pleroma.Web.ActivityPub.UserView
9 alias Pleroma.Web.ActivityPub.ObjectView
12 alias Pleroma.Activity
14 alias Pleroma.Instances
17 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
22 test "with the relay active, it returns the relay user", %{conn: conn} do
25 |> get(activity_pub_path(conn, :relay))
28 assert res["id"] =~ "/relay"
31 test "with the relay disabled, it returns 404", %{conn: conn} do
32 Pleroma.Config.put([:instance, :allow_relay], false)
35 |> get(activity_pub_path(conn, :relay))
39 Pleroma.Config.put([:instance, :allow_relay], true)
43 describe "/users/:nickname" do
44 test "it returns a json representation of the user with accept application/json", %{
51 |> put_req_header("accept", "application/json")
52 |> get("/users/#{user.nickname}")
54 user = Repo.get(User, user.id)
56 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
59 test "it returns a json representation of the user with accept application/activity+json", %{
66 |> put_req_header("accept", "application/activity+json")
67 |> get("/users/#{user.nickname}")
69 user = Repo.get(User, user.id)
71 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
74 test "it returns a json representation of the user with accept application/ld+json", %{
83 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
85 |> get("/users/#{user.nickname}")
87 user = Repo.get(User, user.id)
89 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
93 describe "/object/:uuid" do
94 test "it returns a json representation of the object with accept application/json", %{
98 uuid = String.split(note.data["id"], "/") |> List.last()
102 |> put_req_header("accept", "application/json")
103 |> get("/objects/#{uuid}")
105 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
108 test "it returns a json representation of the object with accept application/activity+json",
111 uuid = String.split(note.data["id"], "/") |> List.last()
115 |> put_req_header("accept", "application/activity+json")
116 |> get("/objects/#{uuid}")
118 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
121 test "it returns a json representation of the object with accept application/ld+json", %{
125 uuid = String.split(note.data["id"], "/") |> List.last()
131 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
133 |> get("/objects/#{uuid}")
135 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
138 test "it returns 404 for non-public messages", %{conn: conn} do
139 note = insert(:direct_note)
140 uuid = String.split(note.data["id"], "/") |> List.last()
144 |> put_req_header("accept", "application/activity+json")
145 |> get("/objects/#{uuid}")
147 assert json_response(conn, 404)
150 test "it returns 404 for tombstone objects", %{conn: conn} do
151 tombstone = insert(:tombstone)
152 uuid = String.split(tombstone.data["id"], "/") |> List.last()
156 |> put_req_header("accept", "application/activity+json")
157 |> get("/objects/#{uuid}")
159 assert json_response(conn, 404)
163 describe "/object/:uuid/likes" do
164 test "it returns the like activities in a collection", %{conn: conn} do
165 like = insert(:like_activity)
166 uuid = String.split(like.data["object"], "/") |> List.last()
170 |> put_req_header("accept", "application/activity+json")
171 |> get("/objects/#{uuid}/likes")
172 |> json_response(200)
174 assert List.first(result["first"]["orderedItems"])["id"] == like.data["id"]
178 describe "/activities/:uuid" do
179 test "it returns a json representation of the activity", %{conn: conn} do
180 activity = insert(:note_activity)
181 uuid = String.split(activity.data["id"], "/") |> List.last()
185 |> put_req_header("accept", "application/activity+json")
186 |> get("/activities/#{uuid}")
188 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: activity})
191 test "it returns 404 for non-public activities", %{conn: conn} do
192 activity = insert(:direct_note_activity)
193 uuid = String.split(activity.data["id"], "/") |> List.last()
197 |> put_req_header("accept", "application/activity+json")
198 |> get("/activities/#{uuid}")
200 assert json_response(conn, 404)
205 test "it inserts an incoming activity into the database", %{conn: conn} do
206 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
210 |> assign(:valid_signature, true)
211 |> put_req_header("content-type", "application/activity+json")
212 |> post("/inbox", data)
214 assert "ok" == json_response(conn, 200)
216 assert Activity.get_by_ap_id(data["id"])
219 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
220 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
222 sender_url = data["actor"]
223 Instances.set_consistently_unreachable(sender_url)
224 refute Instances.reachable?(sender_url)
228 |> assign(:valid_signature, true)
229 |> put_req_header("content-type", "application/activity+json")
230 |> post("/inbox", data)
232 assert "ok" == json_response(conn, 200)
233 assert Instances.reachable?(sender_url)
237 describe "/users/:nickname/inbox" do
238 test "it inserts an incoming activity into the database", %{conn: conn} do
242 File.read!("test/fixtures/mastodon-post-activity.json")
244 |> Map.put("bcc", [user.ap_id])
248 |> assign(:valid_signature, true)
249 |> put_req_header("content-type", "application/activity+json")
250 |> post("/users/#{user.nickname}/inbox", data)
252 assert "ok" == json_response(conn, 200)
254 assert Activity.get_by_ap_id(data["id"])
257 test "it rejects reads from other users", %{conn: conn} do
259 otheruser = insert(:user)
263 |> assign(:user, otheruser)
264 |> put_req_header("accept", "application/activity+json")
265 |> get("/users/#{user.nickname}/inbox")
267 assert json_response(conn, 403)
270 test "it returns a note activity in a collection", %{conn: conn} do
271 note_activity = insert(:direct_note_activity)
272 user = User.get_cached_by_ap_id(hd(note_activity.data["to"]))
276 |> assign(:user, user)
277 |> put_req_header("accept", "application/activity+json")
278 |> get("/users/#{user.nickname}/inbox")
280 assert response(conn, 200) =~ note_activity.data["object"]["content"]
283 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
287 File.read!("test/fixtures/mastodon-post-activity.json")
289 |> Map.put("bcc", [user.ap_id])
291 sender_host = URI.parse(data["actor"]).host
292 Instances.set_consistently_unreachable(sender_host)
293 refute Instances.reachable?(sender_host)
297 |> assign(:valid_signature, true)
298 |> put_req_header("content-type", "application/activity+json")
299 |> post("/users/#{user.nickname}/inbox", data)
301 assert "ok" == json_response(conn, 200)
302 assert Instances.reachable?(sender_host)
306 describe "/users/:nickname/outbox" do
307 test "it returns a note activity in a collection", %{conn: conn} do
308 note_activity = insert(:note_activity)
309 user = User.get_cached_by_ap_id(note_activity.data["actor"])
313 |> put_req_header("accept", "application/activity+json")
314 |> get("/users/#{user.nickname}/outbox")
316 assert response(conn, 200) =~ note_activity.data["object"]["content"]
319 test "it returns an announce activity in a collection", %{conn: conn} do
320 announce_activity = insert(:announce_activity)
321 user = User.get_cached_by_ap_id(announce_activity.data["actor"])
325 |> put_req_header("accept", "application/activity+json")
326 |> get("/users/#{user.nickname}/outbox")
328 assert response(conn, 200) =~ announce_activity.data["object"]
331 test "it rejects posts from other users", %{conn: conn} do
332 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
334 otheruser = insert(:user)
338 |> assign(:user, otheruser)
339 |> put_req_header("content-type", "application/activity+json")
340 |> post("/users/#{user.nickname}/outbox", data)
342 assert json_response(conn, 403)
345 test "it inserts an incoming create activity into the database", %{conn: conn} do
346 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
351 |> assign(:user, user)
352 |> put_req_header("content-type", "application/activity+json")
353 |> post("/users/#{user.nickname}/outbox", data)
355 result = json_response(conn, 201)
356 assert Activity.get_by_ap_id(result["id"])
359 test "it rejects an incoming activity with bogus type", %{conn: conn} do
360 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
365 |> Map.put("type", "BadType")
369 |> assign(:user, user)
370 |> put_req_header("content-type", "application/activity+json")
371 |> post("/users/#{user.nickname}/outbox", data)
373 assert json_response(conn, 400)
376 test "it erects a tombstone when receiving a delete activity", %{conn: conn} do
377 note_activity = insert(:note_activity)
378 user = User.get_cached_by_ap_id(note_activity.data["actor"])
383 id: note_activity.data["object"]["id"]
389 |> assign(:user, user)
390 |> put_req_header("content-type", "application/activity+json")
391 |> post("/users/#{user.nickname}/outbox", data)
393 result = json_response(conn, 201)
394 assert Activity.get_by_ap_id(result["id"])
396 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
398 assert object.data["type"] == "Tombstone"
401 test "it rejects delete activity of object from other actor", %{conn: conn} do
402 note_activity = insert(:note_activity)
408 id: note_activity.data["object"]["id"]
414 |> assign(:user, user)
415 |> put_req_header("content-type", "application/activity+json")
416 |> post("/users/#{user.nickname}/outbox", data)
418 assert json_response(conn, 400)
421 test "it increases like count when receiving a like action", %{conn: conn} do
422 note_activity = insert(:note_activity)
423 user = User.get_cached_by_ap_id(note_activity.data["actor"])
428 id: note_activity.data["object"]["id"]
434 |> assign(:user, user)
435 |> put_req_header("content-type", "application/activity+json")
436 |> post("/users/#{user.nickname}/outbox", data)
438 result = json_response(conn, 201)
439 assert Activity.get_by_ap_id(result["id"])
441 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
443 assert object.data["like_count"] == 1
447 describe "/users/:nickname/followers" do
448 test "it returns the followers in a collection", %{conn: conn} do
450 user_two = insert(:user)
451 User.follow(user, user_two)
455 |> get("/users/#{user_two.nickname}/followers")
456 |> json_response(200)
458 assert result["first"]["orderedItems"] == [user.ap_id]
461 test "it returns returns empty if the user has 'hide_followers' set", %{conn: conn} do
463 user_two = insert(:user, %{info: %{hide_followers: true}})
464 User.follow(user, user_two)
468 |> get("/users/#{user_two.nickname}/followers")
469 |> json_response(200)
471 assert result["first"]["orderedItems"] == []
472 assert result["totalItems"] == 0
475 test "it works for more than 10 users", %{conn: conn} do
478 Enum.each(1..15, fn _ ->
479 other_user = insert(:user)
480 User.follow(other_user, user)
485 |> get("/users/#{user.nickname}/followers")
486 |> json_response(200)
488 assert length(result["first"]["orderedItems"]) == 10
489 assert result["first"]["totalItems"] == 15
490 assert result["totalItems"] == 15
494 |> get("/users/#{user.nickname}/followers?page=2")
495 |> json_response(200)
497 assert length(result["orderedItems"]) == 5
498 assert result["totalItems"] == 15
502 describe "/users/:nickname/following" do
503 test "it returns the following in a collection", %{conn: conn} do
505 user_two = insert(:user)
506 User.follow(user, user_two)
510 |> get("/users/#{user.nickname}/following")
511 |> json_response(200)
513 assert result["first"]["orderedItems"] == [user_two.ap_id]
516 test "it returns returns empty if the user has 'hide_follows' set", %{conn: conn} do
517 user = insert(:user, %{info: %{hide_follows: true}})
518 user_two = insert(:user)
519 User.follow(user, user_two)
523 |> get("/users/#{user.nickname}/following")
524 |> json_response(200)
526 assert result["first"]["orderedItems"] == []
527 assert result["totalItems"] == 0
530 test "it works for more than 10 users", %{conn: conn} do
533 Enum.each(1..15, fn _ ->
534 user = Repo.get(User, user.id)
535 other_user = insert(:user)
536 User.follow(user, other_user)
541 |> get("/users/#{user.nickname}/following")
542 |> json_response(200)
544 assert length(result["first"]["orderedItems"]) == 10
545 assert result["first"]["totalItems"] == 15
546 assert result["totalItems"] == 15
550 |> get("/users/#{user.nickname}/following?page=2")
551 |> json_response(200)
553 assert length(result["orderedItems"]) == 5
554 assert result["totalItems"] == 15