Fixup tests

This commit is contained in:
Chris McCord 2021-12-14 10:35:51 -05:00
parent 57c193e490
commit c8a6035a02
13 changed files with 105 additions and 71 deletions

View file

@ -322,12 +322,6 @@ defmodule LiveBeats.MediaLibrary do
prev || get_last_song(profile) prev || get_last_song(profile)
end end
def create_song(attrs \\ %{}) do
%Song{}
|> Song.changeset(attrs)
|> Repo.insert()
end
def update_song(%Song{} = song, attrs) do def update_song(%Song{} = song, attrs) do
song song
|> Song.changeset(attrs) |> Song.changeset(attrs)
@ -358,6 +352,10 @@ defmodule LiveBeats.MediaLibrary do
[] []
) )
|> Repo.transaction() |> Repo.transaction()
|> case do
{:ok, _} -> :ok
other -> other
end
end end
def change_song(song_or_changeset, attrs \\ %{}) def change_song(song_or_changeset, attrs \\ %{})

View file

@ -11,7 +11,7 @@ defmodule LiveBeatsWeb.LiveHelpers 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 \\ :index) def profile_path(current_user_or_profile, action \\ :show)
def profile_path(%Accounts.User{} = current_user, action) do def profile_path(%Accounts.User{} = current_user, action) do
Routes.profile_path(LiveBeatsWeb.Endpoint, action, current_user.username) Routes.profile_path(LiveBeatsWeb.Endpoint, action, current_user.username)
@ -429,7 +429,7 @@ defmodule LiveBeatsWeb.LiveHelpers do
|> assign_new(:value, fn -> assigns[:min] || 0 end) |> assign_new(:value, fn -> assigns[:min] || 0 end)
~H""" ~H"""
<div class="bg-gray-200 flex-auto dark:bg-black rounded-full overflow-hidden" phx-update="ignore"> <div id={"#{@id}-container"} class="bg-gray-200 flex-auto dark:bg-black rounded-full overflow-hidden" phx-update="ignore">
<div <div
id={@id} id={@id}
class="bg-lime-500 dark:bg-lime-400 h-1.5 w-0" class="bg-lime-500 dark:bg-lime-400 h-1.5 w-0"

View file

@ -9,8 +9,8 @@ defmodule LiveBeatsWeb.PlayerLive do
def render(assigns) do def render(assigns) do
~H""" ~H"""
<!-- player --> <!-- player -->
<div id="audio-player" phx-hook="AudioPlayer" class="w-full" role="region" aria-label="Player" > <div id="audio-player" phx-hook="AudioPlayer" class="w-full" role="region" aria-label="Player">
<div phx-update="ignore"> <div id="audio-ignore" phx-update="ignore">
<audio></audio> <audio></audio>
</div> </div>
<div class="bg-white dark:bg-gray-800 p-4"> <div class="bg-white dark:bg-gray-800 p-4">
@ -28,7 +28,8 @@ defmodule LiveBeatsWeb.PlayerLive do
<.progress_bar id="player-progress" /> <.progress_bar id="player-progress" />
<div class="text-gray-500 dark:text-gray-400 flex-row justify-between text-sm font-medium tabular-nums" <div id="player-info"
class="text-gray-500 dark:text-gray-400 flex-row justify-between text-sm font-medium tabular-nums"
phx-update="ignore"> phx-update="ignore">
<div id="player-time"></div> <div id="player-time"></div>
<div id="player-duration"></div> <div id="player-duration"></div>

View file

@ -211,7 +211,7 @@ defmodule LiveBeatsWeb.ProfileLive do
end end
end end
defp apply_action(socket, :index, _params) do defp apply_action(socket, :show, _params) do
socket socket
|> assign(:page_title, "Listing Songs") |> assign(:page_title, "Listing Songs")
|> assign(:song, nil) |> assign(:song, nil)

View file

@ -55,7 +55,7 @@ defmodule LiveBeatsWeb.Router do
live_session :authenticated, live_session :authenticated,
on_mount: [{LiveBeatsWeb.UserAuth, :ensure_authenticated}, LiveBeatsWeb.Nav] do on_mount: [{LiveBeatsWeb.UserAuth, :ensure_authenticated}, LiveBeatsWeb.Nav] do
live "/:profile_username/songs/new", ProfileLive, :new live "/:profile_username/songs/new", ProfileLive, :new
live "/:profile_username", ProfileLive, :index live "/:profile_username", ProfileLive, :show
live "/profile/settings", SettingsLive, :edit live "/profile/settings", SettingsLive, :edit
end end
end end

View file

@ -1,9 +1,9 @@
defmodule LiveBeats.AccountsTest do defmodule LiveBeats.AccountsTest do
use LiveBeats.DataCase use LiveBeats.DataCase
alias LiveBeats.Accounts
import LiveBeats.AccountsFixtures import LiveBeats.AccountsFixtures
alias LiveBeats.Accounts.{User}
alias LiveBeats.Accounts
describe "get_user!/1" do describe "get_user!/1" do
test "raises if id is invalid" do test "raises if id is invalid" do
@ -14,13 +14,20 @@ defmodule LiveBeats.AccountsTest do
test "returns the user with the given id" do test "returns the user with the given id" do
%{id: id} = user = user_fixture() %{id: id} = user = user_fixture()
assert %User{id: ^id} = Accounts.get_user!(user.id) assert %Accounts.User{id: ^id} = Accounts.get_user!(user.id)
end end
end end
describe "register_github_user/1" do describe "register_github_user/1" do
test "creates users with valid data" do test "creates users with valid data" do
flunk "TODO" info = %{
"id" => "github-id",
"login" => "chrismccord",
"avatar_url" => "https://example.com",
"html_url" => "https://example.com"
}
assert {:ok, _user} = Accounts.register_github_user("chris@example.com", info, [], "123")
end end
end end
end end

View file

@ -6,13 +6,16 @@ defmodule LiveBeats.MediaLibraryTest do
describe "songs" do describe "songs" do
alias LiveBeats.MediaLibrary.Song alias LiveBeats.MediaLibrary.Song
import LiveBeats.AccountsFixtures
import LiveBeats.MediaLibraryFixtures import LiveBeats.MediaLibraryFixtures
@invalid_attrs %{album_artist: nil, artist: nil, date_recorded: nil, date_released: nil, duration: nil, title: nil} @invalid_attrs %{album_artist: nil, artist: nil, date_recorded: nil, date_released: nil, duration: nil, title: nil}
test "list_songs/0 returns all songs" do test "list_profile_songs/1 returns all songs for a profile" do
song = song_fixture() user = user_fixture()
assert MediaLibrary.list_songs() == [song] profile = MediaLibrary.get_profile!(user)
song = song_fixture(%{user_id: user.id})
assert MediaLibrary.list_profile_songs(profile) == [song]
end end
test "get_song!/1 returns the song with given id" do test "get_song!/1 returns the song with given id" do
@ -20,22 +23,6 @@ defmodule LiveBeats.MediaLibraryTest do
assert MediaLibrary.get_song!(song.id) == song assert MediaLibrary.get_song!(song.id) == song
end end
test "create_song/1 with valid data creates a song" do
valid_attrs = %{album_artist: "some album_artist", artist: "some artist", date_recorded: ~N[2021-10-26 20:11:00], date_released: ~N[2021-10-26 20:11:00], duration: 42, title: "some title"}
assert {:ok, %Song{} = song} = MediaLibrary.create_song(valid_attrs)
assert song.album_artist == "some album_artist"
assert song.artist == "some artist"
assert song.date_recorded == ~N[2021-10-26 20:11:00]
assert song.date_released == ~N[2021-10-26 20:11:00]
assert song.duration == 42
assert song.title == "some title"
end
test "create_song/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = MediaLibrary.create_song(@invalid_attrs)
end
test "update_song/2 with valid data updates the song" do test "update_song/2 with valid data updates the song" do
song = song_fixture() song = song_fixture()
update_attrs = %{album_artist: "some updated album_artist", artist: "some updated artist", date_recorded: ~N[2021-10-27 20:11:00], date_released: ~N[2021-10-27 20:11:00], duration: 43, title: "some updated title"} update_attrs = %{album_artist: "some updated album_artist", artist: "some updated artist", date_recorded: ~N[2021-10-27 20:11:00], date_released: ~N[2021-10-27 20:11:00], duration: 43, title: "some updated title"}
@ -45,7 +32,7 @@ defmodule LiveBeats.MediaLibraryTest do
assert song.artist == "some updated artist" assert song.artist == "some updated artist"
assert song.date_recorded == ~N[2021-10-27 20:11:00] assert song.date_recorded == ~N[2021-10-27 20:11:00]
assert song.date_released == ~N[2021-10-27 20:11:00] assert song.date_released == ~N[2021-10-27 20:11:00]
assert song.duration == 43 assert song.duration == 42
assert song.title == "some updated title" assert song.title == "some updated title"
end end
@ -56,8 +43,9 @@ defmodule LiveBeats.MediaLibraryTest do
end end
test "delete_song/1 deletes the song" do test "delete_song/1 deletes the song" do
song = song_fixture() user = user_fixture()
assert {:ok, %Song{}} = MediaLibrary.delete_song(song) song = song_fixture(%{user_id: user.id})
assert :ok = MediaLibrary.delete_song(song)
assert_raise Ecto.NoResultsError, fn -> MediaLibrary.get_song!(song.id) end assert_raise Ecto.NoResultsError, fn -> MediaLibrary.get_song!(song.id) end
end end

View file

@ -11,21 +11,20 @@ defmodule LiveBeatsWeb.GithubCallbackTest do
"valid" -> "valid" ->
{:ok, {:ok,
%{ %{
info: %{"login" => "chrismccord", "name" => "Chris", "id" => 1}, info: %{
"login" => "chrismccord",
"name" => "Chris",
"id" => 1,
"avatar_url" => "",
"html_url" => ""
},
primary_email: "chris@local.test", primary_email: "chris@local.test",
emails: [%{"primary" => true, "email" => "chris@local.test"}], emails: [%{"primary" => true, "email" => "chris@local.test"}],
token: "1234" token: "1234"
}} }}
"invalid" -> "invalid" ->
{:ok, {:error, %{reason: "token"}}
%{
info: %{"login" => "chrismccord"},
primary_email: "chris@local.test",
emails: [%{"primary" => true, "email" => "chris@local.test"}],
token: "1234"
}}
"failed" -> "failed" ->
{:error, %{reason: state}} {:error, %{reason: state}}
@ -45,7 +44,7 @@ defmodule LiveBeatsWeb.GithubCallbackTest do
conn = get(conn, Routes.o_auth_callback_path(conn, :new, "github", params)) conn = get(conn, Routes.o_auth_callback_path(conn, :new, "github", params))
assert redirected_to(conn, 302) == "/" 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")
assert user.name == "Chris" assert user.name == "Chris"
end end
@ -56,7 +55,9 @@ defmodule LiveBeatsWeb.GithubCallbackTest do
conn = get(conn, Routes.o_auth_callback_path(conn, :new, "github", params)) conn = get(conn, Routes.o_auth_callback_path(conn, :new, "github", params))
assert get_flash(conn, :error) == "We were unable to fetch the necessary information from your GithHub account" assert get_flash(conn, :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

@ -1,8 +0,0 @@
defmodule LiveBeatsWeb.PageControllerTest do
use LiveBeatsWeb.ConnCase
test "GET /", %{conn: conn} do
conn = get(conn, "/")
assert html_response(conn, 200) =~ "LiveBeats"
end
end

View file

@ -0,0 +1,27 @@
defmodule LiveBeatsWeb.RedirectControllerTest do
use LiveBeatsWeb.ConnCase
import LiveBeats.AccountsFixtures
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)
end
test "GET / redirects to profile page when signed in", %{conn: conn} do
user = user_fixture(%{"login" => "chrismccord"})
conn =
conn
|> log_in_user(user)
|> get("/")
assert redirected_to(conn, 302) =~ "/chrismccord"
conn =
conn
|> recycle()
|> get("/chrismccord")
assert html_response(conn, 200) =~ "chrismccord&#39;s beats"
end
end

View file

@ -19,7 +19,7 @@ defmodule LiveBeatsWeb.UserAuthTest do
conn = UserAuth.log_in_user(conn, user) conn = UserAuth.log_in_user(conn, user)
assert id = get_session(conn, :user_id) assert id = get_session(conn, :user_id)
assert get_session(conn, :live_socket_id) == "users_sessions:#{id}" assert get_session(conn, :live_socket_id) == "users_sessions:#{id}"
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/chrismccord"
assert Accounts.get_user!(id) assert Accounts.get_user!(id)
end end
@ -43,7 +43,7 @@ defmodule LiveBeatsWeb.UserAuthTest do
|> UserAuth.log_out_user() |> UserAuth.log_out_user()
refute get_session(conn, :user_id) refute get_session(conn, :user_id)
assert redirected_to(conn) == "/" assert redirected_to(conn) == "/signin"
end end
test "broadcasts to the given live_socket_id", %{conn: conn} do test "broadcasts to the given live_socket_id", %{conn: conn} do
@ -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) == "/" assert redirected_to(conn) == Routes.profile_path(conn, :show, user.username)
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

View file

@ -17,12 +17,16 @@ defmodule LiveBeatsWeb.ConnCase do
use ExUnit.CaseTemplate use ExUnit.CaseTemplate
@endpoint LiveBeatsWeb.Endpoint
import Phoenix.ConnTest
using do using do
quote do quote do
# Import conveniences for testing with connections # Import conveniences for testing with connections
import Plug.Conn import Plug.Conn
import Phoenix.ConnTest import Phoenix.ConnTest
import LiveBeatsWeb.ConnCase import LiveBeatsWeb.ConnCase
import unquote(__MODULE__)
alias LiveBeatsWeb.Router.Helpers, as: Routes alias LiveBeatsWeb.Router.Helpers, as: Routes
@ -36,4 +40,12 @@ defmodule LiveBeatsWeb.ConnCase do
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
{:ok, conn: Phoenix.ConnTest.build_conn()} {:ok, conn: Phoenix.ConnTest.build_conn()}
end end
def log_in_user(conn, user) do
conn
|> Phoenix.ConnTest.bypass_through(LiveBeatsWeb.Router, [:browser])
|> get("/")
|> LiveBeatsWeb.UserAuth.log_in_user(user)
|> Phoenix.ConnTest.recycle()
end
end end

View file

@ -4,21 +4,29 @@ defmodule LiveBeats.MediaLibraryFixtures do
entities via the `LiveBeats.MediaLibrary` context. entities via the `LiveBeats.MediaLibrary` context.
""" """
alias LiveBeats.MediaLibrary.Song
@doc """ @doc """
Generate a song. Generate a song.
""" """
def song_fixture(attrs \\ %{}) do def song_fixture(attrs \\ %{}) do
{:ok, song} = {:ok, song} =
attrs struct!(
|> Enum.into(%{ Song,
album_artist: "some album_artist", Enum.into(attrs, %{
artist: "some artist", album_artist: "some album_artist",
date_recorded: ~N[2021-10-26 20:11:00], artist: "some artist",
date_released: ~N[2021-10-26 20:11:00], date_recorded: ~N[2021-10-26 20:11:00],
duration: 42, date_released: ~N[2021-10-26 20:11:00],
title: "some title" duration: 42,
}) title: "some title",
|> LiveBeats.MediaLibrary.create_song() mp3_url: "//example.com/mp3.mp3",
mp3_filename: "mp3.mp3",
mp3_filepath: "/data/mp3.mp3",
status: :stopped
})
)
|> LiveBeats.Repo.insert()
song song
end end