1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ActivityTest do
9 alias Pleroma.ThreadMute
10 import Pleroma.Factory
12 test "returns an activity by it's AP id" do
13 activity = insert(:note_activity)
14 found_activity = Activity.get_by_ap_id(activity.data["id"])
16 assert activity == found_activity
19 test "returns activities by it's objects AP ids" do
20 activity = insert(:note_activity)
21 [found_activity] = Activity.get_all_create_by_object_ap_id(activity.data["object"]["id"])
23 assert activity == found_activity
26 test "returns the activity that created an object" do
27 activity = insert(:note_activity)
29 found_activity = Activity.get_create_by_object_ap_id(activity.data["object"]["id"])
31 assert activity == found_activity
34 test "preloading a bookmark" do
38 activity = insert(:note_activity)
39 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
40 {:ok, _bookmark2} = Bookmark.create(user2.id, activity.id)
41 {:ok, bookmark3} = Bookmark.create(user3.id, activity.id)
44 Ecto.Query.from(Pleroma.Activity)
45 |> Activity.with_preloaded_bookmark(user3)
48 assert queried_activity.bookmark == bookmark3
51 test "setting thread_muted?" do
52 activity = insert(:note_activity)
54 annoyed_user = insert(:user)
55 {:ok, _} = ThreadMute.add_mute(annoyed_user.id, activity.data["context"])
57 activity_with_unset_thread_muted_field =
58 Ecto.Query.from(Activity)
62 Ecto.Query.from(Activity)
63 |> Activity.with_set_thread_muted_field(user)
66 activity_for_annoyed_user =
67 Ecto.Query.from(Activity)
68 |> Activity.with_set_thread_muted_field(annoyed_user)
71 assert activity_with_unset_thread_muted_field.thread_muted? == nil
72 assert activity_for_user.thread_muted? == false
73 assert activity_for_annoyed_user.thread_muted? == true
76 describe "getting a bookmark" do
77 test "when association is loaded" do
79 activity = insert(:note_activity)
80 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
83 Ecto.Query.from(Pleroma.Activity)
84 |> Activity.with_preloaded_bookmark(user)
87 assert Activity.get_bookmark(queried_activity, user) == bookmark
90 test "when association is not loaded" do
92 activity = insert(:note_activity)
93 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
96 Ecto.Query.from(Pleroma.Activity)
99 assert Activity.get_bookmark(queried_activity, user) == bookmark
105 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
110 "@context" => "https://www.w3.org/ns/activitystreams",
111 "actor" => "http://mastodon.example.org/users/admin",
113 "id" => "http://mastodon.example.org/users/admin/activities/1",
116 "content" => "find me!",
117 "id" => "http://mastodon.example.org/users/admin/objects/1",
118 "attributedTo" => "http://mastodon.example.org/users/admin"
120 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
123 {:ok, local_activity} = Pleroma.Web.CommonAPI.post(user, %{"status" => "find me!"})
124 {:ok, remote_activity} = Pleroma.Web.Federator.incoming_ap_doc(params)
125 %{local_activity: local_activity, remote_activity: remote_activity, user: user}
128 test "find local and remote statuses for authenticated users", %{
129 local_activity: local_activity,
130 remote_activity: remote_activity,
133 activities = Enum.sort_by(Activity.search(user, "find me"), & &1.id)
135 assert [^local_activity, ^remote_activity] = activities
138 test "find only local statuses for unauthenticated users", %{local_activity: local_activity} do
139 assert [^local_activity] = Activity.search(nil, "find me")
142 test "find only local statuses for unauthenticated users when `limit_to_local_content` is `:all`",
143 %{local_activity: local_activity} do
144 Pleroma.Config.put([:instance, :limit_to_local_content], :all)
145 assert [^local_activity] = Activity.search(nil, "find me")
146 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
149 test "find all statuses for unauthenticated users when `limit_to_local_content` is `false`",
151 local_activity: local_activity,
152 remote_activity: remote_activity
154 Pleroma.Config.put([:instance, :limit_to_local_content], false)
156 activities = Enum.sort_by(Activity.search(nil, "find me"), & &1.id)
158 assert [^local_activity, ^remote_activity] = activities
160 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)