Better tests

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-10-09 17:01:45 +02:00
parent e454d0d470
commit 4eb51567c3
3 changed files with 44 additions and 23 deletions

View File

@ -522,19 +522,6 @@ defmodule Eventos.Actors do
alias Eventos.Actors.Member alias Eventos.Actors.Member
@doc """
Returns the list of members.
## Examples
iex> list_members()
[%Member{}, ...]
"""
def list_members do
Repo.all(Member)
end
@doc """ @doc """
Gets a single member. Gets a single member.

View File

@ -14,6 +14,8 @@ defmodule EventosWeb.ActivityPubController do
conn conn
|> put_resp_header("content-type", "application/activity+json") |> put_resp_header("content-type", "application/activity+json")
|> json(ActorView.render("actor.json", %{actor: actor})) |> json(ActorView.render("actor.json", %{actor: actor}))
else
nil -> {:error, :not_found}
end end
end end
@ -25,9 +27,7 @@ defmodule EventosWeb.ActivityPubController do
|> json(ObjectView.render("event.json", %{event: event})) |> json(ObjectView.render("event.json", %{event: event}))
else else
false -> false ->
conn {:error, :not_found}
|> put_status(404)
|> json("Not found")
end end
end end
@ -86,19 +86,35 @@ defmodule EventosWeb.ActivityPubController do
json(conn, "ok") json(conn, "ok")
end 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 def inbox(conn, params) do
headers = Enum.into(conn.req_headers, %{}) headers = Enum.into(conn.req_headers, %{})
if String.contains?(headers["signature"] || "", params["actor"]) do if String.contains?(headers["signature"], params["actor"]) do
Logger.info("Signature error") Logger.info(
Logger.info("Could not validate #{params["actor"]}") "Signature validation error for: #{params["actor"]}, make sure you are forwarding the HTTP Host header!"
)
Logger.info(inspect(conn.req_headers)) 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 end
json(conn, "ok") json(conn, "error")
end
def errors(conn, {:error, :not_found}) do
conn
|> put_status(404)
|> json("Not found")
end end
def errors(conn, _e) do def errors(conn, _e) do

View File

@ -140,6 +140,24 @@ defmodule Eventos.EventsTest do
event1 = insert(:event, organizer_actor: actor) event1 = insert(:event, organizer_actor: actor)
assert {:ok, [event_found], 2} = Events.get_events_for_actor(actor, 1, 1) assert {:ok, [event_found], 2} = Events.get_events_for_actor(actor, 1, 1)
end 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 end
describe "categories" do describe "categories" do