a8a7edca5da505f3d7fdf1acf3df0f290cb4542e
[anni] / test / pleroma / web / activity_pub / transmogrifier / add_remove_handling_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.Transmogrifier.AddRemoveHandlingTest do
6   use Oban.Testing, repo: Pleroma.Repo
7   use Pleroma.DataCase, async: true
8
9   require Pleroma.Constants
10
11   import Pleroma.Factory
12
13   alias Pleroma.User
14   alias Pleroma.Web.ActivityPub.Transmogrifier
15
16   test "it accepts Add/Remove activities" do
17     user =
18       "test/fixtures/users_mock/user.json"
19       |> File.read!()
20       |> String.replace("{{nickname}}", "lain")
21
22     object_id = "c61d6733-e256-4fe1-ab13-1e369789423f"
23
24     object =
25       "test/fixtures/statuses/note.json"
26       |> File.read!()
27       |> String.replace("{{nickname}}", "lain")
28       |> String.replace("{{object_id}}", object_id)
29
30     object_url = "https://example.com/objects/#{object_id}"
31
32     actor = "https://example.com/users/lain"
33
34     Tesla.Mock.mock(fn
35       %{
36         method: :get,
37         url: ^actor
38       } ->
39         %Tesla.Env{
40           status: 200,
41           body: user,
42           headers: [{"content-type", "application/activity+json"}]
43         }
44
45       %{
46         method: :get,
47         url: ^object_url
48       } ->
49         %Tesla.Env{
50           status: 200,
51           body: object,
52           headers: [{"content-type", "application/activity+json"}]
53         }
54
55       %{method: :get, url: "https://example.com/users/lain/collections/featured"} ->
56         %Tesla.Env{
57           status: 200,
58           body:
59             "test/fixtures/users_mock/masto_featured.json"
60             |> File.read!()
61             |> String.replace("{{domain}}", "example.com")
62             |> String.replace("{{nickname}}", "lain"),
63           headers: [{"content-type", "application/activity+json"}]
64         }
65     end)
66
67     message = %{
68       "id" => "https://example.com/objects/d61d6733-e256-4fe1-ab13-1e369789423f",
69       "actor" => actor,
70       "object" => object_url,
71       "target" => "https://example.com/users/lain/collections/featured",
72       "type" => "Add",
73       "to" => [Pleroma.Constants.as_public()],
74       "cc" => ["https://example.com/users/lain/followers"],
75       "bcc" => [],
76       "bto" => []
77     }
78
79     assert {:ok, activity} = Transmogrifier.handle_incoming(message)
80     assert activity.data == message
81     user = User.get_cached_by_ap_id(actor)
82     assert user.pinned_objects[object_url]
83
84     remove = %{
85       "id" => "http://localhost:400/objects/d61d6733-e256-4fe1-ab13-1e369789423d",
86       "actor" => actor,
87       "object" => object_url,
88       "target" => "https://example.com/users/lain/collections/featured",
89       "type" => "Remove",
90       "to" => [Pleroma.Constants.as_public()],
91       "cc" => ["https://example.com/users/lain/followers"],
92       "bcc" => [],
93       "bto" => []
94     }
95
96     assert {:ok, activity} = Transmogrifier.handle_incoming(remove)
97     assert activity.data == remove
98
99     user = refresh_record(user)
100     refute user.pinned_objects[object_url]
101   end
102
103   test "Add/Remove activities for remote users without featured address" do
104     user = insert(:user, local: false, domain: "example.com")
105
106     user =
107       user
108       |> Ecto.Changeset.change(featured_address: nil)
109       |> Repo.update!()
110
111     %{host: host} = URI.parse(user.ap_id)
112
113     user_data =
114       "test/fixtures/users_mock/user.json"
115       |> File.read!()
116       |> String.replace("{{nickname}}", user.nickname)
117
118     object_id = "c61d6733-e256-4fe1-ab13-1e369789423f"
119
120     object =
121       "test/fixtures/statuses/note.json"
122       |> File.read!()
123       |> String.replace("{{nickname}}", user.nickname)
124       |> String.replace("{{object_id}}", object_id)
125
126     object_url = "https://#{host}/objects/#{object_id}"
127
128     actor = "https://#{host}/users/#{user.nickname}"
129
130     featured = "https://#{host}/users/#{user.nickname}/collections/featured"
131
132     Tesla.Mock.mock(fn
133       %{
134         method: :get,
135         url: ^actor
136       } ->
137         %Tesla.Env{
138           status: 200,
139           body: user_data,
140           headers: [{"content-type", "application/activity+json"}]
141         }
142
143       %{
144         method: :get,
145         url: ^object_url
146       } ->
147         %Tesla.Env{
148           status: 200,
149           body: object,
150           headers: [{"content-type", "application/activity+json"}]
151         }
152
153       %{method: :get, url: ^featured} ->
154         %Tesla.Env{
155           status: 200,
156           body:
157             "test/fixtures/users_mock/masto_featured.json"
158             |> File.read!()
159             |> String.replace("{{domain}}", "#{host}")
160             |> String.replace("{{nickname}}", user.nickname),
161           headers: [{"content-type", "application/activity+json"}]
162         }
163     end)
164
165     message = %{
166       "id" => "https://#{host}/objects/d61d6733-e256-4fe1-ab13-1e369789423f",
167       "actor" => actor,
168       "object" => object_url,
169       "target" => "https://#{host}/users/#{user.nickname}/collections/featured",
170       "type" => "Add",
171       "to" => [Pleroma.Constants.as_public()],
172       "cc" => ["https://#{host}/users/#{user.nickname}/followers"],
173       "bcc" => [],
174       "bto" => []
175     }
176
177     assert {:ok, activity} = Transmogrifier.handle_incoming(message)
178     assert activity.data == message
179     user = User.get_cached_by_ap_id(actor)
180     assert user.pinned_objects[object_url]
181   end
182 end