# frozen_string_literal: true require 'spec_helper' require 'vagrant-profitbricks/config' describe VagrantPlugins::ProfitBricks::Config do describe 'defaults' do let(:vagrant_public_key) { Vagrant.source_root.join('keys/vagrant.pub') } subject do super().tap(&:finalize!) end its(:password) { should be_nil } its(:url) { should be_nil } its(:cores) { should eq(1) } its(:ram) { should eq(2048) } its(:server_name) { should be_nil } its(:username) { should be_nil } its(:image_password) { should be_nil } its(:image_alias) { should be_nil } end describe 'overriding defaults' do %i[password url cores ram image server_name username image_password image_alias datacenter_id location].each do |attribute| it "should not default #{attribute} if overridden" do subject.send("#{attribute}=".to_sym, 'foo') subject.finalize! end end it 'should not default rsync_includes if overridden' do inc = 'core' subject.send(:rsync_include, inc) subject.finalize! subject.send(:rsync_includes).should include(inc) end end describe 'validation' do let(:machine) { double('machine') } let(:validation_errors) { subject.validate(machine)['ProfitBricks Provider'] } let(:error_message) { double('error message') } before(:each) do machine.stub_chain(:env, :root_path).and_return '/' subject.username = ENV['PROFITBRICKS_USERNAME'] subject.password = ENV['PROFITBRICKS_PASSWORD'] subject.datacenter_id = '00000000-0000-0000-0000-000000000000' subject.image_alias = 'ubuntu:latest' subject.image_password = '6aC6AGY7]k}Z=5G/' subject.public_ssh_keys = [''] end subject do super().tap(&:finalize!) end context 'the API password' do it 'should error if not given' do subject.password = nil I18n.should_receive(:t).with('vagrant_profitbricks.config.password_required').and_return error_message validation_errors.first.should == error_message end end context 'the API username' do it 'should error if not given' do subject.username = nil I18n.should_receive(:t).with('vagrant_profitbricks.config.username_required').and_return error_message validation_errors.first.should == error_message end end context 'the datacenter_id ' do it 'should error if not given' do subject.datacenter_id = nil I18n.should_receive(:t).with('vagrant_profitbricks.config.datacenter_required').and_return error_message validation_errors.first.should == error_message end end context 'the image_alias ' do it 'should error if not given' do subject.image_alias = nil I18n.should_receive(:t).with('vagrant_profitbricks.config.image_or_image_alias_must_be_provided').and_return error_message validation_errors.first.should == error_message end end context 'the ssh keys' do it 'should error if not given' do subject.image_password = nil subject.public_ssh_keys = nil I18n.should_receive(:t).with('vagrant_profitbricks.config.image_password_or_public_ssh_required').and_return error_message validation_errors.first.should == error_message end end [:url].each do |url| context "the #{url}" do it 'should not validate if the URL is invalid' do subject.send "#{url}=", 'baz' I18n.should_receive(:t).with('vagrant_profitbricks.config.invalid_uri', key: url, uri: 'baz').and_return error_message validation_errors.first.should == error_message end end end end end