require File.join(File.dirname(__FILE__), 'setup_test') class XssTerminateTest < Test::Unit::TestCase XssTerminate.sanitize_by_default = true def test_sanitize_by_default_is_true assert XssTerminate.sanitize_by_default end def test_strip_tags_on_discovered_fields c = Comment.create!(:title => "", :body => "") assert_equal "alert('xss in title')", c.title assert_equal "alert('xss in body')", c.body end def test_rails_sanitization_on_specified_fields e = Entry.create!(:title => "", :body => "", :extended => "", :person_id => 1) assert_equal [:body, :extended], e.xss_terminate_options[:only] assert_equal "alert('xss in title')", e.title assert_equal "", e.body assert_equal "", e.extended end def test_excepting_specified_fields p = Person.create!(:name => "Mallory") assert_equal [:name], p.xss_terminate_options[:except] assert_equal "Mallory", p.name end def test_html5lib_sanitization_on_specified_fields r = Review.create!(:title => "", :body => "", :extended => "", :person_id => 1) assert_equal [:body, :extended], r.xss_terminate_options[:html5lib_sanitize] assert_equal "alert('xss in title')", r.title assert_equal "<script>alert('xss in body')</script>", r.body assert_equal "<script>alert('xss in extended')</script>", r.extended end ### Tests for new features (the API rewriten) def test_onlying_specified_fields p = Message.create!(:body => "Mallory") assert_equal [:body], p.xss_terminate_options[:only] assert_equal "Mallory", p.body end end