forked from potsda.mn/mobilizon
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
|
||||
{:error, :comment_not_found} ->
|
||||
case Converter.Comment.as_to_model_data(object) do
|
||||
%{visibility: visibility} = object_data
|
||||
when visibility === :private ->
|
||||
%{visibility: visibility, attributed_to_id: attributed_to_id} = object_data
|
||||
when visibility === :private and is_nil(attributed_to_id) ->
|
||||
Actions.Create.create(:conversation, object_data, false)
|
||||
|
||||
object_data when is_map(object_data) ->
|
||||
|
|
|
@ -23,13 +23,18 @@ defmodule Mobilizon.Federation.ActivityPub.Types.Conversations do
|
|||
@impl Entity
|
||||
@spec create(map(), map()) ::
|
||||
{: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)
|
||||
when not is_nil(conversation_id) do
|
||||
Logger.debug("Creating a reply to a conversation #{inspect(args, pretty: true)}")
|
||||
args = prepare_args(args)
|
||||
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
|
||||
%Conversation{} = conversation ->
|
||||
case Conversations.reply_to_conversation(conversation, args) do
|
||||
|
@ -56,6 +61,7 @@ defmodule Mobilizon.Federation.ActivityPub.Types.Conversations do
|
|||
{:error, :discussion_not_found}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@impl Entity
|
||||
def create(args, additional) do
|
||||
|
|
|
@ -6,6 +6,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.CommentsTest do
|
|||
import ExUnit.CaptureLog
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Conversations.Conversation
|
||||
alias Mobilizon.Discussions
|
||||
alias Mobilizon.Discussions.Comment
|
||||
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
|
||||
|
||||
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!()
|
||||
|
||||
Mock
|
||||
|
@ -245,12 +246,28 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.CommentsTest do
|
|||
end)
|
||||
|
||||
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 = Map.put(object, "inReplyTo", event.url)
|
||||
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
|
||||
|
||||
test "it works for incoming notices" do
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
defmodule Mobilizon.GraphQL.Resolvers.ConversationTest do
|
||||
use Mobilizon.Web.ConnCase
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
|
||||
alias Mobilizon.Discussions
|
||||
alias Mobilizon.GraphQL.AbsintheHelpers
|
||||
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
|
||||
event = insert(:event, organizer_actor: actor)
|
||||
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 =
|
||||
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"])
|
||||
assert conversation_data["id"] == to_string(conversation.id)
|
||||
assert conversation_data["lastComment"]["text"] == conversation.last_comment.text
|
||||
assert conversation_data["comments"]["total"] == 2
|
||||
|
||||
assert conversation_data["comments"]["elements"]
|
||||
|> Enum.any?(fn comment -> comment.text == conversation.origin_comment.text end)
|
||||
assert conversation_data["comments"]["total"] == 2
|
||||
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"] ==
|
||||
conversation_participant.actor.preferred_username
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
defmodule Mobilizon.DiscussionsTest do
|
||||
defmodule Mobilizon.ConversationsTest do
|
||||
use Mobilizon.DataCase
|
||||
|
||||
import Mobilizon.Factory
|
||||
|
|
|
@ -5,7 +5,7 @@ defmodule Mobilizon.Service.Activity.ConversationTest do
|
|||
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Conversations
|
||||
alias Mobilizon.Conversations.Conversation
|
||||
alias Mobilizon.Conversations.{Conversation, ConversationParticipant}
|
||||
alias Mobilizon.Discussions.Comment
|
||||
alias Mobilizon.Service.Activity.Conversation, as: ConversationActivity
|
||||
alias Mobilizon.Service.Workers.LegacyNotifierBuilder
|
||||
|
@ -20,11 +20,19 @@ defmodule Mobilizon.Service.Activity.ConversationTest do
|
|||
%User{} = user = insert(:user)
|
||||
%Actor{id: actor_id} = actor = insert(:actor, user: user)
|
||||
|
||||
%Conversation{id: conversation_id, last_comment: %Comment{actor_id: last_comment_actor_id}} =
|
||||
conversation = insert(:conversation)
|
||||
%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, 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)
|
||||
|
||||
|
@ -35,13 +43,14 @@ defmodule Mobilizon.Service.Activity.ConversationTest do
|
|||
worker: LegacyNotifierBuilder,
|
||||
args: %{
|
||||
"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_type" => "conversation",
|
||||
"op" => "legacy_notify",
|
||||
"subject" => "conversation_created",
|
||||
"subject_params" => %{
|
||||
"conversation_id" => conversation_id
|
||||
"conversation_id" => conversation_id,
|
||||
"conversation_participant_id" => conversation_participant_actor_id
|
||||
},
|
||||
"type" => "conversation"
|
||||
}
|
||||
|
@ -50,19 +59,20 @@ defmodule Mobilizon.Service.Activity.ConversationTest do
|
|||
assert_enqueued(
|
||||
worker: LegacyNotifierBuilder,
|
||||
args: %{
|
||||
"author_id" => author_id,
|
||||
"object_id" => to_string(comment_id),
|
||||
"object_type" => "comment",
|
||||
"op" => "legacy_notify",
|
||||
"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
|
||||
"author_id" => last_comment_actor_id,
|
||||
"participant" => %{
|
||||
"actor_id" => conversation_other_participant_actor_id,
|
||||
"id" => conversation_participant_id
|
||||
},
|
||||
"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
|
||||
|
|
Loading…
Reference in a new issue