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 will not bomb when there is no activity", %{conn: conn} do
312 |> put_req_header("accept", "application/activity+json")
313 |> get("/users/#{user.nickname}/outbox")
315 result = json_response(conn, 200)
316 assert user.ap_id <> "/outbox" == result["id"]
319 test "it returns a note activity in a collection", %{conn: conn} do
320 note_activity = insert(:note_activity)
321 user = User.get_cached_by_ap_id(note_activity.data["actor"])
325 |> put_req_header("accept", "application/activity+json")
326 |> get("/users/#{user.nickname}/outbox")
328 assert response(conn, 200) =~ note_activity.data["object"]["content"]
331 test "it returns an announce activity in a collection", %{conn: conn} do
332 announce_activity = insert(:announce_activity)
333 user = User.get_cached_by_ap_id(announce_activity.data["actor"])
337 |> put_req_header("accept", "application/activity+json")
338 |> get("/users/#{user.nickname}/outbox")
340 assert response(conn, 200) =~ announce_activity.data["object"]
343 test "it rejects posts from other users", %{conn: conn} do
344 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
346 otheruser = insert(:user)
350 |> assign(:user, otheruser)
351 |> put_req_header("content-type", "application/activity+json")
352 |> post("/users/#{user.nickname}/outbox", data)
354 assert json_response(conn, 403)
357 test "it inserts an incoming create activity into the database", %{conn: conn} do
358 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
363 |> assign(:user, user)
364 |> put_req_header("content-type", "application/activity+json")
365 |> post("/users/#{user.nickname}/outbox", data)
367 result = json_response(conn, 201)
368 assert Activity.get_by_ap_id(result["id"])
371 test "it rejects an incoming activity with bogus type", %{conn: conn} do
372 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
377 |> Map.put("type", "BadType")
381 |> assign(:user, user)
382 |> put_req_header("content-type", "application/activity+json")
383 |> post("/users/#{user.nickname}/outbox", data)
385 assert json_response(conn, 400)
388 test "it erects a tombstone when receiving a delete activity", %{conn: conn} do
389 note_activity = insert(:note_activity)
390 user = User.get_cached_by_ap_id(note_activity.data["actor"])
395 id: note_activity.data["object"]["id"]
401 |> assign(:user, user)
402 |> put_req_header("content-type", "application/activity+json")
403 |> post("/users/#{user.nickname}/outbox", data)
405 result = json_response(conn, 201)
406 assert Activity.get_by_ap_id(result["id"])
408 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
410 assert object.data["type"] == "Tombstone"
413 test "it rejects delete activity of object from other actor", %{conn: conn} do
414 note_activity = insert(:note_activity)
420 id: note_activity.data["object"]["id"]
426 |> assign(:user, user)
427 |> put_req_header("content-type", "application/activity+json")
428 |> post("/users/#{user.nickname}/outbox", data)
430 assert json_response(conn, 400)
433 test "it increases like count when receiving a like action", %{conn: conn} do
434 note_activity = insert(:note_activity)
435 user = User.get_cached_by_ap_id(note_activity.data["actor"])
440 id: note_activity.data["object"]["id"]
446 |> assign(:user, user)
447 |> put_req_header("content-type", "application/activity+json")
448 |> post("/users/#{user.nickname}/outbox", data)
450 result = json_response(conn, 201)
451 assert Activity.get_by_ap_id(result["id"])
453 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
455 assert object.data["like_count"] == 1
459 describe "/users/:nickname/followers" do
460 test "it returns the followers in a collection", %{conn: conn} do
462 user_two = insert(:user)
463 User.follow(user, user_two)
467 |> get("/users/#{user_two.nickname}/followers")
468 |> json_response(200)
470 assert result["first"]["orderedItems"] == [user.ap_id]
473 test "it returns returns empty if the user has 'hide_followers' set", %{conn: conn} do
475 user_two = insert(:user, %{info: %{hide_followers: true}})
476 User.follow(user, user_two)
480 |> get("/users/#{user_two.nickname}/followers")
481 |> json_response(200)
483 assert result["first"]["orderedItems"] == []
484 assert result["totalItems"] == 0
487 test "it works for more than 10 users", %{conn: conn} do
490 Enum.each(1..15, fn _ ->
491 other_user = insert(:user)
492 User.follow(other_user, user)
497 |> get("/users/#{user.nickname}/followers")
498 |> json_response(200)
500 assert length(result["first"]["orderedItems"]) == 10
501 assert result["first"]["totalItems"] == 15
502 assert result["totalItems"] == 15
506 |> get("/users/#{user.nickname}/followers?page=2")
507 |> json_response(200)
509 assert length(result["orderedItems"]) == 5
510 assert result["totalItems"] == 15
514 describe "/users/:nickname/following" do
515 test "it returns the following in a collection", %{conn: conn} do
517 user_two = insert(:user)
518 User.follow(user, user_two)
522 |> get("/users/#{user.nickname}/following")
523 |> json_response(200)
525 assert result["first"]["orderedItems"] == [user_two.ap_id]
528 test "it returns returns empty if the user has 'hide_follows' set", %{conn: conn} do
529 user = insert(:user, %{info: %{hide_follows: true}})
530 user_two = insert(:user)
531 User.follow(user, user_two)
535 |> get("/users/#{user.nickname}/following")
536 |> json_response(200)
538 assert result["first"]["orderedItems"] == []
539 assert result["totalItems"] == 0
542 test "it works for more than 10 users", %{conn: conn} do
545 Enum.each(1..15, fn _ ->
546 user = Repo.get(User, user.id)
547 other_user = insert(:user)
548 User.follow(user, other_user)
553 |> get("/users/#{user.nickname}/following")
554 |> json_response(200)
556 assert length(result["first"]["orderedItems"]) == 10
557 assert result["first"]["totalItems"] == 15
558 assert result["totalItems"] == 15
562 |> get("/users/#{user.nickname}/following?page=2")
563 |> json_response(200)
565 assert length(result["orderedItems"]) == 5
566 assert result["totalItems"] == 15