From fa5692c8934a43f17788d1bb7076601745e98533 Mon Sep 17 00:00:00 2001 From: Ismael Bejarano Date: Wed, 28 Aug 2024 14:43:07 -0300 Subject: [PATCH] Fix invalid link (#2363) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix invalid link * Fix CI failure * Fix assertion * Update lib/ask_web/controllers/short_link_controller.ex Co-authored-by: Matías García Isaía * Apply fixes from PR --------- Co-authored-by: Matías García Isaía --- .../controllers/short_link_controller.ex | 21 +++++++++++++------ .../short_link_controller_test.exs | 8 +++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/ask_web/controllers/short_link_controller.ex b/lib/ask_web/controllers/short_link_controller.ex index cf41c2575..fafd88cc8 100644 --- a/lib/ask_web/controllers/short_link_controller.ex +++ b/lib/ask_web/controllers/short_link_controller.ex @@ -9,6 +9,21 @@ defmodule AskWeb.ShortLinkController do ShortLink |> Repo.get_by(hash: hash) + resolve_link(conn, link) + end + + # Copied from Plug.Adapters.Cowboy.Conn + defp split_path(path) do + segments = :binary.split(path, "/", [:global]) + for segment <- segments, segment != "", do: segment + end + + defp resolve_link(conn, nil) do + conn + |> send_resp(:not_found, "Link not found") + end + + defp resolve_link(conn, link) do conn = conn |> assign(:skip_auth, true) @@ -33,10 +48,4 @@ defmodule AskWeb.ShortLinkController do AskWeb.Endpoint.call(conn, []) end - - # Copied from Plug.Adapters.Cowboy.Conn - defp split_path(path) do - segments = :binary.split(path, "/", [:global]) - for segment <- segments, segment != "", do: segment - end end diff --git a/test/ask_web/controllers/short_link_controller_test.exs b/test/ask_web/controllers/short_link_controller_test.exs index e687c1cd9..481733d7b 100644 --- a/test/ask_web/controllers/short_link_controller_test.exs +++ b/test/ask_web/controllers/short_link_controller_test.exs @@ -14,6 +14,14 @@ defmodule AskWeb.ShortLinkControllerTest do {:ok, conn: conn, user: user} end + test "return 404 for an invalid link", %{ + conn: conn + } do + conn = get(conn, short_link_path(conn, :access, "invalid-link")) + + assert response(conn, 404) == "Link not found" + end + test "render surveys if the link specifies that endpoint even if there is no current user", %{ conn: conn, user: user