From f24ec894086840d329b1ab42d936266c6cea2d20 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 18 Oct 2021 11:09:27 +0200 Subject: [PATCH] Fix event notification tests Signed-off-by: Thomas Citharel --- lib/mobilizon/events/events.ex | 5 +++ lib/mobilizon/events/participant.ex | 2 +- lib/service/workers/notification.ex | 36 +++++++++++++------ lib/web/templates/email/email.html.heex | 3 -- .../templates/email/password_reset.html.heex | 2 +- test/graphql/resolvers/event_test.exs | 4 +-- test/service/workers/notification_test.exs | 19 +++++----- 7 files changed, 45 insertions(+), 26 deletions(-) diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index 83001cb1..6960acc9 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -738,6 +738,11 @@ defmodule Mobilizon.Events do |> Repo.one() end + @spec get_participant!(integer | String.t()) :: Participant.t() + def get_participant!(participant_id) do + Repo.get_by!(Participant, id: participant_id) + end + @doc """ Gets a single participation for an event and actor. diff --git a/lib/mobilizon/events/participant.ex b/lib/mobilizon/events/participant.ex index 550b0460..0c4f836f 100644 --- a/lib/mobilizon/events/participant.ex +++ b/lib/mobilizon/events/participant.ex @@ -109,7 +109,7 @@ defmodule Mobilizon.Events.Participant do # No lookalike symbols @symbols '6789BCDFGHJKLMNPQRTW' - @symbol_count Enum.count(@symbols) + @symbol_count Enum.count(@symbols) - 1 @code_length 6 @spec generate_code :: String.t() diff --git a/lib/service/workers/notification.ex b/lib/service/workers/notification.ex index 004ef8e0..10d13c09 100644 --- a/lib/service/workers/notification.ex +++ b/lib/service/workers/notification.ex @@ -3,11 +3,10 @@ defmodule Mobilizon.Service.Workers.Notification do Worker to send notifications """ + alias Mobilizon.{Actors, Events, Users} alias Mobilizon.Actors.Actor - alias Mobilizon.Events alias Mobilizon.Events.{Event, Participant} alias Mobilizon.Storage.Page - alias Mobilizon.Users alias Mobilizon.Users.{Setting, User} alias Mobilizon.Web.Email.{Mailer, Notification} @@ -22,12 +21,19 @@ defmodule Mobilizon.Service.Workers.Notification do def perform(%Job{ args: %{"op" => "before_event_notification", "participant_id" => participant_id} }) do - with %Participant{actor: %Actor{user_id: user_id}, event: %Event{status: :confirmed}} = - participant <- Events.get_participant(participant_id), - %User{email: email, locale: locale, settings: %Setting{notification_before_event: true}} <- - Users.get_user_with_settings!(user_id) do + with %Participant{} = participant <- Events.get_participant(participant_id), + %Event{status: :confirmed} = event <- + Events.get_event_with_preload!(participant.event_id), + %Actor{user_id: user_id} = actor when not is_nil(user_id) <- + Actors.get_actor_with_preload!(participant.actor_id) do + %User{email: email, locale: locale, settings: %Setting{notification_before_event: true}} = + Users.get_user_with_settings!(user_id) + email - |> Notification.before_event_notification(participant, locale) + |> Notification.before_event_notification( + %Participant{participant | event: event, actor: actor}, + locale + ) |> Mailer.send_email_later() :ok @@ -50,7 +56,12 @@ defmodule Mobilizon.Service.Workers.Notification do Enum.filter(participations, fn participation -> participation.event.status == :confirmed end), - true <- length(participations) > 0 do + true <- length(participations) > 0, + participations <- + Enum.map(participations, fn participation -> + %Event{} = event = Events.get_event_with_preload!(participation.event_id) + %Participant{participation | event: event} + end) do user |> Notification.on_day_notification(participations, total, locale) |> Mailer.send_email_later() @@ -79,7 +90,12 @@ defmodule Mobilizon.Service.Workers.Notification do Enum.filter(participations, fn participation -> participation.event.status == :confirmed end), - true <- length(participations) > 0 do + true <- length(participations) > 0, + participations <- + Enum.map(participations, fn participation -> + %Event{} = event = Events.get_event_with_preload!(participation.event_id) + %Participant{participation | event: event} + end) do user |> Notification.weekly_notification(participations, total, locale) |> Mailer.send_email_later() @@ -99,7 +115,7 @@ defmodule Mobilizon.Service.Workers.Notification do } }) do with %User{} = user <- Users.get_user(user_id), - {:ok, %Event{} = event} <- Events.get_event(event_id), + {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id), %Page{total: total} when total > 0 <- Events.list_participants_for_event(event_id, [:not_approved]) do user diff --git a/lib/web/templates/email/email.html.heex b/lib/web/templates/email/email.html.heex index 61af9051..6f480261 100644 --- a/lib/web/templates/email/email.html.heex +++ b/lib/web/templates/email/email.html.heex @@ -1,6 +1,3 @@ - - - diff --git a/lib/web/templates/email/password_reset.html.heex b/lib/web/templates/email/password_reset.html.heex index 72e00784..a9973b02 100644 --- a/lib/web/templates/email/password_reset.html.heex +++ b/lib/web/templates/email/password_reset.html.heex @@ -57,7 +57,7 @@ - diff --git a/test/graphql/resolvers/event_test.exs b/test/graphql/resolvers/event_test.exs index 6ec80707..0f5489b7 100644 --- a/test/graphql/resolvers/event_test.exs +++ b/test/graphql/resolvers/event_test.exs @@ -1009,8 +1009,8 @@ defmodule Mobilizon.Web.Resolvers.EventTest do assert_delivered_email( Email.Event.event_updated( - creator, user.email, + creator, actor, event, new_event, @@ -1020,8 +1020,8 @@ defmodule Mobilizon.Web.Resolvers.EventTest do assert_delivered_email( Email.Event.event_updated( - participant, participant_user.email, + participant, participant_actor, event, new_event, diff --git a/test/service/workers/notification_test.exs b/test/service/workers/notification_test.exs index 3503faa3..cdf84edf 100644 --- a/test/service/workers/notification_test.exs +++ b/test/service/workers/notification_test.exs @@ -25,7 +25,7 @@ defmodule Mobilizon.Service.Workers.NotificationTest do notification_before_event: true ) - user = Map.put(user, :settings, settings) + user = %User{user | settings: settings} %Actor{} = actor = insert(:actor, user: user) @@ -53,8 +53,9 @@ defmodule Mobilizon.Service.Workers.NotificationTest do {:ok, %Participant{id: participant_id} = participant} = Events.create_participant(%{actor_id: actor_id, event_id: event_id, role: :participant}) - actor = Map.put(participant.actor, :user, user) - participant = Map.put(participant, :actor, actor) + actor = %Actor{participant.actor | user: user} + event = Events.get_event_with_preload!(participant.event_id) + participant = %Participant{participant | actor: actor, event: event} assert {:ok, %{participant: %Participant{}}} = Events.delete_participant(participant) @@ -115,7 +116,7 @@ defmodule Mobilizon.Service.Workers.NotificationTest do end test "unless the person is no longer participating" do - %Event{id: event_id} = insert(:event) + %Event{id: event_id} = event = insert(:event) %User{id: user_id} = user = insert(:user) @@ -129,7 +130,7 @@ defmodule Mobilizon.Service.Workers.NotificationTest do Events.create_participant(%{actor_id: actor.id, event_id: event_id, role: :participant}) actor = Map.put(participant.actor, :user, user) - participant = Map.put(participant, :actor, actor) + participant = %Participant{participant | actor: actor, event: event} assert {:ok, %{participant: %Participant{}}} = Events.delete_participant(participant) @@ -227,21 +228,21 @@ defmodule Mobilizon.Service.Workers.NotificationTest do end test "unless the person is no longer participating" do - %Event{id: event_id} = insert(:event) + %Event{id: event_id} = event = insert(:event) %User{id: user_id} = user = insert(:user) settings = insert(:settings, user_id: user_id, notification_each_week: true, timezone: "Europe/Paris") - user = Map.put(user, :settings, settings) + user = %User{user | settings: settings} %Actor{} = actor = insert(:actor, user: user) {:ok, %Participant{} = participant} = Events.create_participant(%{actor_id: actor.id, event_id: event_id, role: :participant}) - actor = Map.put(participant.actor, :user, user) - participant = Map.put(participant, :actor, actor) + actor = %Actor{participant.actor | user: user} + participant = %Participant{participant | actor: actor, event: event} assert {:ok, %{participant: %Participant{}}} = Events.delete_participant(participant)
+ <%= gettext "Reset Password" %>