Top Level Namespace

Defined Under Namespace

Modules: Enum, Etna, StrongConfirmation, WithEtnaClients, WithLogger Classes: DirectedGraph, EnvironmentScoped, EtnaApp, String

Constant Summary collapse

WithEtnaClientsByEnvironment =
EnvironmentScoped.new do
  include WithEtnaClients
end

Instance Method Summary collapse

Instance Method Details

#clean_query(json_or_string) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/etna/spec/vcr.rb', line 8

def clean_query(json_or_string)
  if json_or_string.is_a?(Hash) && json_or_string.include?('upload_path')
    json_or_string['upload_path'] = clean_query(json_or_string['upload_path'])
    json_or_string
  elsif json_or_string.is_a?(String)
    uri = URI(json_or_string)

    if uri.query&.include?('X-Etna-Signature')
      uri.query = 'etna-signature'
    end

    uri.to_s
  else
    json_or_string
  end
end

#prepare_vcr_secretObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/etna/spec/vcr.rb', line 137

def prepare_vcr_secret
  p ENV
  secret = ENV["CI_SECRET"]

  if (secret.nil? || secret.empty?) && ENV['IS_CI'] != '1'
    current_example = RSpec.current_example
    RSpec::Core::Pending.mark_pending! current_example, 'CI_SECRET must be set to run this test'
    raise "CI_SECRET must be set to run this test"
  end

  digest = Digest::SHA256.new
  digest.update(secret)
  digest.digest
end

#setup_base_vcr(spec_helper_dir, server: nil, application: nil) ⇒ Object



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
# File 'lib/etna/spec/vcr.rb', line 25

def setup_base_vcr(spec_helper_dir, server: nil, application: nil)
  VCR.configure do |c|
    c.hook_into :webmock
    c.cassette_serializers
    c.cassette_library_dir = ::File.join(spec_helper_dir, 'fixtures', 'cassettes')
    c.allow_http_connections_when_no_cassette = true

    c.register_request_matcher :verify_uri_route do |request_1, request_2|
      next true if server.nil? || application.nil?

      route_match = request_1.uri =~ /https:\/\/#{application.dev_route}(.*)/
      if route_match && route_match[1]
        def request_1.request_method
          method.to_s.upcase
        end

        def request_1.path
          URI(uri).path
        end

        !!server.find_route(request_1)
      else
        true
      end
    end

    c.register_request_matcher :try_uri do |request_1, request_2|
      clean_query(request_1.uri) == clean_query(request_2.uri)
    end

    c.register_request_matcher :try_body do |request_1, request_2|
      if request_1.headers['Content-Type'].first =~ /application\/json/
        if request_2.headers['Content-Type'].first =~ /application\/json/
          request_1_json = begin
            JSON.parse(request_1.body) rescue 'not-json'
          end
          request_2_json = begin
            JSON.parse(request_2.body) rescue 'not-json'
          end

          clean_query(request_1_json) == clean_query(request_2_json)
        else
          false
        end
      else
        request_1.body == request_2.body
      end
    end

    if File.exists?('log')
      c.debug_logger = File.open('log/vcr_debug.log', 'w')
    end

    c.default_cassette_options = {
        serialize_with: :compressed,
        record: if ENV['IS_CI'] == '1'
          :none
        else
          ENV['RERECORD'] ? :all : :once
        end,
        match_requests_on: [:method, :try_uri, :try_body, :verify_uri_route]
    }

    # Filter the authorization headers of any request by replacing any occurrence of that request's
    # Authorization value with <AUTHORIZATION>
    c.filter_sensitive_data('<AUTHORIZATION>') do |interaction|
      interaction.request.headers['Authorization'].first
    end

    c.before_record do |interaction|
      key = prepare_vcr_secret

      if interaction.response.body && !interaction.response.body.empty?
        cipher = OpenSSL::Cipher.new("AES-256-CBC")
        iv = cipher.random_iv

        cipher.encrypt
        cipher.key = key
        cipher.iv = iv

        encrypted = cipher.update(interaction.response.body)
        encrypted << cipher.final

        interaction.response.body = [iv, encrypted].pack('mm')
      end
    end

    c.before_playback do |interaction|
      key = prepare_vcr_secret

      if interaction.response.body && !interaction.response.body.empty?
        iv, encrypted = interaction.response.body.unpack('mm')

        cipher = OpenSSL::Cipher.new("AES-256-CBC")
        cipher.decrypt
        cipher.key = key
        cipher.iv = iv

        plain = cipher.update(encrypted)
        plain << cipher.final

        interaction.response.body = plain
      end
    end
  end

  require 'multipartable'
  def Multipartable.secure_boundary
    "--THIS-IS-STABLE-FOR-TESTING"
  end
end