2021-03-23 15:18:03 +01:00
|
|
|
defmodule Mobilizon.Service.Workers.ActivityBuilderTest do
|
|
|
|
@moduledoc """
|
|
|
|
Test the ActivityBuilder module
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Activities.Activity
|
|
|
|
alias Mobilizon.Actors.Actor
|
2021-06-04 13:08:05 +02:00
|
|
|
alias Mobilizon.Service.Notifier.Mock2, as: NotifierMock
|
2021-03-23 15:18:03 +01:00
|
|
|
alias Mobilizon.Service.Workers.ActivityBuilder
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
|
|
|
use Mobilizon.DataCase
|
|
|
|
|
|
|
|
import Mobilizon.Factory
|
2021-06-03 17:17:13 +02:00
|
|
|
import Mox
|
|
|
|
|
2021-06-04 13:08:05 +02:00
|
|
|
setup_all do
|
|
|
|
Mox.defmock(NotifierMock, for: Mobilizon.Service.Notifier)
|
|
|
|
|
|
|
|
Mobilizon.Config.put([Mobilizon.Service.Notifier, :notifiers], [
|
|
|
|
NotifierMock
|
|
|
|
])
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2021-06-03 17:17:13 +02:00
|
|
|
setup :verify_on_exit!
|
2021-03-23 15:18:03 +01:00
|
|
|
|
|
|
|
describe "Sends direct email notification to users" do
|
|
|
|
test "if the user has a profile member of a group" do
|
|
|
|
%User{} = user = insert(:user)
|
|
|
|
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
|
|
|
|
%Actor{type: :Group} = group = insert(:group)
|
|
|
|
insert(:member, parent: group, actor: actor, role: :member)
|
|
|
|
|
|
|
|
%Activity{} =
|
|
|
|
activity = insert(:mobilizon_activity, group: group, inserted_at: DateTime.utc_now())
|
|
|
|
|
2021-06-03 17:17:13 +02:00
|
|
|
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)
|
2021-03-23 15:18:03 +01:00
|
|
|
|
2021-06-03 17:17:13 +02:00
|
|
|
assert :ok == ActivityBuilder.notify_activity(activity)
|
2021-03-23 15:18:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "unless if the user has a profile member of a group" do
|
|
|
|
%User{} = user = insert(:user)
|
|
|
|
|
|
|
|
%Actor{} = actor = insert(:actor, user: user)
|
|
|
|
|
|
|
|
%Actor{type: :Group} = group = insert(:group)
|
|
|
|
insert(:member, parent: group, actor: actor, role: :member)
|
|
|
|
|
|
|
|
%Activity{} =
|
|
|
|
activity =
|
|
|
|
insert(:mobilizon_activity, group: group, inserted_at: DateTime.utc_now(), author: actor)
|
|
|
|
|
|
|
|
assert :ok == ActivityBuilder.notify_activity(activity)
|
|
|
|
|
2021-06-03 17:17:13 +02:00
|
|
|
NotifierMock
|
|
|
|
|> expect(:ready?, 0, fn -> true end)
|
|
|
|
|> expect(:send, 0, fn %User{},
|
|
|
|
%Activity{
|
|
|
|
type: :event,
|
|
|
|
subject: :event_created,
|
|
|
|
object_type: :event
|
|
|
|
},
|
|
|
|
[single_activity: true] ->
|
|
|
|
{:ok, :sent}
|
|
|
|
end)
|
2021-03-23 15:18:03 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|