summaryrefslogtreecommitdiff
path: root/test/pleroma/web/activity_pub/transmogrifier/chat_message_test.exs
blob: 958675835ed61039d4e9b05db0ecb5478606e80c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 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
  alias Pleroma.Chat
  alias Pleroma.Object
  alias Pleroma.Web.ActivityPub.Transmogrifier

  describe "handle_incoming" do
    test "handles chonks with attachment" do
      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

    test "it rejects messages that don't contain content" do
      data =
        File.read!("test/fixtures/create-chat-message.json")
        |> Jason.decode!()

      object =
        data["object"]
        |> Map.delete("content")

      data =
        data
        |> Map.put("object", object)

      _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()
        )

      {: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")
        |> Jason.decode!()

      _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()
        )

      {:error, _} = Transmogrifier.handle_incoming(data)
    end

    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")
        |> Jason.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)

      assert match?({:error, _}, Transmogrifier.handle_incoming(data))
      refute Object.get_by_ap_id(data["object"]["id"])
    end

    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")
        |> Jason.decode!()
        |> 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

    test "it doesn't work for deactivated users" do
      data =
        File.read!("test/fixtures/create-chat-message.json")
        |> Jason.decode!()

      _author =
        insert(:user,
          ap_id: data["actor"],
          local: false,
          last_refreshed_at: DateTime.utc_now(),
          is_active: false
        )

      _recipient = insert(:user, ap_id: List.first(data["to"]), local: true)

      assert {:error, _} = Transmogrifier.handle_incoming(data)
    end

    test "it inserts it and creates a chat" do
      data =
        File.read!("test/fixtures/create-chat-message.json")
        |> Jason.decode!()

      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)

      {:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
      assert activity.local == false

      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"])

      assert object
      assert object.data["content"] == "You expected a cute girl? Too bad. alert(&#39;XSS&#39;)"
      assert match?(%{"firefox" => _}, object.data["emoji"])

      refute Chat.get(author.id, recipient.ap_id)
      assert Chat.get(recipient.id, author.ap_id)
    end
  end
end