Merge remote-tracking branch 'remotes/upstream/develop' into 1149-oban-job-queue
[akkoma] / test / activity_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ActivityTest do
6 use Pleroma.DataCase
7 alias Pleroma.Activity
8 alias Pleroma.Bookmark
9 alias Pleroma.Object
10 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.ThreadMute
12 import Pleroma.Factory
13
14 test "returns an activity by it's AP id" do
15 activity = insert(:note_activity)
16 found_activity = Activity.get_by_ap_id(activity.data["id"])
17
18 assert activity == found_activity
19 end
20
21 test "returns activities by it's objects AP ids" do
22 activity = insert(:note_activity)
23 object_data = Object.normalize(activity).data
24
25 [found_activity] = Activity.get_all_create_by_object_ap_id(object_data["id"])
26
27 assert activity == found_activity
28 end
29
30 test "returns the activity that created an object" do
31 activity = insert(:note_activity)
32 object_data = Object.normalize(activity).data
33
34 found_activity = Activity.get_create_by_object_ap_id(object_data["id"])
35
36 assert activity == found_activity
37 end
38
39 test "preloading a bookmark" do
40 user = insert(:user)
41 user2 = insert(:user)
42 user3 = insert(:user)
43 activity = insert(:note_activity)
44 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
45 {:ok, _bookmark2} = Bookmark.create(user2.id, activity.id)
46 {:ok, bookmark3} = Bookmark.create(user3.id, activity.id)
47
48 queried_activity =
49 Ecto.Query.from(Pleroma.Activity)
50 |> Activity.with_preloaded_bookmark(user3)
51 |> Repo.one()
52
53 assert queried_activity.bookmark == bookmark3
54 end
55
56 test "setting thread_muted?" do
57 activity = insert(:note_activity)
58 user = insert(:user)
59 annoyed_user = insert(:user)
60 {:ok, _} = ThreadMute.add_mute(annoyed_user.id, activity.data["context"])
61
62 activity_with_unset_thread_muted_field =
63 Ecto.Query.from(Activity)
64 |> Repo.one()
65
66 activity_for_user =
67 Ecto.Query.from(Activity)
68 |> Activity.with_set_thread_muted_field(user)
69 |> Repo.one()
70
71 activity_for_annoyed_user =
72 Ecto.Query.from(Activity)
73 |> Activity.with_set_thread_muted_field(annoyed_user)
74 |> Repo.one()
75
76 assert activity_with_unset_thread_muted_field.thread_muted? == nil
77 assert activity_for_user.thread_muted? == false
78 assert activity_for_annoyed_user.thread_muted? == true
79 end
80
81 describe "getting a bookmark" do
82 test "when association is loaded" do
83 user = insert(:user)
84 activity = insert(:note_activity)
85 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
86
87 queried_activity =
88 Ecto.Query.from(Pleroma.Activity)
89 |> Activity.with_preloaded_bookmark(user)
90 |> Repo.one()
91
92 assert Activity.get_bookmark(queried_activity, user) == bookmark
93 end
94
95 test "when association is not loaded" do
96 user = insert(:user)
97 activity = insert(:note_activity)
98 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
99
100 queried_activity =
101 Ecto.Query.from(Pleroma.Activity)
102 |> Repo.one()
103
104 assert Activity.get_bookmark(queried_activity, user) == bookmark
105 end
106 end
107
108 describe "search" do
109 setup do
110 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
111
112 user = insert(:user)
113
114 params = %{
115 "@context" => "https://www.w3.org/ns/activitystreams",
116 "actor" => "http://mastodon.example.org/users/admin",
117 "type" => "Create",
118 "id" => "http://mastodon.example.org/users/admin/activities/1",
119 "object" => %{
120 "type" => "Note",
121 "content" => "find me!",
122 "id" => "http://mastodon.example.org/users/admin/objects/1",
123 "attributedTo" => "http://mastodon.example.org/users/admin"
124 },
125 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
126 }
127
128 {:ok, local_activity} = Pleroma.Web.CommonAPI.post(user, %{"status" => "find me!"})
129 {:ok, job} = Pleroma.Web.Federator.incoming_ap_doc(params)
130 {:ok, remote_activity} = ObanHelpers.perform(job)
131 %{local_activity: local_activity, remote_activity: remote_activity, user: user}
132 end
133
134 test "find local and remote statuses for authenticated users", %{
135 local_activity: local_activity,
136 remote_activity: remote_activity,
137 user: user
138 } do
139 activities = Enum.sort_by(Activity.search(user, "find me"), & &1.id)
140
141 assert [^local_activity, ^remote_activity] = activities
142 end
143
144 test "find only local statuses for unauthenticated users", %{local_activity: local_activity} do
145 assert [^local_activity] = Activity.search(nil, "find me")
146 end
147
148 test "find only local statuses for unauthenticated users when `limit_to_local_content` is `:all`",
149 %{local_activity: local_activity} do
150 Pleroma.Config.put([:instance, :limit_to_local_content], :all)
151 assert [^local_activity] = Activity.search(nil, "find me")
152 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
153 end
154
155 test "find all statuses for unauthenticated users when `limit_to_local_content` is `false`",
156 %{
157 local_activity: local_activity,
158 remote_activity: remote_activity
159 } do
160 Pleroma.Config.put([:instance, :limit_to_local_content], false)
161
162 activities = Enum.sort_by(Activity.search(nil, "find me"), & &1.id)
163
164 assert [^local_activity, ^remote_activity] = activities
165
166 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
167 end
168 end
169 end