-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ex/skate_web): add MBTA AUP (#2174)
* feat(ex/skate_web/controllers): add `SkateWeb.Redirect` * feat(ex/skate_web/router): add MBTA AUP redirect
- Loading branch information
Showing
5 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule SkateWeb.Redirect do | ||
@moduledoc """ | ||
Controller that redirects to the `:external` `opts` when `call/2`-ed. | ||
--- | ||
Reference: https://www.viget.com/articles/how-to-redirect-from-the-phoenix-router/ | ||
""" | ||
import Phoenix.Controller, only: [redirect: 2] | ||
|
||
@spec init(Keyword.t()) :: Keyword.t() | ||
def init([external: _] = opts), do: opts | ||
def init(_default), do: raise("Missing required external: option in redirect") | ||
|
||
@spec call(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t() | ||
def call(conn, external: atom) do | ||
case allowed_url_from_atom(atom) do | ||
{:ok, url} -> redirect(conn, external: url) | ||
{:error, :not_found} -> Plug.Conn.send_resp(conn, :not_found, "URL not found") | ||
end | ||
end | ||
|
||
@spec allowed_url_from_atom(atom) :: {:ok, String.t()} | {:error, :not_found} | ||
|
||
@aup_url Application.compile_env(:skate, :acceptable_use_policy) | ||
defp allowed_url_from_atom(:aup), do: {:ok, @aup_url} | ||
|
||
defp allowed_url_from_atom(_), do: {:error, :not_found} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule SkateWeb.RedirectTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias SkateWeb.Redirect | ||
|
||
defmodule Router do | ||
use Phoenix.Router | ||
|
||
get "/valid", Redirect, external: :aup | ||
get "/valid/nested", Redirect, external: :aup | ||
get "/invalid", Redirect, external: :invalid | ||
get "/exceptional", Redirect, [] | ||
end | ||
|
||
test "an exception is raised when `external` isn't defined" do | ||
assert_raise Plug.Conn.WrapperError, ~R[Missing required external: option in redirect], fn -> | ||
call(Router, :get, "/exceptional") | ||
end | ||
end | ||
|
||
test "route redirected to external route" do | ||
conn = call(Router, :get, "/valid") | ||
|
||
assert Phoenix.ConnTest.redirected_to(conn) == | ||
Application.get_env(:skate, :acceptable_use_policy) | ||
end | ||
|
||
test "nested route redirected to external route" do | ||
conn = call(Router, :get, "/valid/nested") | ||
|
||
assert Phoenix.ConnTest.redirected_to(conn) == | ||
Application.get_env(:skate, :acceptable_use_policy) | ||
end | ||
|
||
test "when atom is not in allow list, should return :not_found" do | ||
conn = call(Router, :get, "/invalid") | ||
|
||
assert Phoenix.ConnTest.response(conn, :not_found) == "URL not found" | ||
end | ||
|
||
defp call(router, verb, path) do | ||
verb | ||
|> Plug.Test.conn(path) | ||
|> router.call(router.init([])) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters