<commit_before>#
# REST API Request Tests - Policy Actions
#
# Policy Action primary collection:
#   /api/policy_actions
#
# Policy Action subcollection:
#   /api/policies/:id/policy_actions
#
require 'spec_helper'

describe ApiController do

  include Rack::Test::Methods

  before(:each) do
    init_api_spec_env
  end

  def app
    Vmdb::Application
  end

  def create_actions(count)
    1.upto(count) do |i|
      FactoryGirl.create(:miq_action, :name => "custom_action_#{i}", :description => "Custom Action #{i}")
    end
  end

  context "Policy Action collection" do
    it "query invalid action" do
      basic_authorize @cfme[:user], @cfme[:password]

      @success = run_get "#{@cfme[:policy_actions_url]}/999999"

      expect(@code).to eq(404)
    end

    it "query policy actions with no actions defined" do
      basic_authorize @cfme[:user], @cfme[:password]

      @success = run_get @cfme[:policy_actions_url]

      expect(@code).to eq(200)
      expect(@result).to have_key("name")
      expect(@result["name"]).to eq("policy_actions")
      expect(@result["resources"].size).to eq(0)
    end

    it "query policy actions" do
      basic_authorize @cfme[:user], @cfme[:password]

      create_actions(10)
      @success = run_get @cfme[:policy_actions_url]

      expect(@code).to eq(200)
      expect(@result).to have_key("name")
      expect(@result["name"]).to eq("policy_actions")
      expect(@result["resources"].size).to eq(10)
      hrefs_ids = @result["resources"].collect { |r| r["href"].sub(/^.*#{@cfme[:policy_actions_url]}\//, '') }
      expect(hrefs_ids).to match_array(MiqAction.pluck(:id).collect(&:to_s))
    end

    it "query policy actions in expanded form" do
      basic_authorize @cfme[:user], @cfme[:password]

      create_actions(10)
      @success = run_get "#{@cfme[:policy_actions_url]}?expand=resources"

      expect(@code).to eq(200)
      expect(@result).to have_key("name")
      expect(@result["name"]).to eq("policy_actions")
      expect(@result["resources"].size).to eq(10)
      guids = @result["resources"].collect { |r| r["guid"] }
      expect(guids).to match_array(MiqAction.pluck(:guid))
    end
  end

  context "Policy Action subcollection" do
    before(:each) do
      @policy = FactoryGirl.create(:miq_policy, :name => "Policy 1")
      @policy_url = "#{@cfme[:policies_url]}/#{@policy.id}"
      @policy_actions_url = "#{@policy_url}/policy_actions"
    end

    it "query policy actions with no actions defined" do
      basic_authorize @cfme[:user], @cfme[:password]

      @success = run_get @policy_actions_url

      expect(@code).to eq(200)
      expect(@result).to have_key("name")
      expect(@result["name"]).to eq("policy_actions")
      expect(@result["resources"].size).to eq(0)
    end

    it "query policy actions" do
      basic_authorize @cfme[:user], @cfme[:password]

      create_actions(4)
      MiqAction.all.collect(&:id).each do |action_id|
        MiqPolicyContent.create(:miq_policy_id => @policy.id, :miq_action_id => action_id)
      end

      @success = run_get "#{@policy_actions_url}?expand=resources"

      expect(@code).to eq(200)
      expect(@result).to have_key("name")
      expect(@result["name"]).to eq("policy_actions")
      expect(@result["resources"].size).to eq(4)
      guids = @result["resources"].collect { |r| r["guid"] }
      expect(guids).to match_array(MiqAction.pluck(:guid))
    end

    it "query policy with expanded policy actions" do
      basic_authorize @cfme[:user], @cfme[:password]

      create_actions(4)
      MiqAction.all.collect(&:id).each do |action_id|
        MiqPolicyContent.create(:miq_policy_id => @policy.id, :miq_action_id => action_id)
      end

      @success = run_get "#{@policy_url}?expand=policy_actions"

      expect(@code).to eq(200)
      expect(@result["name"]).to eq(@policy.name)
      expect(@result["description"]).to eq(@policy.description)
      expect(@result["guid"]).to eq(@policy.guid)
      expect(@result).to have_key("policy_actions")
      policy_actions = @result["policy_actions"]
      expect(policy_actions.size).to eq(4)
      guids = policy_actions.collect { |r| r["guid"] }
      expect(guids).to match_array(MiqAction.pluck(:guid))
    end
  end
end
<commit_msg>[api] rspec refactoring - spec/requests/api/policy_actions_spec.rb<commit_after>#
# REST API Request Tests - Policy Actions
#
# Policy Action primary collection:
#   /api/policy_actions
#
# Policy Action subcollection:
#   /api/policies/:id/policy_actions
#
require 'spec_helper'

describe ApiController do

  include Rack::Test::Methods

  before(:each) do
    init_api_spec_env
  end

  def app
    Vmdb::Application
  end

  let(:miq_action_guid_list) { MiqAction.pluck(:guid) }

  def create_actions(count)
    1.upto(count) do |i|
      FactoryGirl.create(:miq_action, :name => "custom_action_#{i}", :description => "Custom Action #{i}")
    end
  end

  context "Policy Action collection" do
    it "query invalid action" do
      api_basic_authorize

      run_get policy_actions_url(999_999)

      expect_resource_not_found
    end

    it "query policy actions with no actions defined" do
      api_basic_authorize

      run_get policy_actions_url

      expect_empty_query_result(:policy_actions)
    end

    it "query policy actions" do
      api_basic_authorize
      create_actions(4)

      run_get policy_actions_url

      expect_query_result(:policy_actions, 4, 4)
      expect_result_resources_to_include_hrefs("resources",
                                               MiqAction.pluck(:id).collect { |id| /^.*#{policy_actions_url(id)}$/ })
    end

    it "query policy actions in expanded form" do
      api_basic_authorize
      create_actions(4)

      run_get "#{policy_actions_url}?expand=resources"

      expect_query_result(:policy_actions, 4, 4)
      expect_result_resources_to_include_data("resources", "guid" => :miq_action_guid_list)
    end
  end

  context "Policy Action subcollection" do
    let(:policy)             { FactoryGirl.create(:miq_policy, :name => "Policy 1") }
    let(:policy_url)         { policies_url(policy.id) }
    let(:policy_actions_url) { "#{policy_url}/policy_actions" }

    def relate_actions_to(policy)
      MiqAction.all.collect(&:id).each do |action_id|
        MiqPolicyContent.create(:miq_policy_id => policy.id, :miq_action_id => action_id)
      end
    end

    it "query policy actions with no actions defined" do
      api_basic_authorize

      run_get policy_actions_url

      expect_empty_query_result(:policy_actions)
    end

    it "query policy actions" do
      api_basic_authorize
      create_actions(4)
      relate_actions_to(policy)

      run_get "#{policy_actions_url}?expand=resources"

      expect_query_result(:policy_actions, 4, 4)
      expect_result_resources_to_include_data("resources", "guid" => :miq_action_guid_list)
    end

    it "query policy with expanded policy actions" do
      api_basic_authorize
      create_actions(4)
      relate_actions_to(policy)

      run_get "#{policy_url}?expand=policy_actions"

      expect_single_resource_query("name" => policy.name, "description" => policy.description, "guid" => policy.guid)
      expect_result_resources_to_include_data("policy_actions", "guid" => :miq_action_guid_list)
    end
  end
end

{'fraction_non_alphanumeric': 0.1293457432247468, 'fraction_numerical': 0.008349301943607994, 'mean_word_length': 3.572590738423029, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5822352', 'n_tokens_mistral': 2420, 'n_tokens_neox': 2328, 'n_words': 465}