From c8a6035a02052e5d578c044b30fa1ce8ccdb2864 Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Tue, 14 Dec 2021 10:35:51 -0500 Subject: [PATCH] Fixup tests --- lib/live_beats/media_library.ex | 10 +++--- lib/live_beats_web/live/live_helpers.ex | 4 +-- lib/live_beats_web/live/player_live.ex | 7 ++-- lib/live_beats_web/live/profile_live.ex | 2 +- lib/live_beats_web/router.ex | 2 +- test/live_beats/accounts_test.exs | 15 ++++++--- test/live_beats/media_library_test.exs | 32 ++++++------------- .../controllers/github_callbacks_test.exs | 23 ++++++------- .../controllers/page_controller_test.exs | 8 ----- .../controllers/redirect_controller_test.exs | 27 ++++++++++++++++ .../controllers/user_auth_test.exs | 6 ++-- test/support/conn_case.ex | 12 +++++++ .../fixtures/media_library_fixtures.ex | 28 ++++++++++------ 13 files changed, 105 insertions(+), 71 deletions(-) delete mode 100644 test/live_beats_web/controllers/page_controller_test.exs create mode 100644 test/live_beats_web/controllers/redirect_controller_test.exs diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index 4f6e19c..982d794 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -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 \\ %{}) diff --git a/lib/live_beats_web/live/live_helpers.ex b/lib/live_beats_web/live/live_helpers.ex index a27d6f0..4414f8f 100644 --- a/lib/live_beats_web/live/live_helpers.ex +++ b/lib/live_beats_web/live/live_helpers.ex @@ -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""" -
+
-
-
+
+
@@ -28,7 +28,8 @@ defmodule LiveBeatsWeb.PlayerLive do <.progress_bar id="player-progress" /> -
diff --git a/lib/live_beats_web/live/profile_live.ex b/lib/live_beats_web/live/profile_live.ex index 9f550fa..42f0950 100644 --- a/lib/live_beats_web/live/profile_live.ex +++ b/lib/live_beats_web/live/profile_live.ex @@ -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) diff --git a/lib/live_beats_web/router.ex b/lib/live_beats_web/router.ex index d59476e..e2d24e5 100644 --- a/lib/live_beats_web/router.ex +++ b/lib/live_beats_web/router.ex @@ -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 diff --git a/test/live_beats/accounts_test.exs b/test/live_beats/accounts_test.exs index bbc7606..00af66d 100644 --- a/test/live_beats/accounts_test.exs +++ b/test/live_beats/accounts_test.exs @@ -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 diff --git a/test/live_beats/media_library_test.exs b/test/live_beats/media_library_test.exs index a9ab49a..6c4beaf 100644 --- a/test/live_beats/media_library_test.exs +++ b/test/live_beats/media_library_test.exs @@ -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 diff --git a/test/live_beats_web/controllers/github_callbacks_test.exs b/test/live_beats_web/controllers/github_callbacks_test.exs index 9f78d27..235a111 100644 --- a/test/live_beats_web/controllers/github_callbacks_test.exs +++ b/test/live_beats_web/controllers/github_callbacks_test.exs @@ -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 diff --git a/test/live_beats_web/controllers/page_controller_test.exs b/test/live_beats_web/controllers/page_controller_test.exs deleted file mode 100644 index a7b70c0..0000000 --- a/test/live_beats_web/controllers/page_controller_test.exs +++ /dev/null @@ -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 diff --git a/test/live_beats_web/controllers/redirect_controller_test.exs b/test/live_beats_web/controllers/redirect_controller_test.exs new file mode 100644 index 0000000..0a88c4a --- /dev/null +++ b/test/live_beats_web/controllers/redirect_controller_test.exs @@ -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's beats" + end +end diff --git a/test/live_beats_web/controllers/user_auth_test.exs b/test/live_beats_web/controllers/user_auth_test.exs index afe7679..54baa0a 100644 --- a/test/live_beats_web/controllers/user_auth_test.exs +++ b/test/live_beats_web/controllers/user_auth_test.exs @@ -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 diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index adc15a3..0c82483 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -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 diff --git a/test/support/fixtures/media_library_fixtures.ex b/test/support/fixtures/media_library_fixtures.ex index dfcd669..c4b3835 100644 --- a/test/support/fixtures/media_library_fixtures.ex +++ b/test/support/fixtures/media_library_fixtures.ex @@ -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