Merge branch 'stable' of git.pleroma.social:pleroma/pleroma into pleroma-2.1-rc0
[akkoma] / test / web / activity_pub / transmogrifier / question_handling_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.QuestionHandlingTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Activity
9 alias Pleroma.Object
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11 alias Pleroma.Web.CommonAPI
12
13 import Pleroma.Factory
14
15 setup_all do
16 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 :ok
18 end
19
20 test "Mastodon Question activity" do
21 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
22
23 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
24
25 object = Object.normalize(activity, false)
26
27 assert object.data["url"] == "https://mastodon.sdf.org/@rinpatch/102070944809637304"
28
29 assert object.data["closed"] == "2019-05-11T09:03:36Z"
30
31 assert object.data["context"] == activity.data["context"]
32
33 assert object.data["context"] ==
34 "tag:mastodon.sdf.org,2019-05-10:objectId=15095122:objectType=Conversation"
35
36 assert object.data["context_id"]
37
38 assert object.data["anyOf"] == []
39
40 assert Enum.sort(object.data["oneOf"]) ==
41 Enum.sort([
42 %{
43 "name" => "25 char limit is dumb",
44 "replies" => %{"totalItems" => 0, "type" => "Collection"},
45 "type" => "Note"
46 },
47 %{
48 "name" => "Dunno",
49 "replies" => %{"totalItems" => 0, "type" => "Collection"},
50 "type" => "Note"
51 },
52 %{
53 "name" => "Everyone knows that!",
54 "replies" => %{"totalItems" => 1, "type" => "Collection"},
55 "type" => "Note"
56 },
57 %{
58 "name" => "I can't even fit a funny",
59 "replies" => %{"totalItems" => 1, "type" => "Collection"},
60 "type" => "Note"
61 }
62 ])
63
64 user = insert(:user)
65
66 {:ok, reply_activity} = CommonAPI.post(user, %{status: "hewwo", in_reply_to_id: activity.id})
67
68 reply_object = Object.normalize(reply_activity, false)
69
70 assert reply_object.data["context"] == object.data["context"]
71 assert reply_object.data["context_id"] == object.data["context_id"]
72 end
73
74 test "Mastodon Question activity with HTML tags in plaintext" do
75 options = [
76 %{
77 "type" => "Note",
78 "name" => "<input type=\"date\">",
79 "replies" => %{"totalItems" => 0, "type" => "Collection"}
80 },
81 %{
82 "type" => "Note",
83 "name" => "<input type=\"date\"/>",
84 "replies" => %{"totalItems" => 0, "type" => "Collection"}
85 },
86 %{
87 "type" => "Note",
88 "name" => "<input type=\"date\" />",
89 "replies" => %{"totalItems" => 1, "type" => "Collection"}
90 },
91 %{
92 "type" => "Note",
93 "name" => "<input type=\"date\"></input>",
94 "replies" => %{"totalItems" => 1, "type" => "Collection"}
95 }
96 ]
97
98 data =
99 File.read!("test/fixtures/mastodon-question-activity.json")
100 |> Poison.decode!()
101 |> Kernel.put_in(["object", "oneOf"], options)
102
103 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
104 object = Object.normalize(activity, false)
105
106 assert Enum.sort(object.data["oneOf"]) == Enum.sort(options)
107 end
108
109 test "returns an error if received a second time" do
110 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
111
112 assert {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
113
114 assert {:error, {:validate_object, {:error, _}}} = Transmogrifier.handle_incoming(data)
115 end
116
117 test "accepts a Question with no content" do
118 data =
119 File.read!("test/fixtures/mastodon-question-activity.json")
120 |> Poison.decode!()
121 |> Kernel.put_in(["object", "content"], "")
122
123 assert {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
124 end
125 end