akkoma/test/web/activity_pub/activity_pub_controller_test.exs

149 lines
4 KiB
Elixir
Raw Normal View History

2017-12-11 17:22:48 +00:00
defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
use Pleroma.Web.ConnCase
import Pleroma.Factory
alias Pleroma.Web.ActivityPub.{UserView, ObjectView}
alias Pleroma.{Repo, User}
2018-02-25 15:40:37 +00:00
alias Pleroma.Activity
2018-05-30 18:00:27 +00:00
alias Pleroma.Web.CommonAPI
2017-12-11 17:22:48 +00:00
describe "/users/:nickname" do
test "it returns a json representation of the user", %{conn: conn} do
user = insert(:user)
2018-03-30 13:01:53 +00:00
conn =
conn
|> put_req_header("accept", "application/activity+json")
|> get("/users/#{user.nickname}")
2017-12-11 17:22:48 +00:00
user = Repo.get(User, user.id)
assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
end
end
describe "/object/:uuid" do
test "it returns a json representation of the object", %{conn: conn} do
note = insert(:note)
2018-03-30 13:01:53 +00:00
uuid = String.split(note.data["id"], "/") |> List.last()
2017-12-11 17:22:48 +00:00
2018-03-30 13:01:53 +00:00
conn =
conn
|> put_req_header("accept", "application/activity+json")
|> get("/objects/#{uuid}")
2017-12-11 17:22:48 +00:00
assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
end
2018-05-30 18:00:27 +00:00
test "it returns 404 for non-public messages", %{conn: conn} do
note = insert(:direct_note)
uuid = String.split(note.data["id"], "/") |> List.last()
conn =
conn
|> put_req_header("accept", "application/activity+json")
|> get("/objects/#{uuid}")
assert json_response(conn, 404)
end
2017-12-11 17:22:48 +00:00
end
describe "/users/:nickname/inbox" do
test "it inserts an incoming activity into the database", %{conn: conn} do
2018-03-30 13:01:53 +00:00
data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
2018-03-30 13:01:53 +00:00
conn =
conn
|> assign(:valid_signature, true)
|> put_req_header("content-type", "application/activity+json")
|> post("/inbox", data)
assert "ok" == json_response(conn, 200)
2018-02-25 15:40:37 +00:00
:timer.sleep(500)
assert Activity.get_by_ap_id(data["id"])
2017-12-11 17:22:48 +00:00
end
end
describe "/users/:nickname/followers" do
test "it returns the followers in a collection", %{conn: conn} do
user = insert(:user)
user_two = insert(:user)
User.follow(user, user_two)
result =
conn
|> get("/users/#{user_two.nickname}/followers")
|> json_response(200)
assert result["first"]["orderedItems"] == [user.ap_id]
end
test "it works for more than 10 users", %{conn: conn} do
user = insert(:user)
Enum.each(1..15, fn _ ->
other_user = insert(:user)
User.follow(other_user, user)
end)
result =
conn
|> get("/users/#{user.nickname}/followers")
|> json_response(200)
assert length(result["first"]["orderedItems"]) == 10
assert result["first"]["totalItems"] == 15
assert result["totalItems"] == 15
result =
conn
|> get("/users/#{user.nickname}/followers?page=2")
|> json_response(200)
assert length(result["orderedItems"]) == 5
assert result["totalItems"] == 15
end
end
describe "/users/:nickname/following" do
test "it returns the following in a collection", %{conn: conn} do
user = insert(:user)
user_two = insert(:user)
User.follow(user, user_two)
result =
conn
|> get("/users/#{user.nickname}/following")
|> json_response(200)
assert result["first"]["orderedItems"] == [user_two.ap_id]
end
test "it works for more than 10 users", %{conn: conn} do
user = insert(:user)
Enum.each(1..15, fn _ ->
user = Repo.get(User, user.id)
other_user = insert(:user)
User.follow(user, other_user)
end)
result =
conn
|> get("/users/#{user.nickname}/following")
|> json_response(200)
assert length(result["first"]["orderedItems"]) == 10
assert result["first"]["totalItems"] == 15
assert result["totalItems"] == 15
result =
conn
|> get("/users/#{user.nickname}/following?page=2")
|> json_response(200)
assert length(result["orderedItems"]) == 5
assert result["totalItems"] == 15
end
end
2017-12-11 17:22:48 +00:00
end