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

View file

@ -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 %></:subtitle>
<: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>
"""
attr :id, :string, required: true

View file

@ -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>
<.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
</.link>
<% 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"
>
<svg
@ -103,8 +101,8 @@ defmodule LiveBeatsWeb.Layouts do
<:title><%= @current_user.name %></:title>
<:subtitle>@<%= @current_user.username %></:subtitle>
<:link navigate={profile_path(@current_user)}>View Profile</:link>
<:link navigate={Routes.settings_path(Endpoint, :edit)}>Settings</:link>
<:link href={Routes.o_auth_callback_path(Endpoint, :sign_out)} method={:delete}>Sign out</:link>
<:link navigate={~p"/profile/settings"}>Settings</:link>
<:link href={~p"/signout"} method={:delete}>Sign out</:link>
</.dropdown>
"""
end

View file

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

View file

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

View file

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

View file

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

View file

@ -46,7 +46,7 @@ defmodule LiveBeatsWeb.ProfileLive do
</.button>
<% 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} /><span class="ml-2">Upload Songs</span>
</.button>
<% 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 =

View file

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

View file

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

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

View file

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

View file

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

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
quote do
# Import conveniences for testing with connections
use LiveBeatsWeb, :verified_routes
import Plug.Conn
import Phoenix.ConnTest
import LiveBeatsWeb.ConnCase