提交 4c44bd76 编写于 作者: J Justin

Merge pull request #700 from presidentbeef/remove_global_scans_in_tests

Remove remaining global scans in tests
require 'brakeman/differ' require 'brakeman/differ'
DiffRun = Brakeman.run :app_path => "#{TEST_PATH}/apps/rails2"
class DifferTests < Test::Unit::TestCase class DifferTests < Test::Unit::TestCase
include BrakemanTester::DiffHelper include BrakemanTester::DiffHelper
def setup def setup
@warnings = DiffRun.warnings @@diffrun ||= Brakeman.run :app_path => "#{TEST_PATH}/apps/rails2"
@warnings ||= @@diffrun.warnings
end end
def diff new, old def diff new, old
......
JSON_REPORT = MultiJson.load(Brakeman.run("#{TEST_PATH}/apps/rails3.2").report.to_json)
class JSONOutputTests < Test::Unit::TestCase class JSONOutputTests < Test::Unit::TestCase
def setup def setup
@json = JSON_REPORT @@json ||= MultiJson.load(Brakeman.run("#{TEST_PATH}/apps/rails3.2").report.to_json)
end end
def test_for_render_path def test_for_render_path
@json["warnings"].each do |warning| @@json["warnings"].each do |warning|
is_right_thing = warning.keys.include?("render_path") && (warning["render_path"].nil? or warning["render_path"].is_a? Array) is_right_thing = warning.keys.include?("render_path") && (warning["render_path"].nil? or warning["render_path"].is_a? Array)
assert is_right_thing, "#{warning["render_path"].class} is not right" assert is_right_thing, "#{warning["render_path"].class} is not right"
end end
end end
def test_for_expected_keys def test_for_expected_keys
assert (@json.keys - ["warnings", "ignored_warnings", "scan_info", "errors"]).empty? assert (@@json.keys - ["warnings", "ignored_warnings", "scan_info", "errors"]).empty?
end end
def test_for_scan_info_keys def test_for_scan_info_keys
...@@ -21,23 +20,23 @@ class JSONOutputTests < Test::Unit::TestCase ...@@ -21,23 +20,23 @@ class JSONOutputTests < Test::Unit::TestCase
"checks_performed", "number_of_controllers", "number_of_models", "number_of_templates", "checks_performed", "number_of_controllers", "number_of_models", "number_of_templates",
"ruby_version", "brakeman_version"] "ruby_version", "brakeman_version"]
assert (@json["scan_info"].keys - info_keys).empty? assert (@@json["scan_info"].keys - info_keys).empty?
end end
def test_for_expected_warning_keys def test_for_expected_warning_keys
expected = ["warning_type", "message", "file", "link", "code", "location", expected = ["warning_type", "message", "file", "link", "code", "location",
"render_path", "user_input", "confidence", "line", "warning_code", "fingerprint"] "render_path", "user_input", "confidence", "line", "warning_code", "fingerprint"]
@json["warnings"].each do |warning| @@json["warnings"].each do |warning|
assert (warning.keys - expected).empty?, "#{(warning.keys - expected).inspect} did not match expected keys" assert (warning.keys - expected).empty?, "#{(warning.keys - expected).inspect} did not match expected keys"
end end
end end
def test_for_errors def test_for_errors
assert @json["errors"].is_a? Array assert @@json["errors"].is_a? Array
end end
def test_paths def test_paths
assert @json["warnings"].all? { |w| not w["file"].start_with? "/" } assert @@json["warnings"].all? { |w| not w["file"].start_with? "/" }
end end
end end
class TestMarkdownOutput < Test::Unit::TestCase class TestMarkdownOutput < Test::Unit::TestCase
Report = Brakeman.run( def setup
@@report ||= Brakeman.run(
:app_path => "#{TEST_PATH}/apps/rails2", :app_path => "#{TEST_PATH}/apps/rails2",
:quiet => true, :quiet => true,
:run_all_checks => true :run_all_checks => true
).report.to_markdown ).report.to_markdown
end
def test_reported_warnings def test_reported_warnings
if Brakeman::Scanner::RUBY_1_9 if Brakeman::Scanner::RUBY_1_9
assert_equal 172, Report.lines.to_a.count assert_equal 172, @@report.lines.to_a.count
else else
assert_equal 173, Report.lines.to_a.count assert_equal 173, @@report.lines.to_a.count
end end
end end
end end
class TestReportGeneration < Test::Unit::TestCase class TestReportGeneration < Test::Unit::TestCase
Report = Brakeman.run(:app_path => "#{TEST_PATH}/apps/rails3.2", :quiet => true, :report_routes => true).report def setup
@@report ||= Brakeman.run(:app_path => "#{TEST_PATH}/apps/rails3.2", :quiet => true, :report_routes => true).report
end
def test_html_sanity def test_html_sanity
report = Report.to_html report = @@report.to_html
assert report.is_a? String assert report.is_a? String
assert report.match(/\A<!DOCTYPE HTML SYSTEM>.*<\/html>\z/m) assert report.match(/\A<!DOCTYPE HTML SYSTEM>.*<\/html>\z/m)
...@@ -12,7 +14,7 @@ class TestReportGeneration < Test::Unit::TestCase ...@@ -12,7 +14,7 @@ class TestReportGeneration < Test::Unit::TestCase
end end
def test_json_sanity def test_json_sanity
report = Report.to_json report = @@report.to_json
expected_keys = ["scan_info", "warnings", "errors"] expected_keys = ["scan_info", "warnings", "errors"]
assert report.is_a? String assert report.is_a? String
...@@ -23,7 +25,7 @@ class TestReportGeneration < Test::Unit::TestCase ...@@ -23,7 +25,7 @@ class TestReportGeneration < Test::Unit::TestCase
end end
def test_csv_sanity def test_csv_sanity
report = Report.to_csv report = @@report.to_csv
parsed = CSV.parse report parsed = CSV.parse report
summary_header = ["Application Path", "Report Generation Time", "Checks Performed", "Rails Version"] summary_header = ["Application Path", "Report Generation Time", "Checks Performed", "Rails Version"]
...@@ -39,35 +41,35 @@ class TestReportGeneration < Test::Unit::TestCase ...@@ -39,35 +41,35 @@ class TestReportGeneration < Test::Unit::TestCase
end end
def test_tabs_sanity def test_tabs_sanity
report = Report.to_tabs report = @@report.to_tabs
assert report.is_a? String assert report.is_a? String
end end
def test_text_sanity def test_text_sanity
report = Report.to_s report = @@report.to_s
assert report.is_a? String assert report.is_a? String
end end
def test_markdown_sanity def test_markdown_sanity
report = Report.to_markdown report = @@report.to_markdown
assert report.is_a? String assert report.is_a? String
end end
def test_bad_format_type def test_bad_format_type
assert_raises RuntimeError do assert_raises RuntimeError do
Report.format(:to_something_else) @@report.format(:to_something_else)
end end
end end
def test_controller_output def test_controller_output
text_report = Report.to_s text_report = @@report.to_s
assert text_report.include? "+CONTROLLERS+" assert text_report.include? "+CONTROLLERS+"
html_report = Report.to_html html_report = @@report.to_html
assert html_report.include? "<h2>Controllers</h2>" assert html_report.include? "<h2>Controllers</h2>"
end end
......
class TestTabsOutput < Test::Unit::TestCase class TestTabsOutput < Test::Unit::TestCase
Report = Brakeman.run( def setup
@@report ||= Brakeman.run(
:app_path => "#{TEST_PATH}/apps/rails2", :app_path => "#{TEST_PATH}/apps/rails2",
:quiet => true, :quiet => true,
:run_all_checks => true :run_all_checks => true
).report.to_tabs ).report.to_tabs
end
def test_reported_warnings def test_reported_warnings
if Brakeman::Scanner::RUBY_1_9 if Brakeman::Scanner::RUBY_1_9
assert_equal 110, Report.lines.to_a.count assert_equal 110, @@report.lines.to_a.count
else else
assert_equal 111, Report.lines.to_a.count assert_equal 111, @@report.lines.to_a.count
end end
end end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册