2020-02-18 07:57:00 +00:00
|
|
|
defmodule Mobilizon.Service.Workers.NotificationTest do
|
|
|
|
@moduledoc """
|
|
|
|
Test the scheduler module
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Events
|
|
|
|
alias Mobilizon.Events.{Event, Participant}
|
|
|
|
alias Mobilizon.Service.Workers.Notification
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
|
|
|
use Mobilizon.DataCase
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
import Swoosh.TestAssertions
|
2020-02-18 07:57:00 +00:00
|
|
|
import Mobilizon.Factory
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
@email "someone@somewhere.tld"
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
describe "A before_event_notification job sends an email" do
|
|
|
|
test "if the user is still participating" do
|
|
|
|
%User{id: user_id} = user = insert(:user)
|
|
|
|
|
|
|
|
settings =
|
|
|
|
insert(:settings,
|
|
|
|
user_id: user_id,
|
|
|
|
notification_before_event: true
|
|
|
|
)
|
|
|
|
|
2021-10-18 09:09:27 +00:00
|
|
|
user = %User{user | settings: settings}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
|
|
|
|
%Participant{id: participant_id} =
|
|
|
|
participant = insert(:participant, role: :participant, actor: actor)
|
|
|
|
|
2020-08-12 12:34:19 +00:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "before_event_notification", "participant_id" => participant_id}
|
|
|
|
})
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
assert_email_sent(to: participant.actor.user.email)
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "unless the person is no longer participating" do
|
|
|
|
%Event{id: event_id} = insert(:event)
|
|
|
|
|
|
|
|
%User{} = user = insert(:user)
|
|
|
|
%Actor{id: actor_id} = insert(:actor, user: user)
|
|
|
|
|
|
|
|
{:ok, %Participant{id: participant_id} = participant} =
|
|
|
|
Events.create_participant(%{actor_id: actor_id, event_id: event_id, role: :participant})
|
|
|
|
|
2021-10-18 09:09:27 +00:00
|
|
|
actor = %Actor{participant.actor | user: user}
|
|
|
|
event = Events.get_event_with_preload!(participant.event_id)
|
|
|
|
participant = %Participant{participant | actor: actor, event: event}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
assert {:ok, %{participant: %Participant{}}} = Events.delete_participant(participant)
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2020-08-12 12:34:19 +00:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "before_event_notification", "participant_id" => participant_id}
|
|
|
|
})
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
refute_email_sent()
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "unless the event has been cancelled" do
|
|
|
|
%Event{} = event = insert(:event, status: :cancelled)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
%Participant{id: participant_id} = insert(:participant, role: :participant, event: event)
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2020-08-12 12:34:19 +00:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "before_event_notification", "participant_id" => participant_id}
|
|
|
|
})
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
refute_email_sent()
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "A on_day_notification job sends an email" do
|
|
|
|
test "if the user is still participating" do
|
|
|
|
%User{id: user_id} = user = insert(:user)
|
|
|
|
|
|
|
|
settings =
|
|
|
|
insert(:settings, user_id: user_id, notification_on_day: true, timezone: "Europe/Paris")
|
|
|
|
|
|
|
|
user = Map.put(user, :settings, settings)
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
%Participant{} = insert(:participant, role: :participant, actor: actor)
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2020-08-12 12:34:19 +00:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "on_day_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
assert_email_sent(to: user.email)
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "unless the person is no longer participating" do
|
2021-10-18 09:09:27 +00:00
|
|
|
%Event{id: event_id} = event = insert(:event)
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
%User{id: user_id} = user = insert(:user)
|
|
|
|
|
|
|
|
settings =
|
|
|
|
insert(:settings, user_id: user_id, notification_on_day: true, timezone: "Europe/Paris")
|
|
|
|
|
|
|
|
user = Map.put(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)
|
2021-10-18 09:09:27 +00:00
|
|
|
participant = %Participant{participant | actor: actor, event: event}
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
assert {:ok, %{participant: %Participant{}}} = Events.delete_participant(participant)
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2020-08-12 12:34:19 +00:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "on_day_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
refute_email_sent()
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "unless the event has been cancelled" do
|
|
|
|
%User{id: user_id} = user = insert(:user)
|
|
|
|
|
|
|
|
settings =
|
|
|
|
insert(:settings, user_id: user_id, notification_on_day: true, timezone: "Europe/Paris")
|
|
|
|
|
|
|
|
user = Map.put(user, :settings, settings)
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
%Event{} = event = insert(:event, status: :cancelled)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
%Participant{} = insert(:participant, role: :participant, event: event, actor: actor)
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2020-08-12 12:34:19 +00:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "on_day_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
refute_email_sent()
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with a lot of events" do
|
2022-04-05 10:16:22 +00:00
|
|
|
%User{id: user_id} = user = insert(:user, email: @email)
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
settings =
|
|
|
|
insert(:settings, user_id: user_id, notification_on_day: true, timezone: "Europe/Paris")
|
|
|
|
|
|
|
|
user = Map.put(user, :settings, settings)
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
Enum.reduce(0..10, [], fn _i, acc ->
|
|
|
|
%Participant{} = participant = insert(:participant, role: :participant, actor: actor)
|
|
|
|
acc ++ [participant]
|
|
|
|
end)
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2020-08-12 12:34:19 +00:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "on_day_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
assert_email_sent(to: @email, subject: "11 events planned today")
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
end
|
2020-06-05 08:12:08 +00:00
|
|
|
|
|
|
|
describe "A weekly_notification job sends an email" do
|
|
|
|
test "if the user is still participating" do
|
|
|
|
%User{id: user_id} = user = insert(:user)
|
|
|
|
|
|
|
|
settings =
|
2023-08-17 11:18:01 +00:00
|
|
|
insert(:settings,
|
|
|
|
user_id: user_id,
|
|
|
|
notification_each_week: true,
|
|
|
|
timezone: "Europe/Paris"
|
|
|
|
)
|
2020-06-05 08:12:08 +00:00
|
|
|
|
|
|
|
user = Map.put(user, :settings, settings)
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
%Participant{} = insert(:participant, role: :participant, actor: actor)
|
2020-06-05 08:12:08 +00:00
|
|
|
|
2020-08-12 12:34:19 +00:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "weekly_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-06-05 08:12:08 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
assert_email_sent(to: user.email)
|
2020-06-05 08:12:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "unless the person is no longer participating" do
|
2021-10-18 09:09:27 +00:00
|
|
|
%Event{id: event_id} = event = insert(:event)
|
2020-06-05 08:12:08 +00:00
|
|
|
|
|
|
|
%User{id: user_id} = user = insert(:user)
|
|
|
|
|
|
|
|
settings =
|
2023-08-17 11:18:01 +00:00
|
|
|
insert(:settings,
|
|
|
|
user_id: user_id,
|
|
|
|
notification_each_week: true,
|
|
|
|
timezone: "Europe/Paris"
|
|
|
|
)
|
2020-06-05 08:12:08 +00:00
|
|
|
|
2021-10-18 09:09:27 +00:00
|
|
|
user = %User{user | settings: settings}
|
2020-06-05 08:12:08 +00:00
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
|
|
|
|
{:ok, %Participant{} = participant} =
|
|
|
|
Events.create_participant(%{actor_id: actor.id, event_id: event_id, role: :participant})
|
|
|
|
|
2021-10-18 09:09:27 +00:00
|
|
|
actor = %Actor{participant.actor | user: user}
|
|
|
|
participant = %Participant{participant | actor: actor, event: event}
|
2020-06-05 08:12:08 +00:00
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
assert {:ok, %{participant: %Participant{}}} = Events.delete_participant(participant)
|
2020-06-05 08:12:08 +00:00
|
|
|
|
2020-08-12 12:34:19 +00:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "weekly_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-06-05 08:12:08 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
refute_email_sent()
|
2020-06-05 08:12:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "unless the event has been cancelled" do
|
|
|
|
%User{id: user_id} = user = insert(:user)
|
|
|
|
|
|
|
|
settings =
|
2023-08-17 11:18:01 +00:00
|
|
|
insert(:settings,
|
|
|
|
user_id: user_id,
|
|
|
|
notification_each_week: true,
|
|
|
|
timezone: "Europe/Paris"
|
|
|
|
)
|
2020-06-05 08:12:08 +00:00
|
|
|
|
|
|
|
user = Map.put(user, :settings, settings)
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
%Event{} = event = insert(:event, status: :cancelled)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
%Participant{} = insert(:participant, role: :participant, event: event, actor: actor)
|
2020-06-05 08:12:08 +00:00
|
|
|
|
2020-08-12 12:34:19 +00:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "weekly_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-06-05 08:12:08 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
refute_email_sent()
|
2020-06-05 08:12:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with a lot of events" do
|
2022-04-05 10:16:22 +00:00
|
|
|
%User{id: user_id} = user = insert(:user, email: @email)
|
2020-06-05 08:12:08 +00:00
|
|
|
|
|
|
|
settings =
|
2023-08-17 11:18:01 +00:00
|
|
|
insert(:settings,
|
|
|
|
user_id: user_id,
|
|
|
|
notification_each_week: true,
|
|
|
|
timezone: "Europe/Paris"
|
|
|
|
)
|
2020-06-05 08:12:08 +00:00
|
|
|
|
|
|
|
user = Map.put(user, :settings, settings)
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
Enum.reduce(0..10, [], fn _i, acc ->
|
|
|
|
%Participant{} = participant = insert(:participant, role: :participant, actor: actor)
|
|
|
|
acc ++ [participant]
|
|
|
|
end)
|
2020-06-05 08:12:08 +00:00
|
|
|
|
2020-08-12 12:34:19 +00:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "weekly_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-06-05 08:12:08 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
assert_email_sent(to: @email, subject: "11 events planned this week")
|
2020-06-05 08:12:08 +00:00
|
|
|
end
|
|
|
|
end
|
2020-06-08 10:28:19 +00:00
|
|
|
|
|
|
|
describe "A pending_participation_notification job sends an email" do
|
|
|
|
test "if there are participants to approve" do
|
|
|
|
%User{id: user_id} = user = insert(:user)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
insert(:settings,
|
|
|
|
user_id: user_id,
|
|
|
|
timezone: "Europe/Paris"
|
|
|
|
)
|
2021-11-15 14:56:28 +00:00
|
|
|
|
2020-06-08 10:28:19 +00:00
|
|
|
%Event{id: event_id} = event = insert(:event)
|
|
|
|
|
|
|
|
%Participant{} = insert(:participant, event: event, role: :not_approved)
|
|
|
|
%Participant{} = insert(:participant, event: event, role: :not_approved)
|
|
|
|
|
2020-08-12 12:34:19 +00:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{
|
2020-06-08 10:28:19 +00:00
|
|
|
"op" => "pending_participation_notification",
|
|
|
|
"user_id" => user_id,
|
|
|
|
"event_id" => event_id
|
2020-08-12 12:34:19 +00:00
|
|
|
}
|
|
|
|
})
|
2020-06-08 10:28:19 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
assert_email_sent(to: user.email)
|
2020-06-08 10:28:19 +00:00
|
|
|
end
|
|
|
|
end
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|