<commit_before>#
# For testing REST API via Rspec requests
#

require 'bcrypt'
require 'json'

module ApiSpecHelper
  HEADER_ALIASES = {
    "auth_token" => "HTTP_X_AUTH_TOKEN",
    "miq_group"  => "HTTP_X_MIQ_GROUP"
  }

  DEF_HEADERS = {
    "Content-Type" => "application/json",
    "Accept"       => "application/json"
  }

  def response_hash
    JSON.parse(response.body)
  end

  def update_headers(headers)
    HEADER_ALIASES.keys.each do |k|
      if headers.key?(k)
        headers[HEADER_ALIASES[k]] = headers[k]
        headers.delete(k)
      end
    end
    headers.merge!("HTTP_AUTHORIZATION" => @http_authorization) if @http_authorization
    headers.merge(DEF_HEADERS)
  end

  def run_get(url, options = {})
    headers = options.delete(:headers) || {}
    get url, :params => options.stringify_keys, :headers => update_headers(headers)
  end

  def run_post(url, body = {}, headers = {})
    post url, :headers => update_headers(headers).merge('RAW_POST_DATA' => body.to_json)
  end

  def run_put(url, body = {}, headers = {})
    put url, :headers => update_headers(headers).merge('RAW_POST_DATA' => body.to_json)
  end

  def run_patch(url, body = {}, headers = {})
    patch url, :headers => update_headers(headers).merge('RAW_POST_DATA' => body.to_json)
  end

  def run_delete(url, headers = {})
    delete url, :headers => update_headers(headers)
  end

  def resources_include_suffix?(resources, key, suffix)
    resources.any? { |r| r.key?(key) && r[key].match("#{suffix}$") }
  end

  def resources_include?(resources, key, value)
    resources.any? { |r| r[key] == value }
  end

  def api_config(param)
    @api_config = {
      :user       => "api_user_id",
      :password   => "<PASSWORD>",
      :user_name  => "API User",
      :group_name => "API User Group",
      :role_name  => "API User Role",
      :entrypoint => "/api"
    }
    @api_config[param]
  end

  def define_user
    @role  = FactoryGirl.create(:miq_user_role, :name => api_config(:role_name))
    @group = FactoryGirl.create(:miq_group, :description => api_config(:group_name), :miq_user_role => @role)
    @user  = FactoryGirl.create(:user,
                                :name             => api_config(:user_name),
                                :userid           => api_config(:user),
                                :password         => api_config(:password),
                                :miq_groups       => [@group],
                                :current_group_id => @group.id)
  end

  def init_api_spec_env
    MiqDatabase.seed
    Vmdb::Application.config.secret_token = MiqDatabase.first.session_secret_token
    @guid, @server, @zone = EvmSpecHelper.create_guid_miq_server_zone

    define_entrypoint_url_methods
    define_url_methods(Api::Settings.collections.keys)
    define_user

    ApplicationController.handle_exceptions = true
  end

  def define_entrypoint_url_methods
    self.class.class_eval do
      define_method(:entrypoint_url) do
        api_config(:entrypoint)
      end
      define_method(:auth_url) do
        "#{api_config(:entrypoint)}/auth"
      end
    end
  end

  def define_url_methods(collections)
    collections.each do |collection|
      self.class.class_eval do
        define_method("#{collection}_url".to_sym) do |id = nil|
          path = "#{api_config(:entrypoint)}/#{collection}"
          id.nil? ? path : "#{path}/#{id}"
        end
      end
    end
  end

  def api_basic_authorize(*identifiers)<commit_msg>Define the dynamic url spec helper methods once<commit_after>#
# For testing REST API via Rspec requests
#

require 'bcrypt'
require 'json'

module ApiSpecHelper
  HEADER_ALIASES = {
    "auth_token" => "<PASSWORD>",
    "miq_group"  => "HTTP_X_MIQ_GROUP"
  }

  DEF_HEADERS = {
    "Content-Type" => "application/json",
    "Accept"       => "application/json"
  }

  def response_hash
    JSON.parse(response.body)
  end

  def update_headers(headers)
    HEADER_ALIASES.keys.each do |k|
      if headers.key?(k)
        headers[HEADER_ALIASES[k]] = headers[k]
        headers.delete(k)
      end
    end
    headers.merge!("HTTP_AUTHORIZATION" => @http_authorization) if @http_authorization
    headers.merge(DEF_HEADERS)
  end

  def run_get(url, options = {})
    headers = options.delete(:headers) || {}
    get url, :params => options.stringify_keys, :headers => update_headers(headers)
  end

  def run_post(url, body = {}, headers = {})
    post url, :headers => update_headers(headers).merge('RAW_POST_DATA' => body.to_json)
  end

  def run_put(url, body = {}, headers = {})
    put url, :headers => update_headers(headers).merge('RAW_POST_DATA' => body.to_json)
  end

  def run_patch(url, body = {}, headers = {})
    patch url, :headers => update_headers(headers).merge('RAW_POST_DATA' => body.to_json)
  end

  def run_delete(url, headers = {})
    delete url, :headers => update_headers(headers)
  end

  def resources_include_suffix?(resources, key, suffix)
    resources.any? { |r| r.key?(key) && r[key].match("#{suffix}$") }
  end

  def resources_include?(resources, key, value)
    resources.any? { |r| r[key] == value }
  end

  def api_config(param)
    @api_config = {
      :user       => "api_user_id",
      :password   => "<PASSWORD>",
      :user_name  => "API User",
      :group_name => "API User Group",
      :role_name  => "API User Role",
      :entrypoint => "/api"
    }
    @api_config[param]
  end

  def define_user
    @role  = FactoryGirl.create(:miq_user_role, :name => api_config(:role_name))
    @group = FactoryGirl.create(:miq_group, :description => api_config(:group_name), :miq_user_role => @role)
    @user  = FactoryGirl.create(:user,
                                :name             => api_config(:user_name),
                                :userid           => api_config(:user),
                                :password         => api_config(:password),
                                :miq_groups       => [@group],
                                :current_group_id => @group.id)
  end

  def init_api_spec_env
    MiqDatabase.seed
    Vmdb::Application.config.secret_token = <PASSWORD>.session_secret_token
    @guid, @server, @zone = EvmSpecHelper.create_guid_miq_server_zone

    define_user

    ApplicationController.handle_exceptions = true
  end

  def entrypoint_url
    api_config(:entrypoint)
  end

  def auth_url
    "#{api_config(:entrypoint)}/auth"
  end

  (Api::Settings.collections.keys - [:auth]).each do |collection|
    define_method("#{collection}_url".to_sym) do |id = nil|
      path = "#{api_config(:entrypoint)}/#{collection}"
      id.nil? ? path : "#{path}/#{id}"
    end
  end

  def api_basic_authorize(*identifiers)
{'fraction_non_alphanumeric': 0.1318664870528364, 'fraction_numerical': 0.0, 'mean_word_length': 2.803073420603301, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3722517', 'n_tokens_mistral': 2215, 'n_tokens_neox': 2169, 'n_words': 417}