From 355dce9dd7b6a5cbc1f84ea9b12a1fc2d10826a9 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 5 Mar 2019 10:13:19 +0100 Subject: [PATCH] Add and fix tests Signed-off-by: Thomas Citharel --- lib/mobilizon/events/events.ex | 4 +-- .../controllers/activity_pub_controller.ex | 2 +- .../controllers/fallback_controller.ex | 2 +- .../controllers/page_controller.ex | 14 +++++++-- lib/mobilizon_web/views/error_view.ex | 5 ++- .../views/json_ld/object_view.ex | 2 +- .../activity_pub_controller_test.exs | 12 +++---- .../controllers/page_controller_test.exs | 31 ++++++++++++++++++- 8 files changed, 55 insertions(+), 17 deletions(-) diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index f9fd63770..1855435f3 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -1028,9 +1028,9 @@ defmodule Mobilizon.Events do end end - def get_cached_comment_full_by_uuid("comment_" <> uuid) do + def get_cached_comment_full_by_uuid(uuid) do Cachex.fetch(:activity_pub, "comment_" <> uuid, fn "comment_" <> uuid -> - with %Comment{} = comment <- Events.get_comment_full_from_uuid(uuid) do + with %Comment{} = comment <- get_comment_full_from_uuid(uuid) do {:commit, comment} else _ -> {:ignore, nil} diff --git a/lib/mobilizon_web/controllers/activity_pub_controller.ex b/lib/mobilizon_web/controllers/activity_pub_controller.ex index 870554108..2f49d16ff 100644 --- a/lib/mobilizon_web/controllers/activity_pub_controller.ex +++ b/lib/mobilizon_web/controllers/activity_pub_controller.ex @@ -37,7 +37,7 @@ defmodule MobilizonWeb.ActivityPubController do """ @spec event(Plug.Conn.t(), map()) :: Plug.Conn.t() def event(conn, %{"uuid" => uuid}) do - with {status, %Event{}} = event when status in [:ok, :commit] <- + with {status, %Event{} = event} when status in [:ok, :commit] <- Events.get_cached_event_full_by_uuid(uuid), true <- event.visibility in [:public, :unlisted] do conn diff --git a/lib/mobilizon_web/controllers/fallback_controller.ex b/lib/mobilizon_web/controllers/fallback_controller.ex index 6817b9cb2..cf85e7ed5 100644 --- a/lib/mobilizon_web/controllers/fallback_controller.ex +++ b/lib/mobilizon_web/controllers/fallback_controller.ex @@ -24,6 +24,6 @@ defmodule MobilizonWeb.FallbackController do conn |> put_status(:not_found) |> put_view(MobilizonWeb.ErrorView) - |> render(:"404") + |> render("404.html") end end diff --git a/lib/mobilizon_web/controllers/page_controller.ex b/lib/mobilizon_web/controllers/page_controller.ex index a3e755a2e..7d00df912 100644 --- a/lib/mobilizon_web/controllers/page_controller.ex +++ b/lib/mobilizon_web/controllers/page_controller.ex @@ -10,6 +10,7 @@ defmodule MobilizonWeb.PageController do alias Mobilizon.Events.{Event, Comment} plug(:put_layout, false) + action_fallback(MobilizonWeb.FallbackController) def index(conn, _params) do conn @@ -23,9 +24,12 @@ defmodule MobilizonWeb.PageController do with {status, %Actor{} = actor} when status in [:ok, :commit] <- Actors.get_cached_local_actor_by_name(name) do render_with_meta(conn, actor) + else + _ -> {:error, :not_found} end - "activity+json" -> + # "activity-json" matches "application/activity+json" inside our config + "activity-json" -> MobilizonWeb.ActivityPubController.call(conn, :actor) _ -> @@ -40,9 +44,11 @@ defmodule MobilizonWeb.PageController do Events.get_cached_event_full_by_uuid(uuid), true <- event.visibility in [:public, :unlisted] do render_with_meta(conn, event) + else + _ -> {:error, :not_found} end - "activity+json" -> + "activity-json" -> MobilizonWeb.ActivityPubController.call(conn, :event) _ -> @@ -59,9 +65,11 @@ defmodule MobilizonWeb.PageController do # TODO : Make comments maybe restricted # true <- comment.public do render_with_meta(conn, comment) + else + _ -> {:error, :not_found} end - "activity+json" -> + "activity-json" -> MobilizonWeb.ActivityPubController.call(conn, :comment) _ -> diff --git a/lib/mobilizon_web/views/error_view.ex b/lib/mobilizon_web/views/error_view.ex index 42714317e..c4d6b3ce4 100644 --- a/lib/mobilizon_web/views/error_view.ex +++ b/lib/mobilizon_web/views/error_view.ex @@ -25,7 +25,10 @@ defmodule MobilizonWeb.ErrorView do # In case no render clause matches or no # template is found, let's render it as 500 - def template_not_found(_template, assigns) do + def template_not_found(template, assigns) do + require Logger + Logger.error("Template not found") + Logger.error(inspect(template)) render("500.html", assigns) end end diff --git a/lib/mobilizon_web/views/json_ld/object_view.ex b/lib/mobilizon_web/views/json_ld/object_view.ex index 7d2686056..2c231a13a 100644 --- a/lib/mobilizon_web/views/json_ld/object_view.ex +++ b/lib/mobilizon_web/views/json_ld/object_view.ex @@ -1,7 +1,7 @@ defmodule MobilizonWeb.JsonLD.ObjectView do use MobilizonWeb, :view - alias Mobilizon.Events.{Event, Comment} + alias Mobilizon.Events.Event alias Mobilizon.Actors.Actor alias Mobilizon.Addresses.Address alias MobilizonWeb.JsonLD.ObjectView diff --git a/test/mobilizon_web/controllers/activity_pub_controller_test.exs b/test/mobilizon_web/controllers/activity_pub_controller_test.exs index 0fe234fbc..096cce389 100644 --- a/test/mobilizon_web/controllers/activity_pub_controller_test.exs +++ b/test/mobilizon_web/controllers/activity_pub_controller_test.exs @@ -13,13 +13,17 @@ defmodule MobilizonWeb.ActivityPubControllerTest do alias Mobilizon.Service.ActivityPub.Utils use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + setup do + conn = build_conn() |> put_req_header("accept", "application/activity+json") + {:ok, conn: conn} + end + describe "/@:preferred_username" do 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("/@#{actor.preferred_username}") actor = Actors.get_actor!(actor.id) @@ -34,7 +38,6 @@ defmodule MobilizonWeb.ActivityPubControllerTest do conn = conn - |> put_req_header("accept", "application/activity+json") |> get("/events/#{event.uuid}") assert json_response(conn, 200) == @@ -46,7 +49,6 @@ defmodule MobilizonWeb.ActivityPubControllerTest do conn = conn - |> put_req_header("accept", "application/activity+json") |> get("/events/#{event.uuid}") assert json_response(conn, 404) @@ -59,7 +61,6 @@ defmodule MobilizonWeb.ActivityPubControllerTest do conn = conn - |> put_req_header("accept", "application/activity+json") |> get("/comments/#{comment.uuid}") assert json_response(conn, 200) == @@ -87,7 +88,6 @@ defmodule MobilizonWeb.ActivityPubControllerTest do conn = conn |> assign(:valid_signature, true) - |> put_req_header("content-type", "application/activity+json") |> post("/inbox", data) assert "ok" == json_response(conn, 200) @@ -104,7 +104,6 @@ defmodule MobilizonWeb.ActivityPubControllerTest do conn = conn - |> put_req_header("accept", "application/activity+json") |> get("/@#{actor.preferred_username}/outbox") assert response(conn, 200) =~ comment.text @@ -116,7 +115,6 @@ defmodule MobilizonWeb.ActivityPubControllerTest do conn = conn - |> put_req_header("accept", "application/activity+json") |> get("/@#{actor.preferred_username}/outbox") assert response(conn, 200) =~ event.title diff --git a/test/mobilizon_web/controllers/page_controller_test.exs b/test/mobilizon_web/controllers/page_controller_test.exs index 37516df23..ef2d96d54 100644 --- a/test/mobilizon_web/controllers/page_controller_test.exs +++ b/test/mobilizon_web/controllers/page_controller_test.exs @@ -2,14 +2,43 @@ defmodule MobilizonWeb.PageControllerTest do use MobilizonWeb.ConnCase import Mobilizon.Factory + setup do + conn = build_conn() |> put_req_header("accept", "text/html") + {:ok, conn: conn} + end + test "GET /", %{conn: conn} do conn = get(conn, "/") assert html_response(conn, 200) end - test "GET /@actor", %{conn: conn} do + test "GET /@actor with existing actor", %{conn: conn} do actor = insert(:actor) conn = get(conn, "/@#{actor.preferred_username}") assert html_response(conn, 200) end + + test "GET /@actor with not existing actor", %{conn: conn} do + conn = get(conn, "/@notexisting") + assert html_response(conn, 404) + end + + test "GET /events/:uuid", %{conn: conn} do + event = insert(:event) + conn = get(conn, "/events/#{event.uuid}") + assert html_response(conn, 200) + end + + test "GET /events/:uuid with not existing event", %{conn: conn} do + conn = get(conn, "/events/not_existing_event") + assert html_response(conn, 404) + end + + test "GET /events/:uuid with event not public", %{conn: conn} do + event = insert(:event, visibility: :restricted) + conn = get(conn, "/events/#{event.uuid}") + assert html_response(conn, 404) + end + + # TODO: Comments end