Make tests great again !

(Also use only one field for public/private key pem)
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-06-14 17:25:55 +02:00
parent 32596c3624
commit ca36dd12e2
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
43 changed files with 498 additions and 656 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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}))

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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"],

View File

@ -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

View File

@ -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 =

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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