From 0a5d97e457ff4be587b13b084fb74a16667321d5 Mon Sep 17 00:00:00 2001 From: Jannik Streek Date: Mon, 18 Nov 2024 17:17:57 +0100 Subject: [PATCH] fix tests and cleanup --- lib/mindwendel/accounts.ex | 2 +- lib/mindwendel_web/live/idea_live/card_component.ex | 2 +- .../live/label_live/captions_component.ex | 3 +-- lib/mindwendel_web/live/live_helpers.ex | 2 +- priv/gettext/de/LC_MESSAGES/default.po | 6 +++--- priv/gettext/default.pot | 6 +++--- priv/gettext/en/LC_MESSAGES/default.po | 6 +++--- test/mindwendel/accounts_test.exs | 2 +- .../mindwendel_web/live/label_live/captions_test.exs | 12 +++++++----- 9 files changed, 21 insertions(+), 20 deletions(-) diff --git a/lib/mindwendel/accounts.ex b/lib/mindwendel/accounts.ex index 57681d98..eadc29ba 100644 --- a/lib/mindwendel/accounts.ex +++ b/lib/mindwendel/accounts.ex @@ -48,7 +48,7 @@ defmodule Mindwendel.Accounts do end def get_user(id) do - Repo.get(User, id) |> Repo.preload(:brainstormings) + Repo.get(User, id) |> Repo.preload([:brainstormings, :moderated_brainstormings]) rescue Ecto.Query.CastError -> nil end diff --git a/lib/mindwendel_web/live/idea_live/card_component.ex b/lib/mindwendel_web/live/idea_live/card_component.ex index 19fc768b..36b468e5 100644 --- a/lib/mindwendel_web/live/idea_live/card_component.ex +++ b/lib/mindwendel_web/live/idea_live/card_component.ex @@ -10,7 +10,7 @@ defmodule MindwendelWeb.IdeaLive.CardComponent do %{current_user: current_user, brainstorming: brainstorming} = socket.assigns - if current_user.id in [idea.user_id | brainstorming.moderating_users |> Enum.map(& &1.id)] do + if has_moderating_or_ownership_permission(brainstorming, idea, current_user) do {:ok, _} = Ideas.delete_idea(idea) end diff --git a/lib/mindwendel_web/live/label_live/captions_component.ex b/lib/mindwendel_web/live/label_live/captions_component.ex index 82108e9d..ba471259 100644 --- a/lib/mindwendel_web/live/label_live/captions_component.ex +++ b/lib/mindwendel_web/live/label_live/captions_component.ex @@ -4,8 +4,7 @@ defmodule MindwendelWeb.LabelLive.CaptionsComponent do alias Mindwendel.Brainstormings def handle_event("set_filter_idea_label", %{"id" => idea_label_id}, socket) do - brainstorming = socket.assigns.brainstorming - current_user = socket.assigns.current_user + %{current_user: current_user, brainstorming: brainstorming} = socket.assigns if has_moderating_permission(brainstorming, current_user) do # If the filter is already present, remove it as its toggled. If not, add it. diff --git a/lib/mindwendel_web/live/live_helpers.ex b/lib/mindwendel_web/live/live_helpers.ex index 84e98c03..1983f1ce 100644 --- a/lib/mindwendel_web/live/live_helpers.ex +++ b/lib/mindwendel_web/live/live_helpers.ex @@ -9,7 +9,7 @@ defmodule MindwendelWeb.LiveHelpers do end def has_moderating_permission(brainstorming, current_user) do - Enum.member?(brainstorming.moderating_users |> Enum.map(& &1.id), current_user.id) + Enum.member?(current_user.moderated_brainstormings |> Enum.map(& &1.id), brainstorming.id) end def has_ownership(record, current_user) do diff --git a/priv/gettext/de/LC_MESSAGES/default.po b/priv/gettext/de/LC_MESSAGES/default.po index 6fd797a0..315a3272 100644 --- a/priv/gettext/de/LC_MESSAGES/default.po +++ b/priv/gettext/de/LC_MESSAGES/default.po @@ -26,12 +26,12 @@ msgstr "Wie können wir ..." msgid "Ready?" msgstr "Fertig?" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:168 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:174 #, elixir-autogen, elixir-format msgid "%{name} - Edit" msgstr "%{name} - Editieren" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:145 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:151 #, elixir-autogen, elixir-format msgid "%{name} - New Idea" msgstr "%{name} - Neue Idee" @@ -403,7 +403,7 @@ msgstr "Löschen" msgid "Type the label name" msgstr "Gebe dem Label einen Namen" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:155 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:161 #, elixir-autogen, elixir-format, fuzzy msgid "%{name} - New Lane" msgstr "%{name} - Neue Idee" diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index ce5580c0..5cfa5827 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -25,12 +25,12 @@ msgstr "" msgid "Ready?" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:168 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:174 #, elixir-autogen, elixir-format msgid "%{name} - Edit" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:145 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:151 #, elixir-autogen, elixir-format msgid "%{name} - New Idea" msgstr "" @@ -402,7 +402,7 @@ msgstr "" msgid "Type the label name" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:155 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:161 #, elixir-autogen, elixir-format msgid "%{name} - New Lane" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index a490ebf3..9bab3f64 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -26,12 +26,12 @@ msgstr "" msgid "Ready?" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:168 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:174 #, elixir-autogen, elixir-format msgid "%{name} - Edit" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:145 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:151 #, elixir-autogen, elixir-format msgid "%{name} - New Idea" msgstr "" @@ -403,7 +403,7 @@ msgstr "" msgid "Type the label name" msgstr "" -#: lib/mindwendel_web/live/brainstorming_live/show.ex:155 +#: lib/mindwendel_web/live/brainstorming_live/show.ex:161 #, elixir-autogen, elixir-format, fuzzy msgid "%{name} - New Lane" msgstr "" diff --git a/test/mindwendel/accounts_test.exs b/test/mindwendel/accounts_test.exs index ae0b3f52..d8e067ef 100644 --- a/test/mindwendel/accounts_test.exs +++ b/test/mindwendel/accounts_test.exs @@ -71,7 +71,7 @@ defmodule Mindwendel.AccountsTest do describe "get_user" do test "returns user when it exists", %{user: existing_user} do - assert existing_user |> Repo.preload(:brainstormings) == + assert existing_user |> Repo.preload([:brainstormings, :moderated_brainstormings]) == Accounts.get_user(existing_user.id) end diff --git a/test/mindwendel_web/live/label_live/captions_test.exs b/test/mindwendel_web/live/label_live/captions_test.exs index 0a205195..21c6e5d3 100644 --- a/test/mindwendel_web/live/label_live/captions_test.exs +++ b/test/mindwendel_web/live/label_live/captions_test.exs @@ -3,6 +3,7 @@ defmodule MindwendelWeb.LabelLive.CaptionsTest do import Phoenix.LiveViewTest + alias Mindwendel.Accounts alias Mindwendel.Brainstormings alias Mindwendel.Factory @@ -16,20 +17,21 @@ defmodule MindwendelWeb.LabelLive.CaptionsTest do brainstorming: brainstorming, user: user } do - preloaded_braisntorming = Brainstormings.get_brainstorming!(brainstorming.id) + preloaded_brainstorming = Brainstormings.get_brainstorming!(brainstorming.id) + preloaded_user = Accounts.get_user(user.id) captions_component = render_component(CaptionsComponent, id: "captions", - brainstorming: preloaded_braisntorming, - current_user: user + brainstorming: preloaded_brainstorming, + current_user: preloaded_user ) # make sure that there is at least one label in the list: - assert length(preloaded_braisntorming.labels) > 0 + assert length(preloaded_brainstorming.labels) > 0 - Enum.each(brainstorming.labels, fn label -> + Enum.each(preloaded_brainstorming.labels, fn label -> assert captions_component =~ label.name end) end