Fix tests

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-06-03 17:17:13 +02:00
parent c0ab3d9905
commit d3164899f3
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
10 changed files with 224 additions and 24 deletions

View file

@ -309,6 +309,10 @@ config :mobilizon, Mobilizon.Service.Notifier,
Mobilizon.Service.Notifier.Push Mobilizon.Service.Notifier.Push
] ]
config :mobilizon, Mobilizon.Service.Notifier.Email, enabled: true
config :mobilizon, Mobilizon.Service.Notifier.Push, enabled: true
# Import environment specific config. This must remain at the bottom # Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above. # of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs" import_config "#{config_env()}.exs"

View file

@ -10,7 +10,7 @@ defmodule Mobilizon.Users.User do
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Crypto alias Mobilizon.Crypto
alias Mobilizon.Events.FeedToken alias Mobilizon.Events.FeedToken
alias Mobilizon.Users.{Setting, UserRole} alias Mobilizon.Users.{ActivitySetting, Setting, UserRole}
alias Mobilizon.Web.Email.Checker alias Mobilizon.Web.Email.Checker
import Mobilizon.Web.Gettext import Mobilizon.Web.Gettext
@ -90,6 +90,7 @@ defmodule Mobilizon.Users.User do
has_many(:actors, Actor) has_many(:actors, Actor)
has_many(:feed_tokens, FeedToken, foreign_key: :user_id) has_many(:feed_tokens, FeedToken, foreign_key: :user_id)
has_one(:settings, Setting) has_one(:settings, Setting)
has_many(:activity_settings, ActivitySetting)
timestamps() timestamps()
end end

View file

@ -14,7 +14,7 @@ defmodule Mobilizon.Service.Notifier.Email do
@impl Notifier @impl Notifier
def ready? do def ready? do
Config.get(__MODULE__, :enabled) Config.get([__MODULE__, :enabled])
end end
def send(user, activity, options \\ []) def send(user, activity, options \\ [])

View file

@ -11,8 +11,11 @@ defmodule Mobilizon.Service.Notifier.Filter do
@spec can_send_activity?(Activity.t(), method(), User.t(), function()) :: boolean() @spec can_send_activity?(Activity.t(), method(), User.t(), function()) :: boolean()
def can_send_activity?(%Activity{} = activity, method, %User{} = user, get_default) do def can_send_activity?(%Activity{} = activity, method, %User{} = user, get_default) do
case map_activity_to_activity_setting(activity) do case map_activity_to_activity_setting(activity) do
false -> false false ->
key -> user |> Users.activity_setting(key, method) |> enabled?(key, get_default) false
key when is_binary(key) ->
user |> Users.activity_setting(key, method) |> enabled?(key, get_default)
end end
end end

View file

@ -14,7 +14,7 @@ defmodule Mobilizon.Service.Notifier.Push do
@impl Notifier @impl Notifier
def ready? do def ready? do
Config.get(__MODULE__, :enabled) Config.get([__MODULE__, :enabled])
end end
@impl Notifier @impl Notifier

View file

@ -20,7 +20,7 @@ defmodule Mobilizon.Service.Activity.CommentTest do
%Event{title: event_title, uuid: event_uuid} = event = insert(:event) %Event{title: event_title, uuid: event_uuid} = event = insert(:event)
%Comment{id: comment_id, actor_id: author_id} = comment = insert(:comment, event: event) %Comment{id: comment_id, actor_id: author_id} = comment = insert(:comment, event: event)
assert [organizer: :enqueued, announcement: :skipped, mentionned: :skipped] == assert {:ok, [organizer: :enqueued, announcement: :skipped, mentionned: :skipped]} ==
CommentActivity.insert_activity(comment) CommentActivity.insert_activity(comment)
refute_enqueued( refute_enqueued(
@ -61,7 +61,7 @@ defmodule Mobilizon.Service.Activity.CommentTest do
] ]
} }
assert [organizer: :enqueued, announcement: :skipped, mentionned: :enqueued] == assert {:ok, [organizer: :enqueued, announcement: :skipped, mentionned: :enqueued]} ==
CommentActivity.insert_activity(comment) CommentActivity.insert_activity(comment)
assert_enqueued( assert_enqueued(
@ -107,7 +107,7 @@ defmodule Mobilizon.Service.Activity.CommentTest do
%Comment{id: comment_id, actor_id: author_id} = %Comment{id: comment_id, actor_id: author_id} =
comment = insert(:comment, text: "Hey you", event: event, is_announcement: true) comment = insert(:comment, text: "Hey you", event: event, is_announcement: true)
assert [organizer: :enqueued, announcement: :enqueued, mentionned: :skipped] == assert {:ok, [organizer: :enqueued, announcement: :enqueued, mentionned: :skipped]} ==
CommentActivity.insert_activity(comment) CommentActivity.insert_activity(comment)
assert_enqueued( assert_enqueued(

View file

@ -0,0 +1,93 @@
defmodule Mobilizon.Service.Notifier.EmailTest do
@moduledoc """
Test the Email notifier module
"""
alias Mobilizon.Activities.Activity
alias Mobilizon.Config
alias Mobilizon.Service.Notifier.Email
alias Mobilizon.Users.{ActivitySetting, Setting, User}
alias Mobilizon.Web.Email.Activity, as: EmailActivity
use Mobilizon.DataCase
use Bamboo.Test
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())
%User{} = user = insert(:user)
%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)
refute_delivered_email(
EmailActivity.direct_activity(
user.email,
[activity]
)
)
end
test "when the user allows it" do
%Activity{} = activity = insert(:mobilizon_activity, inserted_at: DateTime.utc_now())
%User{} = user = insert(:user)
%Setting{} = user_settings = insert(:settings, user_id: user.id)
%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)
assert_delivered_email(
EmailActivity.direct_activity(
user.email,
[activity]
)
)
end
test "not if we already have sent notifications" 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_day,
last_notification_sent: DateTime.add(DateTime.utc_now(), 3600)
)
%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, :skipped} == Email.send(user, activity)
refute_delivered_email(
EmailActivity.direct_activity(
user.email,
[activity]
)
)
end
end
end

View file

@ -0,0 +1,64 @@
defmodule Mobilizon.Service.Notifier.PushTest do
@moduledoc """
Test the Push notifier module
"""
alias Mobilizon.Activities.Activity
alias Mobilizon.Config
alias Mobilizon.Service.Notifier.Push
alias Mobilizon.Users.{ActivitySetting, Setting, User}
use Mobilizon.DataCase
use Bamboo.Test
import Mobilizon.Factory
describe "Returns if the module is loaded" do
test "Loaded by default" do
assert Push.ready?() == true
end
test "If disabled" do
Config.put([Push, :enabled], false)
assert Push.ready?() == false
Config.put([Push, :enabled], true)
end
end
describe "sending push notification for activities" do
test "when the user doesn't allow it" do
%Activity{} = activity = insert(:mobilizon_activity, inserted_at: DateTime.utc_now())
%User{} = user = insert(:user)
%Setting{} = user_settings = insert(:settings, user_id: user.id)
user = %User{user | settings: user_settings}
assert {:ok, :skipped} == Push.send(user, activity)
end
test "when the user allows it" do
event = insert(:event)
%Activity{} =
activity =
insert(:mobilizon_activity,
inserted_at: DateTime.utc_now(),
object_id: to_string(event.id),
subject_params: %{
"event_title" => event.title,
"event_uuid" => event.uuid,
"event_id" => event.id
}
)
%User{} = user = insert(:user)
%Setting{} = user_settings = insert(:settings, user_id: user.id)
%ActivitySetting{} =
activity_setting =
insert(:mobilizon_activity_setting, user_id: user.id, user: user, method: "push")
user = %User{user | settings: user_settings, activity_settings: [activity_setting]}
assert {:ok, :sent} == Push.send(user, activity)
end
end
end

View file

@ -5,14 +5,16 @@ defmodule Mobilizon.Service.Workers.ActivityBuilderTest do
alias Mobilizon.Activities.Activity alias Mobilizon.Activities.Activity
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Service.Notifier.Mock, as: NotifierMock
alias Mobilizon.Service.Workers.ActivityBuilder alias Mobilizon.Service.Workers.ActivityBuilder
alias Mobilizon.Users.User alias Mobilizon.Users.User
alias Mobilizon.Web.Email.Activity, as: EmailActivity
use Mobilizon.DataCase use Mobilizon.DataCase
use Bamboo.Test
import Mobilizon.Factory import Mobilizon.Factory
import Mox
setup :verify_on_exit!
describe "Sends direct email notification to users" do describe "Sends direct email notification to users" do
test "if the user has a profile member of a group" do test "if the user has a profile member of a group" do
@ -26,14 +28,19 @@ defmodule Mobilizon.Service.Workers.ActivityBuilderTest do
%Activity{} = %Activity{} =
activity = insert(:mobilizon_activity, group: group, inserted_at: DateTime.utc_now()) activity = insert(:mobilizon_activity, group: group, inserted_at: DateTime.utc_now())
assert :ok == ActivityBuilder.notify_activity(activity) NotifierMock
|> expect(:ready?, 1, fn -> true end)
|> expect(:send, 1, fn %User{},
%Activity{
type: :event,
subject: :event_created,
object_type: :event
},
[single_activity: true] ->
{:ok, :sent}
end)
assert_delivered_email( assert :ok == ActivityBuilder.notify_activity(activity)
EmailActivity.direct_activity(
user.email,
[activity]
)
)
end end
test "unless if the user has a profile member of a group" do test "unless if the user has a profile member of a group" do
@ -50,12 +57,17 @@ defmodule Mobilizon.Service.Workers.ActivityBuilderTest do
assert :ok == ActivityBuilder.notify_activity(activity) assert :ok == ActivityBuilder.notify_activity(activity)
refute_delivered_email( NotifierMock
EmailActivity.direct_activity( |> expect(:ready?, 0, fn -> true end)
user.email, |> expect(:send, 0, fn %User{},
[activity] %Activity{
) type: :event,
) subject: :event_created,
object_type: :event
},
[single_activity: true] ->
{:ok, :sent}
end)
end end
end end
end end

View file

@ -418,11 +418,34 @@ defmodule Mobilizon.Factory do
%Mobilizon.Activities.Activity{ %Mobilizon.Activities.Activity{
type: :event, type: :event,
subject: :event_created, subject: :event_created,
subject_params: %{event_title: event.title}, subject_params: %{
"event_title" => event.title,
"event_uuid" => event.uuid,
"event_id" => event.id
},
author: actor, author: actor,
group: group, group: group,
object_type: :event, object_type: :event,
object_id: to_string(event.id) object_id: to_string(event.id)
} }
end end
def mobilizon_activity_setting_factory do
%Mobilizon.Users.ActivitySetting{
key: "event_created",
method: "email",
enabled: true,
user: build(:user)
}
end
def push_subscription_factory do
%Mobilizon.Users.PushSubscription{
digest: "",
endpoint: "",
auth: "",
p256dh: "",
user: build(:user)
}
end
end end