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", %{conn: conn} do
49 |> put_req_header("accept", "application/json")
50 |> get("/users/#{user.nickname}")
52 user = Repo.get(User, user.id)
54 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
57 test "it returns a json representation of the user with accept application/activity+json", %{conn: conn} do
62 |> put_req_header("accept", "application/activity+json")
63 |> get("/users/#{user.nickname}")
65 user = Repo.get(User, user.id)
67 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
70 test "it returns a json representation of the user with accept application/ld+json", %{conn: conn} do
75 |> put_req_header("accept", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")
76 |> get("/users/#{user.nickname}")
78 user = Repo.get(User, user.id)
80 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
84 describe "/object/:uuid" do
85 test "it returns a json representation of the object with accept application/json", %{conn: conn} do
87 uuid = String.split(note.data["id"], "/") |> List.last()
91 |> put_req_header("accept", "application/json")
92 |> get("/objects/#{uuid}")
94 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
97 test "it returns a json representation of the object with accept application/activity+json", %{conn: conn} do
99 uuid = String.split(note.data["id"], "/") |> List.last()
103 |> put_req_header("accept", "application/activity+json")
104 |> get("/objects/#{uuid}")
106 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
109 test "it returns a json representation of the object with accept application/ld+json", %{conn: conn} do
111 uuid = String.split(note.data["id"], "/") |> List.last()
115 |> put_req_header("accept", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")
116 |> get("/objects/#{uuid}")
118 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
121 test "it returns 404 for non-public messages", %{conn: conn} do
122 note = insert(:direct_note)
123 uuid = String.split(note.data["id"], "/") |> List.last()
127 |> put_req_header("accept", "application/activity+json")
128 |> get("/objects/#{uuid}")
130 assert json_response(conn, 404)
133 test "it returns 404 for tombstone objects", %{conn: conn} do
134 tombstone = insert(:tombstone)
135 uuid = String.split(tombstone.data["id"], "/") |> List.last()
139 |> put_req_header("accept", "application/activity+json")
140 |> get("/objects/#{uuid}")
142 assert json_response(conn, 404)
146 describe "/object/:uuid/likes" do
147 test "it returns the like activities in a collection", %{conn: conn} do
148 like = insert(:like_activity)
149 uuid = String.split(like.data["object"], "/") |> List.last()
153 |> put_req_header("accept", "application/activity+json")
154 |> get("/objects/#{uuid}/likes")
155 |> json_response(200)
157 assert List.first(result["first"]["orderedItems"])["id"] == like.data["id"]
161 describe "/activities/:uuid" do
162 test "it returns a json representation of the activity", %{conn: conn} do
163 activity = insert(:note_activity)
164 uuid = String.split(activity.data["id"], "/") |> List.last()
168 |> put_req_header("accept", "application/activity+json")
169 |> get("/activities/#{uuid}")
171 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: activity})
174 test "it returns 404 for non-public activities", %{conn: conn} do
175 activity = insert(:direct_note_activity)
176 uuid = String.split(activity.data["id"], "/") |> List.last()
180 |> put_req_header("accept", "application/activity+json")
181 |> get("/activities/#{uuid}")
183 assert json_response(conn, 404)
188 test "it inserts an incoming activity into the database", %{conn: conn} do
189 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
193 |> assign(:valid_signature, true)
194 |> put_req_header("content-type", "application/activity+json")
195 |> post("/inbox", data)
197 assert "ok" == json_response(conn, 200)
199 assert Activity.get_by_ap_id(data["id"])
202 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
203 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
205 sender_url = data["actor"]
206 Instances.set_consistently_unreachable(sender_url)
207 refute Instances.reachable?(sender_url)
211 |> assign(:valid_signature, true)
212 |> put_req_header("content-type", "application/activity+json")
213 |> post("/inbox", data)
215 assert "ok" == json_response(conn, 200)
216 assert Instances.reachable?(sender_url)
220 describe "/users/:nickname/inbox" do
221 test "it inserts an incoming activity into the database", %{conn: conn} do
225 File.read!("test/fixtures/mastodon-post-activity.json")
227 |> Map.put("bcc", [user.ap_id])
231 |> assign(:valid_signature, true)
232 |> put_req_header("content-type", "application/activity+json")
233 |> post("/users/#{user.nickname}/inbox", data)
235 assert "ok" == json_response(conn, 200)
237 assert Activity.get_by_ap_id(data["id"])
240 test "it rejects reads from other users", %{conn: conn} do
242 otheruser = insert(:user)
246 |> assign(:user, otheruser)
247 |> put_req_header("accept", "application/activity+json")
248 |> get("/users/#{user.nickname}/inbox")
250 assert json_response(conn, 403)
253 test "it returns a note activity in a collection", %{conn: conn} do
254 note_activity = insert(:direct_note_activity)
255 user = User.get_cached_by_ap_id(hd(note_activity.data["to"]))
259 |> assign(:user, user)
260 |> put_req_header("accept", "application/activity+json")
261 |> get("/users/#{user.nickname}/inbox")
263 assert response(conn, 200) =~ note_activity.data["object"]["content"]
266 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
270 File.read!("test/fixtures/mastodon-post-activity.json")
272 |> Map.put("bcc", [user.ap_id])
274 sender_host = URI.parse(data["actor"]).host
275 Instances.set_consistently_unreachable(sender_host)
276 refute Instances.reachable?(sender_host)
280 |> assign(:valid_signature, true)
281 |> put_req_header("content-type", "application/activity+json")
282 |> post("/users/#{user.nickname}/inbox", data)
284 assert "ok" == json_response(conn, 200)
285 assert Instances.reachable?(sender_host)
289 describe "/users/:nickname/outbox" do
290 test "it returns a note activity in a collection", %{conn: conn} do
291 note_activity = insert(:note_activity)
292 user = User.get_cached_by_ap_id(note_activity.data["actor"])
296 |> put_req_header("accept", "application/activity+json")
297 |> get("/users/#{user.nickname}/outbox")
299 assert response(conn, 200) =~ note_activity.data["object"]["content"]
302 test "it returns an announce activity in a collection", %{conn: conn} do
303 announce_activity = insert(:announce_activity)
304 user = User.get_cached_by_ap_id(announce_activity.data["actor"])
308 |> put_req_header("accept", "application/activity+json")
309 |> get("/users/#{user.nickname}/outbox")
311 assert response(conn, 200) =~ announce_activity.data["object"]
314 test "it rejects posts from other users", %{conn: conn} do
315 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
317 otheruser = insert(:user)
321 |> assign(:user, otheruser)
322 |> put_req_header("content-type", "application/activity+json")
323 |> post("/users/#{user.nickname}/outbox", data)
325 assert json_response(conn, 403)
328 test "it inserts an incoming create activity into the database", %{conn: conn} do
329 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
334 |> assign(:user, user)
335 |> put_req_header("content-type", "application/activity+json")
336 |> post("/users/#{user.nickname}/outbox", data)
338 result = json_response(conn, 201)
339 assert Activity.get_by_ap_id(result["id"])
342 test "it rejects an incoming activity with bogus type", %{conn: conn} do
343 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
348 |> Map.put("type", "BadType")
352 |> assign(:user, user)
353 |> put_req_header("content-type", "application/activity+json")
354 |> post("/users/#{user.nickname}/outbox", data)
356 assert json_response(conn, 400)
359 test "it erects a tombstone when receiving a delete activity", %{conn: conn} do
360 note_activity = insert(:note_activity)
361 user = User.get_cached_by_ap_id(note_activity.data["actor"])
366 id: note_activity.data["object"]["id"]
372 |> assign(:user, user)
373 |> put_req_header("content-type", "application/activity+json")
374 |> post("/users/#{user.nickname}/outbox", data)
376 result = json_response(conn, 201)
377 assert Activity.get_by_ap_id(result["id"])
379 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
381 assert object.data["type"] == "Tombstone"
384 test "it rejects delete activity of object from other actor", %{conn: conn} do
385 note_activity = insert(:note_activity)
391 id: note_activity.data["object"]["id"]
397 |> assign(:user, user)
398 |> put_req_header("content-type", "application/activity+json")
399 |> post("/users/#{user.nickname}/outbox", data)
401 assert json_response(conn, 400)
404 test "it increases like count when receiving a like action", %{conn: conn} do
405 note_activity = insert(:note_activity)
406 user = User.get_cached_by_ap_id(note_activity.data["actor"])
411 id: note_activity.data["object"]["id"]
417 |> assign(:user, user)
418 |> put_req_header("content-type", "application/activity+json")
419 |> post("/users/#{user.nickname}/outbox", data)
421 result = json_response(conn, 201)
422 assert Activity.get_by_ap_id(result["id"])
424 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
426 assert object.data["like_count"] == 1
430 describe "/users/:nickname/followers" do
431 test "it returns the followers in a collection", %{conn: conn} do
433 user_two = insert(:user)
434 User.follow(user, user_two)
438 |> get("/users/#{user_two.nickname}/followers")
439 |> json_response(200)
441 assert result["first"]["orderedItems"] == [user.ap_id]
444 test "it returns returns empty if the user has 'hide_followers' set", %{conn: conn} do
446 user_two = insert(:user, %{info: %{hide_followers: true}})
447 User.follow(user, user_two)
451 |> get("/users/#{user_two.nickname}/followers")
452 |> json_response(200)
454 assert result["first"]["orderedItems"] == []
455 assert result["totalItems"] == 0
458 test "it works for more than 10 users", %{conn: conn} do
461 Enum.each(1..15, fn _ ->
462 other_user = insert(:user)
463 User.follow(other_user, user)
468 |> get("/users/#{user.nickname}/followers")
469 |> json_response(200)
471 assert length(result["first"]["orderedItems"]) == 10
472 assert result["first"]["totalItems"] == 15
473 assert result["totalItems"] == 15
477 |> get("/users/#{user.nickname}/followers?page=2")
478 |> json_response(200)
480 assert length(result["orderedItems"]) == 5
481 assert result["totalItems"] == 15
485 describe "/users/:nickname/following" do
486 test "it returns the following in a collection", %{conn: conn} do
488 user_two = insert(:user)
489 User.follow(user, user_two)
493 |> get("/users/#{user.nickname}/following")
494 |> json_response(200)
496 assert result["first"]["orderedItems"] == [user_two.ap_id]
499 test "it returns returns empty if the user has 'hide_follows' set", %{conn: conn} do
500 user = insert(:user, %{info: %{hide_follows: true}})
501 user_two = insert(:user)
502 User.follow(user, user_two)
506 |> get("/users/#{user.nickname}/following")
507 |> json_response(200)
509 assert result["first"]["orderedItems"] == []
510 assert result["totalItems"] == 0
513 test "it works for more than 10 users", %{conn: conn} do
516 Enum.each(1..15, fn _ ->
517 user = Repo.get(User, user.id)
518 other_user = insert(:user)
519 User.follow(user, other_user)
524 |> get("/users/#{user.nickname}/following")
525 |> json_response(200)
527 assert length(result["first"]["orderedItems"]) == 10
528 assert result["first"]["totalItems"] == 15
529 assert result["totalItems"] == 15
533 |> get("/users/#{user.nickname}/following?page=2")
534 |> json_response(200)
536 assert length(result["orderedItems"]) == 5
537 assert result["totalItems"] == 15