Class: Hiptest::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hiptest-publisher/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_options, reporter = nil) ⇒ Client

Returns a new instance of Client.



16
17
18
19
# File 'lib/hiptest-publisher/client.rb', line 16

def initialize(cli_options, reporter = nil)
  @cli_options = cli_options
  @reporter = reporter || NullReporter.new
end

Instance Attribute Details

#cli_optionsObject (readonly)

Returns the value of attribute cli_options.



14
15
16
# File 'lib/hiptest-publisher/client.rb', line 14

def cli_options
  @cli_options
end

Instance Method Details

#available_test_runsObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hiptest-publisher/client.rb', line 81

def available_test_runs
  @available_test_runs ||= begin
    response = send_get_request("#{base_publication_path}/test_runs")
    if response.code_type == Net::HTTPNotFound
      :api_not_available
    else
      json_response = JSON.parse(response.body)
      json_response["test_runs"]
    end
  end
end

#fetch_project_exportObject



73
74
75
76
77
78
79
# File 'lib/hiptest-publisher/client.rb', line 73

def fetch_project_export
  response = send_get_request(url)
  if response.code_type == Net::HTTPNotFound
    raise ClientError, "No project found with this secret token."
  end
  response.body
end

#global_failure_urlObject



31
32
33
# File 'lib/hiptest-publisher/client.rb', line 31

def global_failure_url
  "#{cli_options.site}/report_global_failure/#{cli_options.token}/#{cli_options.test_run_id}/"
end

#project_export_filtersObject



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
# File 'lib/hiptest-publisher/client.rb', line 35

def project_export_filters
  mapping = {
    filter_on_scenario_ids: 'filter_scenario_ids',
    filter_on_folder_ids: 'filter_folder_ids',
    filter_on_scenario_name: 'filter_scenario_name',
    filter_on_folder_name: 'filter_folder_name',
    filter_on_tags: 'filter_tags'
  }

  options = []

  mapping.each do |key, filter_name|
    value = @cli_options[key]
    next if value.nil? || value.empty?

    if [:filter_on_scenario_ids, :filter_on_folder_ids, :filter_on_tags].include?(key)
      value = value.split(',').map(&:strip).map{ |s| ERB::Util.url_encode(s) }.join(',')
    else
      value = ERB::Util.url_encode(value)
    end

    options << "#{filter_name}=#{value}"
    if [:filter_on_folder_ids, :filter_on_folder_name].include?(key) && @cli_options[:not_recursive]
      options << "not_recursive=true"
    end
  end


  return options.empty? ? '' : "?#{options.join('&')}"
end

#push_resultsObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hiptest-publisher/client.rb', line 93

def push_results
  # Code from: https://github.com/nicksieger/multipart-post
  uploaded = {}
  Dir.glob(cli_options.push.gsub('\\', '/')).each_with_index do |filename, index|
    uploaded["file-#{filename.normalize}"] = UploadIO.new(File.new(filename), "text", filename)
  end

  if cli_options.global_failure_on_missing_reports && uploaded.empty?
    return send_post_request(global_failure_url)
  end

  uri = URI.parse(url)
  send_request(Net::HTTP::Post::Multipart.new(uri, uploaded))
end

#test_run_export_filterObject



66
67
68
69
70
71
# File 'lib/hiptest-publisher/client.rb', line 66

def test_run_export_filter
  value = @cli_options.filter_on_status
  return '' if value.nil? || value.empty?

  return "?filter_status=#{value}"
end

#urlObject



21
22
23
24
25
26
27
28
29
# File 'lib/hiptest-publisher/client.rb', line 21

def url
  if cli_options.push?
    "#{cli_options.site}/import_test_results/#{cli_options.token}/#{cli_options.push_format}"
  elsif test_run_id
    "#{base_publication_path}/test_run/#{test_run_id}#{test_run_export_filter}"
  else
    "#{base_publication_path}/#{cli_options.leafless_export ? 'leafless_tests' : 'project'}#{project_export_filters}"
  end
end