diff options
author | Timm <kaspth@gmail.com> | 2013-09-14 14:18:19 +0200 |
---|---|---|
committer | Timm <kaspth@gmail.com> | 2014-06-16 21:04:15 +0200 |
commit | 15382e9793c53c858cc880f4d2b9d3a77059cdfe (patch) | |
tree | 00d47a2deb41b4c26ea63439e78689d761de1da2 | |
parent | b4cfb59f42c8e5b9eeda19ba7565b2e359219a34 (diff) | |
download | rails-15382e9793c53c858cc880f4d2b9d3a77059cdfe.tar.gz rails-15382e9793c53c858cc880f4d2b9d3a77059cdfe.tar.bz2 rails-15382e9793c53c858cc880f4d2b9d3a77059cdfe.zip |
Rounded out PermitScrubber tests. Extracted helper methods to a ScrubberTest class.
-rw-r--r-- | actionview/test/template/scrubbers/scrubbers_test.rb | 88 |
1 files changed, 80 insertions, 8 deletions
diff --git a/actionview/test/template/scrubbers/scrubbers_test.rb b/actionview/test/template/scrubbers/scrubbers_test.rb index a4ef36b1fd..61d2c48373 100644 --- a/actionview/test/template/scrubbers/scrubbers_test.rb +++ b/actionview/test/template/scrubbers/scrubbers_test.rb @@ -1,7 +1,30 @@ require 'loofah' require 'abstract_unit' -class PermitScrubberTest < ActionView::TestCase +class ScrubberTest < ActionView::TestCase + protected + + def assert_scrubbed(html, expected = html) + output = Loofah.scrub_fragment(html, @scrubber).to_s + assert_equal expected, output + end + + def assert_node_skipped(text) + node = to_node(text) + assert_equal Loofah::Scrubber::CONTINUE, @scrubber.scrub(node) + end + + def to_node(text) + Loofah.fragment(text).children.first + end + + def scrub_expectations(text, &expectations) + @scrubber.instance_eval(&expectations) + @scrubber.scrub to_node(text) + end +end + +class PermitScrubberTest < ScrubberTest def setup @scrubber = PermitScrubber.new @@ -85,14 +108,63 @@ class PermitScrubberTest < ActionView::TestCase assert_nil @scrubber.attributes, "Attributes should be nil when validation fails" end - protected - def assert_scrubbed(html, expected = html) - output = Loofah.scrub_fragment(html, @scrubber).to_s - assert_equal expected, output + def test_scrub_uses_public_api + @scrubber.tags = %w(tag) + @scrubber.attributes = %w(cooler) + + scrub_expectations '<p id="hello">some text</p>' do + expects(skip_node?: false) + expects(allowed_node?: false) + + expects(:scrub_node) + + expects(scrub_attribute?: false) end + end - def assert_node_skipped(text) - node = Loofah.fragment(text).children.first - assert_equal Loofah::Scrubber::CONTINUE, @scrubber.scrub(node) + def test_keep_node_returns_false_node_will_be_stripped + scrub_expectations '<p>normally p tags are kept<p>' do + stubs(keep_node?: false) + expects(:scrub_node) + end + end + + def test_skip_node_returns_false_node_will_be_stripped + scrub_expectations 'normally text nodes are skipped' do + stubs(skip_node?: false) + expects(keep_node?: true) + end + end + + def test_stripping_of_normally_skipped_and_kept_node + scrub_expectations 'text is skipped by default' do + stubs(skip_node?: false, keep_node?: false) + expects(:scrub_node) + expects(:scrub_attributes) # expected since scrub_node doesn't return STOP + end + end + + def test_attributes_are_scrubbed_for_kept_node + scrub_expectations 'text is kept, but normally skipped' do + stubs(skip_node?: false) + expects(:scrub_attributes) + end + end + + def test_scrubbing_of_empty_node + scrubbing = scrub_expectations '' do + expects(skip_node?: true) end + + assert_equal Loofah::Scrubber::CONTINUE, scrubbing + end + + def test_scrub_returns_stop_if_scrub_node_does + scrubbing = scrub_expectations '<script>free me</script>' do + stubs(scrub_node: Loofah::Scrubber::STOP) + expects(:scrub_attributes).never + end + + assert_equal Loofah::Scrubber::STOP, scrubbing + end end
\ No newline at end of file |