require 'spec_helper'

describe SimpleDeploy::StackAttributeFormatter do
  include_context 'stubbed config'
  include_context 'double stubbed logger'

  before do
    @config_mock.stub(:region).and_return('us-west-1')
    @config_mock.stub(:artifact_cloud_formation_url).and_return('ChefRepoURL')
    @config_mock.stub(:artifacts).and_return(['chef_repo', 'cookbooks', 'app'])
  end

  after do
    SimpleDeploy.release_config
  end

  context "when chef_repo unencrypted" do
    before do
      options = { :environment => 'preprod',
                  :main_attributes => {
                    'chef_repo_bucket_prefix' => 'test-prefix',
                    'chef_repo_domain'        => 'test-domain' }
                }
      @formatter = SimpleDeploy::StackAttributeFormatter.new options
    end

    it 'should return updated attributes including the un encrypted cloud formation url' do
      updates = @formatter.updated_attributes([ { 'chef_repo' => 'test123' } ])
      updates.should == [{ 'chef_repo' => 'test123' }, 
                         { 'ChefRepoURL' => 's3://test-prefix-us-west-1/test-domain/test123.tar.gz' }]
    end
  end

  context "when main_attributes set chef_repo encrypted" do
    before do
      options = { :environment => 'preprod',
                  :main_attributes => {
                    'chef_repo_bucket_prefix' => 'test-prefix',
                    'chef_repo_encrypted'     => 'true',
                    'chef_repo_domain'        => 'test-domain' }
                }
      @formatter = SimpleDeploy::StackAttributeFormatter.new options
    end

    it 'should return updated attributes including the encrypted cloud formation url ' do
      updates = @formatter.updated_attributes([ { 'chef_repo' => 'test123' } ])
      updates.should == [{ 'chef_repo' => 'test123' }, 
                         { 'ChefRepoURL' => 's3://test-prefix-us-west-1/test-domain/test123.tar.gz.gpg' }]
    end
  end

  context "when provided attributes set chef_repo encrypted" do
    before do
      options = { :environment => 'preprod',
                  :main_attributes => {
                    'chef_repo_bucket_prefix' => 'test-prefix',
                    'chef_repo_domain'        => 'test-domain' }
                }
      @formatter = SimpleDeploy::StackAttributeFormatter.new options
    end

    it 'should return updated attributes including the encrypted cloud formation url ' do
      updates = @formatter.updated_attributes([ { 'chef_repo' => 'test123' }, 
                                                { 'chef_repo_encrypted' => 'true' } ])
      updates.should == [{ 'chef_repo' => 'test123' },
                         { 'chef_repo_encrypted' => 'true' },
                         { 'ChefRepoURL' => 's3://test-prefix-us-west-1/test-domain/test123.tar.gz.gpg' }]
    end
  end
end
