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