From 57c07836aabf22a0d03d85b002bc83f93e277d8b Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 2 Jun 2021 11:55:50 +0200 Subject: [PATCH] Add a test to LegacyNotifierBuilder Signed-off-by: Thomas Citharel --- config/test.exs | 5 + .../workers/legacy_notifier_builder.ex | 4 +- .../workers/legacy_notifier_builder_test.exs | 182 ++++++++++++++++++ test/support/data_case.ex | 7 +- 4 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 test/service/workers/legacy_notifier_builder_test.exs diff --git a/config/test.exs b/config/test.exs index 4f024d17..271ad1b6 100644 --- a/config/test.exs +++ b/config/test.exs @@ -82,3 +82,8 @@ config :junit_formatter, report_dir: "." if System.get_env("DOCKER", "false") == "false" && File.exists?("./config/test.secret.exs") do import_config "test.secret.exs" end + +config :mobilizon, Mobilizon.Service.Notifier, + notifiers: [ + Mobilizon.Service.Notifier.Mock + ] diff --git a/lib/service/workers/legacy_notifier_builder.ex b/lib/service/workers/legacy_notifier_builder.ex index af4305e8..80f4c25c 100644 --- a/lib/service/workers/legacy_notifier_builder.ex +++ b/lib/service/workers/legacy_notifier_builder.ex @@ -3,8 +3,8 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do Worker to push legacy notifications """ - alias Mobilizon.{Actors, Events, Users} alias Mobilizon.Activities.Activity + alias Mobilizon.{Actors, Events, Users} alias Mobilizon.Service.Notifier use Mobilizon.Service.Workers.Helper, queue: "activity" @@ -20,7 +20,7 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do end end - def build_activity(args) do + defp build_activity(args) do author = Actors.get_actor(args["author_id"]) %Activity{ diff --git a/test/service/workers/legacy_notifier_builder_test.exs b/test/service/workers/legacy_notifier_builder_test.exs new file mode 100644 index 00000000..35ae41ff --- /dev/null +++ b/test/service/workers/legacy_notifier_builder_test.exs @@ -0,0 +1,182 @@ +defmodule Mobilizon.Service.Workers.LegacyNotifierBuilderTest do + @moduledoc """ + Test the ActivityBuilder module + """ + + alias Mobilizon.Activities.Activity + alias Mobilizon.Actors.Actor + alias Mobilizon.Discussions.Comment + alias Mobilizon.Events.Event + alias Mobilizon.Service.Notifier.Mock, as: NotifierMock + alias Mobilizon.Service.Workers.LegacyNotifierBuilder + alias Mobilizon.Users.User + + use Mobilizon.DataCase, async: true + import Mox + import Mobilizon.Factory + + @mentionned %{ + "type" => "comment", + "subject" => "event_comment_mention", + "object_type" => "comment", + "inserted_at" => DateTime.utc_now(), + "op" => "legacy_notify" + } + + @announcement %{ + "type" => "comment", + "subject" => "participation_event_comment", + "object_type" => "comment", + "inserted_at" => DateTime.utc_now(), + "op" => "legacy_notify" + } + + setup :verify_on_exit! + + describe "Generates a comment mention notification " do + test "not if the actor is remote" do + %User{} = user1 = insert(:user) + + %Actor{id: actor_id} = actor = insert(:actor, user: user1) + %Actor{id: actor_id_2} = insert(:actor, domain: "remote.tld", user: nil) + + %Event{title: title, uuid: uuid} = event = insert(:event) + %Comment{id: comment_id} = insert(:comment, event: event, actor: actor) + + args = + Map.merge(@mentionned, %{ + "subject_params" => %{ + event_uuid: uuid, + event_title: title + }, + "author_id" => actor_id, + "object_id" => to_string(comment_id), + "mentions" => [actor_id_2] + }) + + NotifierMock + |> expect(:ready?, 0, fn -> true end) + |> expect(:send, 0, fn %User{}, + %Activity{ + type: :comment, + subject: :event_comment_mention, + object_type: :comment + }, + [single_activity: true] -> + {:ok, :sent} + end) + + assert :ok == LegacyNotifierBuilder.perform(%Oban.Job{args: args}) + end + + test "if the actor mentionned is local" do + %User{} = user1 = insert(:user) + %User{} = user2 = insert(:user) + + %Actor{id: actor_id} = actor = insert(:actor, user: user1) + %Actor{id: actor_id_2} = insert(:actor, user: user2) + + %Event{title: title, uuid: uuid} = event = insert(:event) + %Comment{id: comment_id} = insert(:comment, event: event, actor: actor) + + args = + Map.merge(@mentionned, %{ + "subject_params" => %{ + event_uuid: uuid, + event_title: title + }, + "author_id" => actor_id, + "object_id" => to_string(comment_id), + "mentions" => [actor_id_2] + }) + + NotifierMock + |> expect(:ready?, fn -> true end) + |> expect(:send, fn %User{}, + %Activity{ + type: :comment, + subject: :event_comment_mention, + object_type: :comment + }, + [single_activity: true] -> + {:ok, :sent} + end) + + assert :ok == LegacyNotifierBuilder.perform(%Oban.Job{args: args}) + end + end + + describe "Generates an announcement comment notification" do + test "not if there's no participants" do + %User{} = user1 = insert(:user) + + %Actor{id: actor_id} = actor = insert(:actor, user: user1) + %Actor{} = insert(:actor, domain: "remote.tld", user: nil) + + %Event{title: title, uuid: uuid, id: event_id} = event = insert(:event) + %Comment{id: comment_id} = insert(:comment, event: event, actor: actor) + + args = + Map.merge(@announcement, %{ + "subject_params" => %{ + "event_uuid" => uuid, + "event_title" => title, + "event_id" => event_id + }, + "author_id" => actor_id, + "object_id" => to_string(comment_id) + }) + + NotifierMock + |> expect(:ready?, 0, fn -> true end) + |> expect(:send, 0, fn %User{}, + %Activity{ + type: :comment, + subject: :participation_event_comment, + object_type: :comment + }, + [single_activity: true] -> + {:ok, :sent} + end) + + assert :ok == LegacyNotifierBuilder.perform(%Oban.Job{args: args}) + end + + test "if there's some participants" do + %User{} = user1 = insert(:user) + %User{} = user2 = insert(:user) + + %Actor{id: actor_id} = actor = insert(:actor, user: user1) + %Actor{} = actor2 = insert(:actor, user: user2) + + %Event{title: title, uuid: uuid, id: event_id} = event = insert(:event) + %Comment{id: comment_id} = insert(:comment, event: event, actor: actor) + insert(:participant, event: event, actor: actor2) + + args = + Map.merge(@announcement, %{ + "subject_params" => %{ + "event_uuid" => uuid, + "event_title" => title, + "event_id" => event_id + }, + "author_id" => actor_id, + "object_id" => to_string(comment_id) + }) + + NotifierMock + |> expect(:ready?, fn -> true end) + |> expect(:send, fn %User{}, + %Activity{ + type: :comment, + subject: :participation_event_comment, + object_type: :comment + }, + [single_activity: true] -> + {:ok, :sent} + end) + + assert :ok == LegacyNotifierBuilder.perform(%Oban.Job{args: args}) + end + end +end diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 575d7839..e8e6c05c 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -75,5 +75,10 @@ defmodule Mobilizon.DataCase do end Mox.defmock(Mobilizon.Service.HTTP.ActivityPub.Mock, for: Tesla.Adapter) - Mox.defmock(Mobilizon.Service.HTTP.GeospatialClient.Mock, for: Tesla.Adapter) + + Mox.defmock(Mobilizon.Service.HTTP.GeospatialClient.Mock, + for: Tesla.Adapter + ) + + Mox.defmock(Mobilizon.Service.Notifier.Mock, for: Mobilizon.Service.Notifier) end