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



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/coverband.rb', line 133

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



50
51
52
53
# File 'lib/coverband/reporters/web.rb', line 50

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

Instance Method Details

#call(env) ⇒ Object



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

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)/
        @static.call(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



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

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



183
184
185
186
187
188
189
190
191
# File 'lib/coverband/reporters/web.rb', line 183

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



204
205
206
207
208
209
210
211
212
# File 'lib/coverband/reporters/web.rb', line 204

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



214
215
216
217
218
219
220
221
222
223
# File 'lib/coverband/reporters/web.rb', line 214

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



193
194
195
196
197
198
199
200
201
202
# File 'lib/coverband/reporters/web.rb', line 193

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



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

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

#display_abstract_tracker(tracker) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/coverband/reporters/web.rb', line 148

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



167
168
169
170
171
172
173
# File 'lib/coverband/reporters/web.rb', line 167

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



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

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

def init_web
  full_path = Gem::Specification.find_by_name("coverband").full_gem_path
  @static = Rack::Static.new(self,
    root: File.expand_path("public", full_path),
    urls: [/.*\.css/, /.*\.js/, /.*\.gif/, /.*\.png/])
end

#jsonObject



127
128
129
# File 'lib/coverband/reporters/web.rb', line 127

def json
  Coverband::Reporters::JSONReport.new(Coverband.configuration.store).report
end

#load_file_detailsObject



175
176
177
178
179
180
181
# File 'lib/coverband/reporters/web.rb', line 175

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



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/coverband/reporters/web.rb', line 131

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



143
144
145
146
# File 'lib/coverband/reporters/web.rb', line 143

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

#view_tracker_dataObject



159
160
161
# File 'lib/coverband/reporters/web.rb', line 159

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