require 'db/mongo/models/stack_template/stack_template_aws'
require_relative 'shared_stack_template_specs'
require 'providers/aws'

RSpec.describe Devops::Model::StackTemplateAws, type: :model do
  let(:stack_template) { build(:stack_template_aws) }
  let(:provider) {
    instance_double(Provider::AwsConnector,
      store_stack_template: {'url' => 'template_url'}
    )
  }

  it 'uploads file to S3' do
    allow_any_instance_of(described_class).to receive(:provider_instance) { provider }
    result = described_class.create('id' => 'foo',
      'template_body' => '{}',
      'owner' => 'root',
      'provider' => 'ec2'
    )
    expect(result).to be_an_instance_of(described_class)
    expect(result.template_url).to eq 'template_url'
  end

  describe 'available parameters fetching' do
    let(:template_body) { %q(
        {
          "Description" : "Example",

          "Parameters" : {
            "KeyName": {
              "Description" : "Name of KeyPair",
              "Type": "AWS::EC2::KeyPair::KeyName"
            },
           "SSHLocation" : {
              "Type": "String",
              "MinLength": "9"
            }
          }
        }

    ) }
    let(:template_attributes) { FactoryGirl.attributes_for(:stack_template_aws, template_body: template_body) }
    let(:template) { described_class.create(template_attributes.stringify_keys) }

    before do
      allow_any_instance_of(described_class).to receive(:provider_instance) { provider }
    end

    it 'fetches parameters from template body' do
      expect(template.available_parameters).to be_a(Hash)
      expect(template.available_parameters).to include('KeyName', 'SSHLocation')
    end

    it 'parses description' do
      expect(template.available_parameters['KeyName']['description']).to eq 'Name of KeyPair'
      expect(template.available_parameters['SSHLocation']['description']).to be nil
    end
  end



end