require 'spec_helper' require 'highline' GITHUB_FP = "SHA1 Fingerprint=A0:C4:A7:46:00:ED:A7:2D:C0:BE:CB:9A:8C:B6:07:CA:58:EE:74:5E" GITHUB_CERT = < "the-account" expect(File).to receive(:open) invoke end end describe_command 'init -a the-account' do it "writes config file" do expect(File).to receive(:open) invoke end end end describe_command 'init -a the-account -h foobar' do it "can't get the cert" do expect { invoke }.to raise_error(GLI::CustomExit, /unable to retrieve certificate/i) end end # KEG: These tests have a nasty habit of hanging # describe_command 'init -a the-account -h google.com' do # it "writes the config and cert" do # HighLine.any_instance.stub(:ask).and_return "yes" # File.should_receive(:open).twice # invoke # end # end # describe_command 'init -a the-account -h https://google.com' do # it "writes the config and cert" do # HighLine.any_instance.stub(:ask).and_return "yes" # File.should_receive(:open).twice # invoke # end # end describe_command 'init -a the-account -h localhost -c the-cert' do it "writes config and cert files" do expect(File).to receive(:open).twice invoke end end context "in a temp dir" do tmpdir = Dir.mktmpdir shared_examples "check config and cert files" do |file, env| around do |example| Dir.foreach(tmpdir) {|f| fn = File.join(tmpdir, f) File.delete(fn) if f != '.' && f != '..' } f = ENV.delete 'CONJURRC' if not env.nil? ENV['CONJURRC'] = env end example.run ENV['CONJURRC'] = f end it "writes config and cert files" do invoke expect(YAML.load(File.read(file))).to eq({ account: 'the-account', appliance_url: "https://localhost/api", cert_file: File.join(File.dirname(file), "conjur-the-account.pem"), plugins: [], }.stringify_keys) end end context "default behavior" do describe_command "init -a the-account -h localhost -c the-cert" do before(:each) { allow(File).to receive(:expand_path).and_call_original allow(File).to receive(:expand_path).with('~/.conjurrc').and_return("#{tmpdir}/.conjurrc") } include_examples "check config and cert files", "#{tmpdir}/.conjurrc" it "prints the config file location" do expect { invoke }.to write("Wrote configuration to #{tmpdir}/.conjurrc") end it "prints the cert location" do expect { invoke }.to write("Wrote certificate to #{tmpdir}/conjur-the-account.pem") end end end context "explicit output file" do describe_command "init -f #{tmpdir}/.conjurrc2 -a the-account -h localhost -c the-cert" do include_examples "check config and cert files", File.join(tmpdir, ".conjurrc2") it "prints the config file location" do expect { invoke }.to write("Wrote configuration to #{tmpdir}/.conjurrc2") end end end context "to CONJURRC" do describe_command "init -a the-account -h localhost -c the-cert" do file = File.join(tmpdir, ".conjurrc_env") include_examples "check config and cert files", file, file end end context "explicit output file overrides CONJURRC" do describe_command "init -f #{tmpdir}/.conjurrc_2 -a the-account -h localhost -c the-cert" do ENV['CONJURRC'] = "#{tmpdir}/.conjurrc_env_2" include_examples "check config and cert files", File.join(tmpdir, ".conjurrc_2") end end end end end