Skip to content

Commit

Permalink
chore: upgrade to Phoenix 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
paulswartz committed Oct 3, 2023
1 parent f74d87a commit b32a038
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 54 deletions.
2 changes: 1 addition & 1 deletion apps/api_web/lib/api_web/swagger_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ defmodule ApiWeb.SwaggerHelpers do
|> String.replace_suffix("Controller", "")
|> String.downcase()

String.to_existing_atom("#{short_name}_path")
String.to_atom("#{short_name}_path")
end

defp attribute_to_json_pointer("id"), do: "/data/{index}/id"
Expand Down
4 changes: 2 additions & 2 deletions apps/api_web/lib/api_web/templates/admin/layout/app.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
<body>
<%= render("navigation.html", assigns) %>

<%= if info = get_flash(@conn, :info) do %>
<%= if info = Phoenix.Flash.get(@flash, :info) do %>
<p class="alert alert-info text-center" role="alert" style="border-radius: 0">
<%= info %>
</p>
<% end %>
<%= if error = get_flash(@conn, :error) do %>
<%= if error = Phoenix.Flash.get(@flash, :error) do %>
<p class="alert alert-danger text-center" role="alert" style="border-radius: 0">
<%= error %>
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
<body>
<%= render("navigation.html", assigns) %>

<%= if info = get_flash(@conn, :success) do %>
<%= if info = Phoenix.Flash.get(@flash, :success) do %>
<div class="alert alert-success text-center" role="alert" style="border-radius: 0">
<div class="container"><%= info %></div>
</div>
<% end %>
<%= if info = get_flash(@conn, :info) do %>
<%= if info = Phoenix.Flash.get(@flash, :info) do %>
<div class="alert alert-info text-center" role="alert" style="border-radius: 0">
<div class="container"><%= info %></div>
</div>
<% end %>
<%= if error = get_flash(@conn, :error) do %>
<%= if error = Phoenix.Flash.get(@flash, :error) do %>
<div class="alert alert-danger text-center" role="alert" style="border-radius: 0">
<div class="container"><%= error %></div>
</div>
Expand Down
16 changes: 10 additions & 6 deletions apps/api_web/lib/api_web/views/route_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@ defmodule ApiWeb.RouteView do
# Override attribute version of type to give the resource type
def type(_, _), do: "route"

def relationships(route, %Plug.Conn{private: %{phoenix_view: __MODULE__}} = conn) do
def relationships(route, conn) do
relationships = super(route, conn)

# only do this include if we're the top-level view, not if we're included
# elsewhere
relationships = super(route, conn)
if Phoenix.Controller.view_module(conn) == __MODULE__ do
include_top_level_relationships(relationships, conn)
else
relationships
end
end

defp include_top_level_relationships(relationships, conn) do
if split_included?("stop", conn) do
stop_id =
case conn.params do
Expand Down Expand Up @@ -91,8 +99,4 @@ defmodule ApiWeb.RouteView do
relationships
end
end

def relationships(route, conn) do
super(route, conn)
end
end
13 changes: 5 additions & 8 deletions apps/api_web/lib/api_web/views/stop_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ defmodule ApiWeb.StopView do

defdelegate recommended_transfers(stop, conn), to: __MODULE__, as: :connecting_stops

def relationships(stop, %Plug.Conn{private: %{phoenix_view: __MODULE__}} = conn) do
# only do this include if we're the top-level view, not if we're included
# somewhere else
def relationships(stop, conn) do
relationships = super(stop, conn)

with true <- split_included?("route", conn),
# only do this include if we're the top-level view, not if we're included
# somewhere else
with __MODULE__ <- Phoenix.Controller.view_module(conn),
true <- split_included?("route", conn),
{:ok, filtered} <- Params.filter_params(conn.params, filters(), conn),
{:ok, route_id} <- Map.fetch(filtered, "route") do
route = State.Route.by_id(route_id)
Expand All @@ -128,10 +129,6 @@ defmodule ApiWeb.StopView do
end
end

def relationships(stop, conn) do
super(stop, conn)
end

def facilities(%{facilities: facilities}, _conn) do
# preloaded
facilities
Expand Down
7 changes: 5 additions & 2 deletions apps/api_web/lib/api_web/web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ defmodule ApiWeb.Web do

def api_controller do
quote location: :keep do
use Phoenix.Controller, namespace: ApiWeb
use Phoenix.Controller,
formats: [{:json, "View"}, {:"json-api", "View"}],
namespace: ApiWeb

use ApiWeb.ApiControllerHelpers
import ApiWeb.ControllerHelpers
alias ApiWeb.Params
Expand Down Expand Up @@ -48,7 +51,7 @@ defmodule ApiWeb.Web do
root: "lib/api_web/templates",
namespace: ApiWeb

import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
import Phoenix.Controller, only: [get_csrf_token: 0, view_module: 1]
use Phoenix.HTML
import ApiWeb.Router.Helpers
import ApiWeb.ErrorHelpers
Expand Down
4 changes: 2 additions & 2 deletions apps/api_web/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule ApiWeb.Mixfile do
aliases: aliases(),
build_embedded: Mix.env() == :prod,
build_path: "../../_build",
compilers: [:phoenix] ++ Mix.compilers() ++ [:phoenix_swagger],
compilers: Mix.compilers() ++ [:phoenix_swagger],
config_path: "../../config/config.exs",
deps: deps(),
deps_path: "../../deps",
Expand Down Expand Up @@ -60,7 +60,7 @@ defmodule ApiWeb.Mixfile do
# Type "mix help deps" for more examples and options
defp deps do
[
{:phoenix, "~> 1.6.15"},
{:phoenix, "~> 1.7"},
{:phoenix_view, "~> 2.0"},
{:phoenix_html, "~> 3.3"},
{:phoenix_live_view, "~> 0.18"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule ApiWeb.Admin.Accounts.KeyControllerTest do
conn = post(form_header(conn), admin_key_path(conn, :create, user))
assert redirected_to(conn) == admin_user_path(conn, :show, user)
assert [key] = ApiAccounts.list_keys_for_user(user)
assert get_flash(conn, :info) =~ key.key
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ key.key
assert key.approved
assert key.created
assert key.requested_date
Expand Down Expand Up @@ -81,7 +81,7 @@ defmodule ApiWeb.Admin.Accounts.KeyControllerTest do
|> put(admin_key_path(base_conn, :approve, user, key))

assert redirected_to(conn) == admin_user_path(conn, :show, user)
assert get_flash(conn, :info)
assert Phoenix.Flash.get(conn.assigns.flash, :info)

key = ApiAccounts.get_key!(key.key)
assert key.approved
Expand Down Expand Up @@ -209,7 +209,7 @@ defmodule ApiWeb.Admin.Accounts.KeyControllerTest do
# ensure there are now two keys
assert [_, _] = keys = ApiAccounts.list_keys_for_user(user)
new_key = Enum.find(keys, &(&1.key != key.key))
assert get_flash(conn, :info) =~ new_key.key
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ new_key.key
end

describe "find_user_by_key/1" do
Expand All @@ -228,7 +228,7 @@ defmodule ApiWeb.Admin.Accounts.KeyControllerTest do
request_path = admin_key_path(conn, :find_user_by_key, %{search: %{key: key_id}})
conn = post(conn, request_path)
assert redirected_to(conn) == expected_path
assert get_flash(conn, :error)
assert Phoenix.Flash.get(conn.assigns.flash, :error)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule ApiWeb.Admin.Accounts.UserControllerTest do

on_exit(fn -> ApiAccounts.Dynamo.delete_all_tables() end)

params = %{email: "admin@mbta.com", role: "administrator", totp_enabled: true}
%{email: "admin@mbta.com", role: "administrator", totp_enabled: true}

{:ok, user} =
ApiAccounts.create_user(%{email: "test@mbta.com", role: "administrator", totp_enabled: true})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule ApiWeb.Admin.SessionControllerTest do
)

assert html_response(conn, 200) =~ "Login"
assert get_flash(conn, :error) != nil
assert Phoenix.Flash.get(conn.assigns.flash, :error) != nil
assert html_response(conn, 200) =~ "Invalid credentials"
end

Expand All @@ -51,7 +51,7 @@ defmodule ApiWeb.Admin.SessionControllerTest do
post(form_header(conn), admin_session_path(conn, :create), user: @unauthorized_user_attrs)

assert html_response(conn, 200) =~ "Login"
assert get_flash(conn, :error) != nil
assert Phoenix.Flash.get(conn.assigns.flash, :error) != nil
assert html_response(conn, 200) =~ "not authorized"
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ defmodule ApiWeb.MfaControllerTest do
)

assert html_response(conn, 200) =~ "TOTP"
assert get_flash(conn, :error) != nil
assert Phoenix.Flash.get(conn.assigns.flash, :error) != nil
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule ApiWeb.ClientPortal.KeyControllerTest do
describe "create" do
test "creates approved key the first time", %{conn: conn, user: user} do
conn = post(conn, key_path(conn, :create))
assert get_flash(conn, :success)
assert Phoenix.Flash.get(conn.assigns.flash, :success)
assert redirected_to(conn) == portal_path(conn, :index)
assert [key] = ApiAccounts.list_keys_for_user(user)
assert key.approved
Expand All @@ -20,7 +20,7 @@ defmodule ApiWeb.ClientPortal.KeyControllerTest do
test "creates a requested key the second time", %{conn: conn, user: user} do
{:ok, _} = ApiAccounts.create_key(user, %{approved: true})
conn = post(conn, key_path(conn, :create))
assert get_flash(conn, :success)
assert Phoenix.Flash.get(conn.assigns.flash, :success)
assert redirected_to(conn) == portal_path(conn, :index)
keys = ApiAccounts.list_keys_for_user(user)
assert Enum.count(keys) == 2
Expand All @@ -33,7 +33,7 @@ defmodule ApiWeb.ClientPortal.KeyControllerTest do
test "doesn't allow more than 1 request per user", %{conn: conn, user: user} do
{:ok, _} = ApiAccounts.create_key(user, %{approved: false})
conn = post(conn, key_path(conn, :create))
assert get_flash(conn, :error)
assert Phoenix.Flash.get(conn.assigns.flash, :error)
assert redirected_to(conn) == portal_path(conn, :index)
assert Enum.count(ApiAccounts.list_keys_for_user(user)) == 1
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule ApiWeb.Portal.SessionControllerTest do
test "shows error for invalid credentials", %{conn: conn} do
conn = post(form_header(conn), session_path(conn, :create), user: @invalid_user_attrs)
assert html_response(conn, 200) =~ "Login"
assert get_flash(conn, :error) != nil
assert Phoenix.Flash.get(conn.assigns.flash, :error) != nil
assert html_response(conn, 200) =~ "Invalid credentials"
end

Expand Down Expand Up @@ -65,7 +65,7 @@ defmodule ApiWeb.Portal.SessionControllerTest do
conn = delete(conn, session_path(conn, :delete))
refute get_session(conn, :user)
assert redirected_to(conn) == session_path(conn, :new)
assert get_flash(conn, :info) =~ ~r"logged out"i
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ ~r"logged out"i
end

test "redirects to 2fa page when user has 2fa enabled", %{conn: conn} do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ defmodule ApiWeb.Portal.UserControllerTest do

page = html_response(conn, 200)
assert page =~ "match"
refute get_flash(conn, :success)
refute Phoenix.Flash.get(conn.assigns.flash, :success)
end

test "updates a users password when valid", %{conn: conn} do
Expand All @@ -132,7 +132,7 @@ defmodule ApiWeb.Portal.UserControllerTest do
|> put(user_path(conn, :update), params)

assert redirected_to(conn) == user_path(conn, :show)
assert get_flash(conn, :success) =~ ~r"password updated"i
assert Phoenix.Flash.get(conn.assigns.flash, :success) =~ ~r"password updated"i
end
end

Expand Down Expand Up @@ -171,7 +171,7 @@ defmodule ApiWeb.Portal.UserControllerTest do

page = html_response(conn, 200)
assert page =~ "already been taken"
refute get_flash(conn, :success)
refute Phoenix.Flash.get(conn.assigns.flash, :success)
end

test "updates account information when valid", %{conn: conn} do
Expand All @@ -186,7 +186,7 @@ defmodule ApiWeb.Portal.UserControllerTest do
|> put(user_path(conn, :update), params)

assert redirected_to(conn) == user_path(conn, :show)
assert get_flash(conn, :success) =~ ~r"account updated"i
assert Phoenix.Flash.get(conn.assigns.flash, :success) =~ ~r"account updated"i
end
end

Expand Down Expand Up @@ -217,7 +217,7 @@ defmodule ApiWeb.Portal.UserControllerTest do
|> form_header()
|> post(user_path(conn, :forgot_password_submit), user: params)

assert get_flash(conn, :info) =~ ~r"check your email"i
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ ~r"check your email"i
assert redirected_to(conn) == portal_path(conn, :landing)
end

Expand All @@ -229,7 +229,7 @@ defmodule ApiWeb.Portal.UserControllerTest do
|> form_header()
|> post(user_path(conn, :forgot_password_submit), user: params)

assert get_flash(conn, :info) =~ ~r"check your email"i
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ ~r"check your email"i
assert redirected_to(conn) == portal_path(conn, :landing)
end
end
Expand Down Expand Up @@ -293,7 +293,7 @@ defmodule ApiWeb.Portal.UserControllerTest do
|> post(user_path(conn, :reset_password_submit, token: token), user: params)

assert redirected_to(conn) == session_path(conn, :new)
assert get_flash(conn, :success) =~ "success"
assert Phoenix.Flash.get(conn.assigns.flash, :success) =~ "success"
assert {:ok, _} = ApiAccounts.authenticate(%{email: user.email, password: params.password})
end

Expand Down
2 changes: 1 addition & 1 deletion apps/api_web/test/api_web/event_stream_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule ApiWeb.EventStreamTest do

conn =
conn
|> put_private(:phoenix_view, ApiWeb.PredictionView)
|> Phoenix.Controller.put_view(ApiWeb.PredictionView)
|> Map.put(:params, %{"route" => "1"})

{:ok, %{conn: conn}}
Expand Down
5 changes: 5 additions & 0 deletions apps/api_web/test/api_web/views/stop_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ defmodule ApiWeb.StopViewTest do
vehicle_type: 3
}

setup %{conn: conn} do
conn = Phoenix.Controller.put_view(conn, StopView)
{:ok, %{conn: conn}}
end

test "can do a basic rendering", %{conn: conn} do
rendered = render("index.json-api", data: @stop, conn: conn)["data"]
assert rendered["type"] == "stop"
Expand Down
5 changes: 3 additions & 2 deletions apps/api_web/test/api_web/views/trip_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ defmodule ApiWeb.TripViewTest do

@stop %Stop{id: "stop1"}

setup do
setup %{conn: conn} do
State.Trip.new_state([@trip])
State.CommuterRailOccupancy.new_state([@occupancy])
State.Schedule.new_state([@schedule])
State.Stop.new_state([@stop])
:ok
conn = Phoenix.Controller.put_view(conn, ApiWeb.TripView)
{:ok, %{conn: conn}}
end

test "render returns JSONAPI", %{conn: conn} do
Expand Down
Loading

0 comments on commit b32a038

Please sign in to comment.