Resolve conflicts
[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.ThreadMute
10 import Pleroma.Factory
11
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"])
15
16 assert activity == found_activity
17 end
18
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"])
22
23 assert activity == found_activity
24 end
25
26 test "returns the activity that created an object" do
27 activity = insert(:note_activity)
28
29 found_activity = Activity.get_create_by_object_ap_id(activity.data["object"]["id"])
30
31 assert activity == found_activity
32 end
33
34 test "preloading a bookmark" do
35 user = insert(:user)
36 user2 = insert(:user)
37 user3 = insert(:user)
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)
42
43 queried_activity =
44 Ecto.Query.from(Pleroma.Activity)
45 |> Activity.with_preloaded_bookmark(user3)
46 |> Repo.one()
47
48 assert queried_activity.bookmark == bookmark3
49 end
50
51 test "setting thread_muted?" do
52 activity = insert(:note_activity)
53 user = insert(:user)
54 annoyed_user = insert(:user)
55 {:ok, _} = ThreadMute.add_mute(annoyed_user.id, activity.data["context"])
56
57 activity_with_unset_thread_muted_field =
58 Ecto.Query.from(Activity)
59 |> Repo.one()
60
61 activity_for_user =
62 Ecto.Query.from(Activity)
63 |> Activity.with_set_thread_muted_field(user)
64 |> Repo.one()
65
66 activity_for_annoyed_user =
67 Ecto.Query.from(Activity)
68 |> Activity.with_set_thread_muted_field(annoyed_user)
69 |> Repo.one()
70
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
74 end
75
76 describe "getting a bookmark" do
77 test "when association is loaded" do
78 user = insert(:user)
79 activity = insert(:note_activity)
80 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
81
82 queried_activity =
83 Ecto.Query.from(Pleroma.Activity)
84 |> Activity.with_preloaded_bookmark(user)
85 |> Repo.one()
86
87 assert Activity.get_bookmark(queried_activity, user) == bookmark
88 end
89
90 test "when association is not loaded" do
91 user = insert(:user)
92 activity = insert(:note_activity)
93 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
94
95 queried_activity =
96 Ecto.Query.from(Pleroma.Activity)
97 |> Repo.one()
98
99 assert Activity.get_bookmark(queried_activity, user) == bookmark
100 end
101 end
102
103 describe "search" do
104 setup do
105 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
106
107 user = insert(:user)
108
109 params = %{
110 "@context" => "https://www.w3.org/ns/activitystreams",
111 "actor" => "http://mastodon.example.org/users/admin",
112 "type" => "Create",
113 "id" => "http://mastodon.example.org/users/admin/activities/1",
114 "object" => %{
115 "type" => "Note",
116 "content" => "find me!",
117 "id" => "http://mastodon.example.org/users/admin/objects/1",
118 "attributedTo" => "http://mastodon.example.org/users/admin"
119 },
120 "to" => ["https://www.w3.org/ns/activitystreams#Public"]
121 }
122
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}
126 end
127
128 test "find local and remote statuses for authenticated users", %{
129 local_activity: local_activity,
130 remote_activity: remote_activity,
131 user: user
132 } do
133 activities = Enum.sort_by(Activity.search(user, "find me"), & &1.id)
134
135 assert [^local_activity, ^remote_activity] = activities
136 end
137
138 test "find only local statuses for unauthenticated users", %{local_activity: local_activity} do
139 assert [^local_activity] = Activity.search(nil, "find me")
140 end
141
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)
147 end
148
149 test "find all statuses for unauthenticated users when `limit_to_local_content` is `false`",
150 %{
151 local_activity: local_activity,
152 remote_activity: remote_activity
153 } do
154 Pleroma.Config.put([:instance, :limit_to_local_content], false)
155
156 activities = Enum.sort_by(Activity.search(nil, "find me"), & &1.id)
157
158 assert [^local_activity, ^remote_activity] = activities
159
160 Pleroma.Config.put([:instance, :limit_to_local_content], :unauthenticated)
161 end
162 end
163 end