summaryrefslogtreecommitdiff
path: root/test/pleroma/web/activity_pub/side_effects_test.exs
diff options
context:
space:
mode:
Diffstat (limited to 'test/pleroma/web/activity_pub/side_effects_test.exs')
-rw-r--r--test/pleroma/web/activity_pub/side_effects_test.exs301
1 files changed, 297 insertions, 4 deletions
diff --git a/test/pleroma/web/activity_pub/side_effects_test.exs b/test/pleroma/web/activity_pub/side_effects_test.exs
index 64c4a8c14..b24831e85 100644
--- a/test/pleroma/web/activity_pub/side_effects_test.exs
+++ b/test/pleroma/web/activity_pub/side_effects_test.exs
@@ -118,7 +118,10 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
describe "update users" do
setup do
user = insert(:user, local: false)
- {:ok, update_data, []} = Builder.update(user, %{"id" => user.ap_id, "name" => "new name!"})
+
+ {:ok, update_data, []} =
+ Builder.update(user, %{"id" => user.ap_id, "type" => "Person", "name" => "new name!"})
+
{:ok, update, _meta} = ActivityPub.persist(update_data, local: true)
%{user: user, update_data: update_data, update: update}
@@ -140,6 +143,298 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
end
end
+ describe "update notes" do
+ setup do
+ make_time = fn ->
+ Pleroma.Web.ActivityPub.Utils.make_date()
+ end
+
+ user = insert(:user)
+ note = insert(:note, user: user, data: %{"published" => make_time.()})
+ _note_activity = insert(:note_activity, note: note)
+
+ updated_note =
+ note.data
+ |> Map.put("summary", "edited summary")
+ |> Map.put("content", "edited content")
+ |> Map.put("updated", make_time.())
+
+ {:ok, update_data, []} = Builder.update(user, updated_note)
+ {:ok, update, _meta} = ActivityPub.persist(update_data, local: true)
+
+ %{
+ user: user,
+ note: note,
+ object_id: note.id,
+ update_data: update_data,
+ update: update,
+ updated_note: updated_note
+ }
+ end
+
+ test "it updates the note", %{
+ object_id: object_id,
+ update: update,
+ updated_note: updated_note
+ } do
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
+ updated_time = updated_note["updated"]
+
+ new_note = Pleroma.Object.get_by_id(object_id)
+
+ assert %{
+ "summary" => "edited summary",
+ "content" => "edited content",
+ "updated" => ^updated_time
+ } = new_note.data
+ end
+
+ test "it rejects updates with no updated attribute in object", %{
+ object_id: object_id,
+ update: update,
+ updated_note: updated_note
+ } do
+ old_note = Pleroma.Object.get_by_id(object_id)
+ updated_note = Map.drop(updated_note, ["updated"])
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
+ new_note = Pleroma.Object.get_by_id(object_id)
+ assert old_note.data == new_note.data
+ end
+
+ test "it rejects updates with updated attribute older than what we have in the original object",
+ %{
+ object_id: object_id,
+ update: update,
+ updated_note: updated_note
+ } do
+ old_note = Pleroma.Object.get_by_id(object_id)
+ {:ok, creation_time, _} = DateTime.from_iso8601(old_note.data["published"])
+
+ updated_note =
+ Map.put(updated_note, "updated", DateTime.to_iso8601(DateTime.add(creation_time, -10)))
+
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
+ new_note = Pleroma.Object.get_by_id(object_id)
+ assert old_note.data == new_note.data
+ end
+
+ test "it rejects updates with updated attribute older than the last Update", %{
+ object_id: object_id,
+ update: update,
+ updated_note: updated_note
+ } do
+ old_note = Pleroma.Object.get_by_id(object_id)
+ {:ok, creation_time, _} = DateTime.from_iso8601(old_note.data["published"])
+
+ updated_note =
+ Map.put(updated_note, "updated", DateTime.to_iso8601(DateTime.add(creation_time, +10)))
+
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
+
+ old_note = Pleroma.Object.get_by_id(object_id)
+ {:ok, update_time, _} = DateTime.from_iso8601(old_note.data["updated"])
+
+ updated_note =
+ Map.put(updated_note, "updated", DateTime.to_iso8601(DateTime.add(update_time, -5)))
+
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
+
+ new_note = Pleroma.Object.get_by_id(object_id)
+ assert old_note.data == new_note.data
+ end
+
+ test "it updates using object_data", %{
+ object_id: object_id,
+ update: update,
+ updated_note: updated_note
+ } do
+ updated_note = Map.put(updated_note, "summary", "mew mew")
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
+ new_note = Pleroma.Object.get_by_id(object_id)
+ assert %{"summary" => "mew mew", "content" => "edited content"} = new_note.data
+ end
+
+ test "it records the original note in formerRepresentations", %{
+ note: note,
+ object_id: object_id,
+ update: update,
+ updated_note: updated_note
+ } do
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
+ %{data: new_note} = Pleroma.Object.get_by_id(object_id)
+ assert %{"summary" => "edited summary", "content" => "edited content"} = new_note
+
+ assert [Map.drop(note.data, ["id", "formerRepresentations"])] ==
+ new_note["formerRepresentations"]["orderedItems"]
+
+ assert new_note["formerRepresentations"]["totalItems"] == 1
+ end
+
+ test "it puts the original note at the front of formerRepresentations", %{
+ user: user,
+ note: note,
+ object_id: object_id,
+ update: update,
+ updated_note: updated_note
+ } do
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
+ %{data: first_edit} = Pleroma.Object.get_by_id(object_id)
+
+ second_updated_note =
+ note.data
+ |> Map.put("summary", "edited summary 2")
+ |> Map.put("content", "edited content 2")
+ |> Map.put(
+ "updated",
+ first_edit["updated"]
+ |> DateTime.from_iso8601()
+ |> elem(1)
+ |> DateTime.add(10)
+ |> DateTime.to_iso8601()
+ )
+
+ {:ok, second_update_data, []} = Builder.update(user, second_updated_note)
+ {:ok, update, _meta} = ActivityPub.persist(second_update_data, local: true)
+ {:ok, _, _} = SideEffects.handle(update, object_data: second_updated_note)
+ %{data: new_note} = Pleroma.Object.get_by_id(object_id)
+ assert %{"summary" => "edited summary 2", "content" => "edited content 2"} = new_note
+
+ original_version = Map.drop(note.data, ["id", "formerRepresentations"])
+ first_edit = Map.drop(first_edit, ["id", "formerRepresentations"])
+
+ assert [first_edit, original_version] ==
+ new_note["formerRepresentations"]["orderedItems"]
+
+ assert new_note["formerRepresentations"]["totalItems"] == 2
+ end
+
+ test "it does not prepend to formerRepresentations if no actual changes are made", %{
+ note: note,
+ object_id: object_id,
+ update: update,
+ updated_note: updated_note
+ } do
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
+ %{data: first_edit} = Pleroma.Object.get_by_id(object_id)
+
+ updated_note =
+ updated_note
+ |> Map.put(
+ "updated",
+ first_edit["updated"]
+ |> DateTime.from_iso8601()
+ |> elem(1)
+ |> DateTime.add(10)
+ |> DateTime.to_iso8601()
+ )
+
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
+ %{data: new_note} = Pleroma.Object.get_by_id(object_id)
+ assert %{"summary" => "edited summary", "content" => "edited content"} = new_note
+
+ original_version = Map.drop(note.data, ["id", "formerRepresentations"])
+
+ assert [original_version] ==
+ new_note["formerRepresentations"]["orderedItems"]
+
+ assert new_note["formerRepresentations"]["totalItems"] == 1
+ end
+ end
+
+ describe "update questions" do
+ setup do
+ user = insert(:user)
+
+ question =
+ insert(:question,
+ user: user,
+ data: %{"published" => Pleroma.Web.ActivityPub.Utils.make_date()}
+ )
+
+ %{user: user, data: question.data, id: question.id}
+ end
+
+ test "allows updating choice count without generating edit history", %{
+ user: user,
+ data: data,
+ id: id
+ } do
+ new_choices =
+ data["oneOf"]
+ |> Enum.map(fn choice -> put_in(choice, ["replies", "totalItems"], 5) end)
+
+ updated_question =
+ data
+ |> Map.put("oneOf", new_choices)
+ |> Map.put("updated", Pleroma.Web.ActivityPub.Utils.make_date())
+
+ {:ok, update_data, []} = Builder.update(user, updated_question)
+ {:ok, update, _meta} = ActivityPub.persist(update_data, local: true)
+
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_question)
+
+ %{data: new_question} = Pleroma.Object.get_by_id(id)
+
+ assert [%{"replies" => %{"totalItems" => 5}}, %{"replies" => %{"totalItems" => 5}}] =
+ new_question["oneOf"]
+
+ refute Map.has_key?(new_question, "formerRepresentations")
+ end
+
+ test "allows updating choice count without updated field", %{
+ user: user,
+ data: data,
+ id: id
+ } do
+ new_choices =
+ data["oneOf"]
+ |> Enum.map(fn choice -> put_in(choice, ["replies", "totalItems"], 5) end)
+
+ updated_question =
+ data
+ |> Map.put("oneOf", new_choices)
+
+ {:ok, update_data, []} = Builder.update(user, updated_question)
+ {:ok, update, _meta} = ActivityPub.persist(update_data, local: true)
+
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_question)
+
+ %{data: new_question} = Pleroma.Object.get_by_id(id)
+
+ assert [%{"replies" => %{"totalItems" => 5}}, %{"replies" => %{"totalItems" => 5}}] =
+ new_question["oneOf"]
+
+ refute Map.has_key?(new_question, "formerRepresentations")
+ end
+
+ test "allows updating choice count with updated field same as the creation date", %{
+ user: user,
+ data: data,
+ id: id
+ } do
+ new_choices =
+ data["oneOf"]
+ |> Enum.map(fn choice -> put_in(choice, ["replies", "totalItems"], 5) end)
+
+ updated_question =
+ data
+ |> Map.put("oneOf", new_choices)
+ |> Map.put("updated", data["published"])
+
+ {:ok, update_data, []} = Builder.update(user, updated_question)
+ {:ok, update, _meta} = ActivityPub.persist(update_data, local: true)
+
+ {:ok, _, _} = SideEffects.handle(update, object_data: updated_question)
+
+ %{data: new_question} = Pleroma.Object.get_by_id(id)
+
+ assert [%{"replies" => %{"totalItems" => 5}}, %{"replies" => %{"totalItems" => 5}}] =
+ new_question["oneOf"]
+
+ refute Map.has_key?(new_question, "formerRepresentations")
+ end
+ end
+
describe "EmojiReact objects" do
setup do
poster = insert(:user)
@@ -544,9 +839,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
]) do
{:ok, announce, _} = SideEffects.handle(announce)
- assert called(
- Pleroma.Web.Streamer.stream(["user", "list", "public", "public:local"], announce)
- )
+ assert called(Pleroma.Web.Streamer.stream(["user", "list"], announce))
assert called(Pleroma.Web.Push.send(:_))
end