Pipeline: Always run common_pipeline in a transaction for now.
[akkoma] / test / web / activity_pub / transmogrifier / chat_message_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.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 "it rejects messages that don't contain content" do
17 data =
18 File.read!("test/fixtures/create-chat-message.json")
19 |> Poison.decode!()
20
21 object =
22 data["object"]
23 |> Map.delete("content")
24
25 data =
26 data
27 |> Map.put("object", object)
28
29 _author = insert(:user, ap_id: data["actor"], local: false)
30 _recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
31
32 {:error, _} = Transmogrifier.handle_incoming(data)
33 end
34
35 test "it rejects messages that don't concern local users" do
36 data =
37 File.read!("test/fixtures/create-chat-message.json")
38 |> Poison.decode!()
39
40 _author = insert(:user, ap_id: data["actor"], local: false)
41 _recipient = insert(:user, ap_id: List.first(data["to"]), local: false)
42
43 {:error, _} = Transmogrifier.handle_incoming(data)
44 end
45
46 test "it rejects messages where the `to` field of activity and object don't match" do
47 data =
48 File.read!("test/fixtures/create-chat-message.json")
49 |> Poison.decode!()
50
51 author = insert(:user, ap_id: data["actor"])
52 _recipient = insert(:user, ap_id: List.first(data["to"]))
53
54 data =
55 data
56 |> Map.put("to", author.ap_id)
57
58 assert match?({:error, _}, Transmogrifier.handle_incoming(data))
59 refute Object.get_by_ap_id(data["object"]["id"])
60 end
61
62 test "it inserts it and creates a chat" do
63 data =
64 File.read!("test/fixtures/create-chat-message.json")
65 |> Poison.decode!()
66
67 author = insert(:user, ap_id: data["actor"], local: false)
68 recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
69
70 {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
71 assert activity.local == false
72
73 assert activity.actor == author.ap_id
74 assert activity.recipients == [recipient.ap_id, author.ap_id]
75
76 %Object{} = object = Object.get_by_ap_id(activity.data["object"])
77
78 assert object
79 assert object.data["content"] == "You expected a cute girl? Too bad. alert(&#39;XSS&#39;)"
80 assert match?(%{"firefox" => _}, object.data["emoji"])
81
82 refute Chat.get(author.id, recipient.ap_id)
83 assert Chat.get(recipient.id, author.ap_id)
84 end
85 end
86 end