Skip to content

Commit

Permalink
Fix invalid link (#2363)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Apply fixes from PR

---------

Co-authored-by: Matías García Isaía <[email protected]>
  • Loading branch information
ismaelbej and matiasgarciaisaia authored Aug 28, 2024
1 parent 98ba225 commit fa5692c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/ask_web/controllers/short_link_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
8 changes: 8 additions & 0 deletions test/ask_web/controllers/short_link_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fa5692c

Please sign in to comment.