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