<commit_before>require "spec_helper"

module MiqAeServiceMiqRequestSpec
  describe MiqAeMethodService::MiqAeServiceMiqRequest do
    def assert_ae_user_matches_ar_user(ae_user, ar_user)
      ae_user.should be_kind_of(MiqAeMethodService::MiqAeServiceUser)
      [:id, :name, :userid, :email].each { |method| ae_user.send(method).should == ar_user.send(method) }
    end

    before(:each) do
      MiqAutomateHelper.create_service_model_method('SPEC_DOMAIN', 'EVM',
                                                    'AUTOMATE', 'test1', 'test')
      @ae_method     = ::MiqAeMethod.find(:first)
      @ae_result_key = 'foo'

      @fred          = FactoryGirl.create(:user, :name => '<NAME>',  :userid => 'fred')
      @approver_role = FactoryGirl.create(:ui_task_set_approver)
      @miq_request   = FactoryGirl.create(:miq_request, :requester => @fred)
    end

    def invoke_ae
      MiqAeEngine.instantiate("/EVM/AUTOMATE/test1?MiqRequest::miq_request=#{@miq_request.id}")
    end

    it "#approve" do
      approver = 'wilma'
      reason   = "Why Not?"
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].approve('#{approver}', '#{reason}')"
      @ae_method.update_attributes(:data => method)
      MiqRequest.any_instance.should_receive(:approve).with(approver, reason).once
      invoke_ae.root(@ae_result_key).should  be_true
    end

    it "#deny" do
      approver = 'wilma'
      reason   = "Why Not?"
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].deny('#{approver}', '#{reason}')"
      @ae_method.update_attributes(:data => method)
      MiqRequest.any_instance.should_receive(:deny).with(approver, reason).once
      invoke_ae.root(@ae_result_key).should  be_true
    end

    it "#pending" do
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].pending"
      @ae_method.update_attributes(:data => method)
      MiqRequest.any_instance.should_receive(:pending).once
      invoke_ae.root(@ae_result_key).should  be_true
    end

    it "#approvers" do
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].approvers"
      @ae_method.update_attributes(:data => method)
      invoke_ae.root(@ae_result_key).should == []

      wilma          = FactoryGirl.create(:user, :name => '<NAME>', :userid => 'wilma',  :email => '<EMAIL>')
      betty          = FactoryGirl.create(:user, :name => '<NAME>',     :userid => 'betty',  :email => '<EMAIL>')
      wilma_approval = FactoryGirl.create(:miq_approval, :approver => wilma)
      betty_approval = FactoryGirl.create(:miq_approval, :approver => betty)
      @miq_request.miq_approvals = [wilma_approval, betty_approval]
      @miq_request.save!

      approvers = invoke_ae.root(@ae_result_key)
      approvers.should be_kind_of(Array)
      approvers.length.should == 2

      approvers.each do |ae_user|
        ae_user.should be_kind_of(MiqAeMethodService::MiqAeServiceUser)
        [wilma.id, betty.id].should include(ae_user.id)
        ar_user =
          case ae_user.id
          when wilma.id then wilma
          when betty.id then betty
          end
        assert_ae_user_matches_ar_user(ae_user, ar_user)
      end
    end

    it "#requester" do
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].requester"
      @ae_method.update_attributes(:data => method)
      fred = invoke_ae.root(@ae_result_key)
      assert_ae_user_matches_ar_user(fred, @fred)
    end

    it "#authorized?" do
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].authorized?"
      @ae_method.update_attributes(:data => method)
      [true, false].each do |expected_authorized|
        MiqRequest.any_instance.stub(:authorized?).and_return(expected_authorized)
        authorized = invoke_ae.root(@ae_result_key)
        authorized.should == expected_authorized
      end
    end

    it "#resource" do
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].resource"
      @ae_method.update_attributes(:data => method)

      vm_template = FactoryGirl.create(:template_vmware, :name => "template1")
      resource    = FactoryGirl.create(:miq_provision_request, :userid => @fred.userid, :src_vm_id => vm_template.id)
      resource.create_request
      @miq_request = resource
      @miq_request.save!

      ae_resource = invoke_ae.root(@ae_result_key)
      ae_class    = "MiqAeMethodService::MiqAeService#{resource.class.name}".constantize
      ae_resource.should be_kind_of(ae_class)
      [:userid, :src_vm_id].each { |method| ae_resource.send(method).should == resource.send(method) }
    end

    it "#reason" do
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].reason"
      @ae_method.update_attributes(:data => method)
      reason = invoke_ae.root(@ae_result_key)
      reason.should == ""

      wilma          = FactoryGirl.create(:user, :name => '<NAME>', :userid => 'wilma',  :email => '<EMAIL>')
      betty          = FactoryGirl.create(:user, :name => '<NAME>',     :userid => 'betty',  :email => '<EMAIL>')
      wilma_approval = FactoryGirl.create(:miq_approval, :approver => wilma)
      betty_approval = FactoryGirl.create(:miq_approval, :approver => betty)
      @miq_request.miq_approvals = [wilma_approval, betty_approval]
      @miq_request.save!

      MiqApproval.any_instance.stub(:authorized?).and_return(true)
      MiqRequest.any_instance.stub(:approval_denied)

      betty_reason = "Where's Barney?"
      betty_approval.deny(betty.userid, betty_reason)
      #betty_approval.update_attributes(:state => 'denied', :reason => betty_reason)
      reason = invoke_ae.root(@ae_result_key)
      reason.should == betty_reason

      wilma_reason = "Where's Fred?"
      wilma_approval.deny(wilma.userid, wilma_reason)
      #wilma_approval.update_attributes(:state => 'denied', :reason => wilma_reason)
      reasons = invoke_ae.root(@ae_result_key)
      # Order of reasons is indeterminate
      reasons.split('; ').each { |reason| [betty_reason, wilma_reason].include?(reason).should be_true }
    end

    it "#options" do<commit_msg>Fixed spec to check for nil instead of blank string<commit_after>require "spec_helper"

module MiqAeServiceMiqRequestSpec
  describe MiqAeMethodService::MiqAeServiceMiqRequest do
    def assert_ae_user_matches_ar_user(ae_user, ar_user)
      ae_user.should be_kind_of(MiqAeMethodService::MiqAeServiceUser)
      [:id, :name, :userid, :email].each { |method| ae_user.send(method).should == ar_user.send(method) }
    end

    before(:each) do
      MiqAutomateHelper.create_service_model_method('SPEC_DOMAIN', 'EVM',
                                                    'AUTOMATE', 'test1', 'test')
      @ae_method     = ::MiqAeMethod.find(:first)
      @ae_result_key = 'foo'

      @fred          = FactoryGirl.create(:user, :name => '<NAME>',  :userid => 'fred')
      @approver_role = FactoryGirl.create(:ui_task_set_approver)
      @miq_request   = FactoryGirl.create(:miq_request, :requester => @fred)
    end

    def invoke_ae
      MiqAeEngine.instantiate("/EVM/AUTOMATE/test1?MiqRequest::miq_request=#{@miq_request.id}")
    end

    it "#approve" do
      approver = 'wilma'
      reason   = "Why Not?"
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].approve('#{approver}', '#{reason}')"
      @ae_method.update_attributes(:data => method)
      MiqRequest.any_instance.should_receive(:approve).with(approver, reason).once
      invoke_ae.root(@ae_result_key).should  be_true
    end

    it "#deny" do
      approver = 'wilma'
      reason   = "Why Not?"
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].deny('#{approver}', '#{reason}')"
      @ae_method.update_attributes(:data => method)
      MiqRequest.any_instance.should_receive(:deny).with(approver, reason).once
      invoke_ae.root(@ae_result_key).should  be_true
    end

    it "#pending" do
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].pending"
      @ae_method.update_attributes(:data => method)
      MiqRequest.any_instance.should_receive(:pending).once
      invoke_ae.root(@ae_result_key).should  be_true
    end

    it "#approvers" do
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].approvers"
      @ae_method.update_attributes(:data => method)
      invoke_ae.root(@ae_result_key).should == []

      wilma          = FactoryGirl.create(:user, :name => '<NAME>', :userid => 'wilma',  :email => '<EMAIL>')
      betty          = FactoryGirl.create(:user, :name => '<NAME>',     :userid => 'betty',  :email => '<EMAIL>')
      wilma_approval = FactoryGirl.create(:miq_approval, :approver => wilma)
      betty_approval = FactoryGirl.create(:miq_approval, :approver => betty)
      @miq_request.miq_approvals = [wilma_approval, betty_approval]
      @miq_request.save!

      approvers = invoke_ae.root(@ae_result_key)
      approvers.should be_kind_of(Array)
      approvers.length.should == 2

      approvers.each do |ae_user|
        ae_user.should be_kind_of(MiqAeMethodService::MiqAeServiceUser)
        [wilma.id, betty.id].should include(ae_user.id)
        ar_user =
          case ae_user.id
          when wilma.id then wilma
          when betty.id then betty
          end
        assert_ae_user_matches_ar_user(ae_user, ar_user)
      end
    end

    it "#requester" do
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].requester"
      @ae_method.update_attributes(:data => method)
      fred = invoke_ae.root(@ae_result_key)
      assert_ae_user_matches_ar_user(fred, @fred)
    end

    it "#authorized?" do
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].authorized?"
      @ae_method.update_attributes(:data => method)
      [true, false].each do |expected_authorized|
        MiqRequest.any_instance.stub(:authorized?).and_return(expected_authorized)
        authorized = invoke_ae.root(@ae_result_key)
        authorized.should == expected_authorized
      end
    end

    it "#resource" do
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].resource"
      @ae_method.update_attributes(:data => method)

      vm_template = FactoryGirl.create(:template_vmware, :name => "template1")
      resource    = FactoryGirl.create(:miq_provision_request, :userid => @fred.userid, :src_vm_id => vm_template.id)
      resource.create_request
      @miq_request = resource
      @miq_request.save!

      ae_resource = invoke_ae.root(@ae_result_key)
      ae_class    = "MiqAeMethodService::MiqAeService#{resource.class.name}".constantize
      ae_resource.should be_kind_of(ae_class)
      [:userid, :src_vm_id].each { |method| ae_resource.send(method).should == resource.send(method) }
    end

    it "#reason" do
      method   = "$evm.root['#{@ae_result_key}'] = $evm.root['miq_request'].reason"
      @ae_method.update_attributes(:data => method)
      reason = invoke_ae.root(@ae_result_key)
      reason.should be_nil

      wilma          = FactoryGirl.create(:user, :name => '<NAME>', :userid => 'wilma',  :email => '<EMAIL>')
      betty          = FactoryGirl.create(:user, :name => '<NAME>',     :userid => 'betty',  :email => '<EMAIL>')
      wilma_approval = FactoryGirl.create(:miq_approval, :approver => wilma)
      betty_approval = FactoryGirl.create(:miq_approval, :approver => betty)
      @miq_request.miq_approvals = [wilma_approval, betty_approval]
      @miq_request.save!

      MiqApproval.any_instance.stub(:authorized?).and_return(true)
      MiqRequest.any_instance.stub(:approval_denied)

      betty_reason = "Where's Barney?"
      betty_approval.deny(betty.userid, betty_reason)
      #betty_approval.update_attributes(:state => 'denied', :reason => betty_reason)
      reason = invoke_ae.root(@ae_result_key)
      reason.should == betty_reason

      wilma_reason = "Where's Fred?"
      wilma_approval.deny(wilma.userid, wilma_reason)
      #wilma_approval.update_attributes(:state => 'denied', :reason => wilma_reason)
      reasons = invoke_ae.root(@ae_result_key)
      # Order of reasons is indeterminate
      reasons.split('; ').each { |reason| [betty_reason, wilma_reason].include?(reason).should be_true }
    end

    it "#options" do
{'fraction_non_alphanumeric': 0.14142729270078933, 'fraction_numerical': 0.0006509886890715274, 'mean_word_length': 3.4224541201871177, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2148244', 'n_tokens_mistral': 4309, 'n_tokens_neox': 4202, 'n_words': 631}