2020-04-08 09:55:43 -04:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# 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 08:23:59 -04:00
|
|
|
alias Pleroma.Chat
|
2020-04-08 09:55:43 -04:00
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
|
|
|
|
|
|
|
describe "handle_incoming" do
|
2020-04-16 09:21:47 -04:00
|
|
|
test "it rejects messages that don't contain content" do
|
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/create-chat-message.json")
|
|
|
|
|> Poison.decode!()
|
|
|
|
|
|
|
|
object =
|
|
|
|
data["object"]
|
|
|
|
|> Map.delete("content")
|
|
|
|
|
|
|
|
data =
|
|
|
|
data
|
|
|
|
|> Map.put("object", object)
|
|
|
|
|
|
|
|
_author = insert(:user, ap_id: data["actor"], local: false)
|
|
|
|
_recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
|
|
|
|
|
|
|
|
{: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")
|
|
|
|
|> Poison.decode!()
|
|
|
|
|
|
|
|
_author = insert(:user, ap_id: data["actor"], local: false)
|
|
|
|
_recipient = insert(:user, ap_id: List.first(data["to"]), local: false)
|
|
|
|
|
|
|
|
{:error, _} = Transmogrifier.handle_incoming(data)
|
|
|
|
end
|
|
|
|
|
2020-04-17 08:23:59 -04: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")
|
|
|
|
|> Poison.decode!()
|
|
|
|
|
|
|
|
author = insert(:user, ap_id: data["actor"])
|
|
|
|
_recipient = insert(:user, ap_id: List.first(data["to"]))
|
|
|
|
|
|
|
|
data =
|
|
|
|
data
|
|
|
|
|> Map.put("to", author.ap_id)
|
|
|
|
|
|
|
|
{:error, _} = Transmogrifier.handle_incoming(data)
|
|
|
|
end
|
|
|
|
|
2020-04-16 09:21:47 -04:00
|
|
|
test "it inserts it and creates a chat" do
|
2020-04-08 09:55:43 -04:00
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/create-chat-message.json")
|
|
|
|
|> Poison.decode!()
|
|
|
|
|
|
|
|
author = insert(:user, ap_id: data["actor"], local: false)
|
2020-04-16 09:21:47 -04:00
|
|
|
recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
|
2020-04-08 09:55:43 -04:00
|
|
|
|
|
|
|
{:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
|
|
|
|
|
|
|
|
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 11:50:24 -04:00
|
|
|
|
2020-04-08 09:55:43 -04:00
|
|
|
assert object
|
2020-04-16 11:50:24 -04:00
|
|
|
assert object.data["content"] == "You expected a cute girl? Too bad. alert('XSS')"
|
2020-04-17 08:23:59 -04:00
|
|
|
|
|
|
|
refute Chat.get(author.id, recipient.ap_id)
|
|
|
|
assert Chat.get(recipient.id, author.ap_id)
|
2020-04-08 09:55:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|