aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/template
diff options
context:
space:
mode:
authorMikel Lindsaar <raasdnil@gmail.com>2010-06-07 18:30:18 -0400
committerMikel Lindsaar <raasdnil@gmail.com>2010-06-07 18:30:18 -0400
commita16fd961310d4df5bd20df5fe64b2f764d8aa471 (patch)
tree0e54acc2ae35e985cd8c8175f051e281acbd4595 /actionpack/test/template
parent735027e58d3000bd5d939e39deabfecef83ee20c (diff)
parentd6953cbfd3b6e06eceba715c60e288b6d7db0d49 (diff)
downloadrails-a16fd961310d4df5bd20df5fe64b2f764d8aa471.tar.gz
rails-a16fd961310d4df5bd20df5fe64b2f764d8aa471.tar.bz2
rails-a16fd961310d4df5bd20df5fe64b2f764d8aa471.zip
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'actionpack/test/template')
-rw-r--r--actionpack/test/template/lookup_context_test.rb16
-rw-r--r--actionpack/test/template/tag_helper_test.rb2
-rw-r--r--actionpack/test/template/text_helper_test.rb102
3 files changed, 100 insertions, 20 deletions
diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb
index df1aa2edb2..cc71cb42d0 100644
--- a/actionpack/test/template/lookup_context_test.rb
+++ b/actionpack/test/template/lookup_context_test.rb
@@ -26,18 +26,6 @@ class LookupContextTest < ActiveSupport::TestCase
assert_equal :en, @lookup_context.locale
end
- test "allows me to update details" do
- @lookup_context.update_details(:formats => [:html], :locale => :pt)
- assert_equal [:html], @lookup_context.formats
- assert_equal :pt, @lookup_context.locale
- end
-
- test "allows me to update an specific detail" do
- @lookup_context.update_details(:locale => :pt)
- assert_equal :pt, I18n.locale
- assert_equal :pt, @lookup_context.locale
- end
-
test "allows me to freeze and retrieve frozen formats" do
@lookup_context.formats.freeze
assert @lookup_context.formats.frozen?
@@ -54,7 +42,7 @@ class LookupContextTest < ActiveSupport::TestCase
end
test "provides getters and setters for formats" do
- @lookup_context.formats = :html
+ @lookup_context.formats = [:html]
assert_equal [:html], @lookup_context.formats
end
@@ -138,7 +126,7 @@ class LookupContextTest < ActiveSupport::TestCase
keys << @lookup_context.details_key
assert_equal 2, keys.uniq.size
- @lookup_context.formats = :html
+ @lookup_context.formats = [:html]
keys << @lookup_context.details_key
assert_equal 3, keys.uniq.size
diff --git a/actionpack/test/template/tag_helper_test.rb b/actionpack/test/template/tag_helper_test.rb
index 256d9bdcde..ec5fe3d1d7 100644
--- a/actionpack/test/template/tag_helper_test.rb
+++ b/actionpack/test/template/tag_helper_test.rb
@@ -39,6 +39,8 @@ class TagHelperTest < ActionView::TestCase
content_tag("a", "Create", :href => "create")
assert_equal "<p>&lt;script&gt;evil_js&lt;/script&gt;</p>",
content_tag(:p, '<script>evil_js</script>')
+ assert_equal "<p><script>evil_js</script></p>",
+ content_tag(:p, '<script>evil_js</script>', nil, false)
end
def test_content_tag_with_block_in_erb
diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb
index bb808b77a5..9d7106b2e5 100644
--- a/actionpack/test/template/text_helper_test.rb
+++ b/actionpack/test/template/text_helper_test.rb
@@ -45,19 +45,42 @@ class TextHelperTest < ActionView::TestCase
assert simple_format("<b> test with html tags </b>").html_safe?
end
- def test_simple_format_should_escape_unsafe_input
- assert_equal "<p>&lt;b&gt; test with unsafe string &lt;/b&gt;</p>", simple_format("<b> test with unsafe string </b>")
+ def test_simple_format_should_sanitize_unsafe_input
+ assert_equal "<p><b> test with unsafe string </b></p>", simple_format("<b> test with unsafe string </b><script>code!</script>")
end
- def test_simple_format_should_not_escape_safe_input
+ def test_simple_format_should_not_sanitize_input_if_safe_option
+ assert_equal "<p><b> test with unsafe string </b><script>code!</script></p>", simple_format("<b> test with unsafe string </b><script>code!</script>", {}, :safe => true)
+ end
+
+ def test_simple_format_should_not_sanitize_safe_input
assert_equal "<p><b> test with safe string </b></p>", simple_format("<b> test with safe string </b>".html_safe)
end
+ def test_truncate_should_be_html_safe
+ assert truncate("Hello World!", :length => 12).html_safe?
+ end
+
def test_truncate
assert_equal "Hello World!", truncate("Hello World!", :length => 12)
assert_equal "Hello Wor...", truncate("Hello World!!", :length => 12)
end
+ def test_truncate_should_sanitize_unsafe_input
+ assert_equal "Hello World!", truncate("Hello <script>code!</script>World!", :length => 12)
+ assert_equal "Hello Wor...", truncate("Hello <script>code!</script>World!!", :length => 12)
+ end
+
+ def test_truncate_should_not_sanitize_input_if_safe_option
+ assert_equal "Hello <sc...", truncate("Hello <script>code!</script>World!", :length => 12, :safe => true)
+ assert_equal "Hello <sc...", truncate("Hello <script>code!</script>World!!", :length => 12, :safe => true)
+ end
+
+ def test_truncate_should_not_sanitize_safe_input
+ assert_equal "Hello <sc...", truncate("Hello <script>code!</script>World!".html_safe, :length => 12)
+ assert_equal "Hello <sc...", truncate("Hello <script>code!</script>World!!".html_safe, :length => 12)
+ end
+
def test_truncate_should_use_default_length_of_30
str = "This is a string that will go longer then the default truncate length of 30"
assert_equal str[0...27] + "...", truncate(str)
@@ -93,7 +116,11 @@ class TextHelperTest < ActionView::TestCase
end
end
- def test_highlighter
+ def test_highlight_should_be_html_safe
+ assert highlight("This is a beautiful morning", "beautiful").html_safe?
+ end
+
+ def test_highlight
assert_equal(
"This is a <strong class=\"highlight\">beautiful</strong> morning",
highlight("This is a beautiful morning", "beautiful")
@@ -117,6 +144,27 @@ class TextHelperTest < ActionView::TestCase
assert_equal ' ', highlight(' ', 'blank text is returned verbatim')
end
+ def test_highlight_should_sanitize_unsafe_input
+ assert_equal(
+ "This is a <strong class=\"highlight\">beautiful</strong> morning",
+ highlight("This is a beautiful morning<script>code!</script>", "beautiful")
+ )
+ end
+
+ def test_highlight_should_not_sanitize_input_if_safe_option
+ assert_equal(
+ "This is a <strong class=\"highlight\">beautiful</strong> morning<script>code!</script>",
+ highlight("This is a beautiful morning<script>code!</script>", "beautiful", :safe => true)
+ )
+ end
+
+ def test_highlight_should_not_sanitize_safe_input
+ assert_equal(
+ "This is a <strong class=\"highlight\">beautiful</strong> morning<script>code!</script>",
+ highlight("This is a beautiful morning<script>code!</script>".html_safe, "beautiful")
+ )
+ end
+
def test_highlight_with_regexp
assert_equal(
"This is a <strong class=\"highlight\">beautiful!</strong> morning",
@@ -163,7 +211,7 @@ class TextHelperTest < ActionView::TestCase
highlight("<p class=\"beautiful\">This is a beautiful morning, but also a beautiful day</p>", "beautiful")
)
assert_equal(
- "<p>This is a <strong class=\"highlight\">beautiful</strong> <a href=\"http://example.com/beautiful\#top?what=beautiful%20morning&when=now+then\">morning</a>, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
+ "<p>This is a <strong class=\"highlight\">beautiful</strong> <a href=\"http://example.com/beautiful\#top?what=beautiful%20morning&amp;when=now+then\">morning</a>, but also a <strong class=\"highlight\">beautiful</strong> day</p>",
highlight("<p>This is a beautiful <a href=\"http://example.com/beautiful\#top?what=beautiful%20morning&when=now+then\">morning</a>, but also a beautiful day</p>", "beautiful")
)
end
@@ -286,7 +334,17 @@ class TextHelperTest < ActionView::TestCase
%{<a href="#{CGI::escapeHTML href}">#{CGI::escapeHTML link_text}</a>}
end
- def test_auto_linking
+ def test_auto_link_should_be_html_safe
+ email_raw = 'santiago@wyeworks.com'
+ link_raw = 'http://www.rubyonrails.org'
+
+ assert auto_link(nil).html_safe?
+ assert auto_link('').html_safe?
+ assert auto_link("#{link_raw} #{link_raw} #{link_raw}").html_safe?
+ assert auto_link("hello #{email_raw}").html_safe?
+ end
+
+ def test_auto_link
email_raw = 'david@loudthinking.com'
email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
link_raw = 'http://www.rubyonrails.com'
@@ -378,6 +436,21 @@ class TextHelperTest < ActionView::TestCase
assert_equal %(<p>#{link10_result} Link</p>), auto_link("<p>#{link10_raw} Link</p>")
end
+ def test_auto_link_should_sanitize_unsafe_input
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
+ assert_equal %{<a href="http://www.rubyonrails.com?id=1&amp;num=2">http://www.rubyonrails.com?id=1&amp;num=2</a>}, auto_link(link_raw)
+ end
+
+ def test_auto_link_should_sanitize_unsafe_input
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
+ assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>}, auto_link(link_raw, :safe => true)
+ end
+
+ def test_auto_link_should_not_sanitize_safe_input
+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
+ assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>}, auto_link(link_raw.html_safe)
+ end
+
def test_auto_link_other_protocols
ftp_raw = 'ftp://example.com/file.txt'
assert_equal %(Download #{generate_result(ftp_raw)}), auto_link("Download #{ftp_raw}")
@@ -587,7 +660,12 @@ class TextHelperTest < ActionView::TestCase
assert_equal(%w{Specialized Fuji Giant}, @cycles)
end
+ # TODO test textilize_without_paragraph and markdown
if defined? RedCloth
+ def test_textilize_should_be_html_safe
+ assert textilize("*This is Textile!* Rejoice!").html_safe?
+ end
+
def test_textilize
assert_equal("<p><strong>This is Textile!</strong> Rejoice!</p>", textilize("*This is Textile!* Rejoice!"))
end
@@ -600,6 +678,18 @@ class TextHelperTest < ActionView::TestCase
assert_equal("<p>This is worded &lt;strong&gt;strongly&lt;/strong&gt;</p>", textilize("This is worded <strong>strongly</strong>", :filter_html))
end
+ def test_textilize_should_sanitize_unsafe_input
+ assert_equal("<p>This is worded <strong>strongly</strong></p>", textilize("This is worded <strong>strongly</strong><script>code!</script>"))
+ end
+
+ def test_textilize_should_not_sanitize_input_if_safe_option
+ assert_equal("<p>This is worded <strong>strongly</strong><script>code!</script></p>", textilize("This is worded <strong>strongly</strong><script>code!</script>", :safe))
+ end
+
+ def test_textilize_should_not_sanitize_safe_input
+ assert_equal("<p>This is worded <strong>strongly</strong><script>code!</script></p>", textilize("This is worded <strong>strongly</strong><script>code!</script>".html_safe))
+ end
+
def test_textilize_with_hard_breaks
assert_equal("<p>This is one scary world.<br />\n True.</p>", textilize("This is one scary world.\n True."))
end