Project

General

Profile

Download (2.03 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / test / vcr_setup.rb @ 400832c6

1
require 'rubygems'
2
require 'vcr'
3
require 'hammer_cli'
4

    
5
def configure_vcr(mode = :none)
6
  if ENV['record'] == 'false' && mode == :none
7
    fail "Record flag is not applicable for mode 'none', please use with 'mode=all'"
8
  end
9

    
10
  VCR.configure do |c|
11
    c.cassette_library_dir = 'test/fixtures/vcr_cassettes'
12
    c.hook_into :webmock
13

    
14
    if ENV['record'] == 'false' && mode != :none
15
      server = HammerCLI::Settings.get(:csv, :host) ||
16
          HammerCLI::Settings.get(:katello, :host) ||
17
          HammerCLI::Settings.get(:foreman, :host)
18
      uri = URI.parse(server)
19
      c.ignore_hosts uri.host
20
    end
21

    
22
    if ENV['debug']
23
      c.debug_logger = File.open(ENV['debug'], 'w')
24
    end
25

    
26
    c.default_cassette_options = {
27
      :record => mode,
28
      :match_requests_on => [:method, :path, :params, :body_json],
29
      :decode_compressed_response => true
30
    }
31

    
32
    # rubocop:disable HandleExceptions
33
    begin
34
      c.register_request_matcher :body_json do |request_1, request_2|
35
        begin
36
          json_1 = JSON.parse(request_1.body)
37
          json_2 = JSON.parse(request_2.body)
38

    
39
          json_1 == json_2
40
        rescue
41
          #fallback incase there is a JSON parse error
42
          request_1.body == request_2.body
43
        end
44
      end
45
    rescue
46
      #ignore the warning thrown about this matcher already being resgistered
47
    end
48

    
49
    begin
50
      c.register_request_matcher :params do |request_1, request_2|
51
        URI(request_1.uri).query == URI(request_2.uri).query
52
      end
53
    rescue
54
      #ignore the warning thrown about this matcher already being resgistered
55
    end
56

    
57
    if ENV['record'] == 'true' && mode != :none
58
      username = ENV['PORTALUSERNAME'] || 'username'
59
      password = ENV['PORTALPASSWORD'] || 'password'
60
      c.filter_sensitive_data('username:password') { "#{username}:#{password}" }
61
      c.filter_sensitive_data('subscription/users/username') { "subscription/users/#{username}" }
62
      c.filter_sensitive_data('"username":"username"') { "\"username\":\"#{username}\"" }
63
    end
64
    # rubocop:enable HandleExceptions
65
  end
66
end