From ca36dd12e261c29ee5cda2f75820999bb6f10ddc Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 14 Jun 2018 17:25:55 +0200 Subject: [PATCH] Make tests great again ! (Also use only one field for public/private key pem) Signed-off-by: Thomas Citharel --- lib/eventos/actors/actor.ex | 39 ++-- lib/eventos/actors/actors.ex | 34 ++-- lib/eventos/addresses/address.ex | 1 + lib/eventos/events/comment.ex | 9 +- lib/eventos/events/event.ex | 20 +- lib/eventos/events/events.ex | 20 +- lib/eventos/events/participant.ex | 3 +- .../controllers/activity_pub_controller.ex | 4 +- lib/eventos_web/controllers/bot_controller.ex | 6 +- .../controllers/comment_controller.ex | 12 +- .../controllers/event_controller.ex | 18 +- .../controllers/session_controller.ex | 4 +- .../controllers/user_controller.ex | 9 +- lib/eventos_web/router.ex | 14 +- .../views/activity_pub/actor_view.ex | 3 +- lib/eventos_web/views/comment_view.ex | 4 +- lib/service/activity_pub/activity_pub.ex | 2 +- lib/service/activity_pub/utils.ex | 27 +++ .../http_signatures/http_signatures.ex | 10 +- .../20180607095732_add_uuid_to_comments.exs | 15 ++ ...4094653_make_shared_inbox_url_nullable.exs | 15 ++ ...on_public_private_key_into_keys_column.exs | 17 ++ ...ibuted_to_field_to_events_and_comments.exs | 23 +++ test/eventos/actors/actors_test.exs | 50 +++-- test/eventos/events/events_test.exs | 176 +++--------------- .../service/activitypub/activitypub_test.exs | 36 ++-- .../service/web_finger/web_finger_test.exs | 32 ++-- .../controllers/account_controller_test.exs | 60 ------ .../activity_pub_controller_test.exs | 26 ++- .../controllers/actor_controller_test.exs | 49 +++++ .../controllers/address_controller_test.exs | 11 +- .../controllers/bot_controller_test.exs | 34 ++-- .../controllers/category_controller_test.exs | 11 +- .../controllers/comment_controller_test.exs | 69 +++---- .../controllers/event_controller_test.exs | 72 +++---- .../controllers/group_controller_test.exs | 109 ----------- .../controllers/session_controller_test.exs | 15 +- .../controllers/tag_controller_test.exs | 11 +- .../controllers/track_controller_test.exs | 13 +- .../controllers/user_controller_test.exs | 23 +-- test/support/conn_case.ex | 7 + test/support/data_case.ex | 2 +- test/support/factory.ex | 39 ++-- 43 files changed, 498 insertions(+), 656 deletions(-) create mode 100644 priv/repo/migrations/20180607095732_add_uuid_to_comments.exs create mode 100644 priv/repo/migrations/20180614094653_make_shared_inbox_url_nullable.exs create mode 100644 priv/repo/migrations/20180614104618_fusion_public_private_key_into_keys_column.exs create mode 100644 priv/repo/migrations/20180614131328_add_attributed_to_field_to_events_and_comments.exs delete mode 100644 test/eventos_web/controllers/account_controller_test.exs create mode 100644 test/eventos_web/controllers/actor_controller_test.exs delete mode 100644 test/eventos_web/controllers/group_controller_test.exs diff --git a/lib/eventos/actors/actor.ex b/lib/eventos/actors/actor.ex index 92f3acce3..4b010ca45 100644 --- a/lib/eventos/actors/actor.ex +++ b/lib/eventos/actors/actor.ex @@ -46,7 +46,7 @@ defmodule Eventos.Actors.Actor do import Logger -# @type t :: %Actor{description: String.t, id: integer(), inserted_at: DateTime.t, updated_at: DateTime.t, display_name: String.t, domain: String.t, private_key: String.t, public_key: String.t, suspended: boolean(), url: String.t, username: String.t, organized_events: list(), groups: list(), group_request: list(), user: User.t, field: ActorTypeEnum.t} +# @type t :: %Actor{description: String.t, id: integer(), inserted_at: DateTime.t, updated_at: DateTime.t, display_name: String.t, domain: String.t, keys: String.t, suspended: boolean(), url: String.t, username: String.t, organized_events: list(), groups: list(), group_request: list(), user: User.t, field: ActorTypeEnum.t} schema "actors" do field :url, :string @@ -55,13 +55,12 @@ defmodule Eventos.Actors.Actor do field :following_url, :string field :followers_url, :string field :shared_inbox_url, :string - field :type, Eventos.Actors.ActorTypeEnum + field :type, Eventos.Actors.ActorTypeEnum, default: :Person field :name, :string field :domain, :string field :summary, :string field :preferred_username, :string - field :public_key, :string - field :private_key, :string + field :keys, :string field :manually_approves_followers, :boolean, default: false field :suspended, :boolean, default: false field :avatar_url, :string @@ -77,24 +76,25 @@ 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, :public_key, :private_key, :manually_approves_followers, :suspended, :avatar_url, :banner_url]) - |> validate_required([:preferred_username, :public_key, :suspended, :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]) + |> validate_required([:preferred_username, :keys, :suspended, :url]) |> unique_constraint(:prefered_username, name: :actors_preferred_username_domain_index) end def registration_changeset(%Actor{} = actor, attrs) do actor - |> Ecto.Changeset.cast(attrs, [:preferred_username, :domain, :name, :summary, :private_key, :public_key, :suspended, :url, :type]) - |> validate_required([:preferred_username, :public_key, :suspended, :url, :type]) - |> unique_constraint(:prefered_username, name: :actors_preferred_username_domain_index) + |> Ecto.Changeset.cast(attrs, [:preferred_username, :domain, :name, :summary, :keys, :keys, :suspended, :url, :type, :avatar_url]) + |> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_index) + |> put_change(:url, "#{EventosWeb.Endpoint.url()}/@#{attrs["prefered_username"]}") + |> validate_required([:preferred_username, :keys, :suspended, :url, :type]) end @email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/ def remote_actor_creation(params) do changes = %Actor{} - |> Ecto.Changeset.cast(params, [:url, :outbox_url, :inbox_url, :shared_inbox_url, :following_url, :followers_url, :type, :name, :domain, :summary, :preferred_username, :public_key, :manually_approves_followers, :avatar_url, :banner_url]) - |> validate_required([:url, :outbox_url, :inbox_url, :type, :name, :domain, :preferred_username, :public_key]) + |> Ecto.Changeset.cast(params, [:url, :outbox_url, :inbox_url, :shared_inbox_url, :following_url, :followers_url, :type, :name, :domain, :summary, :preferred_username, :keys, :manually_approves_followers, :avatar_url, :banner_url]) + |> validate_required([:url, :outbox_url, :inbox_url, :type, :name, :domain, :preferred_username, :keys]) |> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_index) |> validate_length(:summary, max: 5000) |> validate_length(:preferred_username, max: 100) @@ -135,20 +135,31 @@ defmodule Eventos.Actors.Actor do #@spec get_public_key_for_url(Actor.t) :: {:ok, String.t} def get_public_key_for_url(url) do with %Actor{} = actor <- get_or_fetch_by_url(url) do - get_public_key_for_actor(actor) + actor + |> get_keys_for_actor + |> Eventos.Service.ActivityPub.Utils.pem_to_public_key else _ -> :error end end + @deprecated "Use get_keys_for_actor/1 instead" #@spec get_public_key_for_actor(Actor.t) :: {:ok, String.t} def get_public_key_for_actor(%Actor{} = actor) do - {:ok, actor.public_key} + {:ok, actor.keys} end + @doc """ + Returns a pem encoded keypair (if local) or public key + """ + def get_keys_for_actor(%Actor{} = actor) do + actor.keys + end + + @deprecated "Use get_keys_for_actor/1 instead" #@spec get_private_key_for_actor(Actor.t) :: {:ok, String.t} def get_private_key_for_actor(%Actor{} = actor) do - actor.private_key + actor.keys end def get_followers(%Actor{id: actor_id} = actor) do diff --git a/lib/eventos/actors/actors.ex b/lib/eventos/actors/actors.ex index f8373fb1e..4916bc8a6 100644 --- a/lib/eventos/actors/actors.ex +++ b/lib/eventos/actors/actors.ex @@ -185,7 +185,7 @@ defmodule Eventos.Actors do def insert_or_update_actor(data) do cs = Actor.remote_actor_creation(data) - Repo.insert(cs, on_conflict: [set: [public_key: data.public_key, avatar_url: data.avatar_url, banner_url: data.banner_url, name: data.name]], conflict_target: [:preferred_username, :domain]) + Repo.insert(cs, on_conflict: [set: [keys: data.keys, avatar_url: data.avatar_url, banner_url: data.banner_url, name: data.name]], conflict_target: [:preferred_username, :domain]) end # def increase_event_count(%Actor{} = actor) do @@ -335,16 +335,25 @@ defmodule Eventos.Actors do Register user """ def register(%{email: email, password: password, username: username}) do - #{:ok, {privkey, pubkey}} = RsaEx.generate_keypair("4096") - {:ok, rsa_priv_key} = ExPublicKey.generate_key() - {:ok, rsa_pub_key} = ExPublicKey.public_key_from_private_key(rsa_priv_key) + key = :public_key.generate_key({:rsa, 2048, 65537}) + entry = :public_key.pem_entry_encode(:RSAPrivateKey, key) + pem = :public_key.pem_encode([entry]) |> String.trim_trailing() + + import Exgravatar + + avatar_url = gravatar_url(email, default: "404") + avatar = case HTTPoison.get(avatar_url) do + {:ok, %HTTPoison.Response{status_code: 200}} -> + avatar_url + _ -> + nil + end actor = Eventos.Actors.Actor.registration_changeset(%Eventos.Actors.Actor{}, %{ preferred_username: username, domain: nil, - private_key: rsa_priv_key |> ExPublicKey.pem_encode(), - public_key: rsa_pub_key |> ExPublicKey.pem_encode(), - url: EventosWeb.Endpoint.url() <> "/@" <> username, + keys: pem, + avatar_url: avatar, }) user = Eventos.Actors.User.registration_changeset(%Eventos.Actors.User{}, %{ @@ -361,7 +370,7 @@ defmodule Eventos.Actors do {:ok, user} rescue e in Ecto.InvalidChangesetError -> - {:error, e.changeset.changes.user.errors} + {:error, e.changeset} end end @@ -370,15 +379,10 @@ defmodule Eventos.Actors do entry = :public_key.pem_entry_encode(:RSAPrivateKey, key) pem = :public_key.pem_encode([entry]) |> String.trim_trailing() - {:ok, rsa_priv_key} = ExPublicKey.generate_key() - {:ok, rsa_pub_key} = ExPublicKey.public_key_from_private_key(rsa_priv_key) - actor = Eventos.Actors.Actor.registration_changeset(%Eventos.Actors.Actor{}, %{ preferred_username: name, domain: nil, - private_key: pem, - public_key: "toto", - url: EventosWeb.Endpoint.url() <> "/@" <> name, + keys: pem, summary: summary, type: :Service }) @@ -387,7 +391,7 @@ defmodule Eventos.Actors do Eventos.Repo.insert!(actor) rescue e in Ecto.InvalidChangesetError -> - {:error, e} + {:error, e.changeset} end end diff --git a/lib/eventos/addresses/address.ex b/lib/eventos/addresses/address.ex index 97737722f..8f3f016f1 100644 --- a/lib/eventos/addresses/address.ex +++ b/lib/eventos/addresses/address.ex @@ -26,5 +26,6 @@ defmodule Eventos.Addresses.Address do def changeset(%Address{} = address, attrs) do address |> cast(attrs, [:description, :floor, :geom, :addressCountry, :addressLocality, :addressRegion, :postalCode, :streetAddress]) + |> validate_required([:streetAddress]) end end diff --git a/lib/eventos/events/comment.ex b/lib/eventos/events/comment.ex index 0422a452c..e43e72bc4 100644 --- a/lib/eventos/events/comment.ex +++ b/lib/eventos/events/comment.ex @@ -10,7 +10,9 @@ defmodule Eventos.Events.Comment do field :text, :string field :url, :string field :local, :boolean, default: true + field :uuid, Ecto.UUID belongs_to :actor, Actor, [foreign_key: :actor_id] + belongs_to :attributed_to, Actor, [foreign_key: :attributed_to_id] belongs_to :event, Event, [foreign_key: :event_id] belongs_to :in_reply_to_comment, Comment, [foreign_key: :in_reply_to_comment_id] belongs_to :origin_comment, Comment, [foreign_key: :origin_comment_id] @@ -20,8 +22,11 @@ defmodule Eventos.Events.Comment do @doc false def changeset(comment, attrs) do + uuid = Ecto.UUID.generate() comment - |> cast(attrs, [:url, :text, :actor_id, :event_id, :in_reply_to_comment_id]) - |> validate_required([:url, :text, :actor_id]) + |> cast(attrs, [:url, :text, :actor_id, :event_id, :in_reply_to_comment_id, :attributed_to_id]) + |> validate_required([:text, :actor_id]) + |> put_change(:uuid, uuid) + |> put_change(:url, "#{EventosWeb.Endpoint.url()}/comments/#{uuid}") end end diff --git a/lib/eventos/events/event.ex b/lib/eventos/events/event.ex index 3a98a9bfd..653d9a9d8 100644 --- a/lib/eventos/events/event.ex +++ b/lib/eventos/events/event.ex @@ -53,6 +53,7 @@ defmodule Eventos.Events.Event do field :publish_at, Timex.Ecto.DateTimeWithTimezone field :uuid, Ecto.UUID, default: Ecto.UUID.generate() belongs_to :organizer_actor, Actor, [foreign_key: :organizer_actor_id] + belongs_to :attributed_to, Actor, [foreign_key: :attributed_to_id] many_to_many :tags, Tag, join_through: "events_tags" belongs_to :category, Category many_to_many :participants, Actor, join_through: Participant @@ -65,17 +66,22 @@ defmodule Eventos.Events.Event do @doc false def changeset(%Event{} = event, attrs) do - changeset = event + uuid = Ecto.UUID.generate() + + # TODO : check what's the use here. Tests ? + actor_url = if Map.has_key?(attrs, :organizer_actor) do + attrs.organizer_actor.preferred_username + else + "" + end + event |> cast(attrs, [:title, :description, :url, :begins_on, :ends_on, :organizer_actor_id, :category_id, :state, :status, :public, :thumbnail, :large_image, :publish_at]) |> cast_assoc(:tags) |> cast_assoc(:address) - |> validate_required([:title, :description, :begins_on, :ends_on, :organizer_actor_id, :category_id]) |> TitleSlug.maybe_generate_slug() |> TitleSlug.unique_constraint() - |> put_change(:uuid, Ecto.UUID.generate()) - - import Logger - Logger.debug(inspect changeset) - changeset + |> put_change(:uuid, uuid) + |> put_change(:url, "#{EventosWeb.Endpoint.url()}/@#{actor_url}/#{uuid}") + |> validate_required([:title, :description, :begins_on, :ends_on, :organizer_actor_id, :category_id, :url, :uuid]) end end diff --git a/lib/eventos/events/events.ex b/lib/eventos/events/events.ex index 32f6a9729..50f8cabc3 100644 --- a/lib/eventos/events/events.ex +++ b/lib/eventos/events/events.ex @@ -152,10 +152,11 @@ defmodule Eventos.Events do """ def create_event(attrs \\ %{}) do - %Event{} - |> Event.changeset(attrs) - |> Repo.insert!() - |> Repo.preload([:organizer_actor]) + case %Event{} |> Event.changeset(attrs) |> Repo.insert() do + {:ok, %Event{} = event} -> {:ok, Repo.preload(event, [:organizer_actor])} + err -> err + end + end @doc """ @@ -522,8 +523,13 @@ defmodule Eventos.Events do @doc """ Returns the list of sessions for an event """ - def list_sessions_for_event(event_id) do - Repo.all(from s in Session, where: s.event_id == ^event_id) + def list_sessions_for_event(event_uuid) do + Repo.all( + from s in Session, + join: e in Event, + on: s.event_id == e.id, + where: e.uuid == ^event_uuid + ) end @doc """ @@ -741,6 +747,8 @@ defmodule Eventos.Events do """ def get_comment!(id), do: Repo.get!(Comment, id) + def get_comment_with_uuid!(uuid), do: Repo.get_by!(Comment, uuid: uuid) + @doc """ Creates a comment. diff --git a/lib/eventos/events/participant.ex b/lib/eventos/events/participant.ex index 16c3f6cf5..cd1045aa9 100644 --- a/lib/eventos/events/participant.ex +++ b/lib/eventos/events/participant.ex @@ -9,8 +9,7 @@ defmodule Eventos.Events.Participant do @primary_key false schema "participants" do - field :role, :integer, default: 0 # 0 : participant, 1 : moderator, 2 : administrator, 3 : creator - field :approved, :boolean + field :role, :integer, default: 0 # 0 : not_approved, 1 : participant, 2 : moderator, 3 : administrator, 4 : creator belongs_to :event, Event, primary_key: true belongs_to :actor, Actor, primary_key: true diff --git a/lib/eventos_web/controllers/activity_pub_controller.ex b/lib/eventos_web/controllers/activity_pub_controller.ex index 6f079054d..47b01f927 100644 --- a/lib/eventos_web/controllers/activity_pub_controller.ex +++ b/lib/eventos_web/controllers/activity_pub_controller.ex @@ -17,8 +17,8 @@ defmodule EventosWeb.ActivityPubController do end end - def event(conn, %{"name" => name, "slug" => slug}) do - with %Event{} = event <- Events.get_event_full_by_name_and_slug!(name, slug) do + def event(conn, %{"uuid" => uuid}) do + with %Event{} = event <- Events.get_event_full_by_uuid(uuid) do conn |> put_resp_header("content-type", "application/activity+json") |> json(ObjectView.render("event.json", %{event: event})) diff --git a/lib/eventos_web/controllers/bot_controller.ex b/lib/eventos_web/controllers/bot_controller.ex index 5181a2744..c6de235b4 100644 --- a/lib/eventos_web/controllers/bot_controller.ex +++ b/lib/eventos_web/controllers/bot_controller.ex @@ -2,7 +2,7 @@ defmodule EventosWeb.BotController do use EventosWeb, :controller alias Eventos.Actors - alias Eventos.Actors.Bot + alias Eventos.Actors.{Bot, Actor} action_fallback EventosWeb.FallbackController @@ -12,9 +12,9 @@ defmodule EventosWeb.BotController do end def create(conn, %{"bot" => bot_params}) do - with user <- Guardian.Plug.current_resource, + with user <- Guardian.Plug.current_resource(conn), bot_params <- Map.put(bot_params, "user_id", user.id), - {:ok, actor} <- Actors.register_bot_account(%{name: bot_params["name"], summary: bot_params["summary"]}), + %Actor{} = actor <- Actors.register_bot_account(%{name: bot_params["name"], summary: bot_params["summary"]}), bot_params <- Map.put(bot_params, "actor_id", actor.id), {:ok, %Bot{} = bot} <- Actors.create_bot(bot_params) do conn diff --git a/lib/eventos_web/controllers/comment_controller.ex b/lib/eventos_web/controllers/comment_controller.ex index af0560ab6..d8f096314 100644 --- a/lib/eventos_web/controllers/comment_controller.ex +++ b/lib/eventos_web/controllers/comment_controller.ex @@ -20,21 +20,21 @@ defmodule EventosWeb.CommentController do end end - def show(conn, %{"id" => id}) do - comment = Events.get_comment!(id) + def show(conn, %{"uuid" => uuid}) do + comment = Events.get_comment_with_uuid!(uuid) render(conn, "show.json", comment: comment) end - def update(conn, %{"id" => id, "comment" => comment_params}) do - comment = Events.get_comment!(id) + def update(conn, %{"uuid" => uuid, "comment" => comment_params}) do + comment = Events.get_comment_with_uuid!(uuid) with {:ok, %Comment{} = comment} <- Events.update_comment(comment, comment_params) do render(conn, "show.json", comment: comment) end end - def delete(conn, %{"id" => id}) do - comment = Events.get_comment!(id) + def delete(conn, %{"uuid" => uuid}) do + comment = Events.get_comment_with_uuid!(uuid) with {:ok, %Comment{}} <- Events.delete_comment(comment) do send_resp(conn, :no_content, "") end diff --git a/lib/eventos_web/controllers/event_controller.ex b/lib/eventos_web/controllers/event_controller.ex index 93e6c80f7..e4c43bc95 100644 --- a/lib/eventos_web/controllers/event_controller.ex +++ b/lib/eventos_web/controllers/event_controller.ex @@ -9,6 +9,8 @@ defmodule EventosWeb.EventController do alias Eventos.Export.ICalendar alias Eventos.Addresses + import Logger + action_fallback EventosWeb.FallbackController def index(conn, _params) do @@ -32,11 +34,7 @@ defmodule EventosWeb.EventController do end defp process_address(address) do - import Logger - Logger.debug("process address") - Logger.debug(inspect address) geom = EventosWeb.AddressController.process_geom(address["geom"]) - Logger.debug(inspect geom) case geom do nil -> address @@ -53,8 +51,12 @@ defmodule EventosWeb.EventController do end def show(conn, %{"uuid" => uuid}) do - event = Events.get_event_full_by_uuid(uuid) - render(conn, "show.json", event: event) + case Events.get_event_full_by_uuid(uuid) do + nil -> + send_resp(conn, 404, "") + event -> + render(conn, "show.json", event: event) + end end def export_to_ics(conn, %{"uuid" => uuid}) do @@ -71,8 +73,8 @@ defmodule EventosWeb.EventController do end def delete(conn, %{"uuid" => uuid}) do - event = Events.get_event_by_uuid(uuid) - with {:ok, %Event{}} <- Events.delete_event(event) do + with event <- Events.get_event_by_uuid(uuid), + {:ok, %Event{}} <- Events.delete_event(event) do send_resp(conn, :no_content, "") end end diff --git a/lib/eventos_web/controllers/session_controller.ex b/lib/eventos_web/controllers/session_controller.ex index ac1454750..e66dea842 100644 --- a/lib/eventos_web/controllers/session_controller.ex +++ b/lib/eventos_web/controllers/session_controller.ex @@ -28,8 +28,8 @@ defmodule EventosWeb.SessionController do render(conn, "show.json", session: session) end - def show_sessions_for_event(conn, %{"id" => event_id}) do - sessions = Events.list_sessions_for_event(event_id) + def show_sessions_for_event(conn, %{"uuid" => event_uuid}) do + sessions = Events.list_sessions_for_event(event_uuid) render(conn, "index.json", sessions: sessions) end diff --git a/lib/eventos_web/controllers/user_controller.ex b/lib/eventos_web/controllers/user_controller.ex index fbbe872fc..98abb9ffa 100644 --- a/lib/eventos_web/controllers/user_controller.ex +++ b/lib/eventos_web/controllers/user_controller.ex @@ -16,16 +16,11 @@ defmodule EventosWeb.UserController do end def register(conn, %{"username" => username, "email" => email, "password" => password}) do - case Actors.register(%{email: email, password: password, username: username}) do - {:ok, %User{} = user} -> - {:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user) + with {:ok, %User{} = user} <- Actors.register(%{email: email, password: password, username: username}), + {:ok, token, _claims} <- EventosWeb.Guardian.encode_and_sign(user) do conn |> put_status(:created) |> render("show_with_token.json", %{token: token, user: user}) - {:error, error} -> - conn - |> put_resp_content_type("application/json") - |> send_resp(400, Poison.encode!(%{"msg" => handle_changeset_errors(error)})) end end diff --git a/lib/eventos_web/router.ex b/lib/eventos_web/router.ex index b10693c30..9ac52f102 100644 --- a/lib/eventos_web/router.ex +++ b/lib/eventos_web/router.ex @@ -44,8 +44,9 @@ defmodule EventosWeb.Router do get "/events/:uuid/tracks", TrackController, :show_tracks_for_event get "/events/:uuid/sessions", SessionController, :show_sessions_for_event get "/events/:uuid", EventController, :show - resources "/comments", CommentController, only: [:show] - get "/bots/:id", BotController, :view + get "/comments/:uuid", CommentController, :show + get "/bots/:id", BotController, :show + get "/bots", BotController, :index get "/actors", ActorController, :index get "/actors/search/:name", ActorController, :search @@ -74,10 +75,13 @@ defmodule EventosWeb.Router do patch "/events/:uuid", EventController, :update put "/events/:uuid", EventController, :update delete "/events/:uuid", EventController, :delete - resources "/comments", CommentController, except: [:new, :edit, :show] + post "/comments", CommentController, :create + patch "/comments/:uuid", CommentController, :update + put "/comments/:uuid", CommentController, :update + delete "/comments/:uuid", CommentController, :delete #post "/events/:id/request", EventRequestController, :create_for_event resources "/participant", ParticipantController - resources "/bots", BotController, except: [:new, :edit, :show] + resources "/bots", BotController, except: [:new, :edit, :show, :index] #resources "/requests", EventRequestController post "/groups", GroupController, :create post "/groups/:name/join", GroupController, :join @@ -110,7 +114,7 @@ defmodule EventosWeb.Router do get "/@:name/outbox", ActivityPubController, :outbox get "/@:name/following", ActivityPubController, :following get "/@:name/followers", ActivityPubController, :followers - get "/@:name/:slug", ActivityPubController, :event + get "/events/:uuid", ActivityPubController, :event post "/@:name/inbox", ActivityPubController, :inbox post "/inbox", ActivityPubController, :inbox end diff --git a/lib/eventos_web/views/activity_pub/actor_view.ex b/lib/eventos_web/views/activity_pub/actor_view.ex index b2ea02f3c..517c7d6b6 100644 --- a/lib/eventos_web/views/activity_pub/actor_view.ex +++ b/lib/eventos_web/views/activity_pub/actor_view.ex @@ -13,7 +13,8 @@ defmodule EventosWeb.ActivityPub.ActorView do import Ecto.Query def render("actor.json", %{actor: actor}) do - {:ok, public_key} = Actor.get_public_key_for_actor(actor) + pem = Actor.get_keys_for_actor(actor) + public_key = Eventos.Service.ActivityPub.Utils.pem_to_public_key_pem(pem) %{ "id" => actor.url, diff --git a/lib/eventos_web/views/comment_view.ex b/lib/eventos_web/views/comment_view.ex index 4dd58edbd..17654fe43 100644 --- a/lib/eventos_web/views/comment_view.ex +++ b/lib/eventos_web/views/comment_view.ex @@ -12,7 +12,9 @@ defmodule EventosWeb.CommentView do def render("comment.json", %{comment: comment}) do %{id: comment.id, + uuid: comment.uuid, url: comment.url, - text: comment.text} + text: comment.text + } end end diff --git a/lib/service/activity_pub/activity_pub.ex b/lib/service/activity_pub/activity_pub.ex index fe974b41b..265a4dca5 100644 --- a/lib/service/activity_pub/activity_pub.ex +++ b/lib/service/activity_pub/activity_pub.ex @@ -253,7 +253,7 @@ defmodule Eventos.Service.ActivityPub do preferred_username: data["preferredUsername"], follower_address: data["followers"], summary: data["summary"], - public_key: data["publicKey"]["publicKeyPem"], + keys: data["publicKey"]["publicKeyPem"], inbox_url: data["inbox"], outbox_url: data["outbox"], following_url: data["following"], diff --git a/lib/service/activity_pub/utils.ex b/lib/service/activity_pub/utils.ex index 087a64242..ecee714d4 100644 --- a/lib/service/activity_pub/utils.ex +++ b/lib/service/activity_pub/utils.ex @@ -304,4 +304,31 @@ defmodule Eventos.Service.ActivityPub.Utils do } |> Map.merge(additional) end + + @doc """ + Converts PEM encoded keys to a public key representation + """ + def pem_to_public_key(pem) do + [private_key_code] = :public_key.pem_decode(pem) + private_key = :public_key.pem_entry_decode(private_key_code) + {:RSAPrivateKey, _, modulus, exponent, _, _, _, _, _, _, _} = private_key + {:RSAPublicKey, modulus, exponent} + end + + @doc """ + Converts PEM encoded keys to a private key representation + """ + def pem_to_private_key(pem) do + [private_key_code] = :public_key.pem_decode(pem) + :public_key.pem_entry_decode(private_key_code) + end + + @doc """ + Converts PEM encoded keys to a PEM public key representation + """ + def pem_to_public_key_pem(pem) do + public_key = pem_to_public_key(pem) + public_key = :public_key.pem_entry_encode(:RSAPublicKey, public_key) + :public_key.pem_encode([public_key]) + end end diff --git a/lib/service/http_signatures/http_signatures.ex b/lib/service/http_signatures/http_signatures.ex index 517f29650..05e01611b 100644 --- a/lib/service/http_signatures/http_signatures.ex +++ b/lib/service/http_signatures/http_signatures.ex @@ -32,7 +32,7 @@ defmodule Eventos.Service.HTTPSignatures do # TODO: How to get the right key and see if it is actually valid for that request. # For now, fetch the key for the actor. with actor_id <- conn.params["actor"], - {:ok, public_key_code} <- Actor.get_public_key_for_url(actor_id), + public_key_code <- Actor.get_public_key_for_url(actor_id), [public_key] = :public_key.pem_decode(public_key_code), public_key = :public_key.pem_entry_decode(public_key) do if validate_conn(conn, public_key) do @@ -42,7 +42,7 @@ defmodule Eventos.Service.HTTPSignatures do # Fetch user anew and try one more time with actor_id <- conn.params["actor"], {:ok, _actor} <- ActivityPub.make_actor_from_url(actor_id), - {:ok, public_key_code} <- Actor.get_public_key_for_url(actor_id), + public_key_code <- Actor.get_public_key_for_url(actor_id), [public_key] = :public_key.pem_decode(public_key_code), public_key = :public_key.pem_entry_decode(public_key) do validate_conn(conn, public_key) @@ -70,10 +70,8 @@ defmodule Eventos.Service.HTTPSignatures do |> Enum.join("\n") end - def sign(actor, headers) do - with {:ok, private_key_code} = Actor.get_private_key_for_actor(actor), - [private_key] = :public_key.pem_decode(private_key_code), - private_key = :public_key.pem_entry_decode(private_key) do + def sign(%Actor{} = actor, headers) do + with private_key = Actor.get_keys_for_actor(actor) do sigstring = build_signing_string(headers, Map.keys(headers)) signature = diff --git a/priv/repo/migrations/20180607095732_add_uuid_to_comments.exs b/priv/repo/migrations/20180607095732_add_uuid_to_comments.exs new file mode 100644 index 000000000..c0f0785cb --- /dev/null +++ b/priv/repo/migrations/20180607095732_add_uuid_to_comments.exs @@ -0,0 +1,15 @@ +defmodule Eventos.Repo.Migrations.AddUUIDToComments do + use Ecto.Migration + + def up do + alter table(:comments) do + add :uuid, :uuid + end + end + + def down do + alter table(:comments) do + remove :uuid + end + end +end diff --git a/priv/repo/migrations/20180614094653_make_shared_inbox_url_nullable.exs b/priv/repo/migrations/20180614094653_make_shared_inbox_url_nullable.exs new file mode 100644 index 000000000..3b2135361 --- /dev/null +++ b/priv/repo/migrations/20180614094653_make_shared_inbox_url_nullable.exs @@ -0,0 +1,15 @@ +defmodule Eventos.Repo.Migrations.MakeSharedInboxUrlNullable do + use Ecto.Migration + + def up do + alter table(:actors) do + modify :shared_inbox_url, :string, null: true, default: nil + end + end + + def down do + alter table(:actors) do + add :shared_inbox_url, :string, null: false, default: "" + end + end +end diff --git a/priv/repo/migrations/20180614104618_fusion_public_private_key_into_keys_column.exs b/priv/repo/migrations/20180614104618_fusion_public_private_key_into_keys_column.exs new file mode 100644 index 000000000..d9722db4d --- /dev/null +++ b/priv/repo/migrations/20180614104618_fusion_public_private_key_into_keys_column.exs @@ -0,0 +1,17 @@ +defmodule Eventos.Repo.Migrations.FusionPublicPrivateKeyIntoKeysColumn do + use Ecto.Migration + + def up do + rename table(:actors), :private_key, to: :keys + alter table(:actors) do + remove :public_key + end + end + + def down do + alter table(:actors) do + rename :keys, to: :private_key + add :public_key, :text, null: true + end + end +end diff --git a/priv/repo/migrations/20180614131328_add_attributed_to_field_to_events_and_comments.exs b/priv/repo/migrations/20180614131328_add_attributed_to_field_to_events_and_comments.exs new file mode 100644 index 000000000..66b5d9579 --- /dev/null +++ b/priv/repo/migrations/20180614131328_add_attributed_to_field_to_events_and_comments.exs @@ -0,0 +1,23 @@ +defmodule Eventos.Repo.Migrations.AddAttributedToFieldToEventsAndComments do + use Ecto.Migration + + def up do + alter table(:events) do + add :attributed_to_id, references(:actors, on_delete: :nothing) + end + + alter table(:comments) do + add :attributed_to_id, references(:actors, on_delete: :nothing) + end + end + + def down do + alter table(:events) do + remove :attributed_to_id + end + + alter table(:comments) do + remove :attributed_to_id + end + end +end diff --git a/test/eventos/actors/actors_test.exs b/test/eventos/actors/actors_test.exs index 048b9b7c2..709df56b2 100644 --- a/test/eventos/actors/actors_test.exs +++ b/test/eventos/actors/actors_test.exs @@ -1,14 +1,15 @@ defmodule Eventos.ActorsTest do use Eventos.DataCase + import Eventos.Factory alias Eventos.Actors describe "actors" do alias Eventos.Actors.Actor - @valid_attrs %{description: "some description", display_name: "some display_name", domain: "some domain", private_key: "some private_key", public_key: "some public_key", suspended: true, uri: "some uri", url: "some url", username: "some username"} - @update_attrs %{description: "some updated description", display_name: "some updated display_name", domain: "some updated domain", private_key: "some updated private_key", public_key: "some updated public_key", suspended: false, uri: "some updated uri", url: "some updated url", username: "some updated username"} - @invalid_attrs %{description: nil, display_name: nil, domain: nil, private_key: nil, public_key: nil, suspended: nil, uri: nil, url: nil, username: nil} + @valid_attrs %{summary: "some description", name: "some name", domain: "some domain", keys: "some keypair", suspended: true, uri: "some uri", url: "some url", preferred_username: "some username"} + @update_attrs %{summary: "some updated description", name: "some updated name", domain: "some updated domain", keys: "some updated keys", suspended: false, uri: "some updated uri", url: "some updated url", preferred_username: "some updated username"} + @invalid_attrs %{summary: nil, name: nil, domain: nil, keys: nil, suspended: nil, uri: nil, url: nil, preferred_username: nil} def actor_fixture(attrs \\ %{}) do {:ok, actor} = @@ -31,15 +32,13 @@ defmodule Eventos.ActorsTest do test "create_actor/1 with valid data creates a actor" do assert {:ok, %Actor{} = actor} = Actors.create_actor(@valid_attrs) - assert actor.description == "some description" - assert actor.display_name == "some display_name" + assert actor.summary == "some description" + assert actor.name == "some name" assert actor.domain == "some domain" - assert actor.private_key == "some private_key" - assert actor.public_key == "some public_key" + assert actor.keys == "some keypair" assert actor.suspended - assert actor.uri == "some uri" assert actor.url == "some url" - assert actor.username == "some username" + assert actor.preferred_username == "some username" end test "create_actor/1 with invalid data returns error changeset" do @@ -50,15 +49,13 @@ defmodule Eventos.ActorsTest do actor = actor_fixture() assert {:ok, actor} = Actors.update_actor(actor, @update_attrs) assert %Actor{} = actor - assert actor.description == "some updated description" - assert actor.display_name == "some updated display_name" + assert actor.summary == "some updated description" + assert actor.name == "some updated name" assert actor.domain == "some updated domain" - assert actor.private_key == "some updated private_key" - assert actor.public_key == "some updated public_key" + assert actor.keys == "some updated keys" refute actor.suspended - assert actor.uri == "some updated uri" assert actor.url == "some updated url" - assert actor.username == "some updated username" + assert actor.preferred_username == "some updated username" end test "update_actor/2 with invalid data returns error changeset" do @@ -82,7 +79,7 @@ defmodule Eventos.ActorsTest do describe "users" do alias Eventos.Actors.{User, Actor} - @actor_valid_attrs %{description: "some description", display_name: "some display_name", domain: "some domain", private_key: "some private_key", public_key: "some public_key", suspended: true, uri: "some uri", url: "some url", username: "some username"} + @actor_valid_attrs %{description: "some description", display_name: "some display_name", domain: "some domain", keys: "some keys", suspended: true, uri: "some uri", url: "some url", preferred_username: "some username"} @valid_attrs %{email: "foo@bar.tld", password_hash: "some password_hash", role: 42} @update_attrs %{email: "foo@fighters.tld", password_hash: "some updated password_hash", role: 43} @invalid_attrs %{email: nil, password_hash: nil, role: nil} @@ -182,26 +179,26 @@ defmodule Eventos.ActorsTest do @invalid_attrs %{source: nil, type: nil} def bot_fixture(attrs \\ %{}) do - {:ok, bot} = - attrs - |> Enum.into(@valid_attrs) - |> Actors.create_bot() - - bot + insert(:bot) end test "list_bots/0 returns all bots" do bot = bot_fixture() - assert Actors.list_bots() == [bot] + bots = Actors.list_bots() + assert bots = [bot] end test "get_bot!/1 returns the bot with given id" do bot = bot_fixture() - assert Actors.get_bot!(bot.id) == bot + bot_fetched = Actors.get_bot!(bot.id) + assert bot_fetched = bot end test "create_bot/1 with valid data creates a bot" do - assert {:ok, %Bot{} = bot} = Actors.create_bot(@valid_attrs) + attrs = @valid_attrs + |> Map.merge(%{actor_id: insert(:actor).id}) + |> Map.merge(%{user_id: insert(:user).id}) + assert {:ok, %Bot{} = bot} = Actors.create_bot(attrs) assert bot.source == "some source" assert bot.type == "some type" end @@ -221,7 +218,8 @@ defmodule Eventos.ActorsTest do test "update_bot/2 with invalid data returns error changeset" do bot = bot_fixture() assert {:error, %Ecto.Changeset{}} = Actors.update_bot(bot, @invalid_attrs) - assert bot == Actors.get_bot!(bot.id) + bot_fetched = Actors.get_bot!(bot.id) + assert bot = bot_fetched end test "delete_bot/1 deletes the bot" do diff --git a/test/eventos/events/events_test.exs b/test/eventos/events/events_test.exs index 4ee82cf36..abc2b3792 100644 --- a/test/eventos/events/events_test.exs +++ b/test/eventos/events/events_test.exs @@ -4,13 +4,12 @@ defmodule Eventos.EventsTest do import Eventos.Factory alias Eventos.Events - alias Eventos.Accounts + alias Eventos.Actors - @account_valid_attrs %{description: "some description", display_name: "some display_name", domain: "some domain", private_key: "some private_key", public_key: "some public_key", suspended: true, uri: "some uri", url: "some url", username: "some username"} @event_valid_attrs %{begins_on: "2010-04-17 14:00:00.000000Z", description: "some description", ends_on: "2010-04-17 14:00:00.000000Z", title: "some title"} - def account_fixture do - insert(:account) + def actor_fixture do + insert(:actor) end def address_fixture do @@ -28,7 +27,6 @@ defmodule Eventos.EventsTest do describe "events" do alias Eventos.Events.Event - @account_valid_attrs %{description: "some description", display_name: "some display_name", domain: "some domain", private_key: "some private_key", public_key: "some public_key", suspended: true, uri: "some uri", url: "some url", username: "some username"} @valid_attrs %{begins_on: "2010-04-17 14:00:00.000000Z", description: "some description", ends_on: "2010-04-17 14:00:00.000000Z", title: "some title"} @update_attrs %{begins_on: "2011-05-18 15:01:01.000000Z", description: "some updated description", ends_on: "2011-05-18 15:01:01.000000Z", title: "some updated title"} @invalid_attrs %{begins_on: nil, description: nil, ends_on: nil, title: nil} @@ -44,11 +42,12 @@ defmodule Eventos.EventsTest do end test "create_event/1 with valid data creates a event" do - {:ok, account} = Accounts.create_account(@account_valid_attrs) + actor = actor_fixture() category = category_fixture() address = address_fixture() - valid_attrs = Map.put(@event_valid_attrs, :organizer_account_id, account.id) - valid_attrs = valid_attrs + valid_attrs = @event_valid_attrs + |> Map.put(:organizer_actor, actor) + |> Map.put(:organizer_actor_id, actor.id) |> Map.put(:category_id, category.id) |> Map.put(:address_id, address.id) assert {:ok, %Event{} = event} = Events.create_event(valid_attrs) @@ -90,68 +89,6 @@ defmodule Eventos.EventsTest do end end - describe "event_requests" do - alias Eventos.Events.Request - - @valid_attrs %{state: 42} - @update_attrs %{state: 43} - @invalid_attrs %{state: nil} - - def event_request_fixture(attrs \\ %{}) do - event = event_fixture() - valid_attrs = Map.put(@valid_attrs, :event_id, event.id) - {:ok, event_request} = - attrs - |> Enum.into(valid_attrs) - |> Events.create_request() - - event_request - end - - test "list_event_requests/0 returns all event_requests" do - event_request = event_request_fixture() - assert Events.list_requests() == [event_request] - end - - test "get_request!/1 returns the event_request with given id" do - event_request = event_request_fixture() - assert Events.get_request!(event_request.id) == event_request - end - - test "create_request/1 with valid data creates a event_request" do - assert {:ok, %Request{} = event_request} = Events.create_request(@valid_attrs) - assert event_request.state == 42 - end - - test "create_request/1 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = Events.create_request(@invalid_attrs) - end - - test "update_event_request/2 with valid data updates the event_request" do - event_request = event_request_fixture() - assert {:ok, event_request} = Events.update_request(event_request, @update_attrs) - assert %Request{} = event_request - assert event_request.state == 43 - end - - test "update_event_request/2 with invalid data returns error changeset" do - event_request = event_request_fixture() - assert {:error, %Ecto.Changeset{}} = Events.update_request(event_request, @invalid_attrs) - assert event_request == Events.get_request!(event_request.id) - end - - test "delete_event_request/1 deletes the event_request" do - event_request = event_request_fixture() - assert {:ok, %Request{}} = Events.delete_request(event_request) - assert_raise Ecto.NoResultsError, fn -> Events.get_request!(event_request.id) end - end - - test "change_event_request/1 returns a event_request changeset" do - event_request = event_request_fixture() - assert %Ecto.Changeset{} = Events.change_request(event_request) - end - end - describe "categories" do alias Eventos.Events.Category @@ -276,9 +213,9 @@ defmodule Eventos.EventsTest do def participant_fixture(attrs \\ %{}) do event = event_fixture() - account = account_fixture() + actor = actor_fixture() valid_attrs = Map.put(@valid_attrs, :event_id, event.id) - valid_attrs = Map.put(valid_attrs, :account_id, account.id) + valid_attrs = Map.put(valid_attrs, :actor_id, actor.id) {:ok, participant} = attrs |> Enum.into(valid_attrs) @@ -298,10 +235,10 @@ defmodule Eventos.EventsTest do # end test "create_participant/1 with valid data creates a participant" do - account = account_fixture() + actor = actor_fixture() event = event_fixture() valid_attrs = Map.put(@valid_attrs, :event_id, event.id) - valid_attrs = Map.put(valid_attrs, :account_id, account.id) + valid_attrs = Map.put(valid_attrs, :actor_id, actor.id) assert {:ok, %Participant{} = participant} = Events.create_participant(valid_attrs) assert participant.role == 42 end @@ -333,68 +270,6 @@ defmodule Eventos.EventsTest do end end - describe "requests" do - alias Eventos.Events.Request - - @valid_attrs %{state: 42} - @update_attrs %{state: 43} - @invalid_attrs %{state: nil} - - def request_fixture(attrs \\ %{}) do - event = event_fixture() - valid_attrs = Map.put(@valid_attrs, :event_id, event.id) - {:ok, request} = - attrs - |> Enum.into(valid_attrs) - |> Events.create_request() - - request - end - - test "list_requests/0 returns all requests" do - request = request_fixture() - assert Events.list_requests() == [request] - end - - test "get_request!/1 returns the request with given id" do - request = request_fixture() - assert Events.get_request!(request.id) == request - end - - test "create_request/1 with valid data creates a request" do - assert {:ok, %Request{} = request} = Events.create_request(@valid_attrs) - assert request.state == 42 - end - - test "create_request/1 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = Events.create_request(@invalid_attrs) - end - - test "update_request/2 with valid data updates the request" do - request = request_fixture() - assert {:ok, request} = Events.update_request(request, @update_attrs) - assert %Request{} = request - assert request.state == 43 - end - - test "update_request/2 with invalid data returns error changeset" do - request = request_fixture() - assert {:error, %Ecto.Changeset{}} = Events.update_request(request, @invalid_attrs) - assert request == Events.get_request!(request.id) - end - - test "delete_request/1 deletes the request" do - request = request_fixture() - assert {:ok, %Request{}} = Events.delete_request(request) - assert_raise Ecto.NoResultsError, fn -> Events.get_request!(request.id) end - end - - test "change_request/1 returns a request changeset" do - request = request_fixture() - assert %Ecto.Changeset{} = Events.change_request(request) - end - end - describe "sessions" do alias Eventos.Events.Session @@ -544,33 +419,32 @@ defmodule Eventos.EventsTest do describe "comments" do alias Eventos.Events.Comment - @valid_attrs %{text: "some text", url: "some url"} - @update_attrs %{text: "some updated text", url: "some updated url"} + @valid_attrs %{text: "some text"} + @update_attrs %{text: "some updated text"} @invalid_attrs %{text: nil, url: nil} - def comment_fixture(attrs \\ %{}) do - {:ok, comment} = - attrs - |> Enum.into(@valid_attrs) - |> Events.create_comment() - - comment + def comment_fixture() do + insert(:comment) end test "list_comments/0 returns all comments" do comment = comment_fixture() - assert Events.list_comments() == [comment] + comments = Events.list_comments() + assert comments = [comment] end test "get_comment!/1 returns the comment with given id" do comment = comment_fixture() - assert Events.get_comment!(comment.id) == comment + comment_fetched = Events.get_comment!(comment.id) + assert comment_fetched = comment end test "create_comment/1 with valid data creates a comment" do - assert {:ok, %Comment{} = comment} = Events.create_comment(@valid_attrs) + actor = actor_fixture() + comment_data = Map.merge(@valid_attrs, %{actor_id: actor.id}) + assert {:ok, %Comment{} = comment} = Events.create_comment(comment_data) assert comment.text == "some text" - assert comment.url == "some url" + assert comment.actor_id == actor.id end test "create_comment/1 with invalid data returns error changeset" do @@ -582,13 +456,13 @@ defmodule Eventos.EventsTest do assert {:ok, comment} = Events.update_comment(comment, @update_attrs) assert %Comment{} = comment assert comment.text == "some updated text" - assert comment.url == "some updated url" end test "update_comment/2 with invalid data returns error changeset" do comment = comment_fixture() assert {:error, %Ecto.Changeset{}} = Events.update_comment(comment, @invalid_attrs) - assert comment == Events.get_comment!(comment.id) + comment_fetched = Events.get_comment!(comment.id) + assert comment = comment_fetched end test "delete_comment/1 deletes the comment" do diff --git a/test/eventos/service/activitypub/activitypub_test.exs b/test/eventos/service/activitypub/activitypub_test.exs index 6b480d9eb..f7656dc6f 100644 --- a/test/eventos/service/activitypub/activitypub_test.exs +++ b/test/eventos/service/activitypub/activitypub_test.exs @@ -5,30 +5,30 @@ defmodule Eventos.Service.Activitypub.ActivitypubTest do import Eventos.Factory alias Eventos.Events - alias Eventos.Accounts.Account + alias Eventos.Actors.Actor alias Eventos.Service.ActivityPub alias Eventos.Activity - describe "fetching account from it's url" do - test "returns an account" do - assert {:ok, %Account{username: "tcit@framapiaf.org"} = account} = ActivityPub.make_account_from_nickname("tcit@framapiaf.org") + describe "fetching actor from it's url" do + test "returns an actor" do + assert {:ok, %Actor{preferred_username: "tcit", domain: "framapiaf.org"} = actor} = ActivityPub.make_actor_from_nickname("tcit@framapiaf.org") end end describe "create activities" do test "removes doubled 'to' recipients" do - account = insert(:account) + actor = insert(:actor) {:ok, activity} = ActivityPub.create(%{ to: ["user1", "user1", "user2"], - actor: account, + actor: actor, context: "", object: %{} }) assert activity.data["to"] == ["user1", "user2"] - assert activity.actor == account.url + assert activity.actor == actor.url assert activity.recipients == ["user1", "user2"] end end @@ -52,7 +52,7 @@ defmodule Eventos.Service.Activitypub.ActivitypubTest do {:ok, delete} = ActivityPub.delete(event) assert delete.data["type"] == "Delete" - assert delete.data["actor"] == event.organizer_account.url + assert delete.data["actor"] == event.organizer_actor.url assert delete.data["object"] == event.url assert Events.get_event_by_url!(event.url) == nil @@ -60,22 +60,22 @@ defmodule Eventos.Service.Activitypub.ActivitypubTest do end describe "update" do - test "it creates an update activity with the new user data" do - account = insert(:account) - account_data = EventosWeb.ActivityPub.UserView.render("account.json", %{account: account}) + test "it creates an update activity with the new actor data" do + actor = insert(:actor) + actor_data = EventosWeb.ActivityPub.ActorView.render("actor.json", %{actor: actor}) {:ok, update} = ActivityPub.update(%{ - actor: account_data["url"], - to: [account.url <> "/followers"], + actor: actor_data["url"], + to: [actor.url <> "/followers"], cc: [], - object: account_data + object: actor_data }) - assert update.data["actor"] == account.url - assert update.data["to"] == [account.url <> "/followers"] - assert update.data["object"]["id"] == account_data["id"] - assert update.data["object"]["type"] == account_data["type"] + assert update.data["actor"] == actor.url + assert update.data["to"] == [actor.url <> "/followers"] + assert update.data["object"]["id"] == actor_data["id"] + assert update.data["object"]["type"] == actor_data["type"] end end end diff --git a/test/eventos/service/web_finger/web_finger_test.exs b/test/eventos/service/web_finger/web_finger_test.exs index 5be1b3344..e22846120 100644 --- a/test/eventos/service/web_finger/web_finger_test.exs +++ b/test/eventos/service/web_finger/web_finger_test.exs @@ -13,46 +13,46 @@ defmodule Eventos.Service.WebFingerTest do describe "incoming webfinger request" do test "works for fqns" do - account = insert(:account) + actor = insert(:actor) {:ok, result} = - WebFinger.webfinger("#{account.username}@#{EventosWeb.Endpoint.host()}", "JSON") + WebFinger.webfinger("#{actor.preferred_username}@#{EventosWeb.Endpoint.host()}", "JSON") assert is_map(result) end test "works for urls" do - account = insert(:account) + actor = insert(:actor) - {:ok, result} = WebFinger.webfinger(account.url, "JSON") + {:ok, result} = WebFinger.webfinger(actor.url, "JSON") assert is_map(result) end end describe "fingering" do - test "a mastodon account" do - account = "tcit@social.tcit.fr" + test "a mastodon actor" do + actor = "tcit@social.tcit.fr" - assert {:ok, %{"subject" => "acct:" <> account, "url" => "https://social.tcit.fr/users/tcit"}} = WebFinger.finger(account) + assert {:ok, %{"subject" => "acct:" <> actor, "url" => "https://social.tcit.fr/users/tcit"}} = WebFinger.finger(actor) end - test "a pleroma account" do - account = "@lain@pleroma.soykaf.com" + test "a pleroma actor" do + actor = "@lain@pleroma.soykaf.com" - assert {:ok, %{"subject" => "acct:" <> account, "url" => "https://pleroma.soykaf.com/users/lain"}} = WebFinger.finger(account) + assert {:ok, %{"subject" => "acct:" <> actor, "url" => "https://pleroma.soykaf.com/users/lain"}} = WebFinger.finger(actor) end - test "a peertube account" do - account = "framasoft@framatube.org" + test "a peertube actor" do + actor = "framasoft@framatube.org" - assert {:ok, %{"subject" => "acct:" <> account, "url" => "https://framatube.org/accounts/framasoft"}} = WebFinger.finger(account) + assert {:ok, %{"subject" => "acct:" <> actor, "url" => "https://framatube.org/accounts/framasoft"}} = WebFinger.finger(actor) end - test "a friendica account" do + test "a friendica actor" do # Hasn't any ActivityPub - account = "lain@squeet.me" + actor = "lain@squeet.me" - assert {:ok, %{"subject" => "acct:" <> account} = data} = WebFinger.finger(account) + assert {:ok, %{"subject" => "acct:" <> actor} = data} = WebFinger.finger(actor) refute Map.has_key?(data, "url") end end diff --git a/test/eventos_web/controllers/account_controller_test.exs b/test/eventos_web/controllers/account_controller_test.exs deleted file mode 100644 index f41049c4d..000000000 --- a/test/eventos_web/controllers/account_controller_test.exs +++ /dev/null @@ -1,60 +0,0 @@ -defmodule EventosWeb.AccountControllerTest do - use EventosWeb.ConnCase - - import Eventos.Factory - - alias Eventos.Accounts - - @create_attrs %{description: "some description", display_name: "some display_name", domain: "some domain", private_key: "some private_key", public_key: "some public_key", suspended: true, uri: "some uri", url: "some url", username: "some username"} - - def fixture(:account) do - {:ok, account} = Accounts.create_account(@create_attrs) - account - end - - setup %{conn: conn} do - account = insert(:account) - user = insert(:user, account: account) - {:ok, conn: conn, user: user} - end - - describe "index" do - test "lists all accounts", %{conn: conn, user: user} do - conn = get conn, account_path(conn, :index) - assert hd(json_response(conn, 200)["data"])["username"] == user.account.username - end - end - - describe "delete account" do - setup [:create_account] - - test "deletes own account", %{conn: conn, user: user} do - conn = auth_conn(conn, user) - conn = delete conn, account_path(conn, :delete, user.account) - assert response(conn, 204) - assert_error_sent 404, fn -> - get conn, account_path(conn, :show, user.account) - end - end - - test "deletes other account", %{conn: conn, account: account, user: user} do - conn = auth_conn(conn, user) - conn = delete conn, account_path(conn, :delete, account) - assert response(conn, 401) - conn = get conn, account_path(conn, :show, account) - assert response(conn, 200) - end - end - - defp create_account(_) do - account = fixture(:account) - {:ok, account: account} - end - - defp auth_conn(conn, %Eventos.Accounts.User{} = user) do - {:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user) - conn - |> put_req_header("authorization", "Bearer #{token}") - |> put_req_header("accept", "application/json") - end -end diff --git a/test/eventos_web/controllers/activity_pub_controller_test.exs b/test/eventos_web/controllers/activity_pub_controller_test.exs index 976a9191f..bf285e8f8 100644 --- a/test/eventos_web/controllers/activity_pub_controller_test.exs +++ b/test/eventos_web/controllers/activity_pub_controller_test.exs @@ -1,44 +1,42 @@ defmodule EventosWeb.ActivityPubControllerTest do use EventosWeb.ConnCase import Eventos.Factory - alias EventosWeb.ActivityPub.{AccountView, ObjectView} - alias Eventos.{Repo, Accounts, Accounts.Account} + alias EventosWeb.ActivityPub.{ActorView, ObjectView} + alias Eventos.{Repo, Actors, Actors.Actor} alias Eventos.Activity import Logger describe "/@:username" do - test "it returns a json representation of the account", %{conn: conn} do - account = insert(:account) + test "it returns a json representation of the actor", %{conn: conn} do + actor = insert(:actor) conn = conn |> put_req_header("accept", "application/activity+json") - |> get("/@#{account.username}") + |> get("/@#{actor.preferred_username}") - account = Accounts.get_account!(account.id) + actor = Actors.get_actor!(actor.id) - assert json_response(conn, 200) == AccountView.render("account.json", %{account: account}) - Logger.error(inspect AccountView.render("account.json", %{account: account})) + assert json_response(conn, 200) == ActorView.render("actor.json", %{actor: actor}) + Logger.error(inspect ActorView.render("actor.json", %{actor: actor})) end end - describe "/@username/slug" do + describe "/events/uuid" 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}") + |> get("/events/#{event.uuid}") 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 +# describe "/actors/: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!() # @@ -54,7 +52,7 @@ defmodule EventosWeb.ActivityPubControllerTest do # end # end -# describe "/accounts/:nickname/followers" do +# describe "/actors/:nickname/followers" do # test "it returns the followers in a collection", %{conn: conn} do # user = insert(:user) # user_two = insert(:user) diff --git a/test/eventos_web/controllers/actor_controller_test.exs b/test/eventos_web/controllers/actor_controller_test.exs new file mode 100644 index 000000000..c07d9d997 --- /dev/null +++ b/test/eventos_web/controllers/actor_controller_test.exs @@ -0,0 +1,49 @@ +defmodule EventosWeb.ActorControllerTest do + use EventosWeb.ConnCase + + import Eventos.Factory + + alias Eventos.Actors + + setup %{conn: conn} do + actor = insert(:actor) + user = insert(:user, actor: actor) + {:ok, conn: conn, user: user} + end + + describe "index" do + test "lists all actors", %{conn: conn, user: user} do + conn = get conn, actor_path(conn, :index) + assert hd(json_response(conn, 200)["data"])["username"] == user.actor.preferred_username + end + end + +### +# Not possible atm +### +# describe "delete actor" do +# setup [:create_actor] +# +# test "deletes own actor", %{conn: conn, user: user} do +# conn = auth_conn(conn, user) +# conn = delete conn, actor_path(conn, :delete, user.actor) +# assert response(conn, 204) +# assert_error_sent 404, fn -> +# get conn, actor_path(conn, :show, user.actor) +# end +# end +# +# test "deletes other actor", %{conn: conn, actor: actor, user: user} do +# conn = auth_conn(conn, user) +# conn = delete conn, actor_path(conn, :delete, actor) +# assert response(conn, 401) +# conn = get conn, actor_path(conn, :show, actor) +# assert response(conn, 200) +# end +# end + + defp create_actor(_) do + actor = insert(:actor) + {:ok, actor: actor} + end +end diff --git a/test/eventos_web/controllers/address_controller_test.exs b/test/eventos_web/controllers/address_controller_test.exs index 2ecd5c363..7b66c2e42 100644 --- a/test/eventos_web/controllers/address_controller_test.exs +++ b/test/eventos_web/controllers/address_controller_test.exs @@ -16,8 +16,8 @@ defmodule EventosWeb.AddressControllerTest do end setup %{conn: conn} do - account = insert(:account) - user = insert(:user, account: account) + actor = insert(:actor) + user = insert(:user, actor: actor) {:ok, conn: conn, user: user} end @@ -101,11 +101,4 @@ defmodule EventosWeb.AddressControllerTest do defp create_address(_) do {:ok, address: insert(:address)} end - - defp auth_conn(conn, %Eventos.Accounts.User{} = user) do - {:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user) - conn - |> put_req_header("authorization", "Bearer #{token}") - |> put_req_header("accept", "application/json") - end end diff --git a/test/eventos_web/controllers/bot_controller_test.exs b/test/eventos_web/controllers/bot_controller_test.exs index a3170c531..73aad5ac0 100644 --- a/test/eventos_web/controllers/bot_controller_test.exs +++ b/test/eventos_web/controllers/bot_controller_test.exs @@ -1,20 +1,19 @@ defmodule EventosWeb.BotControllerTest do use EventosWeb.ConnCase + import Eventos.Factory + alias Eventos.Actors alias Eventos.Actors.Bot - @create_attrs %{source: "some source", type: "some type"} - @update_attrs %{source: "some updated source", type: "some updated type"} - @invalid_attrs %{source: nil, type: nil} - - def fixture(:bot) do - {:ok, bot} = Actors.create_bot(@create_attrs) - bot - end + @create_attrs %{source: "some source", type: "some type", name: "some name"} + @update_attrs %{source: "some updated source", type: "some updated type", name: "some updated name"} + @invalid_attrs %{source: nil, type: nil, name: nil} setup %{conn: conn} do - {:ok, conn: put_req_header(conn, "accept", "application/json")} + actor = insert(:actor) + user = insert(:user, actor: actor) + {:ok, conn: put_req_header(conn, "accept", "application/json"), user: user} end describe "index" do @@ -25,7 +24,8 @@ defmodule EventosWeb.BotControllerTest do end describe "create bot" do - test "renders bot when data is valid", %{conn: conn} do + test "renders bot when data is valid", %{conn: conn, user: user} do + conn = auth_conn(conn, user) conn = post conn, bot_path(conn, :create), bot: @create_attrs assert %{"id" => id} = json_response(conn, 201)["data"] @@ -36,7 +36,8 @@ defmodule EventosWeb.BotControllerTest do "type" => "some type"} end - test "renders errors when data is invalid", %{conn: conn} do + test "renders errors when data is invalid", %{conn: conn, user: user} do + conn = auth_conn(conn, user) conn = post conn, bot_path(conn, :create), bot: @invalid_attrs assert json_response(conn, 422)["errors"] != %{} end @@ -45,7 +46,8 @@ defmodule EventosWeb.BotControllerTest do describe "update bot" do setup [:create_bot] - test "renders bot when data is valid", %{conn: conn, bot: %Bot{id: id} = bot} do + test "renders bot when data is valid", %{conn: conn, bot: %Bot{id: id} = bot, user: user} do + conn = auth_conn(conn, user) conn = put conn, bot_path(conn, :update, bot), bot: @update_attrs assert %{"id" => ^id} = json_response(conn, 200)["data"] @@ -56,7 +58,8 @@ defmodule EventosWeb.BotControllerTest do "type" => "some updated type"} end - test "renders errors when data is invalid", %{conn: conn, bot: bot} do + test "renders errors when data is invalid", %{conn: conn, bot: bot, user: user} do + conn = auth_conn(conn, user) conn = put conn, bot_path(conn, :update, bot), bot: @invalid_attrs assert json_response(conn, 422)["errors"] != %{} end @@ -65,7 +68,8 @@ defmodule EventosWeb.BotControllerTest do describe "delete bot" do setup [:create_bot] - test "deletes chosen bot", %{conn: conn, bot: bot} do + test "deletes chosen bot", %{conn: conn, bot: bot, user: user} do + conn = auth_conn(conn, user) conn = delete conn, bot_path(conn, :delete, bot) assert response(conn, 204) assert_error_sent 404, fn -> @@ -75,7 +79,7 @@ defmodule EventosWeb.BotControllerTest do end defp create_bot(_) do - bot = fixture(:bot) + bot = insert(:bot) {:ok, bot: bot} end end diff --git a/test/eventos_web/controllers/category_controller_test.exs b/test/eventos_web/controllers/category_controller_test.exs index 4155c0168..c63cf1837 100644 --- a/test/eventos_web/controllers/category_controller_test.exs +++ b/test/eventos_web/controllers/category_controller_test.exs @@ -16,8 +16,8 @@ defmodule EventosWeb.CategoryControllerTest do end setup %{conn: conn} do - account = insert(:account) - user = insert(:user, account: account) + actor = insert(:actor) + user = insert(:user, actor: actor) {:ok, conn: conn, user: user} end @@ -89,11 +89,4 @@ defmodule EventosWeb.CategoryControllerTest do category = fixture(:category) {:ok, category: category} end - - defp auth_conn(conn, %Eventos.Accounts.User{} = user) do - {:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user) - conn - |> put_req_header("authorization", "Bearer #{token}") - |> put_req_header("accept", "application/json") - end end diff --git a/test/eventos_web/controllers/comment_controller_test.exs b/test/eventos_web/controllers/comment_controller_test.exs index 6b532d5ed..e07b995fb 100644 --- a/test/eventos_web/controllers/comment_controller_test.exs +++ b/test/eventos_web/controllers/comment_controller_test.exs @@ -4,39 +4,37 @@ defmodule EventosWeb.CommentControllerTest do 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"} + import Eventos.Factory + + @create_attrs %{text: "some text"} + @update_attrs %{text: "some updated text"} @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 + actor = insert(:actor) + user = insert(:user, actor: actor) + {:ok, conn: put_req_header(conn, "accept", "application/json"), user: user} 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"] + test "renders comment when data is valid", %{conn: conn, user: user} do + conn = auth_conn(conn, user) + actor = insert(:actor) + attrs = Map.merge(@create_attrs, %{actor_id: actor.id}) + conn = post conn, comment_path(conn, :create), comment: attrs + assert %{"uuid" => uuid, "id" => id} = json_response(conn, 201)["data"] - conn = get conn, comment_path(conn, :show, id) + conn = get conn, comment_path(conn, :show, uuid) assert json_response(conn, 200)["data"] == %{ "id" => id, "text" => "some text", - "url" => "some url"} + "uuid" => uuid, + "url" => "#{EventosWeb.Endpoint.url()}/comments/#{uuid}" + } end - test "renders errors when data is invalid", %{conn: conn} do + test "renders errors when data is invalid", %{conn: conn, user: user} do + conn = auth_conn(conn, user) conn = post conn, comment_path(conn, :create), comment: @invalid_attrs assert json_response(conn, 422)["errors"] != %{} end @@ -45,19 +43,25 @@ defmodule EventosWeb.CommentControllerTest do 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"] + test "renders comment when data is valid", %{conn: conn, comment: %Comment{id: id, uuid: uuid} = comment, user: user} do + conn = auth_conn(conn, user) + actor = insert(:actor) + attrs = Map.merge(@update_attrs, %{actor_id: actor.id}) + conn = put conn, comment_path(conn, :update, uuid), comment: attrs + assert %{"uuid" => uuid, "id" => id} = json_response(conn, 200)["data"] - conn = get conn, comment_path(conn, :show, id) + conn = get conn, comment_path(conn, :show, uuid) assert json_response(conn, 200)["data"] == %{ "id" => id, "text" => "some updated text", - "url" => "some updated url"} + "uuid" => uuid, + "url" => "#{EventosWeb.Endpoint.url()}/comments/#{uuid}" + } end - test "renders errors when data is invalid", %{conn: conn, comment: comment} do - conn = put conn, comment_path(conn, :update, comment), comment: @invalid_attrs + test "renders errors when data is invalid", %{conn: conn, comment: comment, user: user} do + conn = auth_conn(conn, user) + conn = put conn, comment_path(conn, :update, comment.uuid), comment: @invalid_attrs assert json_response(conn, 422)["errors"] != %{} end end @@ -65,17 +69,18 @@ defmodule EventosWeb.CommentControllerTest do describe "delete comment" do setup [:create_comment] - test "deletes chosen comment", %{conn: conn, comment: comment} do - conn = delete conn, comment_path(conn, :delete, comment) + test "deletes chosen comment", %{conn: conn, comment: %Comment{uuid: uuid} = comment, user: user} do + conn = auth_conn(conn, user) + conn = delete conn, comment_path(conn, :delete, uuid) assert response(conn, 204) assert_error_sent 404, fn -> - get conn, comment_path(conn, :show, comment) + get conn, comment_path(conn, :show, uuid) end end end defp create_comment(_) do - comment = fixture(:comment) + comment = insert(:comment) {:ok, comment: comment} end end diff --git a/test/eventos_web/controllers/event_controller_test.exs b/test/eventos_web/controllers/event_controller_test.exs index d302367c3..7176fa862 100644 --- a/test/eventos_web/controllers/event_controller_test.exs +++ b/test/eventos_web/controllers/event_controller_test.exs @@ -21,8 +21,8 @@ defmodule EventosWeb.EventControllerTest do end setup %{conn: conn} do - account = insert(:account) - user = insert(:user, account: account) + actor = insert(:actor) + user = insert(:user, actor: actor) {:ok, conn: conn, user: user} end @@ -35,30 +35,21 @@ defmodule EventosWeb.EventControllerTest do describe "create event" do test "renders event when data is valid", %{conn: conn, user: user} do - attrs = Map.put(@create_attrs, :organizer_account_id, user.account.id) + attrs = Map.put(@create_attrs, :organizer_actor_id, user.actor.id) attrs = Map.put(attrs, :address, @create_address_attrs) category = insert(:category) attrs = Map.put(attrs, :category_id, category.id) conn = auth_conn(conn, user) conn = post conn, event_path(conn, :create), event: attrs - assert %{"id" => id} = json_response(conn, 201)["data"] + assert %{"uuid" => uuid} = json_response(conn, 201)["data"] - conn = get conn, event_path(conn, :show, id) + conn = get conn, event_path(conn, :show, uuid) assert %{ "begins_on" => "2010-04-17T14:00:00Z", "description" => "some description", "ends_on" => "2010-04-17T14:00:00Z", "title" => "some title", - "group" => nil, - "organizer" => %{ - "description" => nil, - "display_name" => nil, - "domain" => nil, - "suspended" => false, - "uri" => "https://", - "url" => "https://", - }, "participants" => [], "address" => %{"addressCountry" => "some addressCountry", "addressLocality" => "some addressLocality", "addressRegion" => "some addressRegion", "floor" => "some floor", "geom" => %{"data" => %{"latitude" => -20.0, "longitude" => 30.0}, "type" => "point"}, "postalCode" => "some postalCode", "streetAddress" => "some streetAddress"} } = json_response(conn, 200)["data"] @@ -66,7 +57,7 @@ defmodule EventosWeb.EventControllerTest do test "renders errors when data is invalid", %{conn: conn, user: user} do conn = auth_conn(conn, user) - attrs = Map.put(@invalid_attrs, :organizer_account_id, user.account.id) + attrs = Map.put(@invalid_attrs, :organizer_actor_id, user.actor.id) attrs = Map.put(attrs, :address, @create_address_attrs) conn = post conn, event_path(conn, :create), event: attrs assert json_response(conn, 422)["errors"] != %{} @@ -76,9 +67,9 @@ defmodule EventosWeb.EventControllerTest do describe "export event" do setup [:create_event] - test "renders ics export of event", %{conn: conn, event: %Event{id: id} = event, user: user} do + test "renders ics export of event", %{conn: conn, event: %Event{uuid: uuid} = event, user: user} do conn = auth_conn(conn, user) - conn = get conn, event_path(conn, :export_to_ics, id) + conn = get conn, event_path(conn, :export_to_ics, uuid) exported_event = ICalendar.export_event(event) assert exported_event == response(conn, 200) end @@ -87,38 +78,29 @@ defmodule EventosWeb.EventControllerTest do describe "update event" do setup [:create_event] - test "renders event when data is valid", %{conn: conn, event: %Event{id: id} = event, user: user} do + test "renders event when data is valid", %{conn: conn, event: %Event{uuid: uuid} = event, user: user} do conn = auth_conn(conn, user) address = address_fixture() - attrs = Map.put(@update_attrs, :organizer_account_id, user.account.id) + attrs = Map.put(@update_attrs, :organizer_actor_id, user.actor.id) attrs = Map.put(attrs, :address_id, address.id) - conn = put conn, event_path(conn, :update, event), event: attrs - assert %{"id" => ^id} = json_response(conn, 200)["data"] + conn = put conn, event_path(conn, :update, uuid), event: attrs + assert %{"uuid" => uuid} = json_response(conn, 200)["data"] - conn = get conn, event_path(conn, :show, id) + conn = get conn, event_path(conn, :show, uuid) assert %{ "begins_on" => "2011-05-18T15:01:01Z", "description" => "some updated description", "ends_on" => "2011-05-18T15:01:01Z", "title" => "some updated title", - "group" => nil, - "organizer" => %{ - "description" => nil, - "display_name" => nil, - "domain" => nil, - "suspended" => false, - "uri" => "https://", - "url" => "https://", - }, "participants" => [], "address" => %{"addressCountry" => "My Country", "addressLocality" => "My Locality", "addressRegion" => "My Region", "floor" => "Myfloor", "geom" => %{"data" => %{"latitude" => 30.0, "longitude" => -90.0}, "type" => "point"}, "postalCode" => "My Postal Code", "streetAddress" => "My Street Address"} } = json_response(conn, 200)["data"] end - test "renders errors when data is invalid", %{conn: conn, event: event, user: user} do + test "renders errors when data is invalid", %{conn: conn, event: %Event{uuid: uuid} = event, user: user} do conn = auth_conn(conn, user) - attrs = Map.put(@invalid_attrs, :organizer_account_id, user.account.id) - conn = put conn, event_path(conn, :update, event), event: attrs + attrs = Map.put(@invalid_attrs, :organizer_actor_id, user.actor.id) + conn = put conn, event_path(conn, :update, uuid), event: attrs assert json_response(conn, 422)["errors"] != %{} end end @@ -126,26 +108,18 @@ defmodule EventosWeb.EventControllerTest do describe "delete event" do setup [:create_event] - test "deletes chosen event", %{conn: conn, event: event, user: user} do + test "deletes chosen event", %{conn: conn, event: %Event{uuid: uuid} = event, user: user} do conn = auth_conn(conn, user) - conn = delete conn, event_path(conn, :delete, event) + conn = delete conn, event_path(conn, :delete, uuid) assert response(conn, 204) - assert_error_sent 404, fn -> - get conn, event_path(conn, :show, event) - end + conn = get conn, event_path(conn, :show, uuid) + assert response(conn, 404) end end defp create_event(_) do - account = insert(:account) - event = insert(:event, organizer_account: account) - {:ok, event: event, account: account} - end - - defp auth_conn(conn, %Eventos.Accounts.User{} = user) do - {:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user) - conn - |> put_req_header("authorization", "Bearer #{token}") - |> put_req_header("accept", "application/json") + actor = insert(:actor) + event = insert(:event, organizer_actor: actor) + {:ok, event: event, actor: actor} end end diff --git a/test/eventos_web/controllers/group_controller_test.exs b/test/eventos_web/controllers/group_controller_test.exs deleted file mode 100644 index 6b4813037..000000000 --- a/test/eventos_web/controllers/group_controller_test.exs +++ /dev/null @@ -1,109 +0,0 @@ -defmodule EventosWeb.GroupControllerTest do - use EventosWeb.ConnCase - - import Eventos.Factory - - alias Eventos.Groups - alias Eventos.Groups.Group - - @create_attrs %{description: "some description", suspended: true, title: "some title", uri: "some uri", url: "some url"} - @update_attrs %{description: "some updated description", suspended: false, title: "some updated title", uri: "some updated uri", url: "some updated url"} - @invalid_attrs %{description: nil, suspended: nil, title: nil, uri: nil, url: nil} - - def fixture(:group) do - {:ok, group} = Groups.create_group(@create_attrs) - group - end - - setup %{conn: conn} do - account = insert(:account) - user = insert(:user, account: account) - {:ok, conn: conn, user: user} - end - - describe "index" do - test "lists all groups", %{conn: conn} do - conn = get conn, group_path(conn, :index) - assert json_response(conn, 200)["data"] == [] - end - end - - describe "create group" do - test "renders group when data is valid", %{conn: conn, user: user} do - conn = auth_conn(conn, user) - conn = post conn, group_path(conn, :create), group: @create_attrs - assert %{"id" => id} = json_response(conn, 201)["data"] - - conn = get conn, group_path(conn, :show, id) - assert json_response(conn, 200)["data"] == %{ - "id" => id, - "description" => "some description", - "suspended" => true, - "title" => "some title", - "uri" => "h", - "url" => "h", - "events" => [], - "members" => [] - } - end - - test "renders errors when data is invalid", %{conn: conn, user: user} do - conn = auth_conn(conn, user) - conn = post conn, group_path(conn, :create), group: @invalid_attrs - assert json_response(conn, 422)["errors"] != %{} - end - end - - describe "update group" do - setup [:create_group] - - test "renders group when data is valid", %{conn: conn, group: %Group{id: id} = group, user: user} do - conn = auth_conn(conn, user) - conn = put conn, group_path(conn, :update, group), group: @update_attrs - assert %{"id" => ^id} = json_response(conn, 200)["data"] - - conn = get conn, group_path(conn, :show, id) - assert json_response(conn, 200)["data"] == %{ - "id" => id, - "description" => "some updated description", - "suspended" => false, - "title" => "some updated title", - "uri" => "some updated uri", - "url" => "some updated url", - "events" => [], - "members" => [] - } - end - - test "renders errors when data is invalid", %{conn: conn, group: group, user: user} do - conn = auth_conn(conn, user) - conn = put conn, group_path(conn, :update, group), group: @invalid_attrs - assert json_response(conn, 422)["errors"] != %{} - end - end - - describe "delete group" do - setup [:create_group] - - test "deletes chosen group", %{conn: conn, group: group, user: user} do - conn = auth_conn(conn, user) - conn = delete conn, group_path(conn, :delete, group) - assert response(conn, 204) - assert_error_sent 404, fn -> - get conn, group_path(conn, :show, group) - end - end - end - - defp create_group(_) do - group = fixture(:group) - {:ok, group: group} - end - - defp auth_conn(conn, %Eventos.Accounts.User{} = user) do - {:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user) - conn - |> put_req_header("authorization", "Bearer #{token}") - |> put_req_header("accept", "application/json") - end -end diff --git a/test/eventos_web/controllers/session_controller_test.exs b/test/eventos_web/controllers/session_controller_test.exs index bae187c5b..58c062509 100644 --- a/test/eventos_web/controllers/session_controller_test.exs +++ b/test/eventos_web/controllers/session_controller_test.exs @@ -16,9 +16,9 @@ defmodule EventosWeb.SessionControllerTest do end setup %{conn: conn} do - account = insert(:account) - user = insert(:user, account: account) - event = insert(:event, organizer_account: account) + actor = insert(:actor) + user = insert(:user, actor: actor) + event = insert(:event, organizer_actor: actor) {:ok, conn: conn, user: user, event: event} end @@ -37,7 +37,7 @@ defmodule EventosWeb.SessionControllerTest do conn = post conn, session_path(conn, :create), session: attrs assert %{"id" => id} = json_response(conn, 201)["data"] - conn = get conn, "/api/events/" <> Integer.to_string(event_id) <> "/sessions" + conn = get conn, session_path(conn, :show_sessions_for_event, event.uuid) assert hd(json_response(conn, 200)["data"])["id"] == id conn = get conn, session_path(conn, :show, id) @@ -107,11 +107,4 @@ defmodule EventosWeb.SessionControllerTest do session = insert(:session) {:ok, session: session} end - - defp auth_conn(conn, %Eventos.Accounts.User{} = user) do - {:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user) - conn - |> put_req_header("authorization", "Bearer #{token}") - |> put_req_header("accept", "application/json") - end end diff --git a/test/eventos_web/controllers/tag_controller_test.exs b/test/eventos_web/controllers/tag_controller_test.exs index 8f4dc2bcb..8718cc002 100644 --- a/test/eventos_web/controllers/tag_controller_test.exs +++ b/test/eventos_web/controllers/tag_controller_test.exs @@ -16,8 +16,8 @@ defmodule EventosWeb.TagControllerTest do end setup %{conn: conn} do - account = insert(:account) - user = insert(:user, account: account) + actor = insert(:actor) + user = insert(:user, actor: actor) {:ok, conn: conn, user: user} end @@ -85,11 +85,4 @@ defmodule EventosWeb.TagControllerTest do tag = fixture(:tag) {:ok, tag: tag} end - - defp auth_conn(conn, %Eventos.Accounts.User{} = user) do - {:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user) - conn - |> put_req_header("authorization", "Bearer #{token}") - |> put_req_header("accept", "application/json") - end end diff --git a/test/eventos_web/controllers/track_controller_test.exs b/test/eventos_web/controllers/track_controller_test.exs index 90b815210..56fb0810d 100644 --- a/test/eventos_web/controllers/track_controller_test.exs +++ b/test/eventos_web/controllers/track_controller_test.exs @@ -16,9 +16,9 @@ defmodule EventosWeb.TrackControllerTest do end setup %{conn: conn} do - account = insert(:account) - user = insert(:user, account: account) - event = insert(:event, organizer_account: account) + actor = insert(:actor) + user = insert(:user, actor: actor) + event = insert(:event, organizer_actor: actor) {:ok, conn: conn, user: user, event: event} end @@ -94,11 +94,4 @@ defmodule EventosWeb.TrackControllerTest do track = insert(:track) {:ok, track: track} end - - defp auth_conn(conn, %Eventos.Accounts.User{} = user) do - {:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user) - conn - |> put_req_header("authorization", "Bearer #{token}") - |> put_req_header("accept", "application/json") - end end diff --git a/test/eventos_web/controllers/user_controller_test.exs b/test/eventos_web/controllers/user_controller_test.exs index ceca47fed..5430e8fee 100644 --- a/test/eventos_web/controllers/user_controller_test.exs +++ b/test/eventos_web/controllers/user_controller_test.exs @@ -3,21 +3,21 @@ defmodule EventosWeb.UserControllerTest do import Eventos.Factory - alias Eventos.Accounts - alias Eventos.Accounts.User + alias Eventos.Actors + alias Eventos.Actors.User @create_attrs %{email: "foo@bar.tld", password: "some password_hash", username: "some username"} # @update_attrs %{email: "foo@fighters.tld", password: "some updated password_hash", username: "some updated username"} @invalid_attrs %{email: "not an email", password: nil, username: nil} def fixture(:user) do - {:ok, user} = Accounts.create_user(@create_attrs) + {:ok, user} = Actors.create_user(@create_attrs) user end setup %{conn: conn} do - account = insert(:account) - user = insert(:user, account: account) + actor = insert(:actor) + user = insert(:user, actor: actor) {:ok, conn: conn, user: user} end @@ -32,20 +32,20 @@ defmodule EventosWeb.UserControllerTest do describe "create user" do test "renders user when data is valid", %{conn: conn} do conn = post conn, user_path(conn, :create), @create_attrs - assert %{"user" => %{"id" => id, "account" => %{"avatar_url" => avatar_url}}} = json_response(conn, 201) + assert %{"user" => %{"id" => id, "actor" => %{"avatar" => avatar_url}}} = json_response(conn, 201) assert id > 0 assert avatar_url == nil end test "renders errors when data is invalid", %{conn: conn} do conn = post conn, user_path(conn, :create), @invalid_attrs - assert json_response(conn, 400)["msg"] != %{} + assert json_response(conn, 422)["errors"] != %{} end test "renders user with avatar when email is valid", %{conn: conn} do attrs = %{email: "contact@framasoft.org", password: "some password_hash", username: "framasoft"} conn = post conn, user_path(conn, :create), attrs - assert %{"user" => %{"id" => id, "account" => %{"avatar_url" => avatar_url}}} = json_response(conn, 201) + assert %{"user" => %{"id" => id, "actor" => %{"avatar" => avatar_url}}} = json_response(conn, 201) assert id > 0 assert avatar_url == "https://secure.gravatar.com/avatar/68b2910a6bb84a482d920e1057533100?default=404" end @@ -88,11 +88,4 @@ defmodule EventosWeb.UserControllerTest do user = insert(:user) {:ok, user: user} end - - defp auth_conn(conn, %User{} = user) do - {:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user) - conn - |> put_req_header("authorization", "Bearer #{token}") - |> put_req_header("accept", "application/json") - end end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 7df4085ca..0723cb3da 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -23,6 +23,13 @@ defmodule EventosWeb.ConnCase do # The default endpoint for testing @endpoint EventosWeb.Endpoint + + def auth_conn(%Plug.Conn{} = conn, %Eventos.Actors.User{} = user) do + {:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user) + conn + |> Plug.Conn.put_req_header("authorization", "Bearer #{token}") + |> Plug.Conn.put_req_header("accept", "application/json") + end end end diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 38c7cf419..127cf5f83 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -38,7 +38,7 @@ defmodule Eventos.DataCase do @doc """ A helper that transform changeset errors to a map of messages. - assert {:error, changeset} = Accounts.create_user(%{password: "short"}) + assert {:error, changeset} = Actors.create_user(%{password: "short"}) assert "password is too short" in errors_on(changeset).password assert %{password: ["password is too short"]} = errors_on(changeset) diff --git a/test/support/factory.ex b/test/support/factory.ex index 2d15551aa..67eef5e8d 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -15,13 +15,17 @@ defmodule Eventos.Factory do end def actor_factory do - {:ok, {_, pubkey}} = RsaEx.generate_keypair("4096") - username = sequence("thomas") + key = :public_key.generate_key({:rsa, 2048, 65537}) + entry = :public_key.pem_entry_encode(:RSAPrivateKey, key) + pem = :public_key.pem_encode([entry]) |> String.trim_trailing() + + + preferred_username = sequence("thomas") %Eventos.Actors.Actor{ - preferred_username: username, + preferred_username: preferred_username, domain: nil, - public_key: pubkey, - url: EventosWeb.Endpoint.url() <> "/@#{username}" + keys: pem, + url: EventosWeb.Endpoint.url() <> "/@#{preferred_username}" } end @@ -45,6 +49,15 @@ defmodule Eventos.Factory do } end + def comment_factory do + %Eventos.Events.Comment{ + text: "My Comment", + actor: build(:actor), + event: build(:event), + uuid: Ecto.UUID.generate(), + } + end + def event_factory do actor = build(:actor) slug = sequence("my-event") @@ -58,7 +71,7 @@ defmodule Eventos.Factory do organizer_actor: actor, category: build(:category), address: build(:address), - url: EventosWeb.Endpoint.url() <> "/@" <> actor.username <> "/" <> slug + url: "#{EventosWeb.Endpoint.url()}/@#{actor.url}/#{Ecto.UUID.generate()}" } end @@ -77,14 +90,12 @@ defmodule Eventos.Factory do } end - def group_factory do - username = sequence("My Group") - %Eventos.Actors.Actor{ - preferred_username: username, - summary: "My group", - suspended: false, - url: EventosWeb.Endpoint.url() <> "/@#{username}", - type: "Group", + def bot_factory do + %Eventos.Actors.Bot{ + source: "https://mysource.tld/feed.ics", + type: "ics", + user: build(:user), + actor: build(:actor), } end end