2018-12-27 11:24:04 +01:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/web/web_finger/web_finger_controller_test.exs
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
defmodule MobilizonWeb.ActivityPubControllerTest do
|
|
|
|
use MobilizonWeb.ConnCase
|
|
|
|
import Mobilizon.Factory
|
2019-05-22 14:12:11 +02:00
|
|
|
alias MobilizonWeb.ActivityPub.ActorView
|
2019-05-02 13:04:21 +02:00
|
|
|
alias MobilizonWeb.PageView
|
2018-11-12 18:17:53 +01:00
|
|
|
alias Mobilizon.Actors
|
2018-11-27 17:54:54 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
2018-11-13 12:23:37 +01:00
|
|
|
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
2019-04-25 19:05:05 +02:00
|
|
|
alias MobilizonWeb.Router.Helpers, as: Routes
|
|
|
|
alias MobilizonWeb.Endpoint
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2019-03-05 10:13:19 +01:00
|
|
|
setup do
|
|
|
|
conn = build_conn() |> put_req_header("accept", "application/activity+json")
|
|
|
|
{:ok, conn: conn}
|
|
|
|
end
|
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
describe "/@:preferred_username" do
|
2018-06-14 17:25:55 +02:00
|
|
|
test "it returns a json representation of the actor", %{conn: conn} do
|
|
|
|
actor = insert(:actor)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
2019-04-25 19:05:05 +02:00
|
|
|
|> get(Actor.build_url(actor.preferred_username, :page))
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
actor = Actors.get_actor!(actor.id)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
assert json_response(conn, 200) == ActorView.render("actor.json", %{actor: actor})
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
describe "/events/:uuid" do
|
|
|
|
test "it returns a json representation of the event", %{conn: conn} do
|
2018-05-17 11:32:23 +02:00
|
|
|
event = insert(:event)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
2019-04-25 19:05:05 +02:00
|
|
|
|> get(Routes.page_url(Endpoint, :event, event.uuid))
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
assert json_response(conn, 200) ==
|
2019-05-02 13:04:21 +02:00
|
|
|
PageView.render("event.activity-json", %{conn: %{assigns: %{object: event}}})
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
2018-08-24 11:34:00 +02:00
|
|
|
|
|
|
|
test "it returns 404 for non-public events", %{conn: conn} do
|
2019-01-14 16:21:13 +01:00
|
|
|
event = insert(:event, visibility: :private)
|
2018-08-24 11:34:00 +02:00
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
2019-05-02 13:04:21 +02:00
|
|
|
|> put_req_header("accept", "application/activity+json")
|
2019-04-25 19:05:05 +02:00
|
|
|
|> get(Routes.page_url(Endpoint, :event, event.uuid))
|
2018-08-24 11:34:00 +02:00
|
|
|
|
|
|
|
assert json_response(conn, 404)
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2018-11-27 17:54:54 +01:00
|
|
|
describe "/comments/:uuid" do
|
|
|
|
test "it returns a json representation of the comment", %{conn: conn} do
|
|
|
|
comment = insert(:comment)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
2019-05-02 13:04:21 +02:00
|
|
|
|> put_req_header("accept", "application/activity+json")
|
2019-04-25 19:05:05 +02:00
|
|
|
|> get(Routes.page_url(Endpoint, :comment, comment.uuid))
|
2018-11-27 17:54:54 +01:00
|
|
|
|
|
|
|
assert json_response(conn, 200) ==
|
2019-05-02 13:04:21 +02:00
|
|
|
PageView.render("comment.activity-json", %{conn: %{assigns: %{object: comment}}})
|
2018-11-27 17:54:54 +01:00
|
|
|
end
|
|
|
|
|
2019-04-26 09:56:25 +02:00
|
|
|
test "it returns 404 for non-public comments", %{conn: conn} do
|
|
|
|
comment = insert(:comment, visibility: :private)
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2019-04-26 09:56:25 +02:00
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> put_req_header("accept", "application/activity+json")
|
|
|
|
|> get(Routes.page_url(Endpoint, :comment, comment.uuid))
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2019-04-26 09:56:25 +02:00
|
|
|
assert json_response(conn, 404)
|
|
|
|
end
|
2018-11-27 17:54:54 +01:00
|
|
|
end
|
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
describe "/@:preferred_username/inbox" do
|
|
|
|
test "it inserts an incoming event into the database", %{conn: conn} do
|
2018-11-13 12:23:37 +01:00
|
|
|
use_cassette "activity_pub_controller/mastodon-post-activity_actor_call" do
|
2019-02-14 14:19:55 +01:00
|
|
|
data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2018-11-13 12:23:37 +01:00
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> assign(:valid_signature, true)
|
2019-04-25 19:05:05 +02:00
|
|
|
|> post("#{MobilizonWeb.Endpoint.url()}/inbox", data)
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2018-11-13 12:23:37 +01:00
|
|
|
assert "ok" == json_response(conn, 200)
|
|
|
|
:timer.sleep(500)
|
|
|
|
assert ActivityPub.fetch_object_from_url(data["object"]["id"])
|
|
|
|
end
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "/@:preferred_username/outbox" do
|
|
|
|
test "it returns a note activity in a collection", %{conn: conn} do
|
2019-04-25 19:05:05 +02:00
|
|
|
actor = insert(:actor, visibility: :public)
|
2018-08-24 11:34:00 +02:00
|
|
|
comment = insert(:comment, actor: actor)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
2019-04-25 19:05:05 +02:00
|
|
|
|> get(Actor.build_url(actor.preferred_username, :outbox))
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
assert json_response(conn, 200)["totalItems"] == 1
|
|
|
|
assert json_response(conn, 200)["first"]["orderedItems"] == [comment.url]
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it returns an event activity in a collection", %{conn: conn} do
|
2019-04-25 19:05:05 +02:00
|
|
|
actor = insert(:actor, visibility: :public)
|
2018-08-24 11:34:00 +02:00
|
|
|
event = insert(:event, organizer_actor: actor)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
2019-04-25 19:05:05 +02:00
|
|
|
|> get(Actor.build_url(actor.preferred_username, :outbox))
|
|
|
|
|
|
|
|
assert json_response(conn, 200)["totalItems"] == 1
|
|
|
|
assert json_response(conn, 200)["first"]["orderedItems"] == [event.url]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it works for more than 10 events", %{conn: conn} do
|
|
|
|
actor = insert(:actor, visibility: :public)
|
|
|
|
|
|
|
|
Enum.each(1..15, fn _ ->
|
|
|
|
insert(:event, organizer_actor: actor)
|
|
|
|
end)
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> get(Actor.build_url(actor.preferred_username, :outbox))
|
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert length(result["first"]["orderedItems"]) == 10
|
|
|
|
assert result["totalItems"] == 15
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> get(Actor.build_url(actor.preferred_username, :outbox, page: 2))
|
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert length(result["orderedItems"]) == 5
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it returns an empty collection if the actor has private visibility", %{conn: conn} do
|
|
|
|
actor = insert(:actor, visibility: :private)
|
|
|
|
insert(:event, organizer_actor: actor)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> get(Actor.build_url(actor.preferred_username, :outbox))
|
|
|
|
|
|
|
|
assert json_response(conn, 200)["totalItems"] == 0
|
|
|
|
assert json_response(conn, 200)["first"]["orderedItems"] == []
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it doesn't returns an event activity in a collection if actor has private visibility",
|
|
|
|
%{conn: conn} do
|
|
|
|
actor = insert(:actor, visibility: :private)
|
|
|
|
insert(:event, organizer_actor: actor)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> get(Actor.build_url(actor.preferred_username, :outbox))
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
assert json_response(conn, 200)["totalItems"] == 0
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-11-27 17:54:54 +01:00
|
|
|
describe "/@actor/followers" do
|
|
|
|
test "it returns the followers in a collection", %{conn: conn} do
|
2019-04-25 19:05:05 +02:00
|
|
|
actor = insert(:actor, visibility: :public)
|
2018-11-27 17:54:54 +01:00
|
|
|
actor2 = insert(:actor)
|
|
|
|
Actor.follow(actor, actor2)
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
2019-04-25 19:05:05 +02:00
|
|
|
|> get(Actor.build_url(actor.preferred_username, :followers))
|
2018-11-27 17:54:54 +01:00
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert result["first"]["orderedItems"] == [actor2.url]
|
|
|
|
end
|
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
test "it returns no followers for a private actor", %{conn: conn} do
|
|
|
|
actor = insert(:actor, visibility: :private)
|
|
|
|
actor2 = insert(:actor)
|
|
|
|
Actor.follow(actor, actor2)
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> get(Actor.build_url(actor.preferred_username, :followers))
|
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert result["first"]["orderedItems"] == []
|
|
|
|
end
|
|
|
|
|
2018-11-27 17:54:54 +01:00
|
|
|
test "it works for more than 10 actors", %{conn: conn} do
|
2019-04-25 19:05:05 +02:00
|
|
|
actor = insert(:actor, visibility: :public)
|
2018-11-27 17:54:54 +01:00
|
|
|
|
|
|
|
Enum.each(1..15, fn _ ->
|
|
|
|
other_actor = insert(:actor)
|
|
|
|
Actor.follow(actor, other_actor)
|
|
|
|
end)
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
2019-04-25 19:05:05 +02:00
|
|
|
|> get(Actor.build_url(actor.preferred_username, :followers))
|
2018-11-27 17:54:54 +01:00
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert length(result["first"]["orderedItems"]) == 10
|
2019-04-25 19:05:05 +02:00
|
|
|
assert result["totalItems"] == 15
|
2018-11-27 17:54:54 +01:00
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
2019-04-25 19:05:05 +02:00
|
|
|
|> get(Actor.build_url(actor.preferred_username, :followers, page: 2))
|
2018-11-27 17:54:54 +01:00
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert length(result["orderedItems"]) == 5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "/@actor/following" do
|
|
|
|
test "it returns the followings in a collection", %{conn: conn} do
|
|
|
|
actor = insert(:actor)
|
2019-04-25 19:05:05 +02:00
|
|
|
actor2 = insert(:actor, visibility: :public)
|
2018-11-27 17:54:54 +01:00
|
|
|
Actor.follow(actor, actor2)
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
2019-04-25 19:05:05 +02:00
|
|
|
|> get(Actor.build_url(actor2.preferred_username, :following))
|
2018-11-27 17:54:54 +01:00
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert result["first"]["orderedItems"] == [actor.url]
|
|
|
|
end
|
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
test "it returns no followings for a private actor", %{conn: conn} do
|
2018-11-27 17:54:54 +01:00
|
|
|
actor = insert(:actor)
|
2019-04-25 19:05:05 +02:00
|
|
|
actor2 = insert(:actor, visibility: :private)
|
|
|
|
Actor.follow(actor, actor2)
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> get(Actor.build_url(actor2.preferred_username, :following))
|
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert result["first"]["orderedItems"] == []
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it works for more than 10 actors", %{conn: conn} do
|
|
|
|
actor = insert(:actor, visibility: :public)
|
2018-11-27 17:54:54 +01:00
|
|
|
|
|
|
|
Enum.each(1..15, fn _ ->
|
|
|
|
other_actor = insert(:actor)
|
|
|
|
Actor.follow(other_actor, actor)
|
|
|
|
end)
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
2019-04-25 19:05:05 +02:00
|
|
|
|> get(Actor.build_url(actor.preferred_username, :following))
|
2018-11-27 17:54:54 +01:00
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert length(result["first"]["orderedItems"]) == 10
|
|
|
|
# assert result["first"]["totalItems"] == 15
|
|
|
|
# assert result["totalItems"] == 15
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
2019-04-25 19:05:05 +02:00
|
|
|
|> get(Actor.build_url(actor.preferred_username, :following, page: 2))
|
2018-11-27 17:54:54 +01:00
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert length(result["orderedItems"]) == 5
|
|
|
|
# assert result["totalItems"] == 15
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
#
|
2018-08-24 11:34:00 +02:00
|
|
|
# describe "/@:preferred_username/following" do
|
2018-07-27 10:45:35 +02:00
|
|
|
# test "it returns the following in a collection", %{conn: conn} do
|
2018-08-24 11:34:00 +02:00
|
|
|
# actor = insert(:actor)
|
|
|
|
# actor2 = insert(:actor)
|
2018-10-11 17:37:39 +02:00
|
|
|
# Mobilizon.Service.ActivityPub.follow(actor, actor2)
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
# result =
|
|
|
|
# conn
|
2018-08-24 11:34:00 +02:00
|
|
|
# |> get("/@#{actor.preferred_username}/following")
|
2018-07-27 10:45:35 +02:00
|
|
|
# |> json_response(200)
|
2018-08-24 11:34:00 +02:00
|
|
|
|
|
|
|
# assert result["first"]["orderedItems"] == [actor2.url]
|
2018-07-27 10:45:35 +02:00
|
|
|
# end
|
2018-08-24 11:34:00 +02:00
|
|
|
|
|
|
|
# test "it works for more than 10 actors", %{conn: conn} do
|
|
|
|
# actor = insert(:actor)
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
# Enum.each(1..15, fn _ ->
|
2018-08-24 11:34:00 +02:00
|
|
|
# actor = Repo.get(Actor, actor.id)
|
|
|
|
# other_actor = insert(:actor)
|
|
|
|
# Actor.follow(actor, other_actor)
|
2018-07-27 10:45:35 +02:00
|
|
|
# end)
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
# result =
|
|
|
|
# conn
|
2018-08-24 11:34:00 +02:00
|
|
|
# |> get("/@#{actor.preferred_username}/following")
|
2018-07-27 10:45:35 +02:00
|
|
|
# |> json_response(200)
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
# assert length(result["first"]["orderedItems"]) == 10
|
|
|
|
# assert result["first"]["totalItems"] == 15
|
|
|
|
# assert result["totalItems"] == 15
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
# result =
|
|
|
|
# conn
|
2018-08-24 11:34:00 +02:00
|
|
|
# |> get("/@#{actor.preferred_username}/following?page=2")
|
2018-07-27 10:45:35 +02:00
|
|
|
# |> json_response(200)
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
# assert length(result["orderedItems"]) == 5
|
|
|
|
# assert result["totalItems"] == 15
|
|
|
|
# end
|
|
|
|
# end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|