<commit_before>require File.dirname(__FILE__) + '/../base'

module Heroku::Command
	describe Auth do
		before do
			@cli = prepare_command(Auth)
		end

		it "reads credentials from the credentials file" do
			sandbox = "#{Dir.tmpdir}/cli_spec_#{Process.pid}"
			File.open(sandbox, "w") { |f| f.write "user\npass\n" }
			@cli.stub!(:credentials_file).and_return(sandbox)
			@cli.read_credentials.should == %w(user pass)
		end

		it "takes the user from the first line and the password from the second line" do
			@cli.stub!(:read_credentials).and_return(%w(user pass))
			@cli.user.should == 'user'
			@cli.password.should == '<PASSWORD>'
		end

		it "asks for credentials when the file doesn't exist" do
			sandbox = "#{Dir.tmpdir}/cli_spec_#{Process.pid}"
			FileUtils.rm_rf(sandbox)
			@cli.stub!(:credentials_file).and_return(sandbox)
			@cli.should_receive(:ask_for_credentials).and_return([ 'u', 'p'])
			@cli.should_receive(:save_credentials)
			@cli.get_credentials.should == [ 'u', 'p' ]
		end

		it "writes the credentials to a file" do
			sandbox = "#{Dir.tmpdir}/cli_spec_#{Process.pid}"
			FileUtils.rm_rf(sandbox)
			@cli.stub!(:credentials_file).and_return(sandbox)
			@cli.stub!(:credentials).and_return(['one', 'two'])
			@cli.should_receive(:set_credentials_permissions)
			@cli.write_credentials
			File.read(sandbox).should == "one\ntwo\n"
		end

		it "sets ~/.heroku/credentials to be readable only by the user" do
			sandbox = "#{Dir.tmpdir}/cli_spec_#{Process.pid}"
			FileUtils.rm_rf(sandbox)
			FileUtils.mkdir_p(sandbox)
			fname = "#{sandbox}/file"
			system "touch #{fname}"
			@cli.stub!(:credentials_file).and_return(fname)
			@cli.set_credentials_permissions
			File.stat(sandbox).mode.should == 040700
			File.stat(fname).mode.should == 0100600
		end

		it "writes credentials and uploads authkey when credentials are saved" do
			@cli.stub!(:credentials)
			@cli.should_receive(:write_credentials)
			Heroku::Command.should_receive(:run_internal).with('keys:add', [])
			@cli.save_credentials
		end

		it "save_credentials deletes the credentials when the upload authkey is unauthorized" do<commit_msg>do not check for file permissions on windows<commit_after>require File.dirname(__FILE__) + '/../base'

module Heroku::Command
	describe Auth do
		before do
			@cli = prepare_command(Auth)
		end

		it "reads credentials from the credentials file" do
			sandbox = "#{Dir.tmpdir}/cli_spec_#{Process.pid}"
			File.open(sandbox, "w") { |f| f.write "user\npass\n" }
			@cli.stub!(:credentials_file).and_return(sandbox)
			@cli.read_credentials.should == %w(user pass)
		end

		it "takes the user from the first line and the password from the second line" do
			@cli.stub!(:read_credentials).and_return(%w(user pass))
			@cli.user.should == 'user'
			@cli.password.should == '<PASSWORD>'
		end

		it "asks for credentials when the file doesn't exist" do
			sandbox = "#{Dir.tmpdir}/cli_spec_#{Process.pid}"
			FileUtils.rm_rf(sandbox)
			@cli.stub!(:credentials_file).and_return(sandbox)
			@cli.should_receive(:ask_for_credentials).and_return([ 'u', 'p'])
			@cli.should_receive(:save_credentials)
			@cli.get_credentials.should == [ 'u', 'p' ]
		end

		it "writes the credentials to a file" do
			sandbox = "#{Dir.tmpdir}/cli_spec_#{Process.pid}"
			FileUtils.rm_rf(sandbox)
			@cli.stub!(:credentials_file).and_return(sandbox)
			@cli.stub!(:credentials).and_return(['one', 'two'])
			@cli.should_receive(:set_credentials_permissions)
			@cli.write_credentials
			File.read(sandbox).should == "one\ntwo\n"
		end

		it "sets ~/.heroku/credentials to be readable only by the user" do
			unless RUBY_PLATFORM =~ /mswin32/
				sandbox = "#{Dir.tmpdir}/cli_spec_#{Process.pid}"
				FileUtils.rm_rf(sandbox)
				FileUtils.mkdir_p(sandbox)
				fname = "#{sandbox}/file"
				system "touch #{fname}"
				@cli.stub!(:credentials_file).and_return(fname)
				@cli.set_credentials_permissions
				File.stat(sandbox).mode.should == 040700
				File.stat(fname).mode.should == 0100600
			end
		end

		it "writes credentials and uploads authkey when credentials are saved" do
			@cli.stub!(:credentials)
			@cli.should_receive(:write_credentials)
			Heroku::Command.should_receive(:run_internal).with('keys:add', [])
			@cli.save_credentials
		end

		it "save_credentials deletes the credentials when the upload authkey is unauthorized" do
{'fraction_non_alphanumeric': 0.14154411764705882, 'fraction_numerical': 0.006433823529411764, 'mean_word_length': 4.496212121212121, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1176817', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1530, 'n_words': 314}