Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2018-05-17 11:32:23 +02:00
parent 2c1abe5e19
commit e14007bac5
45 changed files with 2916 additions and 111 deletions

View File

@@ -0,0 +1,139 @@
defmodule EventosWeb.ActivityPubControllerTest do
use EventosWeb.ConnCase
import Eventos.Factory
alias EventosWeb.ActivityPub.{AccountView, ObjectView}
alias Eventos.{Repo, Accounts, Accounts.Account}
alias Eventos.Activity
import Logger
describe "/@:username" do
test "it returns a json representation of the account", %{conn: conn} do
account = insert(:account)
conn =
conn
|> put_req_header("accept", "application/activity+json")
|> get("/@#{account.username}")
account = Accounts.get_account!(account.id)
assert json_response(conn, 200) == AccountView.render("account.json", %{account: account})
Logger.error(inspect AccountView.render("account.json", %{account: account}))
end
end
describe "/@username/slug" do
test "it returns a json representation of the object", %{conn: conn} do
event = insert(:event)
{slug, parts} = List.pop_at(String.split(event.url, "/"), -1)
"@" <> username = List.last(parts)
conn =
conn
|> put_req_header("accept", "application/activity+json")
|> get("/@#{username}/#{slug}")
assert json_response(conn, 200) == ObjectView.render("event.json", %{event: event})
Logger.error(inspect ObjectView.render("event.json", %{event: event}))
end
end
# describe "/accounts/:username/inbox" do
# test "it inserts an incoming activity into the database", %{conn: conn} do
# data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
#
# conn =
# conn
# |> assign(:valid_signature, true)
# |> put_req_header("content-type", "application/activity+json")
# |> post("/inbox", data)
#
# assert "ok" == json_response(conn, 200)
# :timer.sleep(500)
# assert Activity.get_by_ap_id(data["id"])
# end
# end
# describe "/accounts/: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
end

View File

@@ -0,0 +1,81 @@
defmodule EventosWeb.CommentControllerTest do
use EventosWeb.ConnCase
alias Eventos.Events
alias Eventos.Events.Comment
@create_attrs %{text: "some text", url: "some url"}
@update_attrs %{text: "some updated text", url: "some updated url"}
@invalid_attrs %{text: nil, url: nil}
def fixture(:comment) do
{:ok, comment} = Events.create_comment(@create_attrs)
comment
end
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "index" do
test "lists all comments", %{conn: conn} do
conn = get conn, comment_path(conn, :index)
assert json_response(conn, 200)["data"] == []
end
end
describe "create comment" do
test "renders comment when data is valid", %{conn: conn} do
conn = post conn, comment_path(conn, :create), comment: @create_attrs
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get conn, comment_path(conn, :show, id)
assert json_response(conn, 200)["data"] == %{
"id" => id,
"text" => "some text",
"url" => "some url"}
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post conn, comment_path(conn, :create), comment: @invalid_attrs
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update comment" do
setup [:create_comment]
test "renders comment when data is valid", %{conn: conn, comment: %Comment{id: id} = comment} do
conn = put conn, comment_path(conn, :update, comment), comment: @update_attrs
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get conn, comment_path(conn, :show, id)
assert json_response(conn, 200)["data"] == %{
"id" => id,
"text" => "some updated text",
"url" => "some updated url"}
end
test "renders errors when data is invalid", %{conn: conn, comment: comment} do
conn = put conn, comment_path(conn, :update, comment), comment: @invalid_attrs
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "delete comment" do
setup [:create_comment]
test "deletes chosen comment", %{conn: conn, comment: comment} do
conn = delete conn, comment_path(conn, :delete, comment)
assert response(conn, 204)
assert_error_sent 404, fn ->
get conn, comment_path(conn, :show, comment)
end
end
end
defp create_comment(_) do
comment = fixture(:comment)
{:ok, comment: comment}
end
end