From 35f6571ab2ca1e5f6473e9551defaa5b58e300ea Mon Sep 17 00:00:00 2001 From: Inhji Date: Thu, 22 Jun 2023 21:08:59 +0200 Subject: [PATCH 1/5] make tests pass --- test/chiya/channels_test.exs | 6 ++++-- test/chiya_web/controllers/channel_controller_test.exs | 2 +- test/chiya_web/controllers/identity_controller_test.exs | 2 +- test/chiya_web/controllers/note_controller_test.exs | 2 +- test/chiya_web/live/user_login_live_test.exs | 4 ++-- test/chiya_web/live/user_registration_live_test.exs | 6 ++---- test/chiya_web/live/user_reset_password_live_test.exs | 2 +- test/support/fixtures/channels_fixtures.ex | 3 ++- test/support/fixtures/notes_fixtures.ex | 2 +- 9 files changed, 15 insertions(+), 14 deletions(-) diff --git a/test/chiya/channels_test.exs b/test/chiya/channels_test.exs index 5d14092..27300aa 100644 --- a/test/chiya/channels_test.exs +++ b/test/chiya/channels_test.exs @@ -23,7 +23,8 @@ defmodule Chiya.ChannelsTest do valid_attrs = %{ content: "some content", name: "some name", - visibility: :public + visibility: :public, + layout: :default } assert {:ok, %Channel{} = channel} = Channels.create_channel(valid_attrs) @@ -44,7 +45,8 @@ defmodule Chiya.ChannelsTest do content: "some updated content", name: "some updated name", slug: "some updated slug", - visibility: :private + visibility: :private, + layout: :default } assert {:ok, %Channel{} = channel} = Channels.update_channel(channel, update_attrs) diff --git a/test/chiya_web/controllers/channel_controller_test.exs b/test/chiya_web/controllers/channel_controller_test.exs index 46cda7e..72609b3 100644 --- a/test/chiya_web/controllers/channel_controller_test.exs +++ b/test/chiya_web/controllers/channel_controller_test.exs @@ -22,7 +22,7 @@ defmodule ChiyaWeb.ChannelControllerTest do describe "index" do test "lists all channels", %{conn: conn} do conn = get(conn, ~p"/admin/channels") - assert html_response(conn, 200) =~ "Listing Channels" + assert html_response(conn, 200) =~ "Channels" end end diff --git a/test/chiya_web/controllers/identity_controller_test.exs b/test/chiya_web/controllers/identity_controller_test.exs index 458dfaa..359cee1 100644 --- a/test/chiya_web/controllers/identity_controller_test.exs +++ b/test/chiya_web/controllers/identity_controller_test.exs @@ -26,7 +26,7 @@ defmodule ChiyaWeb.IdentityControllerTest do describe "index" do test "lists all identities", %{conn: conn} do conn = get(conn, ~p"/admin/identities") - assert html_response(conn, 200) =~ "Listing Identities" + assert html_response(conn, 200) =~ "Identities" end end diff --git a/test/chiya_web/controllers/note_controller_test.exs b/test/chiya_web/controllers/note_controller_test.exs index ac118da..f1c132d 100644 --- a/test/chiya_web/controllers/note_controller_test.exs +++ b/test/chiya_web/controllers/note_controller_test.exs @@ -86,7 +86,7 @@ defmodule ChiyaWeb.NoteControllerTest do assert redirected_to(conn) == ~p"/admin/notes/#{note}" conn = get(conn, ~p"/admin/notes/#{note}") - assert html_response(conn, 200) =~ "some updated content" + assert html_response(conn, 200) =~ "some updated name" end test "renders errors when data is invalid", %{conn: conn, note: note} do diff --git a/test/chiya_web/live/user_login_live_test.exs b/test/chiya_web/live/user_login_live_test.exs index dfbaeef..f57460a 100644 --- a/test/chiya_web/live/user_login_live_test.exs +++ b/test/chiya_web/live/user_login_live_test.exs @@ -8,8 +8,8 @@ defmodule ChiyaWeb.UserLoginLiveTest do test "renders log in page", %{conn: conn} do {:ok, _lv, html} = live(conn, ~p"/user/log_in") - assert html =~ "Log in" - assert html =~ "Register" + assert html =~ "Sign in" + assert html =~ "Sign up" assert html =~ "Forgot your password?" end diff --git a/test/chiya_web/live/user_registration_live_test.exs b/test/chiya_web/live/user_registration_live_test.exs index 6cc03b2..0a7f105 100644 --- a/test/chiya_web/live/user_registration_live_test.exs +++ b/test/chiya_web/live/user_registration_live_test.exs @@ -9,7 +9,7 @@ defmodule ChiyaWeb.UserRegistrationLiveTest do {:ok, _lv, html} = live(conn, ~p"/user/register") assert html =~ "Register" - assert html =~ "Log in" + assert html =~ "Sign in" end test "redirects if already logged in", %{conn: conn} do @@ -50,9 +50,7 @@ defmodule ChiyaWeb.UserRegistrationLiveTest do # Now do a logged in request and assert on the menu conn = get(conn, "/") response = html_response(conn, 200) - assert response =~ email assert response =~ "Admin" - assert response =~ "Log out" end end @@ -83,7 +81,7 @@ defmodule ChiyaWeb.UserRegistrationLiveTest do |> render_click() |> follow_redirect(conn, ~p"/user/log_in") - assert login_html =~ "Log in" + assert login_html =~ "Sign in" end end end diff --git a/test/chiya_web/live/user_reset_password_live_test.exs b/test/chiya_web/live/user_reset_password_live_test.exs index 5bc77bb..64f28db 100644 --- a/test/chiya_web/live/user_reset_password_live_test.exs +++ b/test/chiya_web/live/user_reset_password_live_test.exs @@ -97,7 +97,7 @@ defmodule ChiyaWeb.UserResetPasswordLiveTest do |> render_click() |> follow_redirect(conn, ~p"/user/log_in") - assert conn.resp_body =~ "Log in" + assert conn.resp_body =~ "Sign in" end test "redirects to password reset page when the Register button is clicked", %{ diff --git a/test/support/fixtures/channels_fixtures.ex b/test/support/fixtures/channels_fixtures.ex index 62b2d75..6df15a9 100644 --- a/test/support/fixtures/channels_fixtures.ex +++ b/test/support/fixtures/channels_fixtures.ex @@ -19,7 +19,8 @@ defmodule Chiya.ChannelsFixtures do content: "some content", name: "some name", slug: unique_channel_slug(), - visibility: :public + visibility: :public, + layout: :default }) |> Chiya.Channels.create_channel() diff --git a/test/support/fixtures/notes_fixtures.ex b/test/support/fixtures/notes_fixtures.ex index 8594cfb..629d342 100644 --- a/test/support/fixtures/notes_fixtures.ex +++ b/test/support/fixtures/notes_fixtures.ex @@ -25,6 +25,6 @@ defmodule Chiya.NotesFixtures do }) |> Chiya.Notes.create_note() - note + Chiya.Notes.preload_note(note) end end From 35d6398e15ca05880fa61d8c81ee23d6d620a9c7 Mon Sep 17 00:00:00 2001 From: Inhji Date: Thu, 22 Jun 2023 21:09:17 +0200 Subject: [PATCH 2/5] update megaphone button on home --- lib/chiya_web/controllers/page_html/home.html.heex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chiya_web/controllers/page_html/home.html.heex b/lib/chiya_web/controllers/page_html/home.html.heex index c5869d4..da0e638 100644 --- a/lib/chiya_web/controllers/page_html/home.html.heex +++ b/lib/chiya_web/controllers/page_html/home.html.heex @@ -12,7 +12,7 @@
  • <.icon name="hero-megaphone" /> From aa36502ddba75ccb25cac12b0061abfa79871357 Mon Sep 17 00:00:00 2001 From: Inhji Date: Fri, 23 Jun 2023 06:37:48 +0200 Subject: [PATCH 3/5] fix tag updater crashing when error happens --- lib/chiya/tags/tag_updater.ex | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/chiya/tags/tag_updater.ex b/lib/chiya/tags/tag_updater.ex index dfe9a98..634f97c 100644 --- a/lib/chiya/tags/tag_updater.ex +++ b/lib/chiya/tags/tag_updater.ex @@ -14,6 +14,10 @@ defmodule Chiya.Tags.TagUpdater do {:ok, note} end + def update_tags({:error, changeset}, _attrs) do + {:error, changeset} + end + @doc """ Updates the tags for the given schema From 92e548e96c28b705af0a8c25a7b4217b16eaf9bb Mon Sep 17 00:00:00 2001 From: Inhji Date: Fri, 23 Jun 2023 06:38:31 +0200 Subject: [PATCH 4/5] add selected_channels to note_form to properly set default channel --- lib/chiya_web/controllers/note_controller.ex | 29 ++++++++++++++++--- lib/chiya_web/controllers/note_html.ex | 4 +-- .../controllers/note_html/edit.html.heex | 1 + .../controllers/note_html/new.html.heex | 7 ++++- .../controllers/note_html/note_form.html.heex | 2 +- 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/lib/chiya_web/controllers/note_controller.ex b/lib/chiya_web/controllers/note_controller.ex index f489f06..48cdb4c 100644 --- a/lib/chiya_web/controllers/note_controller.ex +++ b/lib/chiya_web/controllers/note_controller.ex @@ -23,21 +23,22 @@ defmodule ChiyaWeb.NoteController do end def new(conn, _params) do - settings = Chiya.Site.get_settings() + default_channels = get_default_channels(conn) changeset = - %Note{channels: [settings.default_channel]} + %Note{} + |> Notes.preload_note() |> Notes.change_note() render(conn, :new, changeset: changeset, channels: to_channel_options(), + selected_channels: default_channels, tags: [] ) end def create(conn, %{"note" => note_params}) do - IO.inspect(note_params) note_params = from_channel_ids(note_params) case Notes.create_note(note_params) do @@ -46,8 +47,15 @@ defmodule ChiyaWeb.NoteController do |> put_flash(:info, "Note created successfully.") |> redirect(to: ~p"/admin/notes/#{note}") + # TODO: set channels from changeset when error happened? + {:error, %Ecto.Changeset{} = changeset} -> - render(conn, :new, changeset: changeset, channels: to_channel_options(), tags: []) + render(conn, :new, + changeset: changeset, + channels: to_channel_options(), + selected_channels: nil, + tags: [] + ) end end @@ -60,11 +68,13 @@ defmodule ChiyaWeb.NoteController do def edit(conn, %{"id" => id}) do note = Notes.get_note_preloaded!(id) changeset = Notes.change_note(note) + selected_channels = Enum.map(note.channels, fn c -> c.id end) render(conn, :edit, note: note, changeset: changeset, channels: to_channel_options(), + selected_channels: selected_channels, tags: note.tags ) end @@ -79,11 +89,14 @@ defmodule ChiyaWeb.NoteController do |> put_flash(:info, "Note updated successfully.") |> redirect(to: ~p"/admin/notes/#{note}") + # TODO: set channels from changeset when error happened? + {:error, %Ecto.Changeset{} = changeset} -> render(conn, :edit, note: note, changeset: changeset, channels: to_channel_options(), + selected_channels: nil, tags: note.tags ) end @@ -218,6 +231,14 @@ defmodule ChiyaWeb.NoteController do ) end + defp get_default_channels(conn = %Plug.Conn{}) do + if conn.assigns.settings.default_channel do + [conn.assigns.settings.default_channel.id] + else + [] + end + end + defp from_channel_ids(note_params) do selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1) diff --git a/lib/chiya_web/controllers/note_html.ex b/lib/chiya_web/controllers/note_html.ex index fa20b65..517fdd9 100644 --- a/lib/chiya_web/controllers/note_html.ex +++ b/lib/chiya_web/controllers/note_html.ex @@ -10,11 +10,9 @@ defmodule ChiyaWeb.NoteHTML do attr(:action, :string, required: true) attr(:channels, :list, required: true) attr(:tags, :list, required: true) + attr(:selected_channels, :list, required: true) def note_form(assigns) - def selected_channels(changeset), - do: Enum.map(changeset.data.channels, fn c -> c.id end) - def tags_to_string(tags), do: Enum.map_join(tags, ", ", fn t -> t.name end) end diff --git a/lib/chiya_web/controllers/note_html/edit.html.heex b/lib/chiya_web/controllers/note_html/edit.html.heex index d0b38f6..bb40517 100644 --- a/lib/chiya_web/controllers/note_html/edit.html.heex +++ b/lib/chiya_web/controllers/note_html/edit.html.heex @@ -7,6 +7,7 @@ changeset={@changeset} action={~p"/admin/notes/#{@note}"} channels={@channels} + selected_channels={@selected_channels} tags={@tags} /> diff --git a/lib/chiya_web/controllers/note_html/new.html.heex b/lib/chiya_web/controllers/note_html/new.html.heex index f916670..9058b0b 100644 --- a/lib/chiya_web/controllers/note_html/new.html.heex +++ b/lib/chiya_web/controllers/note_html/new.html.heex @@ -3,6 +3,11 @@ <:subtitle>Use this form to manage note records in your database. -<.note_form changeset={@changeset} action={~p"/admin/notes"} channels={@channels} tags={@tags} /> +<.note_form + changeset={@changeset} + action={~p"/admin/notes"} + channels={@channels} + tags={@tags} + selected_channels={@selected_channels} /> <.back navigate={~p"/admin/notes"}>Back to notes diff --git a/lib/chiya_web/controllers/note_html/note_form.html.heex b/lib/chiya_web/controllers/note_html/note_form.html.heex index 947e0cc..d853c62 100644 --- a/lib/chiya_web/controllers/note_html/note_form.html.heex +++ b/lib/chiya_web/controllers/note_html/note_form.html.heex @@ -21,7 +21,7 @@ label="Channel" multiple={true} options={@channels} - value={selected_channels(@changeset)} + value={@selected_channels} /> <:actions> From ca0c1a2aa3396da3ac51926e43024b857fb5d618 Mon Sep 17 00:00:00 2001 From: Inhji Date: Fri, 23 Jun 2023 06:59:38 +0200 Subject: [PATCH 5/5] ALL TESTS ARE GREEN --- test/chiya_web/controllers/channel_controller_test.exs | 2 ++ test/chiya_web/controllers/note_controller_test.exs | 8 ++++---- .../controllers/user_session_controller_test.exs | 4 +--- test/support/fixtures/channels_fixtures.ex | 3 +-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/test/chiya_web/controllers/channel_controller_test.exs b/test/chiya_web/controllers/channel_controller_test.exs index 72609b3..861ce86 100644 --- a/test/chiya_web/controllers/channel_controller_test.exs +++ b/test/chiya_web/controllers/channel_controller_test.exs @@ -7,12 +7,14 @@ defmodule ChiyaWeb.ChannelControllerTest do content: "some content", name: "some name", slug: "some slug", + layout: :default, visibility: :public } @update_attrs %{ content: "some updated content", name: "some updated name", slug: "some updated slug", + layout: :default, visibility: :private } @invalid_attrs %{content: nil, name: nil, slug: nil, visibility: nil} diff --git a/test/chiya_web/controllers/note_controller_test.exs b/test/chiya_web/controllers/note_controller_test.exs index f1c132d..48a6ddb 100644 --- a/test/chiya_web/controllers/note_controller_test.exs +++ b/test/chiya_web/controllers/note_controller_test.exs @@ -26,7 +26,7 @@ defmodule ChiyaWeb.NoteControllerTest do describe "index" do test "lists all notes", %{conn: conn} do conn = get(conn, ~p"/admin/notes") - assert html_response(conn, 200) =~ "Listing Notes" + assert html_response(conn, 200) =~ "Notes" end end @@ -45,7 +45,7 @@ defmodule ChiyaWeb.NoteControllerTest do assert redirected_to(conn) == ~p"/admin/notes/#{id}" conn = get(conn, ~p"/admin/notes/#{id}") - assert html_response(conn, 200) =~ "Note #{id}" + assert html_response(conn, 200) =~ @create_attrs.name end test "renders errors when data is invalid", %{conn: conn} do @@ -65,7 +65,7 @@ defmodule ChiyaWeb.NoteControllerTest do assert redirected_to(conn) == ~p"/admin/notes/#{id}" conn = get(conn, ~p"/admin/notes/#{id}") - assert html_response(conn, 200) =~ "Note #{id}" + assert html_response(conn, 200) =~ @create_attrs.name end end @@ -113,7 +113,7 @@ defmodule ChiyaWeb.NoteControllerTest do assert redirected_to(conn) == ~p"/admin/notes/#{note}" conn = get(conn, ~p"/admin/notes/#{note}") - assert html_response(conn, 200) =~ "some updated content" + assert html_response(conn, 200) =~ "some updated name" end end diff --git a/test/chiya_web/controllers/user_session_controller_test.exs b/test/chiya_web/controllers/user_session_controller_test.exs index f8c1017..60f0b09 100644 --- a/test/chiya_web/controllers/user_session_controller_test.exs +++ b/test/chiya_web/controllers/user_session_controller_test.exs @@ -20,9 +20,7 @@ defmodule ChiyaWeb.UserSessionControllerTest do # Now do a logged in request and assert on the menu conn = get(conn, ~p"/") response = html_response(conn, 200) - assert response =~ user.email - assert response =~ ~p"/user" - assert response =~ ~p"/user/log_out" + assert response =~ ~p"/admin" end test "logs the user in with remember me", %{conn: conn, user: user} do diff --git a/test/support/fixtures/channels_fixtures.ex b/test/support/fixtures/channels_fixtures.ex index 6df15a9..039c6c5 100644 --- a/test/support/fixtures/channels_fixtures.ex +++ b/test/support/fixtures/channels_fixtures.ex @@ -17,8 +17,7 @@ defmodule Chiya.ChannelsFixtures do attrs |> Enum.into(%{ content: "some content", - name: "some name", - slug: unique_channel_slug(), + name: "some name #{System.unique_integer([:positive])}", visibility: :public, layout: :default })