<commit_before>require 'spec_helper'

describe CloudAccount do
  fixtures :all
  before(:each) do
    @cloud_account = Factory :mock_cloud_account
  end

  it "should not be destroyable if it has instances" do
    @cloud_account.instances << Instance.new
    @cloud_account.destroyable?.should_not be_true
    @cloud_account.destroy
    CloudAccount.find(@cloud_account.id).should_not be_nil


    @cloud_account.instances.clear
    @cloud_account.destroyable?.should be_true
    @cloud_account.destroy
    CloudAccount.find(:first, :conditions => ['id = ?', @cloud_account.id]).should be_nil
  end

  it "should check the validitiy of the cloud account login credentials" do
    mock_provider = Factory :mock_provider

    invalid_cloud_account = Factory.build(:cloud_account, :username => "wrong_username", :password => "<PASSWORD>", :provider => mock_provider)
    invalid_cloud_account.should_not be_valid

    valid_cloud_account = Factory.build(:mock_cloud_account, :provider => mock_provider)
    valid_cloud_account.should be_valid
  end

  it "should fail to create a cloud account if the provider credentials are invalid" do
    cloud_account = Factory.build(:mock_cloud_account, :password => "<PASSWORD>")
    cloud_account.save.should == false
  end

  it "should create an instance_key if provider is EC2" do
    @client = mock('DeltaCloud', :null_object => true)
    @provider = Factory.build :ec2_provider
    @key = mock('Key', :null_object => true)
    @key.stub!(:pem).and_return("PEM")
    @key.stub!(:id).and_return("1_user")
    @client.stub!(:"feature?").and_return(true)
    @client.stub!(:"create_key").and_return(@key)

    cloud_account = Factory.build :ec2_cloud_account
    cloud_account.stub!(:connect).and_return(@client)
    cloud_account.save
    cloud_account.instance_key.should_not == nil
    cloud_account.instance_key.pem == "PEM"
    cloud_account.instance_key.id == "1_user"
  end

end<commit_msg>Rspec test for ClouadAccount build_credentials method.<commit_after>require 'spec_helper'

describe CloudAccount do
  fixtures :all
  before(:each) do
    @cloud_account = Factory :mock_cloud_account
  end

  it "should not be destroyable if it has instances" do
    @cloud_account.instances << Instance.new
    @cloud_account.destroyable?.should_not be_true
    @cloud_account.destroy
    CloudAccount.find(@cloud_account.id).should_not be_nil


    @cloud_account.instances.clear
    @cloud_account.destroyable?.should be_true
    @cloud_account.destroy
    CloudAccount.find(:first, :conditions => ['id = ?', @cloud_account.id]).should be_nil
  end

  it "should check the validitiy of the cloud account login credentials" do
    mock_provider = Factory :mock_provider

    invalid_cloud_account = Factory.build(:cloud_account, :username => "wrong_username", :password => "<PASSWORD>", :provider => mock_provider)
    invalid_cloud_account.should_not be_valid

    valid_cloud_account = Factory.build(:mock_cloud_account, :provider => mock_provider)
    valid_cloud_account.should be_valid
  end

  it "should fail to create a cloud account if the provider credentials are invalid" do
    cloud_account = Factory.build(:mock_cloud_account, :password => "<PASSWORD>")
    cloud_account.save.should == false
  end

  it "should create an instance_key if provider is EC2" do
    @client = mock('DeltaCloud', :null_object => true)
    @provider = Factory.build :ec2_provider
    @key = mock('Key', :null_object => true)
    @key.stub!(:pem).and_return("PEM")
    @key.stub!(:id).and_return("1_user")
    @client.stub!(:"feature?").and_return(true)
    @client.stub!(:"create_key").and_return(@key)

    cloud_account = Factory.build :ec2_cloud_account
    cloud_account.stub!(:connect).and_return(@client)
    cloud_account.save
    cloud_account.instance_key.should_not == nil
    cloud_account.instance_key.pem == "PEM"
    cloud_account.instance_key.id == "1_user"
  end


  it "should generate credentials xml" do
    expected_xml = <<EOT
<?xml version="1.0"?>
<provider_credentials>
  <ec2_credentials>
    <account_number>1234</account_number>
    <access_key>user</access_key>
    <secret_access_key>pass</secret_access_key>
    <certificate>cert</certificate>
    <key>priv_key</key>
  </ec2_credentials>
</provider_credentials>
EOT
    cloud_account = Factory.build(:mock_cloud_account,
                                  :username => 'user',
                                  :password => '<PASSWORD>',
                                  :account_number => '1234',
                                  :x509_cert_priv => 'priv_key',
                                  :x509_cert_pub => 'cert'
                                 )
    cloud_account.build_credentials.should eql(expected_xml)
  end
end
{'fraction_non_alphanumeric': 0.11019632678910703, 'fraction_numerical': 0.005910914080641756, 'mean_word_length': 3.752256770310933, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 1, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4923188', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1438, 'n_words': 303}