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)
end
def create_song(attrs \\ %{}) do
%Song{}
|> Song.changeset(attrs)
|> Repo.insert()
end
def update_song(%Song{} = song, attrs) do
song
|> Song.changeset(attrs)
@ -358,6 +352,10 @@ defmodule LiveBeats.MediaLibrary do
[]
)
|> Repo.transaction()
|> case do
{:ok, _} -> :ok
other -> other
end
end
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(%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
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)
~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
id={@id}
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
~H"""
<!-- player -->
<div id="audio-player" phx-hook="AudioPlayer" class="w-full" role="region" aria-label="Player" >
<div phx-update="ignore">
<div id="audio-player" phx-hook="AudioPlayer" class="w-full" role="region" aria-label="Player">
<div id="audio-ignore" phx-update="ignore">
<audio></audio>
</div>
<div class="bg-white dark:bg-gray-800 p-4">
@ -28,7 +28,8 @@ defmodule LiveBeatsWeb.PlayerLive do
<.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">
<div id="player-time"></div>
<div id="player-duration"></div>

View file

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

View file

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

View file

@ -1,9 +1,9 @@
defmodule LiveBeats.AccountsTest do
use LiveBeats.DataCase
alias LiveBeats.Accounts
import LiveBeats.AccountsFixtures
alias LiveBeats.Accounts.{User}
alias LiveBeats.Accounts
describe "get_user!/1" 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
%{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
describe "register_github_user/1" 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

View file

@ -6,13 +6,16 @@ defmodule LiveBeats.MediaLibraryTest do
describe "songs" do
alias LiveBeats.MediaLibrary.Song
import LiveBeats.AccountsFixtures
import LiveBeats.MediaLibraryFixtures
@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
song = song_fixture()
assert MediaLibrary.list_songs() == [song]
test "list_profile_songs/1 returns all songs for a profile" do
user = user_fixture()
profile = MediaLibrary.get_profile!(user)
song = song_fixture(%{user_id: user.id})
assert MediaLibrary.list_profile_songs(profile) == [song]
end
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
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
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"}
@ -45,7 +32,7 @@ defmodule LiveBeats.MediaLibraryTest do
assert song.artist == "some updated artist"
assert song.date_recorded == ~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"
end
@ -56,8 +43,9 @@ defmodule LiveBeats.MediaLibraryTest do
end
test "delete_song/1 deletes the song" do
song = song_fixture()
assert {:ok, %Song{}} = MediaLibrary.delete_song(song)
user = user_fixture()
song = song_fixture(%{user_id: user.id})
assert :ok = MediaLibrary.delete_song(song)
assert_raise Ecto.NoResultsError, fn -> MediaLibrary.get_song!(song.id) end
end

View file

@ -11,21 +11,20 @@ defmodule LiveBeatsWeb.GithubCallbackTest do
"valid" ->
{:ok,
%{
info: %{"login" => "chrismccord", "name" => "Chris", "id" => 1},
info: %{
"login" => "chrismccord",
"name" => "Chris",
"id" => 1,
"avatar_url" => "",
"html_url" => ""
},
primary_email: "chris@local.test",
emails: [%{"primary" => true, "email" => "chris@local.test"}],
token: "1234"
}}
"invalid" ->
{:ok,
%{
info: %{"login" => "chrismccord"},
primary_email: "chris@local.test",
emails: [%{"primary" => true, "email" => "chris@local.test"}],
token: "1234"
}}
{:error, %{reason: "token"}}
"failed" ->
{:error, %{reason: state}}
@ -45,7 +44,7 @@ defmodule LiveBeatsWeb.GithubCallbackTest do
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 user.name == "Chris"
end
@ -56,7 +55,9 @@ defmodule LiveBeatsWeb.GithubCallbackTest do
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 Accounts.list_users(limit: 100) == []
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)
assert id = get_session(conn, :user_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)
end
@ -43,7 +43,7 @@ defmodule LiveBeatsWeb.UserAuthTest do
|> UserAuth.log_out_user()
refute get_session(conn, :user_id)
assert redirected_to(conn) == "/"
assert redirected_to(conn) == "/signin"
end
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
conn = conn |> assign(:current_user, user) |> UserAuth.redirect_if_user_is_authenticated([])
assert conn.halted
assert redirected_to(conn) == "/"
assert redirected_to(conn) == Routes.profile_path(conn, :show, user.username)
end
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
@endpoint LiveBeatsWeb.Endpoint
import Phoenix.ConnTest
using do
quote do
# Import conveniences for testing with connections
import Plug.Conn
import Phoenix.ConnTest
import LiveBeatsWeb.ConnCase
import unquote(__MODULE__)
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)
{:ok, conn: Phoenix.ConnTest.build_conn()}
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

View file

@ -4,21 +4,29 @@ defmodule LiveBeats.MediaLibraryFixtures do
entities via the `LiveBeats.MediaLibrary` context.
"""
alias LiveBeats.MediaLibrary.Song
@doc """
Generate a song.
"""
def song_fixture(attrs \\ %{}) do
{:ok, song} =
attrs
|> Enum.into(%{
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"
})
|> LiveBeats.MediaLibrary.create_song()
struct!(
Song,
Enum.into(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",
mp3_url: "//example.com/mp3.mp3",
mp3_filename: "mp3.mp3",
mp3_filepath: "/data/mp3.mp3",
status: :stopped
})
)
|> LiveBeats.Repo.insert()
song
end