Sha256: a90613971528726c61d9f373b457e21196451e3128e61a128808028ab53c46ac

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

RSpec.configure do |c|
  c.after(:each) { auto_teardown }
end

def auth_token
  @auth_token ||= Pacto::ValidationRegistry.instance.validations.map do | val |
    token = val.request.headers['X-Auth-Token']
  end.compact.reject(&:empty?).first
end

def auto_teardown
  # HACK: This should be simplified and moved to Pacto

  created_resources = auto_find_prg
  auto_delete created_resources, auth_token
end

REDIRECTS = [201, 202, (300...400).to_a.flatten]

def auto_find_prg
  # Find URLs that were the "Get" part of a Post-Redirect-Get pattern
  created_uris = Pacto::ValidationRegistry.instance.validations.map {|validation|
    validation.response.headers['Location'] if validation.request.method == :post and REDIRECTS.include? validation.response.status
  }.compact

  created_uris.map do | created_uri |
    Addressable::URI.parse created_uri
  end
end

def auto_delete uris, auth_token
  uris.group_by(&:site).each do | site, uris |
    connection = Excon.new(site)
    uris.each do | uri |
      puts "Removing #{uri}"
      connection.delete(:path => uri.path,
        :debug_request => true,
        :debug_response => true,
        :expects => [204],
        :headers => {
          "User-Agent" => "fog/1.18.0",
          "Content-Type" => "application/json",
          "Accept" => "application/json",
          "X-Auth-Token" => auth_token
        }
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polytrix-0.0.1 features/helpers/teardown_helper.rb