]> git.openstreetmap.org Git - rails.git/blob - test/lib/rich_text_test.rb
Really remove login.live.com from CSP allow list
[rails.git] / test / lib / rich_text_test.rb
1 require "test_helper"
2
3 class RichTextTest < ActiveSupport::TestCase
4   include Rails::Dom::Testing::Assertions::SelectorAssertions
5
6   def test_html_to_html
7     r = RichText.new("html", "foo http://example.com/ bar")
8     assert_html r do
9       assert_select "a", 1
10       assert_select "a[href='http://example.com/']", 1
11       assert_select "a[rel='nofollow noopener noreferrer']", 1
12     end
13
14     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
15     assert_html r do
16       assert_select "a", 1
17       assert_select "a[href='http://example.com/']", 1
18       assert_select "a[rel='nofollow noopener noreferrer']", 1
19     end
20
21     r = RichText.new("html", "foo <a rel='junk me trash' href='http://example.com/'>bar</a> baz")
22     assert_html r do
23       assert_select "a", 1
24       assert_select "a[href='http://example.com/']", 1
25       assert_select "a[rel='me nofollow noopener noreferrer']", 1
26     end
27
28     r = RichText.new("html", "foo example@example.com bar")
29     assert_html r do
30       assert_select "a", 0
31     end
32
33     r = RichText.new("html", "foo <a href='mailto:example@example.com'>bar</a> baz")
34     assert_html r do
35       assert_select "a", 1
36       assert_select "a[href='mailto:example@example.com']", 1
37       assert_select "a[rel='nofollow noopener noreferrer']", 1
38     end
39
40     r = RichText.new("html", "foo <div>bar</div> baz")
41     assert_html r do
42       assert_select "div", false
43       assert_select "p", /^foo *bar *baz$/
44     end
45
46     r = RichText.new("html", "foo <script>bar = 1;</script> baz")
47     assert_html r do
48       assert_select "script", false
49       assert_select "p", /^foo *baz$/
50     end
51
52     r = RichText.new("html", "foo <style>div { display: none; }</style> baz")
53     assert_html r do
54       assert_select "style", false
55       assert_select "p", /^foo *baz$/
56     end
57
58     r = RichText.new("html", "<table><tr><td>column</td></tr></table>")
59     assert_html r do
60       assert_select "table[class='table table-sm w-auto']"
61     end
62
63     r = RichText.new("html", "<p class='btn btn-warning'>Click Me</p>")
64     assert_html r do
65       assert_select "p[class='btn btn-warning']", false
66       assert_select "p", /^Click Me$/
67     end
68
69     r = RichText.new("html", "<p style='color:red'>Danger</p>")
70     assert_html r do
71       assert_select "p[style='color:red']", false
72       assert_select "p", /^Danger$/
73     end
74   end
75
76   def test_html_to_text
77     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
78     assert_equal "foo <a href='http://example.com/'>bar</a> baz", r.to_text
79   end
80
81   def test_html_spam_score
82     r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
83     assert_equal 55, r.spam_score.round
84   end
85
86   def test_markdown_to_html
87     r = RichText.new("markdown", "foo http://example.com/ bar")
88     assert_html r do
89       assert_select "a", 1
90       assert_select "a[href='http://example.com/']", 1
91       assert_select "a[rel='nofollow noopener noreferrer']", 1
92     end
93
94     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
95     assert_html r do
96       assert_select "a", 1
97       assert_select "a[href='http://example.com/']", 1
98       assert_select "a[rel='nofollow noopener noreferrer']", 1
99     end
100
101     r = RichText.new("markdown", "foo <a rel='junk me trash' href='http://example.com/'>bar</a>) baz")
102     assert_html r do
103       assert_select "a", 1
104       assert_select "a[href='http://example.com/']", 1
105       assert_select "a[rel='me nofollow noopener noreferrer']", 1
106     end
107
108     r = RichText.new("markdown", "foo example@example.com bar")
109     assert_html r do
110       assert_select "a", 1
111       assert_select "a[href='mailto:example@example.com']", 1
112       assert_select "a[rel='nofollow noopener noreferrer']", 1
113     end
114
115     r = RichText.new("markdown", "foo [bar](mailto:example@example.com) bar")
116     assert_html r do
117       assert_select "a", 1
118       assert_select "a[href='mailto:example@example.com']", 1
119       assert_select "a[rel='nofollow noopener noreferrer']", 1
120     end
121
122     r = RichText.new("markdown", "foo ![bar](http://example.com/example.png) bar")
123     assert_html r do
124       assert_select "img", 1
125       assert_select "img[alt='bar']", 1
126       assert_select "img[src='http://example.com/example.png']", 1
127     end
128
129     r = RichText.new("markdown", "# foo bar baz")
130     assert_html r do
131       assert_select "h1", "foo bar baz"
132     end
133
134     r = RichText.new("markdown", "## foo bar baz")
135     assert_html r do
136       assert_select "h2", "foo bar baz"
137     end
138
139     r = RichText.new("markdown", "### foo bar baz")
140     assert_html r do
141       assert_select "h3", "foo bar baz"
142     end
143
144     r = RichText.new("markdown", "* foo bar baz")
145     assert_html r do
146       assert_select "ul" do
147         assert_select "li", "foo bar baz"
148       end
149     end
150
151     r = RichText.new("markdown", "1. foo bar baz")
152     assert_html r do
153       assert_select "ol" do
154         assert_select "li", "foo bar baz"
155       end
156     end
157
158     r = RichText.new("markdown", "foo *bar* _baz_ qux")
159     assert_html r do
160       assert_select "em", "bar"
161       assert_select "em", "baz"
162     end
163
164     r = RichText.new("markdown", "foo **bar** __baz__ qux")
165     assert_html r do
166       assert_select "strong", "bar"
167       assert_select "strong", "baz"
168     end
169
170     r = RichText.new("markdown", "foo `bar` baz")
171     assert_html r do
172       assert_select "code", "bar"
173     end
174
175     r = RichText.new("markdown", "    foo bar baz")
176     assert_html r do
177       assert_select "pre", /^\s*foo bar baz\s*$/
178     end
179
180     r = RichText.new("markdown", "|column|column")
181     assert_html r do
182       assert_select "table[class='table table-sm w-auto']"
183     end
184
185     r = RichText.new("markdown", "Click Me\n{:.btn.btn-warning}")
186     assert_html r do
187       assert_select "p[class='btn btn-warning']", false
188       assert_select "p", /^Click Me$/
189     end
190
191     r = RichText.new("markdown", "<p style='color:red'>Danger</p>")
192     assert_html r do
193       assert_select "p[style='color:red']", false
194       assert_select "p", /^Danger$/
195     end
196   end
197
198   def test_markdown_table_alignment
199     # Ensure that kramdown table alignment styles are converted to bootstrap classes
200     markdown_table = <<~MARKDOWN
201       | foo  | bar |
202       |:----:|----:|
203       |center|right|
204     MARKDOWN
205     r = RichText.new("markdown", markdown_table)
206     assert_html r do
207       assert_select "td[style='text-align:center']", false
208       assert_select "td[class='text-center']", true
209       assert_select "td[style='text-align:right']", false
210       assert_select "td[class='text-end']", true
211     end
212   end
213
214   def test_markdown_to_text
215     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
216     assert_equal "foo [bar](http://example.com/) baz", r.to_text
217   end
218
219   def test_markdown_spam_score
220     r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
221     assert_equal 50, r.spam_score.round
222   end
223
224   def test_text_to_html
225     r = RichText.new("text", "foo http://example.com/ bar")
226     assert_html r do
227       assert_select "a", 1
228       assert_select "a[href='http://example.com/']", 1
229       assert_select "a[rel='nofollow noopener noreferrer']", 1
230     end
231
232     r = RichText.new("text", "foo example@example.com bar")
233     assert_html r do
234       assert_select "a", 0
235     end
236
237     r = RichText.new("text", "foo < bar & baz > qux")
238     assert_html r do
239       assert_select "p", "foo < bar & baz > qux"
240     end
241   end
242
243   def test_text_to_text
244     r = RichText.new("text", "foo http://example.com/ bar")
245     assert_equal "foo http://example.com/ bar", r.to_text
246   end
247
248   def test_text_spam_score
249     r = RichText.new("text", "foo http://example.com/ bar")
250     assert_equal 141, r.spam_score.round
251   end
252
253   private
254
255   def assert_html(richtext, &block)
256     html = richtext.to_html
257     assert_predicate html, :html_safe?
258     root = Nokogiri::HTML::DocumentFragment.parse(html)
259     assert_select root, "*" do
260       yield block
261     end
262   end
263 end