Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid link #2363

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +15 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't even have moved this function, so the diff was even more clear :)


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