module Helpers
  # Remove this after refactored to vcr
  def load_fixture(name)
    File.new(File.dirname(__FILE__) + "/../fixtures/#{name}.xml")
  end

  # Check if the current example's VCR cassette exists
  def cassette_exist?
    File.exist?(
      File.join(
        VCR.configuration.cassette_library_dir,
        "#{RSpec.current_example.metadata[:vcr][:cassette_name]}.yml"
      )
    )
  end

  # Helpful when running integration/acceptance tests
  # against a new version of Rundeck.
  #
  # We don't want to record the 'preparation' type requests.
  # Example - Before running tests that need to 'get' a project,
  # we must create the project.
  def prepare
    unless cassette_exist?
      cassette = RSpec.current_example.metadata[:vcr][:cassette_name]
      VCR.eject_cassette(cassette)
      VCR.turned_off { yield }
      VCR.insert_cassette(cassette)
    end
  end

  def endpoint
    "#{Rundeck.endpoint}/api/#{Rundeck.api_version}"
  end

  # GET
  def a_get(path, accept = 'xml')
    a_request(:get, "#{endpoint}#{path}")
      .with(headers: { 'Accept' => "application/#{accept}",
                       'X-Rundeck-Auth-Token' => Rundeck.api_token })
  end

  # POST
  def a_post(path, accept = 'xml')
    a_request(:post, "#{endpoint}#{path}")
      .with(headers: { 'Accept' => "application/#{accept}",
                       'X-Rundeck-Auth-Token' => Rundeck.api_token })
  end

  def a_simple_post(path)
    a_request(:post, "#{endpoint}#{path}")
  end

  # PUT
  def a_put(path, accept = 'xml')
    a_request(:put, "#{endpoint}#{path}")
      .with(headers: { 'Accept' => "application/#{accept}",
                       'X-Rundeck-Auth-Token' => Rundeck.api_token })
  end

  # DELETE
  def a_delete(path, accept = 'xml')
    a_request(:delete, "#{endpoint}#{path}")
      .with(headers: { 'Accept' => "application/#{accept}",
                       'X-Rundeck-Auth-Token' => Rundeck.api_token })
  end

  def job_yaml
    <<-EOS
- project: anvils
  loglevel: INFO
  sequence:
    keepgoing: true
    strategy: node-first
    commands:
    - exec: echo 'hello world'
  description: Check the status of anvils
  name: My YAML Job
  group: anvils
    EOS
  end

  def job_xml
    <<-EOS
<joblist>
  <job>
    <loglevel>INFO</loglevel>
    <sequence keepgoing='true' strategy='node-first'>
      <command>
        <exec>echo 'hello world'</exec>
      </command>
    </sequence>
    <description>Check the status of anvils</description>
    <name>My XML Job</name>
    <context>
      <project>anvils</project>
    </context>
    <group>anvils</group>
  </job>
</joblist>
    EOS
  end

  def project_anvils
    '{ "name": "anvils" }'
  end

  def project_json
    '{ "name": "json_project" }'
  end

  def project_deleteme
    '{ "name": "deleteme" }'
  end

  def project_xml
    <<-EOS
<project>
  <name>xml_project</name>
</project>
    EOS
  end

  def private_key
    <<-EOS
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAt6G5YJC4x4UiRQB08dsLavDM92HVnQINs4fiAUgZsPICTAhb
Agn7XRXU+P8fX1aReRi2p4LHSItdl5hjT2WkNCH1Niw9fpltiNhnlOlrpoBt/eWM
dd2rJtC00jQ4pDv+1edw9pc+Re7fDQYD4HSmhm/expQLAgu+LSFCdL7Fj05OAN7D
0RxwFfQ3wrZe5qGt4tr5objA2wXwGEnMpFqFGGI9+uqSKnuw1aUgVf7iRvec1fJT
/hmFSastozEnBJmBB/mQoFvjOqz+oSiXmXiwvoHfw9B8AdbxKpmi4+AfSTmJqYT1
j4FPO6JJf78Qx0Uh7eWQcC7uxhLa5XpHr6C/qQIDAQABAoIBAAR18ULfQR3XphV3
BWA6qfRXFSONRNsjiaGq01qknbsmpdei/FL4WxrPxPSnfeOa/r2qVAWNr7mbaRKd
qQvstChwCrzeJkBFCdwhJaMAaJUK2aEpSlgyok23FC1nB1k1++LGVIAo/GJGgzSV
yNJTAxiQ7yBzyDCsiFogTLT5TWNFwSPOSWTbUQIMW0BGVnVlecB/VIJN1zrBZSAe
T/MZenbjybkuTuXWod09YS9Yvx17eG/21I11AwBcmc5MlAn9nkZbydQs6kbLd9hx
KT7d9c42ocrTVrmMBcF7S5W8BdMp90snNc7sG99sQJ4W5BL8a46i4kVf6yURh5dy
orDDdAECgYEA4wh/jPEcfW/gP1nE9ydLYvgz7tC6+sWR1I7519W15NFABzoW6cw6
wjyWhR61EM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A3sPRHHAV9DfCtl7moa3i2vmhuMDbBfAYScykWoUYYj366pIqe7DVpSBV/uJG95zV8lP+GYVJqy2jMScEmYEH+ZCgW+M6rP6hKJeZeLC+gd/D0HwB1vEqmaLj4B9JOYmphPWPgU87okl/vxDHRSHt5ZBwLu7GEtrlekevoL+p test@localhost
    EOS
  end
end
