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
9 alias Pleroma.Instances
12 alias Pleroma.Web.ActivityPub.ObjectView
13 alias Pleroma.Web.ActivityPub.UserView
16 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 test "with the relay active, it returns the relay user", %{conn: conn} do
24 |> get(activity_pub_path(conn, :relay))
27 assert res["id"] =~ "/relay"
30 test "with the relay disabled, it returns 404", %{conn: conn} do
31 Pleroma.Config.put([:instance, :allow_relay], false)
34 |> get(activity_pub_path(conn, :relay))
38 Pleroma.Config.put([:instance, :allow_relay], true)
42 describe "/users/:nickname" do
43 test "it returns a json representation of the user with accept application/json", %{
50 |> put_req_header("accept", "application/json")
51 |> get("/users/#{user.nickname}")
53 user = User.get_by_id(user.id)
55 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
58 test "it returns a json representation of the user with accept application/activity+json", %{
65 |> put_req_header("accept", "application/activity+json")
66 |> get("/users/#{user.nickname}")
68 user = User.get_by_id(user.id)
70 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
73 test "it returns a json representation of the user with accept application/ld+json", %{
82 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
84 |> get("/users/#{user.nickname}")
86 user = User.get_by_id(user.id)
88 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
92 describe "/object/:uuid" do
93 test "it returns a json representation of the object with accept application/json", %{
97 uuid = String.split(note.data["id"], "/") |> List.last()
101 |> put_req_header("accept", "application/json")
102 |> get("/objects/#{uuid}")
104 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
107 test "it returns a json representation of the object with accept application/activity+json",
110 uuid = String.split(note.data["id"], "/") |> List.last()
114 |> put_req_header("accept", "application/activity+json")
115 |> get("/objects/#{uuid}")
117 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
120 test "it returns a json representation of the object with accept application/ld+json", %{
124 uuid = String.split(note.data["id"], "/") |> List.last()
130 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
132 |> get("/objects/#{uuid}")
134 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
137 test "it returns 404 for non-public messages", %{conn: conn} do
138 note = insert(:direct_note)
139 uuid = String.split(note.data["id"], "/") |> List.last()
143 |> put_req_header("accept", "application/activity+json")
144 |> get("/objects/#{uuid}")
146 assert json_response(conn, 404)
149 test "it returns 404 for tombstone objects", %{conn: conn} do
150 tombstone = insert(:tombstone)
151 uuid = String.split(tombstone.data["id"], "/") |> List.last()
155 |> put_req_header("accept", "application/activity+json")
156 |> get("/objects/#{uuid}")
158 assert json_response(conn, 404)
162 describe "/object/:uuid/likes" do
163 test "it returns the like activities in a collection", %{conn: conn} do
164 like = insert(:like_activity)
165 uuid = String.split(like.data["object"], "/") |> List.last()
169 |> put_req_header("accept", "application/activity+json")
170 |> get("/objects/#{uuid}/likes")
171 |> json_response(200)
173 assert List.first(result["first"]["orderedItems"])["id"] == like.data["id"]
177 describe "/activities/:uuid" do
178 test "it returns a json representation of the activity", %{conn: conn} do
179 activity = insert(:note_activity)
180 uuid = String.split(activity.data["id"], "/") |> List.last()
184 |> put_req_header("accept", "application/activity+json")
185 |> get("/activities/#{uuid}")
187 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: activity})
190 test "it returns 404 for non-public activities", %{conn: conn} do
191 activity = insert(:direct_note_activity)
192 uuid = String.split(activity.data["id"], "/") |> List.last()
196 |> put_req_header("accept", "application/activity+json")
197 |> get("/activities/#{uuid}")
199 assert json_response(conn, 404)
204 test "it inserts an incoming activity into the database", %{conn: conn} do
205 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
209 |> assign(:valid_signature, true)
210 |> put_req_header("content-type", "application/activity+json")
211 |> post("/inbox", data)
213 assert "ok" == json_response(conn, 200)
215 assert Activity.get_by_ap_id(data["id"])
218 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
219 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
221 sender_url = data["actor"]
222 Instances.set_consistently_unreachable(sender_url)
223 refute Instances.reachable?(sender_url)
227 |> assign(:valid_signature, true)
228 |> put_req_header("content-type", "application/activity+json")
229 |> post("/inbox", data)
231 assert "ok" == json_response(conn, 200)
232 assert Instances.reachable?(sender_url)
236 describe "/users/:nickname/inbox" do
237 test "it inserts an incoming activity into the database", %{conn: conn} do
241 File.read!("test/fixtures/mastodon-post-activity.json")
243 |> Map.put("bcc", [user.ap_id])
247 |> assign(:valid_signature, true)
248 |> put_req_header("content-type", "application/activity+json")
249 |> post("/users/#{user.nickname}/inbox", data)
251 assert "ok" == json_response(conn, 200)
253 assert Activity.get_by_ap_id(data["id"])
256 test "it accepts messages from actors that are followed by the user", %{conn: conn} do
257 recipient = insert(:user)
258 actor = insert(:user, %{ap_id: "http://mastodon.example.org/users/actor"})
260 {:ok, recipient} = User.follow(recipient, actor)
263 File.read!("test/fixtures/mastodon-post-activity.json")
268 |> Map.put("attributedTo", actor.ap_id)
272 |> Map.put("actor", actor.ap_id)
273 |> Map.put("object", object)
277 |> assign(:valid_signature, true)
278 |> put_req_header("content-type", "application/activity+json")
279 |> post("/users/#{recipient.nickname}/inbox", data)
281 assert "ok" == json_response(conn, 200)
283 assert Activity.get_by_ap_id(data["id"])
286 test "it rejects reads from other users", %{conn: conn} do
288 otheruser = insert(:user)
292 |> assign(:user, otheruser)
293 |> put_req_header("accept", "application/activity+json")
294 |> get("/users/#{user.nickname}/inbox")
296 assert json_response(conn, 403)
299 test "it returns a note activity in a collection", %{conn: conn} do
300 note_activity = insert(:direct_note_activity)
301 user = User.get_cached_by_ap_id(hd(note_activity.data["to"]))
305 |> assign(:user, user)
306 |> put_req_header("accept", "application/activity+json")
307 |> get("/users/#{user.nickname}/inbox")
309 assert response(conn, 200) =~ note_activity.data["object"]["content"]
312 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
316 File.read!("test/fixtures/mastodon-post-activity.json")
318 |> Map.put("bcc", [user.ap_id])
320 sender_host = URI.parse(data["actor"]).host
321 Instances.set_consistently_unreachable(sender_host)
322 refute Instances.reachable?(sender_host)
326 |> assign(:valid_signature, true)
327 |> put_req_header("content-type", "application/activity+json")
328 |> post("/users/#{user.nickname}/inbox", data)
330 assert "ok" == json_response(conn, 200)
331 assert Instances.reachable?(sender_host)
335 describe "/users/:nickname/outbox" do
336 test "it will not bomb when there is no activity", %{conn: conn} do
341 |> put_req_header("accept", "application/activity+json")
342 |> get("/users/#{user.nickname}/outbox")
344 result = json_response(conn, 200)
345 assert user.ap_id <> "/outbox" == result["id"]
348 test "it returns a note activity in a collection", %{conn: conn} do
349 note_activity = insert(:note_activity)
350 user = User.get_cached_by_ap_id(note_activity.data["actor"])
354 |> put_req_header("accept", "application/activity+json")
355 |> get("/users/#{user.nickname}/outbox")
357 assert response(conn, 200) =~ note_activity.data["object"]["content"]
360 test "it returns an announce activity in a collection", %{conn: conn} do
361 announce_activity = insert(:announce_activity)
362 user = User.get_cached_by_ap_id(announce_activity.data["actor"])
366 |> put_req_header("accept", "application/activity+json")
367 |> get("/users/#{user.nickname}/outbox")
369 assert response(conn, 200) =~ announce_activity.data["object"]
372 test "it rejects posts from other users", %{conn: conn} do
373 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
375 otheruser = insert(:user)
379 |> assign(:user, otheruser)
380 |> put_req_header("content-type", "application/activity+json")
381 |> post("/users/#{user.nickname}/outbox", data)
383 assert json_response(conn, 403)
386 test "it inserts an incoming create activity into the database", %{conn: conn} do
387 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
392 |> assign(:user, user)
393 |> put_req_header("content-type", "application/activity+json")
394 |> post("/users/#{user.nickname}/outbox", data)
396 result = json_response(conn, 201)
397 assert Activity.get_by_ap_id(result["id"])
400 test "it rejects an incoming activity with bogus type", %{conn: conn} do
401 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
406 |> Map.put("type", "BadType")
410 |> assign(:user, user)
411 |> put_req_header("content-type", "application/activity+json")
412 |> post("/users/#{user.nickname}/outbox", data)
414 assert json_response(conn, 400)
417 test "it erects a tombstone when receiving a delete activity", %{conn: conn} do
418 note_activity = insert(:note_activity)
419 user = User.get_cached_by_ap_id(note_activity.data["actor"])
424 id: note_activity.data["object"]["id"]
430 |> assign(:user, user)
431 |> put_req_header("content-type", "application/activity+json")
432 |> post("/users/#{user.nickname}/outbox", data)
434 result = json_response(conn, 201)
435 assert Activity.get_by_ap_id(result["id"])
437 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
439 assert object.data["type"] == "Tombstone"
442 test "it rejects delete activity of object from other actor", %{conn: conn} do
443 note_activity = insert(:note_activity)
449 id: note_activity.data["object"]["id"]
455 |> assign(:user, user)
456 |> put_req_header("content-type", "application/activity+json")
457 |> post("/users/#{user.nickname}/outbox", data)
459 assert json_response(conn, 400)
462 test "it increases like count when receiving a like action", %{conn: conn} do
463 note_activity = insert(:note_activity)
464 user = User.get_cached_by_ap_id(note_activity.data["actor"])
469 id: note_activity.data["object"]["id"]
475 |> assign(:user, user)
476 |> put_req_header("content-type", "application/activity+json")
477 |> post("/users/#{user.nickname}/outbox", data)
479 result = json_response(conn, 201)
480 assert Activity.get_by_ap_id(result["id"])
482 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
484 assert object.data["like_count"] == 1
488 describe "/users/:nickname/followers" do
489 test "it returns the followers in a collection", %{conn: conn} do
491 user_two = insert(:user)
492 User.follow(user, user_two)
496 |> get("/users/#{user_two.nickname}/followers")
497 |> json_response(200)
499 assert result["first"]["orderedItems"] == [user.ap_id]
502 test "it returns returns empty if the user has 'hide_followers' set", %{conn: conn} do
504 user_two = insert(:user, %{info: %{hide_followers: true}})
505 User.follow(user, user_two)
509 |> get("/users/#{user_two.nickname}/followers")
510 |> json_response(200)
512 assert result["first"]["orderedItems"] == []
513 assert result["totalItems"] == 0
516 test "it works for more than 10 users", %{conn: conn} do
519 Enum.each(1..15, fn _ ->
520 other_user = insert(:user)
521 User.follow(other_user, user)
526 |> get("/users/#{user.nickname}/followers")
527 |> json_response(200)
529 assert length(result["first"]["orderedItems"]) == 10
530 assert result["first"]["totalItems"] == 15
531 assert result["totalItems"] == 15
535 |> get("/users/#{user.nickname}/followers?page=2")
536 |> json_response(200)
538 assert length(result["orderedItems"]) == 5
539 assert result["totalItems"] == 15
543 describe "/users/:nickname/following" do
544 test "it returns the following in a collection", %{conn: conn} do
546 user_two = insert(:user)
547 User.follow(user, user_two)
551 |> get("/users/#{user.nickname}/following")
552 |> json_response(200)
554 assert result["first"]["orderedItems"] == [user_two.ap_id]
557 test "it returns returns empty if the user has 'hide_follows' set", %{conn: conn} do
558 user = insert(:user, %{info: %{hide_follows: true}})
559 user_two = insert(:user)
560 User.follow(user, user_two)
564 |> get("/users/#{user.nickname}/following")
565 |> json_response(200)
567 assert result["first"]["orderedItems"] == []
568 assert result["totalItems"] == 0
571 test "it works for more than 10 users", %{conn: conn} do
574 Enum.each(1..15, fn _ ->
575 user = User.get_by_id(user.id)
576 other_user = insert(:user)
577 User.follow(user, other_user)
582 |> get("/users/#{user.nickname}/following")
583 |> json_response(200)
585 assert length(result["first"]["orderedItems"]) == 10
586 assert result["first"]["totalItems"] == 15
587 assert result["totalItems"] == 15
591 |> get("/users/#{user.nickname}/following?page=2")
592 |> json_response(200)
594 assert length(result["orderedItems"]) == 5
595 assert result["totalItems"] == 15