test: fix unit backend tests
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
5d65981dcd
commit
e051df1ab3
@ -71,8 +71,8 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
|
|||||||
case Discussions.get_comment_from_url_with_preload(object["id"]) do
|
case Discussions.get_comment_from_url_with_preload(object["id"]) do
|
||||||
{:error, :comment_not_found} ->
|
{:error, :comment_not_found} ->
|
||||||
case Converter.Comment.as_to_model_data(object) do
|
case Converter.Comment.as_to_model_data(object) do
|
||||||
%{visibility: visibility} = object_data
|
%{visibility: visibility, attributed_to_id: attributed_to_id} = object_data
|
||||||
when visibility === :private ->
|
when visibility === :private and is_nil(attributed_to_id) ->
|
||||||
Actions.Create.create(:conversation, object_data, false)
|
Actions.Create.create(:conversation, object_data, false)
|
||||||
|
|
||||||
object_data when is_map(object_data) ->
|
object_data when is_map(object_data) ->
|
||||||
|
@ -23,13 +23,18 @@ defmodule Mobilizon.Federation.ActivityPub.Types.Conversations do
|
|||||||
@impl Entity
|
@impl Entity
|
||||||
@spec create(map(), map()) ::
|
@spec create(map(), map()) ::
|
||||||
{:ok, Conversation.t(), ActivityStream.t()}
|
{:ok, Conversation.t(), ActivityStream.t()}
|
||||||
| {:error, :conversation_not_found | :last_comment_not_found | Ecto.Changeset.t()}
|
| {:error,
|
||||||
|
:conversation_not_found
|
||||||
|
| :last_comment_not_found
|
||||||
|
| :empty_participants
|
||||||
|
| Ecto.Changeset.t()}
|
||||||
def create(%{conversation_id: conversation_id} = args, additional)
|
def create(%{conversation_id: conversation_id} = args, additional)
|
||||||
when not is_nil(conversation_id) do
|
when not is_nil(conversation_id) do
|
||||||
Logger.debug("Creating a reply to a conversation #{inspect(args, pretty: true)}")
|
Logger.debug("Creating a reply to a conversation #{inspect(args, pretty: true)}")
|
||||||
args = prepare_args(args)
|
args = prepare_args(args)
|
||||||
Logger.debug("Creating a reply to a conversation #{inspect(args, pretty: true)}")
|
Logger.debug("Creating a reply to a conversation #{inspect(args, pretty: true)}")
|
||||||
|
|
||||||
|
with args when is_map(args) <- prepare_args(args) do
|
||||||
case Conversations.get_conversation(conversation_id) do
|
case Conversations.get_conversation(conversation_id) do
|
||||||
%Conversation{} = conversation ->
|
%Conversation{} = conversation ->
|
||||||
case Conversations.reply_to_conversation(conversation, args) do
|
case Conversations.reply_to_conversation(conversation, args) do
|
||||||
@ -56,6 +61,7 @@ defmodule Mobilizon.Federation.ActivityPub.Types.Conversations do
|
|||||||
{:error, :discussion_not_found}
|
{:error, :discussion_not_found}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@impl Entity
|
@impl Entity
|
||||||
def create(args, additional) do
|
def create(args, additional) do
|
||||||
|
@ -6,6 +6,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.CommentsTest do
|
|||||||
import ExUnit.CaptureLog
|
import ExUnit.CaptureLog
|
||||||
alias Mobilizon.Actors
|
alias Mobilizon.Actors
|
||||||
alias Mobilizon.Actors.Actor
|
alias Mobilizon.Actors.Actor
|
||||||
|
alias Mobilizon.Conversations.Conversation
|
||||||
alias Mobilizon.Discussions
|
alias Mobilizon.Discussions
|
||||||
alias Mobilizon.Discussions.Comment
|
alias Mobilizon.Discussions.Comment
|
||||||
alias Mobilizon.Events.Event
|
alias Mobilizon.Events.Event
|
||||||
@ -218,7 +219,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.CommentsTest do
|
|||||||
end) =~ "[warning] Parent object is something we don't handle"
|
end) =~ "[warning] Parent object is something we don't handle"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it ignores incoming private notes" do
|
test "it accepts incoming private notes" do
|
||||||
actor_data = File.read!("test/fixtures/mastodon-actor.json") |> Jason.decode!()
|
actor_data = File.read!("test/fixtures/mastodon-actor.json") |> Jason.decode!()
|
||||||
|
|
||||||
Mock
|
Mock
|
||||||
@ -245,12 +246,28 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.CommentsTest do
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
data = File.read!("test/fixtures/mastodon-post-activity-private.json") |> Jason.decode!()
|
data = File.read!("test/fixtures/mastodon-post-activity-private.json") |> Jason.decode!()
|
||||||
event = insert(:event)
|
%Event{organizer_actor_id: organizer_actor_id} = event = insert(:event)
|
||||||
object = data["object"]
|
object = data["object"]
|
||||||
object = Map.put(object, "inReplyTo", event.url)
|
object = Map.put(object, "inReplyTo", event.url)
|
||||||
data = Map.put(data, "object", object)
|
data = Map.put(data, "object", object)
|
||||||
|
|
||||||
:error = Transmogrifier.handle_incoming(data)
|
{:ok, %Activity{data: _data, local: false}, %Conversation{} = conversation} =
|
||||||
|
Transmogrifier.handle_incoming(data)
|
||||||
|
|
||||||
|
{:ok, %Actor{id: actor_1_id}} = Actors.get_actor_by_url("https://framapiaf.org/users/tcit")
|
||||||
|
{:ok, %Actor{id: actor_2_id}} = Actors.get_actor_by_url("https://framapiaf.org/users/admin")
|
||||||
|
|
||||||
|
assert conversation.event_id == event.id
|
||||||
|
assert conversation.last_comment.visibility == :private
|
||||||
|
assert conversation.last_comment.actor_id == actor_2_id
|
||||||
|
assert conversation.last_comment_id == conversation.origin_comment_id
|
||||||
|
assert is_nil(conversation.last_comment.in_reply_to_comment_id)
|
||||||
|
assert is_nil(conversation.last_comment.origin_comment_id)
|
||||||
|
|
||||||
|
participants_id = Enum.map(conversation.participants, & &1.id)
|
||||||
|
|
||||||
|
assert MapSet.new(participants_id) ==
|
||||||
|
MapSet.new([organizer_actor_id, actor_1_id, actor_2_id])
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it works for incoming notices" do
|
test "it works for incoming notices" do
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
defmodule Mobilizon.GraphQL.Resolvers.ConversationTest do
|
defmodule Mobilizon.GraphQL.Resolvers.ConversationTest do
|
||||||
use Mobilizon.Web.ConnCase
|
use Mobilizon.Web.ConnCase
|
||||||
alias Mobilizon.Actors
|
alias Mobilizon.Discussions
|
||||||
alias Mobilizon.Actors.Actor
|
|
||||||
|
|
||||||
alias Mobilizon.GraphQL.AbsintheHelpers
|
alias Mobilizon.GraphQL.AbsintheHelpers
|
||||||
import Mobilizon.Factory
|
import Mobilizon.Factory
|
||||||
|
|
||||||
@ -47,7 +45,10 @@ defmodule Mobilizon.GraphQL.Resolvers.ConversationTest do
|
|||||||
test "for a given event", %{conn: conn, user: user, actor: actor} do
|
test "for a given event", %{conn: conn, user: user, actor: actor} do
|
||||||
event = insert(:event, organizer_actor: actor)
|
event = insert(:event, organizer_actor: actor)
|
||||||
conversation = insert(:conversation, event: event)
|
conversation = insert(:conversation, event: event)
|
||||||
another_comment = build(:comment, origin_comment: conversation.origin_comment)
|
another_comment = insert(:comment, origin_comment: conversation.origin_comment)
|
||||||
|
|
||||||
|
Discussions.update_comment(conversation.origin_comment, %{conversation_id: conversation.id})
|
||||||
|
Discussions.update_comment(another_comment, %{conversation_id: conversation.id})
|
||||||
|
|
||||||
conversation_participant =
|
conversation_participant =
|
||||||
insert(:conversation_participant, actor: actor, conversation: conversation)
|
insert(:conversation_participant, actor: actor, conversation: conversation)
|
||||||
@ -66,10 +67,18 @@ defmodule Mobilizon.GraphQL.Resolvers.ConversationTest do
|
|||||||
conversation_data = hd(res["data"]["event"]["conversations"]["elements"])
|
conversation_data = hd(res["data"]["event"]["conversations"]["elements"])
|
||||||
assert conversation_data["id"] == to_string(conversation.id)
|
assert conversation_data["id"] == to_string(conversation.id)
|
||||||
assert conversation_data["lastComment"]["text"] == conversation.last_comment.text
|
assert conversation_data["lastComment"]["text"] == conversation.last_comment.text
|
||||||
assert conversation_data["comments"]["total"] == 2
|
|
||||||
|
|
||||||
assert conversation_data["comments"]["elements"]
|
assert conversation_data["comments"]["total"] == 2
|
||||||
|> Enum.any?(fn comment -> comment.text == conversation.origin_comment.text end)
|
comments = conversation_data["comments"]["elements"]
|
||||||
|
|
||||||
|
assert MapSet.new(Enum.map(comments, & &1["id"])) ==
|
||||||
|
[conversation.origin_comment.id, another_comment.id]
|
||||||
|
|> Enum.map(&to_string/1)
|
||||||
|
|> MapSet.new()
|
||||||
|
|
||||||
|
assert Enum.any?(comments, fn comment ->
|
||||||
|
comment["text"] == conversation.origin_comment.text
|
||||||
|
end)
|
||||||
|
|
||||||
assert conversation_data["actor"]["preferredUsername"] ==
|
assert conversation_data["actor"]["preferredUsername"] ==
|
||||||
conversation_participant.actor.preferred_username
|
conversation_participant.actor.preferred_username
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
defmodule Mobilizon.DiscussionsTest do
|
defmodule Mobilizon.ConversationsTest do
|
||||||
use Mobilizon.DataCase
|
use Mobilizon.DataCase
|
||||||
|
|
||||||
import Mobilizon.Factory
|
import Mobilizon.Factory
|
||||||
|
@ -5,7 +5,7 @@ defmodule Mobilizon.Service.Activity.ConversationTest do
|
|||||||
|
|
||||||
alias Mobilizon.Actors.Actor
|
alias Mobilizon.Actors.Actor
|
||||||
alias Mobilizon.Conversations
|
alias Mobilizon.Conversations
|
||||||
alias Mobilizon.Conversations.Conversation
|
alias Mobilizon.Conversations.{Conversation, ConversationParticipant}
|
||||||
alias Mobilizon.Discussions.Comment
|
alias Mobilizon.Discussions.Comment
|
||||||
alias Mobilizon.Service.Activity.Conversation, as: ConversationActivity
|
alias Mobilizon.Service.Activity.Conversation, as: ConversationActivity
|
||||||
alias Mobilizon.Service.Workers.LegacyNotifierBuilder
|
alias Mobilizon.Service.Workers.LegacyNotifierBuilder
|
||||||
@ -20,11 +20,19 @@ defmodule Mobilizon.Service.Activity.ConversationTest do
|
|||||||
%User{} = user = insert(:user)
|
%User{} = user = insert(:user)
|
||||||
%Actor{id: actor_id} = actor = insert(:actor, user: user)
|
%Actor{id: actor_id} = actor = insert(:actor, user: user)
|
||||||
|
|
||||||
%Conversation{id: conversation_id, last_comment: %Comment{actor_id: last_comment_actor_id}} =
|
%Conversation{
|
||||||
conversation = insert(:conversation)
|
id: conversation_id,
|
||||||
|
last_comment: %Comment{actor_id: last_comment_actor_id}
|
||||||
|
} =
|
||||||
|
conversation = insert(:conversation, event: nil)
|
||||||
|
|
||||||
|
%ConversationParticipant{id: conversation_participant_actor_id} =
|
||||||
insert(:conversation_participant, actor: actor, conversation: conversation)
|
insert(:conversation_participant, actor: actor, conversation: conversation)
|
||||||
insert(:conversation_participant, conversation: conversation)
|
|
||||||
|
%ConversationParticipant{
|
||||||
|
id: conversation_participant_id,
|
||||||
|
actor: %Actor{id: conversation_other_participant_actor_id}
|
||||||
|
} = insert(:conversation_participant, conversation: conversation)
|
||||||
|
|
||||||
conversation = Conversations.get_conversation(conversation_id)
|
conversation = Conversations.get_conversation(conversation_id)
|
||||||
|
|
||||||
@ -35,13 +43,14 @@ defmodule Mobilizon.Service.Activity.ConversationTest do
|
|||||||
worker: LegacyNotifierBuilder,
|
worker: LegacyNotifierBuilder,
|
||||||
args: %{
|
args: %{
|
||||||
"author_id" => last_comment_actor_id,
|
"author_id" => last_comment_actor_id,
|
||||||
"participants" => [actor_id],
|
"participant" => %{"actor_id" => actor_id, "id" => conversation_participant_actor_id},
|
||||||
"object_id" => to_string(conversation_id),
|
"object_id" => to_string(conversation_id),
|
||||||
"object_type" => "conversation",
|
"object_type" => "conversation",
|
||||||
"op" => "legacy_notify",
|
"op" => "legacy_notify",
|
||||||
"subject" => "conversation_created",
|
"subject" => "conversation_created",
|
||||||
"subject_params" => %{
|
"subject_params" => %{
|
||||||
"conversation_id" => conversation_id
|
"conversation_id" => conversation_id,
|
||||||
|
"conversation_participant_id" => conversation_participant_actor_id
|
||||||
},
|
},
|
||||||
"type" => "conversation"
|
"type" => "conversation"
|
||||||
}
|
}
|
||||||
@ -50,19 +59,20 @@ defmodule Mobilizon.Service.Activity.ConversationTest do
|
|||||||
assert_enqueued(
|
assert_enqueued(
|
||||||
worker: LegacyNotifierBuilder,
|
worker: LegacyNotifierBuilder,
|
||||||
args: %{
|
args: %{
|
||||||
"author_id" => author_id,
|
"author_id" => last_comment_actor_id,
|
||||||
"object_id" => to_string(comment_id),
|
"participant" => %{
|
||||||
"object_type" => "comment",
|
"actor_id" => conversation_other_participant_actor_id,
|
||||||
"op" => "legacy_notify",
|
"id" => conversation_participant_id
|
||||||
"subject" => "event_new_comment",
|
|
||||||
"subject_params" => %{
|
|
||||||
"comment_reply_to_uuid" => nil,
|
|
||||||
"event_title" => event_title,
|
|
||||||
"event_uuid" => event_uuid,
|
|
||||||
"comment_reply_to" => false,
|
|
||||||
"comment_uuid" => comment_uuid
|
|
||||||
},
|
},
|
||||||
"type" => "comment"
|
"object_id" => to_string(conversation_id),
|
||||||
|
"object_type" => "conversation",
|
||||||
|
"op" => "legacy_notify",
|
||||||
|
"subject" => "conversation_created",
|
||||||
|
"subject_params" => %{
|
||||||
|
"conversation_id" => conversation_id,
|
||||||
|
"conversation_participant_id" => conversation_participant_id
|
||||||
|
},
|
||||||
|
"type" => "conversation"
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user