spec/relish/commands/base_spec.rb in relish-0.0.5 vs spec/relish/commands/base_spec.rb in relish-0.0.6
- old
+ new
@@ -4,32 +4,23 @@
module Command
describe Base do
{:organization => 'rspec', :project => 'rspec-core'}.each do |meth, name|
describe "##{meth}" do
- context 'passed in command line as full arg' do
+ context 'passed in command line' do
let(:base) { described_class.new(["--#{meth}", name]) }
it 'returns the value' do
base.send(meth).should eq(name)
end
end
- context 'passed in command line as short arg' do
- let(:short_arg) { meth.to_s[0,1] }
- let(:base) { described_class.new(["-#{short_arg}", name]) }
-
- it 'returns the value' do
- base.send(meth).should eq(name)
- end
- end
-
- context 'contained in the options file' do
+ context 'contained in the local options file' do
let(:base) { described_class.new }
before do
- base.stub(:parsed_options_file).and_return({meth.to_s => name})
+ OptionsFile.stub(:new).with(Relish.local_options_file).and_return({meth.to_s => name})
end
it 'returns the value' do
base.send(meth).should eq(name)
end
@@ -55,37 +46,64 @@
context 'host not passed in command line' do
let(:base) { described_class.new }
it 'returns the default host' do
- base.url.should eq("http://#{Base::DEFAULT_HOST}/api")
+ base.url.should eq("http://#{Relish.default_host}/api")
end
end
end
- describe '#resource' do
- let(:base) { described_class.new }
-
- before do
- base.should_receive(:url).and_return('url')
- RestClient::Resource.should_receive(:new).with('url')
- end
-
- specify { base.resource }
- end
-
describe '#api_token' do
let(:base) { described_class.new }
- let(:options) { {'api_token' => '12345'} }
-
+ let(:ui) { double(Ui) }
+
before do
- base.should_receive(:parsed_options_file).and_return(options)
+ Ui.stub(:new).and_return(ui)
+ OptionsFile.stub(:new => options)
end
- it 'parses the api token' do
- base.api_token.should eq('12345')
+ context "when the token is stored locally" do
+ let(:options) { {'api_token' => '12345'} }
+
+ it 'parses the api token' do
+ base.api_token.should eq('12345')
+ end
end
+
+ context "when the token is not stored locally" do
+ let(:options) { {} }
+ let(:api_token) { 'abasfawer23123' }
+ let(:credentials) { ['testuser', 'testpassword'] }
+
+ let(:global_options) do
+ double = double(OptionsFile, :[] => nil)
+ OptionsFile.stub(:new => double)
+ double
+ end
+
+ def api_endpoint(name, credentials)
+ user, password = *credentials
+ endpoint = double
+
+ RestClient::Resource.stub(:new).
+ with(anything, :user => user, :password => password).
+ and_return name => endpoint
+
+ endpoint
+ end
+
+ it "asks the user for their credentials and send them to the server" do
+ ui.should_receive(:get_credentials).and_return(credentials)
+ api_endpoint('token', credentials).should_receive(:get).and_return(api_token)
+ global_options.should_receive(:store).with 'api_token' => api_token
+
+ base.api_token
+ end
+
+ end
+
end
describe '#get_param' do
context 'given a command param' do
@@ -107,57 +125,9 @@
base
end
it 'returns nil' do
base.get_param.should be_nil
- end
- end
- end
-
- describe '#get_options' do
- let(:options) { {'project' => 'rspec-core'} }
- let(:base) { described_class.new(['--project', 'rspec-core']) }
-
- before do
- base.should_receive(:parsed_options_file).and_return(options)
- end
-
- it 'combines the args and options file' do
- base.get_options.should eq(
- {'project' => 'rspec-core', '--project' => 'rspec-core'}
- )
- end
- end
-
- describe '#parsed_options_file' do
- let(:base) { described_class.new }
-
- context 'with options file that exists' do
- let(:options) do
- {'organization' => 'rspec', 'project' => 'rspec-core'}
- end
-
- before do
- File.should_receive(:exist?).twice.and_return(true)
- YAML.should_receive(:load_file).twice.and_return(options)
- end
-
- it 'parses the organization' do
- base.parsed_options_file['organization'].should eq('rspec')
- end
-
- it 'parses the project' do
- base.parsed_options_file['project'].should eq('rspec-core')
- end
- end
-
- context 'with options file that does not exist' do
- before do
- File.stub(:exist?).and_return(false)
- end
-
- it 'returns an empty hash' do
- base.parsed_options_file.should eq({})
end
end
end
end
\ No newline at end of file