e895636b550097ecd53770d03c394e6fa7a2dc94
[akkoma] / test / pleroma / web / activity_pub / transmogrifier / announce_handling_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.Transmogrifier.AnnounceHandlingTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Activity
9 alias Pleroma.Object
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11 alias Pleroma.Web.CommonAPI
12
13 import Pleroma.Factory
14
15 test "it works for incoming honk announces" do
16 user = insert(:user, ap_id: "https://honktest/u/test", local: false)
17 other_user = insert(:user)
18 {:ok, post} = CommonAPI.post(other_user, %{status: "bonkeronk"})
19
20 announce = %{
21 "@context" => "https://www.w3.org/ns/activitystreams",
22 "actor" => "https://honktest/u/test",
23 "id" => "https://honktest/u/test/bonk/1793M7B9MQ48847vdx",
24 "object" => post.data["object"],
25 "published" => "2019-06-25T19:33:58Z",
26 "to" => "https://www.w3.org/ns/activitystreams#Public",
27 "type" => "Announce"
28 }
29
30 {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(announce)
31
32 object = Object.get_by_ap_id(post.data["object"])
33
34 assert length(object.data["announcements"]) == 1
35 assert user.ap_id in object.data["announcements"]
36 end
37
38 test "it works for incoming announces with actor being inlined (kroeg)" do
39 data = File.read!("test/fixtures/kroeg-announce-with-inline-actor.json") |> Poison.decode!()
40
41 _user = insert(:user, local: false, ap_id: data["actor"]["id"])
42 other_user = insert(:user)
43
44 {:ok, post} = CommonAPI.post(other_user, %{status: "kroegeroeg"})
45
46 data =
47 data
48 |> put_in(["object", "id"], post.data["object"])
49
50 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
51
52 assert data["actor"] == "https://puckipedia.com/"
53 end
54
55 test "it works for incoming announces, fetching the announced object" do
56 data =
57 File.read!("test/fixtures/mastodon-announce.json")
58 |> Poison.decode!()
59 |> Map.put("object", "http://mastodon.example.org/users/admin/statuses/99541947525187367")
60
61 Tesla.Mock.mock(fn
62 %{method: :get} ->
63 %Tesla.Env{status: 200, body: File.read!("test/fixtures/mastodon-note-object.json")}
64 end)
65
66 _user = insert(:user, local: false, ap_id: data["actor"])
67
68 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
69
70 assert data["actor"] == "http://mastodon.example.org/users/admin"
71 assert data["type"] == "Announce"
72
73 assert data["id"] ==
74 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
75
76 assert data["object"] ==
77 "http://mastodon.example.org/users/admin/statuses/99541947525187367"
78
79 assert(Activity.get_create_by_object_ap_id(data["object"]))
80 end
81
82 @tag capture_log: true
83 test "it works for incoming announces with an existing activity" do
84 user = insert(:user)
85 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
86
87 data =
88 File.read!("test/fixtures/mastodon-announce.json")
89 |> Poison.decode!()
90 |> Map.put("object", activity.data["object"])
91
92 _user = insert(:user, local: false, ap_id: data["actor"])
93
94 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
95
96 assert data["actor"] == "http://mastodon.example.org/users/admin"
97 assert data["type"] == "Announce"
98
99 assert data["id"] ==
100 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
101
102 assert data["object"] == activity.data["object"]
103
104 assert Activity.get_create_by_object_ap_id(data["object"]).id == activity.id
105 end
106
107 # Ignore inlined activities for now
108 @tag skip: true
109 test "it works for incoming announces with an inlined activity" do
110 data =
111 File.read!("test/fixtures/mastodon-announce-private.json")
112 |> Poison.decode!()
113
114 _user =
115 insert(:user,
116 local: false,
117 ap_id: data["actor"],
118 follower_address: data["actor"] <> "/followers"
119 )
120
121 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
122
123 assert data["actor"] == "http://mastodon.example.org/users/admin"
124 assert data["type"] == "Announce"
125
126 assert data["id"] ==
127 "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
128
129 object = Object.normalize(data["object"])
130
131 assert object.data["id"] == "http://mastodon.example.org/@admin/99541947525187368"
132 assert object.data["content"] == "this is a private toot"
133 end
134
135 @tag capture_log: true
136 test "it rejects incoming announces with an inlined activity from another origin" do
137 Tesla.Mock.mock(fn
138 %{method: :get} -> %Tesla.Env{status: 404, body: ""}
139 end)
140
141 data =
142 File.read!("test/fixtures/bogus-mastodon-announce.json")
143 |> Poison.decode!()
144
145 _user = insert(:user, local: false, ap_id: data["actor"])
146
147 assert {:error, e} = Transmogrifier.handle_incoming(data)
148 end
149
150 test "it does not clobber the addressing on announce activities" do
151 user = insert(:user)
152 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
153
154 data =
155 File.read!("test/fixtures/mastodon-announce.json")
156 |> Poison.decode!()
157 |> Map.put("object", Object.normalize(activity).data["id"])
158 |> Map.put("to", ["http://mastodon.example.org/users/admin/followers"])
159 |> Map.put("cc", [])
160
161 _user =
162 insert(:user,
163 local: false,
164 ap_id: data["actor"],
165 follower_address: "http://mastodon.example.org/users/admin/followers"
166 )
167
168 {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
169
170 assert data["to"] == ["http://mastodon.example.org/users/admin/followers"]
171 end
172 end