2020-04-08 13:55:43 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-26 06:11:42 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2020-04-08 13:55:43 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageTest do
|
|
|
|
use Pleroma.DataCase
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
|
|
|
|
alias Pleroma.Activity
|
2020-04-17 12:23:59 +00:00
|
|
|
alias Pleroma.Chat
|
2020-04-08 13:55:43 +00:00
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
|
|
|
|
|
|
|
describe "handle_incoming" do
|
2020-06-04 15:16:10 +00:00
|
|
|
test "handles chonks with attachment" do
|
2020-05-18 18:17:28 +00:00
|
|
|
data = %{
|
|
|
|
"@context" => "https://www.w3.org/ns/activitystreams",
|
|
|
|
"actor" => "https://honk.tedunangst.com/u/tedu",
|
|
|
|
"id" => "https://honk.tedunangst.com/u/tedu/honk/x6gt8X8PcyGkQcXxzg1T",
|
|
|
|
"object" => %{
|
|
|
|
"attachment" => [
|
|
|
|
%{
|
|
|
|
"mediaType" => "image/jpeg",
|
|
|
|
"name" => "298p3RG7j27tfsZ9RQ.jpg",
|
|
|
|
"summary" => "298p3RG7j27tfsZ9RQ.jpg",
|
|
|
|
"type" => "Document",
|
|
|
|
"url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"attributedTo" => "https://honk.tedunangst.com/u/tedu",
|
|
|
|
"content" => "",
|
|
|
|
"id" => "https://honk.tedunangst.com/u/tedu/chonk/26L4wl5yCbn4dr4y1b",
|
|
|
|
"published" => "2020-05-18T01:13:03Z",
|
|
|
|
"to" => [
|
|
|
|
"https://dontbulling.me/users/lain"
|
|
|
|
],
|
|
|
|
"type" => "ChatMessage"
|
|
|
|
},
|
|
|
|
"published" => "2020-05-18T01:13:03Z",
|
|
|
|
"to" => [
|
|
|
|
"https://dontbulling.me/users/lain"
|
|
|
|
],
|
|
|
|
"type" => "Create"
|
|
|
|
}
|
|
|
|
|
|
|
|
_user = insert(:user, ap_id: data["actor"])
|
|
|
|
_user = insert(:user, ap_id: hd(data["to"]))
|
|
|
|
|
|
|
|
assert {:ok, _activity} = Transmogrifier.handle_incoming(data)
|
|
|
|
end
|
|
|
|
|
2020-04-16 13:21:47 +00:00
|
|
|
test "it rejects messages that don't contain content" do
|
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/create-chat-message.json")
|
2020-11-23 19:28:55 +00:00
|
|
|
|> Jason.decode!()
|
2020-04-16 13:21:47 +00:00
|
|
|
|
|
|
|
object =
|
|
|
|
data["object"]
|
|
|
|
|> Map.delete("content")
|
|
|
|
|
|
|
|
data =
|
|
|
|
data
|
|
|
|
|> Map.put("object", object)
|
|
|
|
|
2020-04-29 12:25:33 +00:00
|
|
|
_author =
|
|
|
|
insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
|
|
|
|
|
|
|
|
_recipient =
|
|
|
|
insert(:user,
|
|
|
|
ap_id: List.first(data["to"]),
|
|
|
|
local: true,
|
|
|
|
last_refreshed_at: DateTime.utc_now()
|
|
|
|
)
|
2020-04-16 13:21:47 +00:00
|
|
|
|
|
|
|
{:error, _} = Transmogrifier.handle_incoming(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it rejects messages that don't concern local users" do
|
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/create-chat-message.json")
|
2020-11-23 19:28:55 +00:00
|
|
|
|> Jason.decode!()
|
2020-04-16 13:21:47 +00:00
|
|
|
|
2020-04-29 12:25:33 +00:00
|
|
|
_author =
|
|
|
|
insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
|
|
|
|
|
|
|
|
_recipient =
|
|
|
|
insert(:user,
|
|
|
|
ap_id: List.first(data["to"]),
|
|
|
|
local: false,
|
|
|
|
last_refreshed_at: DateTime.utc_now()
|
|
|
|
)
|
2020-04-16 13:21:47 +00:00
|
|
|
|
|
|
|
{:error, _} = Transmogrifier.handle_incoming(data)
|
|
|
|
end
|
|
|
|
|
2020-04-17 12:23:59 +00:00
|
|
|
test "it rejects messages where the `to` field of activity and object don't match" do
|
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/create-chat-message.json")
|
2020-11-23 19:28:55 +00:00
|
|
|
|> Jason.decode!()
|
2020-04-17 12:23:59 +00:00
|
|
|
|
|
|
|
author = insert(:user, ap_id: data["actor"])
|
|
|
|
_recipient = insert(:user, ap_id: List.first(data["to"]))
|
|
|
|
|
|
|
|
data =
|
|
|
|
data
|
|
|
|
|> Map.put("to", author.ap_id)
|
|
|
|
|
2020-04-28 14:26:19 +00:00
|
|
|
assert match?({:error, _}, Transmogrifier.handle_incoming(data))
|
|
|
|
refute Object.get_by_ap_id(data["object"]["id"])
|
2020-04-17 12:23:59 +00:00
|
|
|
end
|
|
|
|
|
2020-04-29 12:25:33 +00:00
|
|
|
test "it fetches the actor if they aren't in our system" do
|
|
|
|
Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
|
|
|
|
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/create-chat-message.json")
|
2020-11-23 19:28:55 +00:00
|
|
|
|> Jason.decode!()
|
2020-04-29 12:25:33 +00:00
|
|
|
|> Map.put("actor", "http://mastodon.example.org/users/admin")
|
|
|
|
|> put_in(["object", "actor"], "http://mastodon.example.org/users/admin")
|
|
|
|
|
|
|
|
_recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
|
|
|
|
|
|
|
|
{:ok, %Activity{} = _activity} = Transmogrifier.handle_incoming(data)
|
|
|
|
end
|
|
|
|
|
2020-08-04 12:17:03 +00:00
|
|
|
test "it doesn't work for deactivated users" do
|
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/create-chat-message.json")
|
2020-11-23 19:28:55 +00:00
|
|
|
|> Jason.decode!()
|
2020-08-04 12:17:03 +00:00
|
|
|
|
|
|
|
_author =
|
|
|
|
insert(:user,
|
|
|
|
ap_id: data["actor"],
|
|
|
|
local: false,
|
|
|
|
last_refreshed_at: DateTime.utc_now(),
|
2020-10-12 22:42:27 +00:00
|
|
|
is_active: false
|
2020-08-04 12:17:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
_recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
|
|
|
|
|
|
|
|
assert {:error, _} = Transmogrifier.handle_incoming(data)
|
|
|
|
end
|
|
|
|
|
2020-04-16 13:21:47 +00:00
|
|
|
test "it inserts it and creates a chat" do
|
2020-04-08 13:55:43 +00:00
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/create-chat-message.json")
|
2020-11-23 19:28:55 +00:00
|
|
|
|> Jason.decode!()
|
2020-04-08 13:55:43 +00:00
|
|
|
|
2020-04-29 12:25:33 +00:00
|
|
|
author =
|
|
|
|
insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
|
|
|
|
|
2020-04-16 13:21:47 +00:00
|
|
|
recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
|
2020-04-08 13:55:43 +00:00
|
|
|
|
|
|
|
{:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
|
2020-04-29 11:45:50 +00:00
|
|
|
assert activity.local == false
|
2020-04-08 13:55:43 +00:00
|
|
|
|
|
|
|
assert activity.actor == author.ap_id
|
|
|
|
assert activity.recipients == [recipient.ap_id, author.ap_id]
|
|
|
|
|
|
|
|
%Object{} = object = Object.get_by_ap_id(activity.data["object"])
|
2020-04-16 15:50:24 +00:00
|
|
|
|
2020-04-08 13:55:43 +00:00
|
|
|
assert object
|
2020-04-16 15:50:24 +00:00
|
|
|
assert object.data["content"] == "You expected a cute girl? Too bad. alert('XSS')"
|
2020-04-23 14:19:49 +00:00
|
|
|
assert match?(%{"firefox" => _}, object.data["emoji"])
|
2020-04-17 12:23:59 +00:00
|
|
|
|
|
|
|
refute Chat.get(author.id, recipient.ap_id)
|
|
|
|
assert Chat.get(recipient.id, author.ap_id)
|
2020-04-08 13:55:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|