Merge branch 'develop' into fix/ldap-auth-issues
[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["closed"] == "2019-05-11T09:03:36Z"
28
29 assert object.data["context"] == activity.data["context"]
30
31 assert object.data["context"] ==
32 "tag:mastodon.sdf.org,2019-05-10:objectId=15095122:objectType=Conversation"
33
34 assert object.data["context_id"]
35
36 assert object.data["anyOf"] == []
37
38 assert Enum.sort(object.data["oneOf"]) ==
39 Enum.sort([
40 %{
41 "name" => "25 char limit is dumb",
42 "replies" => %{"totalItems" => 0, "type" => "Collection"},
43 "type" => "Note"
44 },
45 %{
46 "name" => "Dunno",
47 "replies" => %{"totalItems" => 0, "type" => "Collection"},
48 "type" => "Note"
49 },
50 %{
51 "name" => "Everyone knows that!",
52 "replies" => %{"totalItems" => 1, "type" => "Collection"},
53 "type" => "Note"
54 },
55 %{
56 "name" => "I can't even fit a funny",
57 "replies" => %{"totalItems" => 1, "type" => "Collection"},
58 "type" => "Note"
59 }
60 ])
61
62 user = insert(:user)
63
64 {:ok, reply_activity} = CommonAPI.post(user, %{status: "hewwo", in_reply_to_id: activity.id})
65
66 reply_object = Object.normalize(reply_activity, false)
67
68 assert reply_object.data["context"] == object.data["context"]
69 assert reply_object.data["context_id"] == object.data["context_id"]
70 end
71
72 test "Mastodon Question activity with HTML tags in plaintext" do
73 options = [
74 %{
75 "type" => "Note",
76 "name" => "<input type=\"date\">",
77 "replies" => %{"totalItems" => 0, "type" => "Collection"}
78 },
79 %{
80 "type" => "Note",
81 "name" => "<input type=\"date\"/>",
82 "replies" => %{"totalItems" => 0, "type" => "Collection"}
83 },
84 %{
85 "type" => "Note",
86 "name" => "<input type=\"date\" />",
87 "replies" => %{"totalItems" => 1, "type" => "Collection"}
88 },
89 %{
90 "type" => "Note",
91 "name" => "<input type=\"date\"></input>",
92 "replies" => %{"totalItems" => 1, "type" => "Collection"}
93 }
94 ]
95
96 data =
97 File.read!("test/fixtures/mastodon-question-activity.json")
98 |> Poison.decode!()
99 |> Kernel.put_in(["object", "oneOf"], options)
100
101 {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
102 object = Object.normalize(activity, false)
103
104 assert Enum.sort(object.data["oneOf"]) == Enum.sort(options)
105 end
106
107 test "returns an error if received a second time" do
108 data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
109
110 assert {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
111
112 assert {:error, {:validate_object, {:error, _}}} = Transmogrifier.handle_incoming(data)
113 end
114
115 test "accepts a Question with no content" do
116 data =
117 File.read!("test/fixtures/mastodon-question-activity.json")
118 |> Poison.decode!()
119 |> Kernel.put_in(["object", "content"], "")
120
121 assert {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
122 end
123 end