First
[anni] / test / pleroma / web / activity_pub / transmogrifier / chat_message_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.ChatMessageTest do
6   use Pleroma.DataCase
7
8   import Pleroma.Factory
9
10   alias Pleroma.Activity
11   alias Pleroma.Chat
12   alias Pleroma.Object
13   alias Pleroma.Web.ActivityPub.Transmogrifier
14
15   describe "handle_incoming" do
16     test "handles chonks with attachment" do
17       data = %{
18         "@context" => "https://www.w3.org/ns/activitystreams",
19         "actor" => "https://honk.tedunangst.com/u/tedu",
20         "id" => "https://honk.tedunangst.com/u/tedu/honk/x6gt8X8PcyGkQcXxzg1T",
21         "object" => %{
22           "attachment" => [
23             %{
24               "mediaType" => "image/jpeg",
25               "name" => "298p3RG7j27tfsZ9RQ.jpg",
26               "summary" => "298p3RG7j27tfsZ9RQ.jpg",
27               "type" => "Document",
28               "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
29             }
30           ],
31           "attributedTo" => "https://honk.tedunangst.com/u/tedu",
32           "content" => "",
33           "id" => "https://honk.tedunangst.com/u/tedu/chonk/26L4wl5yCbn4dr4y1b",
34           "published" => "2020-05-18T01:13:03Z",
35           "to" => [
36             "https://dontbulling.me/users/lain"
37           ],
38           "type" => "ChatMessage"
39         },
40         "published" => "2020-05-18T01:13:03Z",
41         "to" => [
42           "https://dontbulling.me/users/lain"
43         ],
44         "type" => "Create"
45       }
46
47       _user = insert(:user, ap_id: data["actor"])
48       _user = insert(:user, ap_id: hd(data["to"]))
49
50       assert {:ok, _activity} = Transmogrifier.handle_incoming(data)
51     end
52
53     test "it rejects messages that don't contain content" do
54       data =
55         File.read!("test/fixtures/create-chat-message.json")
56         |> Jason.decode!()
57
58       object =
59         data["object"]
60         |> Map.delete("content")
61
62       data =
63         data
64         |> Map.put("object", object)
65
66       _author =
67         insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
68
69       _recipient =
70         insert(:user,
71           ap_id: List.first(data["to"]),
72           local: true,
73           last_refreshed_at: DateTime.utc_now()
74         )
75
76       {:error, _} = Transmogrifier.handle_incoming(data)
77     end
78
79     test "it rejects messages that don't concern local users" do
80       data =
81         File.read!("test/fixtures/create-chat-message.json")
82         |> Jason.decode!()
83
84       _author =
85         insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
86
87       _recipient =
88         insert(:user,
89           ap_id: List.first(data["to"]),
90           local: false,
91           last_refreshed_at: DateTime.utc_now()
92         )
93
94       {:error, _} = Transmogrifier.handle_incoming(data)
95     end
96
97     test "it rejects messages where the `to` field of activity and object don't match" do
98       data =
99         File.read!("test/fixtures/create-chat-message.json")
100         |> Jason.decode!()
101
102       author = insert(:user, ap_id: data["actor"])
103       _recipient = insert(:user, ap_id: List.first(data["to"]))
104
105       data =
106         data
107         |> Map.put("to", author.ap_id)
108
109       assert match?({:error, _}, Transmogrifier.handle_incoming(data))
110       refute Object.get_by_ap_id(data["object"]["id"])
111     end
112
113     test "it fetches the actor if they aren't in our system" do
114       Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
115
116       data =
117         File.read!("test/fixtures/create-chat-message.json")
118         |> Jason.decode!()
119         |> Map.put("actor", "http://mastodon.example.org/users/admin")
120         |> put_in(["object", "actor"], "http://mastodon.example.org/users/admin")
121
122       _recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
123
124       {:ok, %Activity{} = _activity} = Transmogrifier.handle_incoming(data)
125     end
126
127     test "it doesn't work for deactivated users" do
128       data =
129         File.read!("test/fixtures/create-chat-message.json")
130         |> Jason.decode!()
131
132       _author =
133         insert(:user,
134           ap_id: data["actor"],
135           local: false,
136           last_refreshed_at: DateTime.utc_now(),
137           is_active: false
138         )
139
140       _recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
141
142       assert {:error, _} = Transmogrifier.handle_incoming(data)
143     end
144
145     test "it inserts it and creates a chat" do
146       data =
147         File.read!("test/fixtures/create-chat-message.json")
148         |> Jason.decode!()
149
150       author =
151         insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
152
153       recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
154
155       {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
156       assert activity.local == false
157
158       assert activity.actor == author.ap_id
159       assert activity.recipients == [recipient.ap_id, author.ap_id]
160
161       %Object{} = object = Object.get_by_ap_id(activity.data["object"])
162
163       assert object
164       assert object.data["content"] == "You expected a cute girl? Too bad. alert(&#39;XSS&#39;)"
165       assert match?(%{"firefox" => _}, object.data["emoji"])
166
167       refute Chat.get(author.id, recipient.ap_id)
168       assert Chat.get(recipient.id, author.ap_id)
169     end
170   end
171 end