diff --git a/lib/sauce/heroku/api/sauce.rb b/lib/sauce/heroku/api/sauce.rb index b49f858..b64074d 100644 --- a/lib/sauce/heroku/api/sauce.rb +++ b/lib/sauce/heroku/api/sauce.rb @@ -7,6 +7,8 @@ module Sauce class Sauce attr_reader :config + API_CLIENT_ID = '0C309258-40FA-4138-91AF-C226F17C6954' + def initialize(config) @config = config end @@ -24,7 +26,7 @@ module Sauce :body => body.to_json, :basic_auth => {:username => config.username, :password => config.access_key}, - :headers => {'Content-Type' => 'application/json'}) + :headers => default_headers) if response.code == 401 raise Errors::SauceAuthenticationError, 'Authentication failed' @@ -43,10 +45,34 @@ module Sauce return response['embed'] end + def default_headers + @headers ||= begin + { + 'Content-Type' => 'application/json' + } + end + end + def scout_url return nil unless config.configured? "https://saucelabs.com/rest/v1/users/#{config.username}/scout" end + + def create_account(username, password, email, full_name) + if username.nil? || email.nil? || password.nil? + raise Errors::InvalidParametersError + end + full_name ||= '' + data = {:username => username, + :email => email, + :password => password, + :name => full_name, + :token => API_CLIENT_ID} + + response = HTTParty.post(account_url, + :body => data.to_json, + :headers => default_headers) + end end end end diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 30616be..a41d92a 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -108,4 +108,41 @@ describe Sauce::Heroku::API::Sauce do it { should_not be_empty } end end + + describe '#default_headers' do + subject { api.default_headers } + it { should have_key 'Content-Type' } + it { expect(subject['Content-Type']).to eql('application/json') } + end + + describe '#create_account' do + it { should respond_to :create_account } + + context 'without valid parameters' do + it 'should raise an error if nils are passed' do + expect { + api.create_account(nil, nil, nil, nil) + }.to raise_error(Sauce::Heroku::Errors::InvalidParametersError) + end + end + + context 'with valid parameters' do + let(:url) { 'http://create/user' } + let(:username) { 'rspec' } + let(:password) { 'bdd' } + let(:email) { 'rspec@example.org ' } + + before :each do + api.stub(:account_url => url) + end + + it 'should POST to Sauce Labs' do + HTTParty.should_receive(:post).with( + url, + hash_including(:body) + ) + api.create_account(username, password, email, nil) + end + end + end end diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 67c91e5..da3c0cf 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -143,9 +143,5 @@ describe Sauce::Heroku::Config do end describe '#write!' do - subject { config.write! } - context 'without credentials' do - it { should be_nil } - end end end