-
Notifications
You must be signed in to change notification settings - Fork 734
/
report_generation.rb
197 lines (152 loc) · 5.34 KB
/
report_generation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require 'json'
require_relative '../test'
class TestReportGeneration < Minitest::Test
def setup
@@tracker ||= Brakeman.run(:app_path => "#{TEST_PATH}/apps/rails4", :quiet => true, :report_routes => true, :output_color => false)
@@report ||= @@tracker.report
end
def test_html_sanity
report = @@report.to_html
assert report.is_a? String
assert report.match(/\A<!DOCTYPE HTML SYSTEM>.*<\/html>\z/m)
report.scan(/<a[^>]+>/).each do |a|
assert a.include?("noreferrer"), "#{a} does not include 'noreferrer'"
end
end
def test_table_sanity
require 'highline/io_console_compatible' # For StringIO compatibility
@@report.to_table
end
def test_json_sanity
report = @@report.to_json
expected_keys = ["scan_info", "warnings", "errors"]
assert report.is_a? String
report_hash = JSON.parse report
assert (expected_keys - report_hash.keys).empty?, "Expected #{expected_keys - report_hash.keys} to be empty"
end
def test_codeclimate_sanity
report = @@report.to_codeclimate
assert report.is_a? String
refute report.include? Dir.pwd # Ensure output does not include absolute paths
end
def test_csv_sanity
headers = ["Confidence", "Warning Type", "CWE", "File", "Line", "Message", "Code", "User Input", "Check Name", "Warning Code", "Fingerprint", "Link"]
report = @@report.to_csv
parsed = CSV.parse report, headers: true
row = parsed.first
assert_equal row.headers, headers
assert_equal parsed.length, @@report.tracker.filtered_warnings.length
end
def test_csv_report_no_warnings
assert_nothing_raised do
Brakeman.run(:app_path => "#{TEST_PATH}/apps/rails4_non_standard_structure", :quiet => true, :report_routes => true).report.to_csv
end
end
def test_obsolete_reporting
report = @@report.to_s
assert report.include? "Obsolete Ignore Entries"
assert report.include? "abcdef01234567890ba28050e7faf1d54f218dfa9435c3f65f47cb378c18cf98"
end
def test_tabs_sanity
report = @@report.to_tabs
assert report.is_a? String
end
def test_text_sanity
report = @@report.to_s
assert report.is_a? String
end
def test_text_debug_sanity
@@tracker.options[:debug] = true
report = @@report.to_s
assert report.is_a? String
ensure
@@tracker.options[:debug] = false
end
def test_text_format_all
require 'brakeman/options'
options, _ = Brakeman::Options.parse(["--text-fields", "all"])
tracker = Brakeman.run(:app_path => "#{TEST_PATH}/apps/rails5", :quiet => true, :report_routes => true, :text_fields => options[:text_fields], :output_color => false)
report = tracker.report.to_s
assert_includes report, "Confidence:"
assert_includes report, "Category:"
assert_includes report, "Category ID:"
assert_includes report, "Check:"
assert_includes report, "Code:"
assert_includes report, "CWE:"
assert_includes report, "File:"
assert_includes report, "Fingerprint:"
assert_includes report, "Line:"
assert_includes report, "Link:"
assert_includes report, "Message:"
assert_includes report, "Render Path:"
end
def test_text_format
@@tracker.options[:text_fields] =
[:confidence, :category, :category_id, :code, :fingerprint, :line]
report = @@report.to_s
assert_includes report, "Confidence:"
assert_includes report, "Category:"
assert_includes report, "Category ID:"
assert_includes report, "Code:"
assert_includes report, "Fingerprint:"
assert_includes report, "Line:"
refute_includes report, "Check:"
refute_includes report, "File:"
refute_includes report, "Link:"
refute_includes report, "Message:"
refute_includes report, "Render Path:"
ensure
@@tracker.options.delete(:text_fields)
end
def test_markdown_sanity
report = @@report.to_markdown
assert report.is_a? String
assert report.include? "High"
assert report.include? "Medium"
assert report.include? "Weak"
end
def test_markdown_debug_sanity
@@tracker.options[:debug] = true
report = @@report.to_markdown
assert report.is_a?(String), "Report wasn't a String, it was a #{report.class}"
ensure
@@tracker.options[:debug] = false
end
def test_github_sanity
report = @@report.to_github
assert report.is_a? String
assert report.include? "::warning"
end
def test_bad_format_type
assert_raises RuntimeError do
@@report.format(:to_something_else)
end
end
def test_controller_output
text_report = @@report.to_s
assert text_report.include? "Controller Overview"
html_report = @@report.to_html
assert html_report.include? "<h2>Controllers</h2>"
end
def test_plain_debug_sanity
@@tracker.options[:debug] = true
report = @@report.to_plain
assert report.is_a? String
assert report.match(/Overview.*Warning Types.*Controller Overview.*Template Output.*Warnings/m)
ensure
@@tracker.options[:debug] = false
end
def test_github_markdown_sanity
@@tracker.options[:github_url] = "https://github.com/presidentbeef/brakeman/blob/master/"
report = @@report.to_markdown
assert report.is_a? String
assert report.include? @@tracker.options[:github_url]
ensure
@@tracker.options[:github_url] = nil
end
def test_junit_sanity
report = @@report.to_junit
assert report.is_a? String
assert report.match(/\A.*<\/testsuites>\z/m)
end
end