require 'helper'

class TestAwsCredentials < Test::Unit::TestCase

  def setup
    AWSCredentials.config_path = File.join(File.dirname(__FILE__), 'test_aws.conf')
  end

  should "return access_key specified in YML" do
    assert_equal '12345A', AWSCredentials.access_key
  end

  should "return secret_access_key specified in YML" do
    assert_equal 'ASDF', AWSCredentials.secret_access_key
  end
  
  should "default config_path to /etc/aws.conf if environment is not defined" do
    AWSCredentials.config_path = nil
    assert_equal '/etc/aws.conf', AWSCredentials.config_path
  end
  
  context "if environment is defined" do
    setup do
      AWSCredentials.config_path = nil # zero out existing data
      AWSCredentials.environment = nil
    end
    
    should "default config_path to /etc/aws.conf if environment == production" do
      AWSCredentials.environment = :production
      assert_equal '/etc/aws.conf', AWSCredentials.config_path
    end
    
    should "default config_path to /etc/aws.{environment}.conf if environment != production" do
      AWSCredentials.environment = :staging
      assert_equal '/etc/aws.staging.conf', AWSCredentials.config_path
    end
  end

  should "return specified config path if overridden" do
    AWSCredentials.config_path = '/some/other/path'
    assert_equal '/some/other/path', AWSCredentials.config_path
  end

  context "default_environment" do
    
    setup do
      AWSCredentials.environment = nil
    end
    
    teardown do
      ENV['RACK_ENV'], ENV['RAILS_ENV'] = nil, nil
    end
        
    should "set return supplied value when default is explicitly set" do
      AWSCredentials.environment = 'some-other-env'
      ENV['RAILS_ENV'] = 'staging'
      assert_equal 'some-other-env', AWSCredentials.default_environment
    end    
    
    should "return RAILS_ENV if defined and environment was not explicitly set" do
      ENV['RAILS_ENV'] = 'staging'
      assert_equal 'staging', AWSCredentials.default_environment
    end
    
    should "return RACK_ENV if defined and environment was not explicitly set" do
      ENV['RACK_ENV'] = 'internal_staging'
      assert_equal 'internal_staging', AWSCredentials.default_environment
    end
    
    should "ignore default_environment and look for the specified enviroment when it is passed to access_key" do
      ENV['RACK_ENV'] = 'internal_staging'
      assert_equal '12345A', AWSCredentials.access_key(:test_aws)
    end

  end

end