2020-02-18 08:57:00 +01: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
|
|
|
|
alias Mobilizon.Web.Email.Notification, as: NotificationMailer
|
|
|
|
|
|
|
|
use Mobilizon.DataCase
|
|
|
|
use Bamboo.Test
|
|
|
|
|
|
|
|
import Mobilizon.Factory
|
|
|
|
|
|
|
|
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 11:09:27 +02:00
|
|
|
user = %User{user | settings: settings}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
|
|
|
|
%Participant{id: participant_id} =
|
|
|
|
participant = insert(:participant, role: :participant, actor: actor)
|
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "before_event_notification", "participant_id" => participant_id}
|
|
|
|
})
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
assert_delivered_email(
|
|
|
|
NotificationMailer.before_event_notification(
|
|
|
|
participant.actor.user.email,
|
|
|
|
participant
|
|
|
|
)
|
|
|
|
)
|
|
|
|
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 11:09:27 +02: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 08:57:00 +01:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
assert {:ok, %{participant: %Participant{}}} = Events.delete_participant(participant)
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "before_event_notification", "participant_id" => participant_id}
|
|
|
|
})
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
refute_delivered_email(
|
|
|
|
NotificationMailer.before_event_notification(
|
|
|
|
participant.actor.user.email,
|
|
|
|
participant
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "unless the event has been cancelled" do
|
|
|
|
%Event{} = event = insert(:event, status: :cancelled)
|
|
|
|
|
|
|
|
%Participant{id: participant_id} =
|
|
|
|
participant = insert(:participant, role: :participant, event: event)
|
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "before_event_notification", "participant_id" => participant_id}
|
|
|
|
})
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
refute_delivered_email(
|
|
|
|
NotificationMailer.before_event_notification(
|
|
|
|
participant.actor.user.email,
|
|
|
|
participant
|
|
|
|
)
|
|
|
|
)
|
|
|
|
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)
|
|
|
|
|
|
|
|
%Participant{} = participant = insert(:participant, role: :participant, actor: actor)
|
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "on_day_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
assert_delivered_email(
|
|
|
|
NotificationMailer.on_day_notification(
|
|
|
|
user,
|
|
|
|
[participant],
|
|
|
|
1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "unless the person is no longer participating" do
|
2021-10-18 11:09:27 +02:00
|
|
|
%Event{id: event_id} = event = insert(:event)
|
2020-02-18 08:57:00 +01: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 11:09:27 +02:00
|
|
|
participant = %Participant{participant | actor: actor, event: event}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
assert {:ok, %{participant: %Participant{}}} = Events.delete_participant(participant)
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "on_day_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
refute_delivered_email(
|
|
|
|
NotificationMailer.on_day_notification(
|
|
|
|
user,
|
|
|
|
[participant],
|
|
|
|
1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
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)
|
|
|
|
|
|
|
|
%Participant{} =
|
|
|
|
participant = insert(:participant, role: :participant, event: event, actor: actor)
|
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "on_day_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
refute_delivered_email(
|
|
|
|
NotificationMailer.on_day_notification(
|
|
|
|
user,
|
|
|
|
[participant],
|
|
|
|
1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a lot of events" 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)
|
|
|
|
|
|
|
|
participants =
|
|
|
|
Enum.reduce(0..10, [], fn _i, acc ->
|
|
|
|
%Participant{} = participant = insert(:participant, role: :participant, actor: actor)
|
|
|
|
acc ++ [participant]
|
|
|
|
end)
|
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "on_day_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
refute_delivered_email(
|
|
|
|
NotificationMailer.on_day_notification(
|
|
|
|
user,
|
|
|
|
participants,
|
|
|
|
3
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-06-05 10:12:08 +02: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 =
|
|
|
|
insert(:settings, user_id: user_id, notification_each_week: true, timezone: "Europe/Paris")
|
|
|
|
|
|
|
|
user = Map.put(user, :settings, settings)
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
|
|
|
|
%Participant{} = participant = insert(:participant, role: :participant, actor: actor)
|
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "weekly_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-06-05 10:12:08 +02:00
|
|
|
|
|
|
|
assert_delivered_email(
|
|
|
|
NotificationMailer.weekly_notification(
|
|
|
|
user,
|
|
|
|
[participant],
|
|
|
|
1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "unless the person is no longer participating" do
|
2021-10-18 11:09:27 +02:00
|
|
|
%Event{id: event_id} = event = insert(:event)
|
2020-06-05 10:12:08 +02:00
|
|
|
|
|
|
|
%User{id: user_id} = user = insert(:user)
|
|
|
|
|
|
|
|
settings =
|
|
|
|
insert(:settings, user_id: user_id, notification_each_week: true, timezone: "Europe/Paris")
|
|
|
|
|
2021-10-18 11:09:27 +02:00
|
|
|
user = %User{user | settings: settings}
|
2020-06-05 10:12:08 +02: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 11:09:27 +02:00
|
|
|
actor = %Actor{participant.actor | user: user}
|
|
|
|
participant = %Participant{participant | actor: actor, event: event}
|
2020-06-05 10:12:08 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
assert {:ok, %{participant: %Participant{}}} = Events.delete_participant(participant)
|
2020-06-05 10:12:08 +02:00
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "weekly_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-06-05 10:12:08 +02:00
|
|
|
|
|
|
|
refute_delivered_email(
|
|
|
|
NotificationMailer.weekly_notification(
|
|
|
|
user,
|
|
|
|
[participant],
|
|
|
|
1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "unless the event has been cancelled" do
|
|
|
|
%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)
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
%Event{} = event = insert(:event, status: :cancelled)
|
|
|
|
|
|
|
|
%Participant{} =
|
|
|
|
participant = insert(:participant, role: :participant, event: event, actor: actor)
|
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "weekly_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-06-05 10:12:08 +02:00
|
|
|
|
|
|
|
refute_delivered_email(
|
|
|
|
NotificationMailer.weekly_notification(
|
|
|
|
user,
|
|
|
|
[participant],
|
|
|
|
1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a lot of events" do
|
|
|
|
%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)
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
|
|
|
|
participants =
|
|
|
|
Enum.reduce(0..10, [], fn _i, acc ->
|
|
|
|
%Participant{} = participant = insert(:participant, role: :participant, actor: actor)
|
|
|
|
acc ++ [participant]
|
|
|
|
end)
|
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{"op" => "weekly_notification", "user_id" => user_id}
|
|
|
|
})
|
2020-06-05 10:12:08 +02:00
|
|
|
|
|
|
|
refute_delivered_email(
|
|
|
|
NotificationMailer.weekly_notification(
|
|
|
|
user,
|
|
|
|
participants,
|
|
|
|
3
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-06-08 12:28:19 +02: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)
|
|
|
|
|
|
|
|
%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 14:34:19 +02:00
|
|
|
Notification.perform(%Oban.Job{
|
|
|
|
args: %{
|
2020-06-08 12:28:19 +02:00
|
|
|
"op" => "pending_participation_notification",
|
|
|
|
"user_id" => user_id,
|
|
|
|
"event_id" => event_id
|
2020-08-12 14:34:19 +02:00
|
|
|
}
|
|
|
|
})
|
2020-06-08 12:28:19 +02:00
|
|
|
|
|
|
|
assert_delivered_email(
|
|
|
|
NotificationMailer.pending_participation_notification(
|
|
|
|
user,
|
|
|
|
event,
|
|
|
|
2
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|