2021-06-03 17:17:13 +02:00
|
|
|
defmodule Mobilizon.Service.Notifier.EmailTest do
|
|
|
|
@moduledoc """
|
|
|
|
Test the Email notifier module
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Activities.Activity
|
2022-05-10 13:13:48 +02:00
|
|
|
alias Mobilizon.{Config, Users}
|
2021-06-03 17:17:13 +02:00
|
|
|
alias Mobilizon.Service.Notifier.Email
|
|
|
|
alias Mobilizon.Users.{ActivitySetting, Setting, User}
|
|
|
|
|
|
|
|
use Mobilizon.DataCase
|
2022-04-05 12:16:22 +02:00
|
|
|
import Swoosh.TestAssertions
|
2021-06-03 17:17:13 +02:00
|
|
|
import Mobilizon.Factory
|
|
|
|
|
|
|
|
describe "Returns if the module is loaded" do
|
|
|
|
test "Loaded by default" do
|
|
|
|
assert Email.ready?() == true
|
|
|
|
end
|
|
|
|
|
|
|
|
test "If disabled" do
|
|
|
|
Config.put([Email, :enabled], false)
|
|
|
|
assert Email.ready?() == false
|
|
|
|
Config.put([Email, :enabled], true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "sending email for activities" do
|
|
|
|
test "when the user doesn't allow it" do
|
|
|
|
%Activity{} = activity = insert(:mobilizon_activity, inserted_at: DateTime.utc_now())
|
2021-11-03 08:50:28 +01:00
|
|
|
%User{} = user = insert(:user, activity_settings: [])
|
2021-06-03 17:17:13 +02:00
|
|
|
%Setting{} = user_settings = insert(:settings, user_id: user.id, group_notifications: :none)
|
|
|
|
user = %User{user | settings: user_settings}
|
|
|
|
|
|
|
|
assert {:ok, :skipped} == Email.send(user, activity)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
refute_email_sent()
|
2021-06-03 17:17:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "when the user allows it" do
|
|
|
|
%Activity{} = activity = insert(:mobilizon_activity, inserted_at: DateTime.utc_now())
|
|
|
|
%User{} = user = insert(:user)
|
2021-06-26 15:23:22 +02:00
|
|
|
|
|
|
|
%Setting{} =
|
|
|
|
user_settings = insert(:settings, user_id: user.id, group_notifications: :direct)
|
|
|
|
|
|
|
|
%ActivitySetting{} =
|
|
|
|
activity_setting = insert(:mobilizon_activity_setting, user_id: user.id, user: user)
|
|
|
|
|
|
|
|
user = %User{user | settings: user_settings, activity_settings: [activity_setting]}
|
|
|
|
|
|
|
|
assert {:ok, :sent} == Email.send(user, activity)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
assert_email_sent(to: user.email)
|
2021-06-26 15:23:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "if it's been an hour since the last notification" do
|
|
|
|
%Activity{} = activity = insert(:mobilizon_activity, inserted_at: DateTime.utc_now())
|
|
|
|
%User{} = user = insert(:user)
|
|
|
|
|
|
|
|
%Setting{} =
|
|
|
|
user_settings =
|
|
|
|
insert(:settings,
|
|
|
|
user_id: user.id,
|
|
|
|
group_notifications: :one_hour,
|
|
|
|
last_notification_sent: DateTime.add(DateTime.utc_now(), -3_659)
|
|
|
|
)
|
|
|
|
|
|
|
|
%ActivitySetting{} =
|
|
|
|
activity_setting = insert(:mobilizon_activity_setting, user_id: user.id, user: user)
|
|
|
|
|
|
|
|
user = %User{user | settings: user_settings, activity_settings: [activity_setting]}
|
|
|
|
|
|
|
|
assert {:ok, :sent} == Email.send(user, activity)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
assert_email_sent(to: user.email)
|
2021-06-26 15:23:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "if there's no delay since the last notification" do
|
|
|
|
%Activity{} = activity = insert(:mobilizon_activity, inserted_at: DateTime.utc_now())
|
|
|
|
%User{} = user = insert(:user)
|
|
|
|
|
|
|
|
%Setting{} =
|
|
|
|
user_settings =
|
|
|
|
insert(:settings,
|
|
|
|
user_id: user.id,
|
|
|
|
group_notifications: :one_hour,
|
|
|
|
last_notification_sent: nil
|
|
|
|
)
|
2021-06-03 17:17:13 +02:00
|
|
|
|
|
|
|
%ActivitySetting{} =
|
|
|
|
activity_setting = insert(:mobilizon_activity_setting, user_id: user.id, user: user)
|
|
|
|
|
|
|
|
user = %User{user | settings: user_settings, activity_settings: [activity_setting]}
|
|
|
|
|
|
|
|
assert {:ok, :sent} == Email.send(user, activity)
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
assert_email_sent(to: user.email)
|
2021-06-03 17:17:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "not if we already have sent notifications" do
|
|
|
|
%Activity{} = activity = insert(:mobilizon_activity, inserted_at: DateTime.utc_now())
|
|
|
|
%User{} = user = insert(:user)
|
|
|
|
|
2022-05-10 13:13:48 +02:00
|
|
|
old = DateTime.add(DateTime.utc_now(), -3600 * 24 * 3)
|
|
|
|
|
2021-06-03 17:17:13 +02:00
|
|
|
%Setting{} =
|
|
|
|
user_settings =
|
|
|
|
insert(:settings,
|
|
|
|
user_id: user.id,
|
|
|
|
group_notifications: :one_day,
|
2022-05-10 13:13:48 +02:00
|
|
|
last_notification_sent: old
|
2021-06-03 17:17:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
%ActivitySetting{} =
|
|
|
|
activity_setting = insert(:mobilizon_activity_setting, user_id: user.id, user: user)
|
|
|
|
|
|
|
|
user = %User{user | settings: user_settings, activity_settings: [activity_setting]}
|
|
|
|
|
2022-05-10 13:13:48 +02:00
|
|
|
assert {:ok, :sent} == Email.send(user, activity, recap: :one_day)
|
|
|
|
|
|
|
|
assert_email_sent(to: user.email)
|
|
|
|
|
|
|
|
assert %{last_notification_sent: updated_last_notification_sent} =
|
|
|
|
user_settings = Users.get_setting(user.id)
|
|
|
|
|
|
|
|
assert old != updated_last_notification_sent
|
|
|
|
assert DateTime.diff(DateTime.utc_now(), updated_last_notification_sent) < 5
|
|
|
|
|
|
|
|
user = %User{user | settings: user_settings, activity_settings: [activity_setting]}
|
|
|
|
|
|
|
|
assert {:ok, :skipped} == Email.send(user, activity, recap: :one_day)
|
2021-06-03 17:17:13 +02:00
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
refute_email_sent()
|
2021-06-03 17:17:13 +02:00
|
|
|
end
|
|
|
|
end
|
2021-06-27 18:16:34 +02:00
|
|
|
|
|
|
|
describe "send_anonymous_activity" do
|
|
|
|
@email "someone@somewhere.tld"
|
|
|
|
|
|
|
|
test "send activity notification to anonymous user" do
|
|
|
|
%Activity{} = activity = insert(:mobilizon_activity, inserted_at: DateTime.utc_now())
|
|
|
|
|
|
|
|
Email.send_anonymous_activity(@email, activity, locale: "en")
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
assert_email_sent(to: @email)
|
2021-06-27 18:16:34 +02:00
|
|
|
end
|
|
|
|
end
|
2021-06-03 17:17:13 +02:00
|
|
|
end
|