Class: Coverband::Reporters::Web

Inherits:
Object
  • Object
show all
Defined in:
lib/coverband/reporters/web.rb,
lib/coverband.rb

Constant Summary collapse

CSP_HEADER =
[
  "default-src 'self' https: http:",
  "child-src 'self'",
  "connect-src 'self' https: http: wss: ws:",
  "font-src 'self' https: http:",
  "frame-src 'self'",
  "img-src 'self' https: http: data:",
  "manifest-src 'self'",
  "media-src 'self'",
  "object-src 'none'",
  "script-src 'self' https: http: 'unsafe-inline'",
  "style-src 'self' https: http: 'unsafe-inline'",
  "worker-src 'self'",
  "base-uri 'self'"
].join("; ").freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWeb

NOTE: if the user doesn’t setup the webreporter we don’t need any of the below files loaded or using memory



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/coverband.rb', line 159

def initialize
  require "coverband/reporters/web"
  require "coverband/utils/html_formatter"
  require "coverband/utils/result"
  require "coverband/utils/file_list"
  require "coverband/utils/source_file"
  require "coverband/utils/lines_classifier"
  require "coverband/utils/results"
  require "coverband/reporters/html_report"
  require "coverband/reporters/json_report"
  init_web
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



15
16
17
# File 'lib/coverband/reporters/web.rb', line 15

def request
  @request
end

Class Method Details

.call(env) ⇒ Object



52
53
54
55
# File 'lib/coverband/reporters/web.rb', line 52

def self.call(env)
  @app ||= new
  @app.call(env)
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/coverband/reporters/web.rb', line 57

def call(env)
  @request = Rack::Request.new(env)

  return [401, {"www-authenticate" => 'Basic realm=""'}, [""]] unless check_auth

  request_path_info = (request.path_info == "") ? "/" : request.path_info
  tracker_route = false
  Coverband.configuration.trackers.each do |tracker|
    if request_path_info.match(tracker.class::REPORT_ROUTE)
      tracker_route = true
      if request_path_info =~ %r{/clear_.*_key}
        return clear_abstract_tracking_key(tracker)
      elsif request_path_info =~ %r{/clear_.*}
        return clear_abstract_tracking(tracker)
      else
        return [200, {"content-type" => "text/html"}, [display_abstract_tracker(tracker)]]
      end
    end
  end

  unless tracker_route
    if request.post?
      case request_path_info
      when %r{/clear_file}
        clear_file
      when %r{/clear}
        clear
      else
        [404, coverband_headers, ["404 error!"]]
      end
    else
      case request_path_info
      when /.*\.(css|js|gif|png)/
        @file_server.get(env)
      when %r{/settings}
        [200, coverband_headers, [settings]]
      when %r{/view_tracker_data}
        [200, coverband_headers(content_type: "text/json"), [view_tracker_data]]
      when %r{/enriched_debug_data}
        [200, coverband_headers(content_type: "text/json"), [enriched_debug_data]]
      when %r{/debug_data}
        [200, coverband_headers(content_type: "text/json"), [debug_data]]
      when %r{/load_file_details}
        [200, coverband_headers(content_type: "text/json"), [load_file_details]]
      when %r{/json}
        [200, coverband_headers(content_type: "text/json"), [json]]
      when %r{/report_json}
        [200, coverband_headers(content_type: "text/json"), [report_json]]
      when %r{/$}
        [200, coverband_headers, [index]]
      else
        [404, coverband_headers, ["404 error!"]]
      end
    end
  end
end

#check_authObject



42
43
44
45
46
47
48
49
50
# File 'lib/coverband/reporters/web.rb', line 42

def check_auth
  return true unless Coverband.configuration.password

  # support rack 1.6.x and rack 2.0 (get_header)
  auth_header = request.respond_to?(:get_header) ? request.get_header("HTTP_AUTHORIZATION") : request.env["HTTP_AUTHORIZATION"]
  return unless auth_header

  Coverband.configuration.password == Base64.decode64(auth_header.split[1]).split(":")[1]
end

#clearObject



188
189
190
191
192
193
194
195
196
# File 'lib/coverband/reporters/web.rb', line 188

def clear
  if Coverband.configuration.web_enable_clear
    Coverband.configuration.store.clear!
    notice = "coverband coverage cleared"
  else
    notice = "web_enable_clear isn't enabled in your configuration"
  end
  [302, {"Location" => "#{base_path}?notice=#{notice}"}, []]
end

#clear_abstract_tracking(tracker) ⇒ Object



209
210
211
212
213
214
215
216
217
# File 'lib/coverband/reporters/web.rb', line 209

def clear_abstract_tracking(tracker)
  if Coverband.configuration.web_enable_clear
    tracker.reset_recordings
    notice = "#{tracker.title} tracking reset"
  else
    notice = "web_enable_clear isn't enabled in your configuration"
  end
  [302, {"Location" => "#{base_path}/#{tracker.route}?notice=#{notice}"}, []]
end

#clear_abstract_tracking_key(tracker) ⇒ Object



219
220
221
222
223
224
225
226
227
228
# File 'lib/coverband/reporters/web.rb', line 219

def clear_abstract_tracking_key(tracker)
  if Coverband.configuration.web_enable_clear
    key = request.params["key"]
    tracker.clear_key!(key)
    notice = "coverage for #{tracker.title} #{key} cleared"
  else
    notice = "web_enable_clear isn't enabled in your configuration"
  end
  [302, {"Location" => "#{base_path}/#{tracker.route}?notice=#{notice}"}, []]
end

#clear_fileObject



198
199
200
201
202
203
204
205
206
207
# File 'lib/coverband/reporters/web.rb', line 198

def clear_file
  if Coverband.configuration.web_enable_clear
    filename = request.params["filename"]
    Coverband.configuration.store.clear_file!(filename)
    notice = "coverage for file #{filename} cleared"
  else
    notice = "web_enable_clear isn't enabled in your configuration"
  end
  [302, {"Location" => "#{base_path}?notice=#{notice}"}, []]
end

#debug_dataObject



168
169
170
# File 'lib/coverband/reporters/web.rb', line 168

def debug_data
  Coverband.configuration.store.get_coverage_report.to_json
end

#display_abstract_tracker(tracker) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/coverband/reporters/web.rb', line 153

def display_abstract_tracker(tracker)
  notice = "<strong>Notice:</strong> #{Rack::Utils.escape_html(request.params["notice"])}<br/>"
  notice = request.params["notice"] ? notice : ""
  options = {
    tracker: tracker,
    notice: notice,
    base_path: base_path
  }
  Coverband::Utils::HTMLFormatter.new(nil, options).format_abstract_tracker!
end

#enriched_debug_dataObject



172
173
174
175
176
177
178
# File 'lib/coverband/reporters/web.rb', line 172

def enriched_debug_data
  Coverband::Reporters::HTMLReport.new(Coverband.configuration.store,
    static: false,
    base_path: base_path,
    notice: "",
    open_report: false).report_data
end

#indexObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/coverband/reporters/web.rb', line 114

def index
  notice = "<strong>Notice:</strong> #{Rack::Utils.escape_html(request.params["notice"])}<br/>"
  notice = request.params["notice"] ? notice : ""
  page = (request.params["page"] || 1).to_i
  options = {
    static: false,
    base_path: base_path,
    notice: notice,
    open_report: false
  }
  options[:page] = page if Coverband.configuration.paged_reporting
  Coverband::Reporters::HTMLReport.new(Coverband.configuration.store,
    options).report
end

#init_webObject



33
34
35
36
37
38
39
40
# File 'lib/coverband/reporters/web.rb', line 33

def init_web
  full_path = Gem::Specification.find_by_name("coverband").full_gem_path
  # Rack::Files was introduced in Rack 2.0; Rack 1.x uses Rack::File
  rack_file_server = defined?(Rack::Files) ? Rack::Files : Rack::File
  @file_server = rack_file_server.new(
    File.expand_path("public", full_path)
  )
end

#jsonObject



129
130
131
132
133
134
# File 'lib/coverband/reporters/web.rb', line 129

def json
  Coverband::Reporters::JSONReport.new(
    Coverband.configuration.store,
    line_coverage: request.params["line_coverage"] == "true"
  ).report
end

#load_file_detailsObject



180
181
182
183
184
185
186
# File 'lib/coverband/reporters/web.rb', line 180

def load_file_details
  filename = request.params["filename"]
  Coverband::Reporters::HTMLReport.new(Coverband.configuration.store,
    filename: filename,
    base_path: base_path,
    open_report: false).file_details
end

#report_jsonObject



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/coverband/reporters/web.rb', line 136

def report_json
  report_options = {
    as_report: true,
    base_path: base_path
  }
  report_options[:page] = (request.params["page"] || 1).to_i if request.params["page"]
  Coverband::Reporters::JSONReport.new(
    Coverband.configuration.store,
    report_options
  ).report
end

#settingsObject



148
149
150
151
# File 'lib/coverband/reporters/web.rb', line 148

def settings
  return "" if Coverband.configuration.hide_settings
  Coverband::Utils::HTMLFormatter.new(nil, base_path: base_path).format_settings!
end

#view_tracker_dataObject



164
165
166
# File 'lib/coverband/reporters/web.rb', line 164

def view_tracker_data
  Coverband::Collectors::ViewTracker.new.as_json
end