From e5c15c7166d271911f6df81e710f77ad45a673f1 Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Thu, 17 Nov 2022 15:36:58 -0500 Subject: [PATCH] Finish 1.7 upgrade with ~p --- lib/live_beats_web.ex | 4 +- .../components/core_components.ex | 22 ++-- lib/live_beats_web/components/layouts.ex | 10 +- .../components/layouts/root.html.heex | 16 +-- lib/live_beats_web/controllers/error_html.ex | 2 +- .../controllers/redirect_controller.ex | 2 +- lib/live_beats_web/controllers/user_auth.ex | 8 +- lib/live_beats_web/live/profile_live.ex | 3 +- .../profile_live/upload_form_component.ex | 4 +- lib/live_beats_web/live/settings_live.ex | 1 - .../presence/presence_client_test.exs | 120 ------------------ .../controllers/error_html_test.exs | 14 ++ .../controllers/github_callbacks_test.exs | 10 +- .../controllers/redirect_controller_test.exs | 2 +- .../controllers/user_auth_test.exs | 4 +- test/live_beats_web/views/error_view_test.exs | 14 -- .../live_beats_web/views/layout_view_test.exs | 8 -- test/live_beats_web/views/page_view_test.exs | 3 - test/support/conn_case.ex | 1 + 19 files changed, 57 insertions(+), 191 deletions(-) delete mode 100644 test/live_beats/presence/presence_client_test.exs create mode 100644 test/live_beats_web/controllers/error_html_test.exs delete mode 100644 test/live_beats_web/views/error_view_test.exs delete mode 100644 test/live_beats_web/views/layout_view_test.exs delete mode 100644 test/live_beats_web/views/page_view_test.exs diff --git a/lib/live_beats_web.ex b/lib/live_beats_web.ex index 58372fd..da8af77 100644 --- a/lib/live_beats_web.ex +++ b/lib/live_beats_web.ex @@ -29,6 +29,7 @@ defmodule LiveBeatsWeb do import Plug.Conn import LiveBeatsWeb.Gettext alias LiveBeatsWeb.Router.Helpers, as: Routes + unquote(verified_routes()) end end @@ -79,7 +80,7 @@ defmodule LiveBeatsWeb do def router do quote do - use Phoenix.Router + use Phoenix.Router, helpers: false import Plug.Conn import Phoenix.Controller @@ -106,6 +107,7 @@ defmodule LiveBeatsWeb do import LiveBeatsWeb.Gettext alias LiveBeatsWeb.Router.Helpers, as: Routes alias Phoenix.LiveView.JS + unquote(verified_routes()) end end diff --git a/lib/live_beats_web/components/core_components.ex b/lib/live_beats_web/components/core_components.ex index 1fcd418..29c67cd 100644 --- a/lib/live_beats_web/components/core_components.ex +++ b/lib/live_beats_web/components/core_components.ex @@ -1,7 +1,7 @@ defmodule LiveBeatsWeb.CoreComponents do use Phoenix.Component + use LiveBeatsWeb, :verified_routes - alias LiveBeatsWeb.Router.Helpers, as: Routes alias Phoenix.LiveView.JS alias LiveBeats.Accounts @@ -10,18 +10,20 @@ defmodule LiveBeatsWeb.CoreComponents do def home_path(nil = _current_user), do: "/" def home_path(%Accounts.User{} = current_user), do: profile_path(current_user) - def profile_path(current_user_or_profile, action \\ :show) - - def profile_path(username, action) when is_binary(username) do - Routes.profile_path(LiveBeatsWeb.Endpoint, action, username) + def profile_upload_path(%Accounts.User{} = user) do + ~p"/#{user.username}/songs/new" end - def profile_path(%Accounts.User{} = current_user, action) do - profile_path(current_user.username, action) + def profile_path(username) when is_binary(username) do + unverified_path(LiveBeatsWeb.Endpoint, LiveBeatsWeb.Router, ~p"/#{username}") end - def profile_path(%MediaLibrary.Profile{} = profile, action) do - profile_path(profile.username, action) + def profile_path(%Accounts.User{} = current_user) do + profile_path(current_user.username) + end + + def profile_path(%MediaLibrary.Profile{} = profile) do + profile_path(profile.username) end slot :inner_block @@ -176,7 +178,7 @@ defmodule LiveBeatsWeb.CoreComponents do <:subtitle>@<%= @current_user.username %> <:link navigate={profile_path(@current_user)}>View Profile - <:link navigate={Routes.settings_path(LiveBeatsWeb.Endpoint, :edit)}Settings + <:link navigate={~p"/profile/settings"}Settings """ attr :id, :string, required: true diff --git a/lib/live_beats_web/components/layouts.ex b/lib/live_beats_web/components/layouts.ex index 8ee2c52..5c318db 100644 --- a/lib/live_beats_web/components/layouts.ex +++ b/lib/live_beats_web/components/layouts.ex @@ -1,8 +1,6 @@ defmodule LiveBeatsWeb.Layouts do use LiveBeatsWeb, :html - alias LiveBeatsWeb.Endpoint - embed_templates "layouts/*" attr :id, :string @@ -54,7 +52,7 @@ defmodule LiveBeatsWeb.Layouts do <.link - navigate={Routes.settings_path(Endpoint, :edit)} + navigate={~p"/profile/settings"} class={ "text-gray-700 hover:text-gray-900 group flex items-center px-2 py-2 text-sm font-medium rounded-md #{if @active_tab == :settings, do: "bg-gray-200", else: "hover:bg-gray-50"}" } @@ -68,7 +66,7 @@ defmodule LiveBeatsWeb.Layouts do <% else %> <.link - navigate={Routes.sign_in_path(Endpoint, :index)} + navigate={~p"/signin"} class="text-gray-700 hover:text-gray-900 hover:bg-gray-50 group flex items-center px-2 py-2 text-sm font-medium rounded-md" > <%= @current_user.name %> <:subtitle>@<%= @current_user.username %> <:link navigate={profile_path(@current_user)}>View Profile - <:link navigate={Routes.settings_path(Endpoint, :edit)}>Settings - <:link href={Routes.o_auth_callback_path(Endpoint, :sign_out)} method={:delete}>Sign out + <:link navigate={~p"/profile/settings"}>Settings + <:link href={~p"/signout"} method={:delete}>Sign out """ end diff --git a/lib/live_beats_web/components/layouts/root.html.heex b/lib/live_beats_web/components/layouts/root.html.heex index c83932c..3e472e8 100644 --- a/lib/live_beats_web/components/layouts/root.html.heex +++ b/lib/live_beats_web/components/layouts/root.html.heex @@ -4,16 +4,12 @@ - <%= csrf_meta_tag() %> - <%= live_title_tag(assigns[:page_title] || "LiveBeats", suffix: " · Phoenix Framework") %> - - + + <.live_title suffix=" · Phoenix Framework"> + <%= assigns[:page_title] || "LiveBeats" %> + + + <%= @inner_content %> diff --git a/lib/live_beats_web/controllers/error_html.ex b/lib/live_beats_web/controllers/error_html.ex index fe78605..0460dc4 100644 --- a/lib/live_beats_web/controllers/error_html.ex +++ b/lib/live_beats_web/controllers/error_html.ex @@ -1,7 +1,7 @@ defmodule LiveBeatsWeb.ErrorHTML do use LiveBeatsWeb, :html - def template_not_found(template, _assigns) do + def render(template, _assigns) do Phoenix.Controller.status_message_from_template(template) end end diff --git a/lib/live_beats_web/controllers/redirect_controller.ex b/lib/live_beats_web/controllers/redirect_controller.ex index 7769cdb..e8ff1a8 100644 --- a/lib/live_beats_web/controllers/redirect_controller.ex +++ b/lib/live_beats_web/controllers/redirect_controller.ex @@ -9,7 +9,7 @@ defmodule LiveBeatsWeb.RedirectController do if conn.assigns.current_user do LiveBeatsWeb.UserAuth.redirect_if_user_is_authenticated(conn, []) else - redirect(conn, to: Routes.sign_in_path(conn, :index)) + redirect(conn, to: ~p"/signin") end end end diff --git a/lib/live_beats_web/controllers/user_auth.ex b/lib/live_beats_web/controllers/user_auth.ex index 93a328e..26303c3 100644 --- a/lib/live_beats_web/controllers/user_auth.ex +++ b/lib/live_beats_web/controllers/user_auth.ex @@ -1,10 +1,10 @@ defmodule LiveBeatsWeb.UserAuth do + use LiveBeatsWeb, :verified_routes import Plug.Conn import Phoenix.Controller alias Phoenix.LiveView alias LiveBeats.Accounts - alias LiveBeatsWeb.Router.Helpers, as: Routes def on_mount(:current_user, _params, session, socket) do case session do @@ -35,7 +35,7 @@ defmodule LiveBeatsWeb.UserAuth do defp redirect_require_login(socket) do socket |> LiveView.put_flash(:error, "Please sign in") - |> LiveView.redirect(to: Routes.sign_in_path(socket, :index)) + |> LiveView.redirect(to: ~p"/signin") end @doc """ @@ -79,7 +79,7 @@ defmodule LiveBeatsWeb.UserAuth do conn |> renew_session() - |> redirect(to: Routes.sign_in_path(conn, :index)) + |> redirect(to: ~p"/signin") end @doc """ @@ -117,7 +117,7 @@ defmodule LiveBeatsWeb.UserAuth do conn |> put_flash(:error, "You must log in to access this page.") |> maybe_store_return_to() - |> redirect(to: Routes.sign_in_path(conn, :index)) + |> redirect(to: ~p"/signin") |> halt() end end diff --git a/lib/live_beats_web/live/profile_live.ex b/lib/live_beats_web/live/profile_live.ex index 1ee098e..b0cbe60 100644 --- a/lib/live_beats_web/live/profile_live.ex +++ b/lib/live_beats_web/live/profile_live.ex @@ -46,7 +46,7 @@ defmodule LiveBeatsWeb.ProfileLive do <% end %> <%= if @owns_profile? do %> - <.button id="upload-btn" primary patch={profile_path(@current_user, :new)}> + <.button id="upload-btn" primary patch={profile_upload_path(@current_user)}> <.icon name={:upload} />Upload Songs <% end %> @@ -109,7 +109,6 @@ defmodule LiveBeatsWeb.ProfileLive do end def mount(%{"profile_username" => profile_username}, _session, socket) do - IO.inspect(get_connect_params(socket)) %{current_user: current_user} = socket.assigns profile = diff --git a/lib/live_beats_web/live/profile_live/upload_form_component.ex b/lib/live_beats_web/live/profile_live/upload_form_component.ex index e9227c0..4301bda 100644 --- a/lib/live_beats_web/live/profile_live/upload_form_component.ex +++ b/lib/live_beats_web/live/profile_live/upload_form_component.ex @@ -169,10 +169,10 @@ defmodule LiveBeatsWeb.ProfileLive.UploadFormComponent do do: ~H|Something went wrong| defp file_error(%{kind: %Ecto.Changeset{}} = assigns), - do: ~H|<%= @label %>: translate_changeset_errors(@kind) %>| + do: ~H|<%= @label %>: <%= translate_changeset_errors(@kind) %>| defp file_error(%{kind: {msg, opts}} = assigns) when is_binary(msg) and is_list(opts), - do: ~H|<%= @label %>: translate_error(@kind) %>| + do: ~H|<%= @label %>: <%= translate_error(@kind) %>| defp put_stats(socket, entry_ref, %MP3Stat{} = stat) do if changeset = get_changeset(socket, entry_ref) do diff --git a/lib/live_beats_web/live/settings_live.ex b/lib/live_beats_web/live/settings_live.ex index eb27de1..643b99b 100644 --- a/lib/live_beats_web/live/settings_live.ex +++ b/lib/live_beats_web/live/settings_live.ex @@ -98,7 +98,6 @@ defmodule LiveBeatsWeb.SettingsLive do end def mount(_params, _session, socket) do - IO.inspect(get_connect_params(socket)) changeset = Accounts.change_settings(socket.assigns.current_user, %{}) {:ok, assign(socket, changeset: changeset)} end diff --git a/test/live_beats/presence/presence_client_test.exs b/test/live_beats/presence/presence_client_test.exs deleted file mode 100644 index 9665a0f..0000000 --- a/test/live_beats/presence/presence_client_test.exs +++ /dev/null @@ -1,120 +0,0 @@ -defmodule Phoenix.Presence.ClientTest.Presence do - use Phoenix.Presence, - otp_app: :live_beats, - pubsub_server: LiveBeats.PubSub -end - -defmodule Phoenix.Presence.ClientTest do - use ExUnit.Case - - alias Phoenix.Presence.Client.PresenceMock - alias Phoenix.Presence.Client - - @pubsub LiveBeats.PubSub - @client Phoenix.Presence.Client.Mock - @presence Phoenix.Presence.ClientTest.Presence - - @presence_client_opts [client: @client, pubsub: @pubsub, presence: @presence] - - setup tags do - start_supervised!({@presence, []}) - pid = Ecto.Adapters.SQL.Sandbox.start_owner!(LiveBeats.Repo, shared: not tags[:async]) - on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) - - :ok - end - - test "A topic key is added to the topics state when a new process is tracked" do - presence_key = 1 - topic = topic(100) - - {:ok, presence_client} = start_supervised({Client, @presence_client_opts}) - {:ok, presence_process} = start_supervised({PresenceMock, id: presence_key}) - - Phoenix.PubSub.subscribe(@pubsub, topic) - Process.monitor(presence_process) - - PresenceMock.track(presence_client, presence_process, topic, presence_key) - assert_receive %{event: "presence_diff"} - - client_state = :sys.get_state(presence_client) - assert %{topics: %{^topic => %{"1" => [%{phx_ref: _ref}]}}} = client_state - end - - test "topic is removed from the topics state when there is no more presences" do - presence_key = 1 - topic = topic(100) - - {:ok, presence_client} = start_supervised({Client, @presence_client_opts}) - {:ok, presence_process} = start_supervised({PresenceMock, id: presence_key}) - - Phoenix.PubSub.subscribe(@pubsub, topic) - Process.monitor(presence_process) - - PresenceMock.track(presence_client, presence_process, topic, presence_key) - assert Process.alive?(presence_process) - assert_receive %{event: "presence_diff"} - - client_state = :sys.get_state(presence_client) - assert %{topics: %{^topic => %{"1" => [%{phx_ref: _ref}]}}} = client_state - - send(presence_process, :quit) - assert_receive {:DOWN, _ref, :process, ^presence_process, _reason} - - client_state = :sys.get_state(presence_client) - assert %{topics: %{}} = client_state - end - - test "metas are accumulated when there are two presences for the same key" do - presence_key = 1 - topic = topic(100) - - {:ok, presence_client} = start_supervised({Client, @presence_client_opts}) - {:ok, presence_process_1} = start_supervised({PresenceMock, id: presence_key}, id: :mock_1) - {:ok, presence_process_2} = start_supervised({PresenceMock, id: presence_key}, id: :mock_2) - - Phoenix.PubSub.subscribe(@pubsub, topic) - - PresenceMock.track(presence_client, presence_process_1, topic, presence_key, %{m1: :m1}) - assert_receive %{event: "presence_diff"} - - PresenceMock.track(presence_client, presence_process_2, topic, presence_key, %{m2: :m2}) - assert_receive %{event: "presence_diff"} - - client_state = :sys.get_state(presence_client) - - assert %{topics: %{^topic => %{"1" => [%{m1: :m1}, %{m2: :m2}]}}} = client_state - end - - test "Just one meta is deleted when there are two presences for the same key and one leaves" do - presence_key = 1 - topic = topic(100) - - {:ok, presence_client} = start_supervised({Client, @presence_client_opts}) - {:ok, presence_process_1} = start_supervised({PresenceMock, id: presence_key}, id: :mock_1) - {:ok, presence_process_2} = start_supervised({PresenceMock, id: presence_key}, id: :mock_2) - - Phoenix.PubSub.subscribe(@pubsub, topic) - Process.monitor(presence_process_1) - - PresenceMock.track(presence_client, presence_process_1, topic, presence_key, %{m1: :m1}) - assert_receive %{event: "presence_diff"} - - PresenceMock.track(presence_client, presence_process_2, topic, presence_key, %{m2: :m2}) - assert_receive %{event: "presence_diff"} - - client_state = :sys.get_state(presence_client) - assert %{topics: %{^topic => %{"1" => [%{m1: :m1}, %{m2: :m2}]}}} = client_state - - send(presence_process_1, :quit) - assert_receive {:DOWN, _ref, :process, ^presence_process_1, _reason} - assert_receive %{event: "presence_diff"} - - client_state = :sys.get_state(presence_client) - assert %{topics: %{^topic => %{"1" => [%{m2: :m2}]}}} = client_state - end - - defp topic(id) do - "mock_topic:#{id}" - end -end diff --git a/test/live_beats_web/controllers/error_html_test.exs b/test/live_beats_web/controllers/error_html_test.exs new file mode 100644 index 0000000..b014b52 --- /dev/null +++ b/test/live_beats_web/controllers/error_html_test.exs @@ -0,0 +1,14 @@ +defmodule LiveBeatsWeb.ErrorViewTest do + use LiveBeatsWeb.ConnCase, async: true + + # Bring render_to_string/3 for testing custom views + import Phoenix.Template + + test "renders 404.html" do + assert render_to_string(LiveBeatsWeb.ErrorHTML, "404", "html", []) == "Not Found" + end + + test "renders 500.html" do + assert render_to_string(LiveBeatsWeb.ErrorHTML, "500", "html", []) == "Internal Server Error" + end +end diff --git a/test/live_beats_web/controllers/github_callbacks_test.exs b/test/live_beats_web/controllers/github_callbacks_test.exs index 235a111..6bd6a44 100644 --- a/test/live_beats_web/controllers/github_callbacks_test.exs +++ b/test/live_beats_web/controllers/github_callbacks_test.exs @@ -42,7 +42,7 @@ defmodule LiveBeatsWeb.GithubCallbackTest do assert Accounts.get_user_by_email("chris@local.test") == nil - conn = get(conn, Routes.o_auth_callback_path(conn, :new, "github", params)) + conn = get(conn, ~p"/oauth/callbacks/github?#{params}") assert redirected_to(conn, 302) == "/chrismccord" assert %Accounts.User{} = user = Accounts.get_user_by_email("chris@local.test") @@ -53,9 +53,9 @@ defmodule LiveBeatsWeb.GithubCallbackTest do params = %{"code" => "66e1c4202275d071eced", "state" => "invalid"} assert Accounts.list_users(limit: 100) == [] - conn = get(conn, Routes.o_auth_callback_path(conn, :new, "github", params)) + conn = get(conn, ~p"/oauth/callbacks/github?#{params}") - assert get_flash(conn, :error) == + assert Phoenix.Flash.get(conn.assigns.flash, :error) == "We were unable to contact GitHub. Please try again later" assert redirected_to(conn, 302) == "/" @@ -67,9 +67,9 @@ defmodule LiveBeatsWeb.GithubCallbackTest do assert Accounts.list_users(limit: 100) == [] - conn = get(conn, Routes.o_auth_callback_path(conn, :new, "github", params)) + conn = get(conn, ~p"/oauth/callbacks/github?#{params}") - assert get_flash(conn, :error) == "We were unable to contact GitHub. Please try again later" + assert Phoenix.Flash.get(conn.assigns.flash, :error) == "We were unable to contact GitHub. Please try again later" assert redirected_to(conn, 302) == "/" assert Accounts.list_users(limit: 100) == [] end diff --git a/test/live_beats_web/controllers/redirect_controller_test.exs b/test/live_beats_web/controllers/redirect_controller_test.exs index 0a88c4a..016368e 100644 --- a/test/live_beats_web/controllers/redirect_controller_test.exs +++ b/test/live_beats_web/controllers/redirect_controller_test.exs @@ -4,7 +4,7 @@ defmodule LiveBeatsWeb.RedirectControllerTest do test "GET / redirects to signin when not logged in", %{conn: conn} do conn = get(conn, "/") - assert redirected_to(conn, 302) =~ Routes.sign_in_path(conn, :index) + assert redirected_to(conn, 302) =~ ~p"/signin" end test "GET / redirects to profile page when signed in", %{conn: conn} do diff --git a/test/live_beats_web/controllers/user_auth_test.exs b/test/live_beats_web/controllers/user_auth_test.exs index 54baa0a..c888a01 100644 --- a/test/live_beats_web/controllers/user_auth_test.exs +++ b/test/live_beats_web/controllers/user_auth_test.exs @@ -72,7 +72,7 @@ defmodule LiveBeatsWeb.UserAuthTest do test "redirects if user is authenticated", %{conn: conn, user: user} do conn = conn |> assign(:current_user, user) |> UserAuth.redirect_if_user_is_authenticated([]) assert conn.halted - assert redirected_to(conn) == Routes.profile_path(conn, :show, user.username) + assert redirected_to(conn) == LiveBeatsWeb.CoreComponents.profile_path(user) end test "does not redirect if user is not authenticated", %{conn: conn} do @@ -87,7 +87,7 @@ defmodule LiveBeatsWeb.UserAuthTest do conn = conn |> fetch_flash() |> UserAuth.require_authenticated_user([]) assert conn.halted assert redirected_to(conn) - assert get_flash(conn, :error) == "You must log in to access this page." + assert Phoenix.Flash.get(conn.assigns.flash, :error) == "You must log in to access this page." end test "stores the path to redirect to on GET", %{conn: conn} do diff --git a/test/live_beats_web/views/error_view_test.exs b/test/live_beats_web/views/error_view_test.exs deleted file mode 100644 index 7ef394a..0000000 --- a/test/live_beats_web/views/error_view_test.exs +++ /dev/null @@ -1,14 +0,0 @@ -defmodule LiveBeatsWeb.ErrorViewTest do - use LiveBeatsWeb.ConnCase, async: true - - # Bring render/3 and render_to_string/3 for testing custom views - import Phoenix.View - - test "renders 404.html" do - assert render_to_string(LiveBeatsWeb.ErrorView, "404.html", []) == "Not Found" - end - - test "renders 500.html" do - assert render_to_string(LiveBeatsWeb.ErrorView, "500.html", []) == "Internal Server Error" - end -end diff --git a/test/live_beats_web/views/layout_view_test.exs b/test/live_beats_web/views/layout_view_test.exs deleted file mode 100644 index ab0a0e1..0000000 --- a/test/live_beats_web/views/layout_view_test.exs +++ /dev/null @@ -1,8 +0,0 @@ -defmodule LiveBeatsWeb.LayoutViewTest do - use LiveBeatsWeb.ConnCase, async: true - - # When testing helpers, you may want to import Phoenix.HTML and - # use functions such as safe_to_string() to convert the helper - # result into an HTML string. - # import Phoenix.HTML -end diff --git a/test/live_beats_web/views/page_view_test.exs b/test/live_beats_web/views/page_view_test.exs deleted file mode 100644 index 28277bc..0000000 --- a/test/live_beats_web/views/page_view_test.exs +++ /dev/null @@ -1,3 +0,0 @@ -defmodule LiveBeatsWeb.PageViewTest do - use LiveBeatsWeb.ConnCase, async: true -end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index bc1415c..7ab4365 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -23,6 +23,7 @@ defmodule LiveBeatsWeb.ConnCase do using do quote do # Import conveniences for testing with connections + use LiveBeatsWeb, :verified_routes import Plug.Conn import Phoenix.ConnTest import LiveBeatsWeb.ConnCase