Add tests, changelog entry
[akkoma] / test / pleroma / web / activity_pub / mrf / normalize_markup_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.NormalizeMarkupTest do
6 use Pleroma.DataCase, async: true
7 alias Pleroma.Web.ActivityPub.MRF
8 alias Pleroma.Web.ActivityPub.MRF.NormalizeMarkup
9
10 @html_sample """
11 <b>this is in bold</b>
12 <p>this is a paragraph</p>
13 this is a linebreak<br />
14 this is a link with allowed "rel" attribute: <a href="http://example.com/" rel="tag">example.com</a>
15 this is a link with not allowed "rel" attribute: <a href="http://example.com/" rel="tag noallowed">example.com</a>
16 this is an image: <img src="http://example.com/image.jpg"><br />
17 <script>alert('hacked')</script>
18 <div class="wow no classes here">mean</div>
19 """
20
21 @expected """
22 <b>this is in bold</b>
23 <p>this is a paragraph</p>
24 this is a linebreak<br/>
25 this is a link with allowed &quot;rel&quot; attribute: <a href="http://example.com/" rel="tag">example.com</a>
26 this is a link with not allowed &quot;rel&quot; attribute: <a href="http://example.com/">example.com</a>
27 this is an image: <img src="http://example.com/image.jpg"/><br/>
28 alert(&#39;hacked&#39;)
29 mean
30 """
31
32 test "it filter html tags" do
33 message = %{"type" => "Create", "object" => %{"content" => @html_sample}}
34
35 assert {:ok, res} = NormalizeMarkup.filter(message)
36 assert res["object"]["content"] == @expected
37 end
38
39 test "history-aware" do
40 message = %{
41 "type" => "Create",
42 "object" => %{
43 "content" => @html_sample,
44 "formerRepresentations" => %{"orderedItems" => [%{"content" => @html_sample}]}
45 }
46 }
47
48 assert {:ok, res} = MRF.filter_one(NormalizeMarkup, message)
49
50 assert %{
51 "content" => @expected,
52 "formerRepresentations" => %{"orderedItems" => [%{"content" => @expected}]}
53 } = res["object"]
54 end
55
56 test "works with Updates" do
57 message = %{
58 "type" => "Update",
59 "object" => %{
60 "content" => @html_sample,
61 "formerRepresentations" => %{"orderedItems" => [%{"content" => @html_sample}]}
62 }
63 }
64
65 assert {:ok, res} = MRF.filter_one(NormalizeMarkup, message)
66
67 assert %{
68 "content" => @expected,
69 "formerRepresentations" => %{"orderedItems" => [%{"content" => @expected}]}
70 } = res["object"]
71 end
72
73 test "it skips filter if type isn't `Create` or `Update`" do
74 message = %{"type" => "Note", "object" => %{}}
75
76 assert {:ok, res} = NormalizeMarkup.filter(message)
77 assert res == message
78 end
79 end