From 4eb51567c3a995c3d19a8e728f7ccac4e40c562f Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 9 Oct 2018 17:01:45 +0200 Subject: [PATCH] Better tests Signed-off-by: Thomas Citharel --- lib/eventos/actors/actors.ex | 13 ------- .../controllers/activity_pub_controller.ex | 36 +++++++++++++------ test/eventos/events/events_test.exs | 18 ++++++++++ 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/lib/eventos/actors/actors.ex b/lib/eventos/actors/actors.ex index 77d4042e1..3e1e579c9 100644 --- a/lib/eventos/actors/actors.ex +++ b/lib/eventos/actors/actors.ex @@ -522,19 +522,6 @@ defmodule Eventos.Actors do alias Eventos.Actors.Member - @doc """ - Returns the list of members. - - ## Examples - - iex> list_members() - [%Member{}, ...] - - """ - def list_members do - Repo.all(Member) - end - @doc """ Gets a single member. diff --git a/lib/eventos_web/controllers/activity_pub_controller.ex b/lib/eventos_web/controllers/activity_pub_controller.ex index 3789d9c4a..81fb0a99e 100644 --- a/lib/eventos_web/controllers/activity_pub_controller.ex +++ b/lib/eventos_web/controllers/activity_pub_controller.ex @@ -14,6 +14,8 @@ defmodule EventosWeb.ActivityPubController do conn |> put_resp_header("content-type", "application/activity+json") |> json(ActorView.render("actor.json", %{actor: actor})) + else + nil -> {:error, :not_found} end end @@ -25,9 +27,7 @@ defmodule EventosWeb.ActivityPubController do |> json(ObjectView.render("event.json", %{event: event})) else false -> - conn - |> put_status(404) - |> json("Not found") + {:error, :not_found} end end @@ -86,19 +86,35 @@ defmodule EventosWeb.ActivityPubController do json(conn, "ok") end + # only accept relayed Creates + def inbox(conn, %{"type" => "Create"} = params) do + Logger.info( + "Signature missing or not from author, relayed Create message, fetching object from source" + ) + + ActivityPub.fetch_object_from_url(params["object"]["id"]) + + json(conn, "ok") + end + def inbox(conn, params) do headers = Enum.into(conn.req_headers, %{}) - if String.contains?(headers["signature"] || "", params["actor"]) do - Logger.info("Signature error") - Logger.info("Could not validate #{params["actor"]}") + if String.contains?(headers["signature"], params["actor"]) do + Logger.info( + "Signature validation error for: #{params["actor"]}, make sure you are forwarding the HTTP Host header!" + ) + Logger.info(inspect(conn.req_headers)) - else - Logger.info("Signature not from author, relayed message, fetching from source") - ActivityPub.fetch_event_from_url(params["object"]["id"]) end - json(conn, "ok") + json(conn, "error") + end + + def errors(conn, {:error, :not_found}) do + conn + |> put_status(404) + |> json("Not found") end def errors(conn, _e) do diff --git a/test/eventos/events/events_test.exs b/test/eventos/events/events_test.exs index 2623509ad..cacde8327 100644 --- a/test/eventos/events/events_test.exs +++ b/test/eventos/events/events_test.exs @@ -140,6 +140,24 @@ defmodule Eventos.EventsTest do event1 = insert(:event, organizer_actor: actor) assert {:ok, [event_found], 2} = Events.get_events_for_actor(actor, 1, 1) end + + test "get_event_by_url/1 with valid url", %{actor: actor, event: event} do + assert event = Events.get_event_by_url(event.url) + end + + test "get_event_by_url/1 with bad url", %{actor: actor, event: event} do + refute event == Events.get_event_by_url("not valid") + end + + test "get_event_by_url!/1 with valid url", %{actor: actor, event: event} do + assert event = Events.get_event_by_url!(event.url) + end + + test "get_event_by_url!/1 with bad url", %{actor: actor, event: event} do + assert_raise Ecto.NoResultsError, fn -> + Events.get_event_by_url!("not valid") + end + end end describe "categories" do