Skip to content

Commit

Permalink
feat(ex/skate_web): add MBTA AUP (#2174)
Browse files Browse the repository at this point in the history
* feat(ex/skate_web/controllers): add `SkateWeb.Redirect`

* feat(ex/skate_web/router): add MBTA AUP redirect
  • Loading branch information
firestack authored Aug 8, 2023
1 parent 6a91ed3 commit 0bc586f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
4 changes: 3 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ config :skate,
min_length: 5
}
]
}
},
acceptable_use_policy:
"https://mbta.sharepoint.com/:b:/s/CTD/ER2vUlgzH_xMuNTwKZHsvb0B80yH5XIQFLX7A4e6crycMA?e=GwAHOn"

config :skate, Schedule.CacheFile, cache_filename: nil

Expand Down
29 changes: 29 additions & 0 deletions lib/skate_web/controllers/redirect_controller.ex
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
4 changes: 4 additions & 0 deletions lib/skate_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ defmodule SkateWeb.Router do
plug :put_secure_browser_headers
end

scope "/docs", SkateWeb do
get "/agency-policies/aup", Redirect, external: :aup
end

scope "/auth", SkateWeb do
pipe_through([:redirect_prod_http, :accepts_html, :browser])

Expand Down
46 changes: 46 additions & 0 deletions test/skate_web/controllers/redirect_controller_test.exs
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
8 changes: 8 additions & 0 deletions test/skate_web/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ defmodule SkateWeb.RouterTest do
assert html_response(conn, 200) =~ "<div class=\"window-title\">Features</div>"
end
end

describe "GET /docs" do
test "GET /agency-policies/aup, should return :skate, :acceptable_use_policy", %{conn: conn} do
conn = get(conn, "/docs/agency-policies/aup")

assert redirected_to(conn) == Application.get_env(:skate, :acceptable_use_policy)
end
end
end

0 comments on commit 0bc586f

Please sign in to comment.