Finish 1.7 upgrade with ~p

This commit is contained in:
Chris McCord 2022-11-17 15:36:58 -05:00
parent 7bbce31bb9
commit e5c15c7166
19 changed files with 57 additions and 191 deletions

View file

@ -29,6 +29,7 @@ defmodule LiveBeatsWeb do
import Plug.Conn import Plug.Conn
import LiveBeatsWeb.Gettext import LiveBeatsWeb.Gettext
alias LiveBeatsWeb.Router.Helpers, as: Routes alias LiveBeatsWeb.Router.Helpers, as: Routes
unquote(verified_routes())
end end
end end
@ -79,7 +80,7 @@ defmodule LiveBeatsWeb do
def router do def router do
quote do quote do
use Phoenix.Router use Phoenix.Router, helpers: false
import Plug.Conn import Plug.Conn
import Phoenix.Controller import Phoenix.Controller
@ -106,6 +107,7 @@ defmodule LiveBeatsWeb do
import LiveBeatsWeb.Gettext import LiveBeatsWeb.Gettext
alias LiveBeatsWeb.Router.Helpers, as: Routes alias LiveBeatsWeb.Router.Helpers, as: Routes
alias Phoenix.LiveView.JS alias Phoenix.LiveView.JS
unquote(verified_routes())
end end
end end

View file

@ -1,7 +1,7 @@
defmodule LiveBeatsWeb.CoreComponents do defmodule LiveBeatsWeb.CoreComponents do
use Phoenix.Component use Phoenix.Component
use LiveBeatsWeb, :verified_routes
alias LiveBeatsWeb.Router.Helpers, as: Routes
alias Phoenix.LiveView.JS alias Phoenix.LiveView.JS
alias LiveBeats.Accounts alias LiveBeats.Accounts
@ -10,18 +10,20 @@ defmodule LiveBeatsWeb.CoreComponents do
def home_path(nil = _current_user), do: "/" def home_path(nil = _current_user), do: "/"
def home_path(%Accounts.User{} = current_user), do: profile_path(current_user) def home_path(%Accounts.User{} = current_user), do: profile_path(current_user)
def profile_path(current_user_or_profile, action \\ :show) def profile_upload_path(%Accounts.User{} = user) do
~p"/#{user.username}/songs/new"
def profile_path(username, action) when is_binary(username) do
Routes.profile_path(LiveBeatsWeb.Endpoint, action, username)
end end
def profile_path(%Accounts.User{} = current_user, action) do def profile_path(username) when is_binary(username) do
profile_path(current_user.username, action) unverified_path(LiveBeatsWeb.Endpoint, LiveBeatsWeb.Router, ~p"/#{username}")
end end
def profile_path(%MediaLibrary.Profile{} = profile, action) do def profile_path(%Accounts.User{} = current_user) do
profile_path(profile.username, action) profile_path(current_user.username)
end
def profile_path(%MediaLibrary.Profile{} = profile) do
profile_path(profile.username)
end end
slot :inner_block slot :inner_block
@ -176,7 +178,7 @@ defmodule LiveBeatsWeb.CoreComponents do
<:subtitle>@<%= @current_user.username %></:subtitle> <:subtitle>@<%= @current_user.username %></:subtitle>
<:link navigate={profile_path(@current_user)}>View Profile</:link> <:link navigate={profile_path(@current_user)}>View Profile</:link>
<:link navigate={Routes.settings_path(LiveBeatsWeb.Endpoint, :edit)}Settings</:link> <:link navigate={~p"/profile/settings"}Settings</:link>
</.dropdown> </.dropdown>
""" """
attr :id, :string, required: true attr :id, :string, required: true

View file

@ -1,8 +1,6 @@
defmodule LiveBeatsWeb.Layouts do defmodule LiveBeatsWeb.Layouts do
use LiveBeatsWeb, :html use LiveBeatsWeb, :html
alias LiveBeatsWeb.Endpoint
embed_templates "layouts/*" embed_templates "layouts/*"
attr :id, :string attr :id, :string
@ -54,7 +52,7 @@ defmodule LiveBeatsWeb.Layouts do
</.link> </.link>
<.link <.link
navigate={Routes.settings_path(Endpoint, :edit)} navigate={~p"/profile/settings"}
class={ 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"}" "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
</.link> </.link>
<% else %> <% else %>
<.link <.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" 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"
> >
<svg <svg
@ -103,8 +101,8 @@ defmodule LiveBeatsWeb.Layouts do
<:title><%= @current_user.name %></:title> <:title><%= @current_user.name %></:title>
<:subtitle>@<%= @current_user.username %></:subtitle> <:subtitle>@<%= @current_user.username %></:subtitle>
<:link navigate={profile_path(@current_user)}>View Profile</:link> <:link navigate={profile_path(@current_user)}>View Profile</:link>
<:link navigate={Routes.settings_path(Endpoint, :edit)}>Settings</:link> <:link navigate={~p"/profile/settings"}>Settings</:link>
<:link href={Routes.o_auth_callback_path(Endpoint, :sign_out)} method={:delete}>Sign out</:link> <:link href={~p"/signout"} method={:delete}>Sign out</:link>
</.dropdown> </.dropdown>
""" """
end end

View file

@ -4,16 +4,12 @@
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<%= csrf_meta_tag() %> <meta name="csrf-token" content={get_csrf_token()} />
<%= live_title_tag(assigns[:page_title] || "LiveBeats", suffix: " · Phoenix Framework") %> <.live_title suffix=" · Phoenix Framework">
<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/assets/app.css")} /> <%= assigns[:page_title] || "LiveBeats" %>
<script </.live_title>
defer <link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
phx-track-static <script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}></script>
type="text/javascript"
src={Routes.static_path(@conn, "/assets/app.js")}
>
</script>
</head> </head>
<body> <body>
<%= @inner_content %> <%= @inner_content %>

View file

@ -1,7 +1,7 @@
defmodule LiveBeatsWeb.ErrorHTML do defmodule LiveBeatsWeb.ErrorHTML do
use LiveBeatsWeb, :html use LiveBeatsWeb, :html
def template_not_found(template, _assigns) do def render(template, _assigns) do
Phoenix.Controller.status_message_from_template(template) Phoenix.Controller.status_message_from_template(template)
end end
end end

View file

@ -9,7 +9,7 @@ defmodule LiveBeatsWeb.RedirectController do
if conn.assigns.current_user do if conn.assigns.current_user do
LiveBeatsWeb.UserAuth.redirect_if_user_is_authenticated(conn, []) LiveBeatsWeb.UserAuth.redirect_if_user_is_authenticated(conn, [])
else else
redirect(conn, to: Routes.sign_in_path(conn, :index)) redirect(conn, to: ~p"/signin")
end end
end end
end end

View file

@ -1,10 +1,10 @@
defmodule LiveBeatsWeb.UserAuth do defmodule LiveBeatsWeb.UserAuth do
use LiveBeatsWeb, :verified_routes
import Plug.Conn import Plug.Conn
import Phoenix.Controller import Phoenix.Controller
alias Phoenix.LiveView alias Phoenix.LiveView
alias LiveBeats.Accounts alias LiveBeats.Accounts
alias LiveBeatsWeb.Router.Helpers, as: Routes
def on_mount(:current_user, _params, session, socket) do def on_mount(:current_user, _params, session, socket) do
case session do case session do
@ -35,7 +35,7 @@ defmodule LiveBeatsWeb.UserAuth do
defp redirect_require_login(socket) do defp redirect_require_login(socket) do
socket socket
|> LiveView.put_flash(:error, "Please sign in") |> LiveView.put_flash(:error, "Please sign in")
|> LiveView.redirect(to: Routes.sign_in_path(socket, :index)) |> LiveView.redirect(to: ~p"/signin")
end end
@doc """ @doc """
@ -79,7 +79,7 @@ defmodule LiveBeatsWeb.UserAuth do
conn conn
|> renew_session() |> renew_session()
|> redirect(to: Routes.sign_in_path(conn, :index)) |> redirect(to: ~p"/signin")
end end
@doc """ @doc """
@ -117,7 +117,7 @@ defmodule LiveBeatsWeb.UserAuth do
conn conn
|> put_flash(:error, "You must log in to access this page.") |> put_flash(:error, "You must log in to access this page.")
|> maybe_store_return_to() |> maybe_store_return_to()
|> redirect(to: Routes.sign_in_path(conn, :index)) |> redirect(to: ~p"/signin")
|> halt() |> halt()
end end
end end

View file

@ -46,7 +46,7 @@ defmodule LiveBeatsWeb.ProfileLive do
</.button> </.button>
<% end %> <% end %>
<%= if @owns_profile? do %> <%= 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} /><span class="ml-2">Upload Songs</span> <.icon name={:upload} /><span class="ml-2">Upload Songs</span>
</.button> </.button>
<% end %> <% end %>
@ -109,7 +109,6 @@ defmodule LiveBeatsWeb.ProfileLive do
end end
def mount(%{"profile_username" => profile_username}, _session, socket) do def mount(%{"profile_username" => profile_username}, _session, socket) do
IO.inspect(get_connect_params(socket))
%{current_user: current_user} = socket.assigns %{current_user: current_user} = socket.assigns
profile = profile =

View file

@ -169,10 +169,10 @@ defmodule LiveBeatsWeb.ProfileLive.UploadFormComponent do
do: ~H|Something went wrong| do: ~H|Something went wrong|
defp file_error(%{kind: %Ecto.Changeset{}} = assigns), 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), 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 defp put_stats(socket, entry_ref, %MP3Stat{} = stat) do
if changeset = get_changeset(socket, entry_ref) do if changeset = get_changeset(socket, entry_ref) do

View file

@ -98,7 +98,6 @@ defmodule LiveBeatsWeb.SettingsLive do
end end
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
IO.inspect(get_connect_params(socket))
changeset = Accounts.change_settings(socket.assigns.current_user, %{}) changeset = Accounts.change_settings(socket.assigns.current_user, %{})
{:ok, assign(socket, changeset: changeset)} {:ok, assign(socket, changeset: changeset)}
end end

View file

@ -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

View file

@ -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

View file

@ -42,7 +42,7 @@ defmodule LiveBeatsWeb.GithubCallbackTest do
assert Accounts.get_user_by_email("chris@local.test") == nil 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 redirected_to(conn, 302) == "/chrismccord"
assert %Accounts.User{} = user = Accounts.get_user_by_email("chris@local.test") 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"} params = %{"code" => "66e1c4202275d071eced", "state" => "invalid"}
assert Accounts.list_users(limit: 100) == [] 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" "We were unable to contact GitHub. Please try again later"
assert redirected_to(conn, 302) == "/" assert redirected_to(conn, 302) == "/"
@ -67,9 +67,9 @@ defmodule LiveBeatsWeb.GithubCallbackTest do
assert Accounts.list_users(limit: 100) == [] 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 redirected_to(conn, 302) == "/"
assert Accounts.list_users(limit: 100) == [] assert Accounts.list_users(limit: 100) == []
end end

View file

@ -4,7 +4,7 @@ defmodule LiveBeatsWeb.RedirectControllerTest do
test "GET / redirects to signin when not logged in", %{conn: conn} do test "GET / redirects to signin when not logged in", %{conn: conn} do
conn = get(conn, "/") conn = get(conn, "/")
assert redirected_to(conn, 302) =~ Routes.sign_in_path(conn, :index) assert redirected_to(conn, 302) =~ ~p"/signin"
end end
test "GET / redirects to profile page when signed in", %{conn: conn} do test "GET / redirects to profile page when signed in", %{conn: conn} do

View file

@ -72,7 +72,7 @@ defmodule LiveBeatsWeb.UserAuthTest do
test "redirects if user is authenticated", %{conn: conn, user: user} do test "redirects if user is authenticated", %{conn: conn, user: user} do
conn = conn |> assign(:current_user, user) |> UserAuth.redirect_if_user_is_authenticated([]) conn = conn |> assign(:current_user, user) |> UserAuth.redirect_if_user_is_authenticated([])
assert conn.halted assert conn.halted
assert redirected_to(conn) == Routes.profile_path(conn, :show, user.username) assert redirected_to(conn) == LiveBeatsWeb.CoreComponents.profile_path(user)
end end
test "does not redirect if user is not authenticated", %{conn: conn} do 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([]) conn = conn |> fetch_flash() |> UserAuth.require_authenticated_user([])
assert conn.halted assert conn.halted
assert redirected_to(conn) 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 end
test "stores the path to redirect to on GET", %{conn: conn} do test "stores the path to redirect to on GET", %{conn: conn} do

View file

@ -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

View file

@ -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

View file

@ -1,3 +0,0 @@
defmodule LiveBeatsWeb.PageViewTest do
use LiveBeatsWeb.ConnCase, async: true
end

View file

@ -23,6 +23,7 @@ defmodule LiveBeatsWeb.ConnCase do
using do using do
quote do quote do
# Import conveniences for testing with connections # Import conveniences for testing with connections
use LiveBeatsWeb, :verified_routes
import Plug.Conn import Plug.Conn
import Phoenix.ConnTest import Phoenix.ConnTest
import LiveBeatsWeb.ConnCase import LiveBeatsWeb.ConnCase