1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageTest do
10 alias Pleroma.Activity
13 alias Pleroma.Web.ActivityPub.Transmogrifier
15 describe "handle_incoming" do
16 test "handles chonks with attachment" do
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",
24 "mediaType" => "image/jpeg",
25 "name" => "298p3RG7j27tfsZ9RQ.jpg",
26 "summary" => "298p3RG7j27tfsZ9RQ.jpg",
28 "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
31 "attributedTo" => "https://honk.tedunangst.com/u/tedu",
33 "id" => "https://honk.tedunangst.com/u/tedu/chonk/26L4wl5yCbn4dr4y1b",
34 "published" => "2020-05-18T01:13:03Z",
36 "https://dontbulling.me/users/lain"
38 "type" => "ChatMessage"
40 "published" => "2020-05-18T01:13:03Z",
42 "https://dontbulling.me/users/lain"
47 _user = insert(:user, ap_id: data["actor"])
48 _user = insert(:user, ap_id: hd(data["to"]))
50 assert {:ok, _activity} = Transmogrifier.handle_incoming(data)
53 test "it rejects messages that don't contain content" do
55 File.read!("test/fixtures/create-chat-message.json")
60 |> Map.delete("content")
64 |> Map.put("object", object)
67 insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
71 ap_id: List.first(data["to"]),
73 last_refreshed_at: DateTime.utc_now()
76 {:error, _} = Transmogrifier.handle_incoming(data)
79 test "it rejects messages that don't concern local users" do
81 File.read!("test/fixtures/create-chat-message.json")
85 insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
89 ap_id: List.first(data["to"]),
91 last_refreshed_at: DateTime.utc_now()
94 {:error, _} = Transmogrifier.handle_incoming(data)
97 test "it rejects messages where the `to` field of activity and object don't match" do
99 File.read!("test/fixtures/create-chat-message.json")
102 author = insert(:user, ap_id: data["actor"])
103 _recipient = insert(:user, ap_id: List.first(data["to"]))
107 |> Map.put("to", author.ap_id)
109 assert match?({:error, _}, Transmogrifier.handle_incoming(data))
110 refute Object.get_by_ap_id(data["object"]["id"])
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)
117 File.read!("test/fixtures/create-chat-message.json")
119 |> Map.put("actor", "http://mastodon.example.org/users/admin")
120 |> put_in(["object", "actor"], "http://mastodon.example.org/users/admin")
122 _recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
124 {:ok, %Activity{} = _activity} = Transmogrifier.handle_incoming(data)
127 test "it doesn't work for deactivated users" do
129 File.read!("test/fixtures/create-chat-message.json")
134 ap_id: data["actor"],
136 last_refreshed_at: DateTime.utc_now(),
140 _recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
142 assert {:error, _} = Transmogrifier.handle_incoming(data)
145 test "it inserts it and creates a chat" do
147 File.read!("test/fixtures/create-chat-message.json")
151 insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
153 recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
155 {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
156 assert activity.local == false
158 assert activity.actor == author.ap_id
159 assert activity.recipients == [recipient.ap_id, author.ap_id]
161 %Object{} = object = Object.get_by_ap_id(activity.data["object"])
164 assert object.data["content"] == "You expected a cute girl? Too bad. alert('XSS')"
165 assert match?(%{"firefox" => _}, object.data["emoji"])
167 refute Chat.get(author.id, recipient.ap_id)
168 assert Chat.get(recipient.id, author.ap_id)