Update tests

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-07-10 14:56:10 +02:00
parent dee437e4f3
commit 73ef0dc6af
5 changed files with 29 additions and 4 deletions

View file

@ -76,7 +76,8 @@ defmodule Eventos.Actors.Actor do
@doc false
def changeset(%Actor{} = actor, attrs) do
actor
|> Ecto.Changeset.cast(attrs, [:url, :outbox_url, :inbox_url, :shared_inbox_url, :following_url, :followers_url, :type, :name, :domain, :summary, :preferred_username, :keys, :manually_approves_followers, :suspended, :avatar_url, :banner_url])
|> Ecto.Changeset.cast(attrs, [:url, :outbox_url, :inbox_url, :shared_inbox_url, :following_url, :followers_url, :type, :name, :domain, :summary, :preferred_username, :keys, :manually_approves_followers, :suspended, :avatar_url, :banner_url, :user_id])
|> put_change(:url, "#{EventosWeb.Endpoint.url()}/@#{attrs["prefered_username"]}")
|> validate_required([:preferred_username, :keys, :suspended, :url])
|> unique_constraint(:prefered_username, name: :actors_preferred_username_domain_index)
end

View file

@ -5,7 +5,7 @@ defmodule EventosWeb.ActorController do
use EventosWeb, :controller
alias Eventos.Actors
alias Eventos.Actors.Actor
alias Eventos.Actors.{Actor, User}
alias Eventos.Service.ActivityPub
action_fallback EventosWeb.FallbackController
@ -15,6 +15,17 @@ defmodule EventosWeb.ActorController do
render(conn, "index.json", actors: actors)
end
def create(conn, %{"actor" => actor_params}) do
with %User{} = user <- Guardian.Plug.current_resource(conn),
actor_params <- Map.put(actor_params, "user_id", user.id),
{:ok, %Actor{} = actor} <- Actors.create_actor(actor_params) do
conn
|> put_status(:created)
|> put_resp_header("location", actor_path(conn, :show, actor.preferred_username))
|> render("show_basic.json", actor: actor)
end
end
def show(conn, %{"name" => name}) do
actor = Actors.get_actor_by_name_with_everything(name)
render(conn, "show.json", actor: actor)

View file

@ -76,6 +76,7 @@ defmodule EventosWeb.Router do
get "/user", UserController, :show_current_actor
post "/sign-out", UserSessionController, :sign_out
resources "/users", UserController, except: [:new, :edit, :show]
post "/actors", ActorController, :create
patch "/actors/:name", ActorController, :update
post "/events", EventController, :create
patch "/events/:uuid", EventController, :update

View file

@ -37,7 +37,6 @@ defmodule Eventos.ActorsTest do
assert actor.domain == "some domain"
assert actor.keys == "some keypair"
assert actor.suspended
assert actor.url == "some url"
assert actor.preferred_username == "some username"
end
@ -54,7 +53,6 @@ defmodule Eventos.ActorsTest do
assert actor.domain == "some updated domain"
assert actor.keys == "some updated keys"
refute actor.suspended
assert actor.url == "some updated url"
assert actor.preferred_username == "some updated username"
end

View file

@ -11,6 +11,12 @@ defmodule EventosWeb.ActorControllerTest do
{:ok, conn: conn, user: user, actor: actor}
end
key = :public_key.generate_key({:rsa, 2048, 65_537})
entry = :public_key.pem_entry_encode(:RSAPrivateKey, key)
pem = [entry] |> :public_key.pem_encode() |> String.trim_trailing()
@create_attrs %{preferred_username: "otheridentity", summary: "This is my other identity", domain: nil, keys: pem, user: nil}
describe "index" do
test "lists all actors", %{conn: conn, user: user, actor: actor} do
conn = get conn, actor_path(conn, :index)
@ -18,6 +24,14 @@ defmodule EventosWeb.ActorControllerTest do
end
end
describe "create actor" do
test "from an existing user", %{conn: conn, user: user} do
conn = auth_conn(conn, user)
conn = post conn, actor_path(conn, :create), actor: @create_attrs
assert json_response(conn, 201)["data"]["username"] == @create_attrs.preferred_username
end
end
###
# Not possible atm
###