Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[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 {:error, _} = Transmogrifier.handle_incoming(data)
59 end
60
61 test "it inserts it and creates a chat" do
62 data =
63 File.read!("test/fixtures/create-chat-message.json")
64 |> Poison.decode!()
65
66 author = insert(:user, ap_id: data["actor"], local: false)
67 recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
68
69 {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
70
71 assert activity.actor == author.ap_id
72 assert activity.recipients == [recipient.ap_id, author.ap_id]
73
74 %Object{} = object = Object.get_by_ap_id(activity.data["object"])
75
76 assert object
77 assert object.data["content"] == "You expected a cute girl? Too bad. alert(&#39;XSS&#39;)"
78
79 refute Chat.get(author.id, recipient.ap_id)
80 assert Chat.get(recipient.id, author.ap_id)
81 end
82 end
83 end