Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update client/cli to reflect readme

  • Loading branch information...
commit 33b2c8fe5bc5b8f94070787927270638c9e03498 1 parent bac5496
@csquared csquared authored
View
9 lib/addons-client/cli.rb
@@ -2,7 +2,6 @@ module Addons::CLI
extend self
def run!
- define_settings
load_settings!
run_command!
end
@@ -24,13 +23,7 @@ def run_command!
end
def client
- @client ||= Addons::Client.new(:username => 'heroku',
- :salt => Settings[:api_salt],
- :password => Settings[:api_password])
- end
-
- def define_settings
- load File.expand_path('../settings.rb', __FILE__)
+ @client ||= Addons::Client.new
end
def load_settings!
View
61 lib/addons-client/client.rb
@@ -1,36 +1,35 @@
-class Addons::Client
- attr_accessor :scheme
+module Addons
+ class Client
+ def initialize
+ validate_addons_api_url!
+ end
- def initialize(opts = {})
- validate_new_options! opts
- @username = opts[:username]
- pre_token = [opts[:salt], opts[:password]].join(':')
- @password = Digest::SHA1.hexdigest(pre_token)
- @consumer_id = opts.fetch :consumer_id, 'api-client@localhost'
- @scheme = opts.fetch(:scheme, 'https')
- end
-
- def validate_new_options!(opts)
- raise UserError, "No username given" unless opts[:username]
- raise UserError, "No salt given" unless opts[:salt]
- raise UserError, "No password given" unless opts[:password]
- end
+ def validate_addons_api_url!
+ raise UserError, "ADDONS_API_URL must be set" unless ENV['ADDONS_API_URL']
+ begin
+ @api_url = URI.parse(ENV['ADDONS_API_URL'])
+ rescue URL::InvalidUriError
+ raise UserError, "ADDONS_API_URL is an invalid url"
+ end
+ raise UserError, "No username given" unless @api_url.user
+ raise UserError, "No password given" unless @api_url.password
+ end
- def provision!(slug, opts = {})
- addon_name, plan = slug.split(':')
- raise UserError, "No add-on name given" unless addon_name
- raise UserError, "No plan name given" unless plan
- payload = {
- addon: addon_name,
- plan: plan,
- consumer_id: opts.fetch(:consumer_id, "api-client@localhost")
- }
- payload.merge! :options => opts[:options] if opts[:options]
- resource.post payload
- end
+ def provision!(slug, opts = {})
+ addon_name, plan = slug.split(':')
+ raise UserError, "No add-on name given" unless addon_name
+ raise UserError, "No plan name given" unless plan
+ payload = {
+ addon: addon_name,
+ plan: plan,
+ consumer_id: opts.fetch(:consumer_id, "api-client@localhost")
+ }
+ payload.merge! :options => opts[:options] if opts[:options]
+ resource.post payload
+ end
- def resource
- addons_api_url = "#{@scheme}://#{@username}:#{@password}@localhost:3000"
- RestClient::Resource.new(addons_api_url)['/api/1/resources']
+ def resource
+ RestClient::Resource.new(@api_url.to_s)
+ end
end
end
View
8 lib/addons-client/settings.rb
@@ -1,8 +0,0 @@
-Settings.define :api_salt,
- :env_var => "ADDONS_API_SALT",
- :description => "Salt used for hashing login",
- :required => true
-Settings.define :api_password,
- :env_var => "ADDONS_API_PASSWORD",
- :description => "Addons API password",
- :required => true
View
65 test/client_test.rb
@@ -2,57 +2,26 @@
class SettingsTest < Addons::Client::TestCase
def setup
- super
- stub_request(:any, /api\/1\//)
+ stub_request(:any, /api\/1\/resources/)
end
- def test_works_when_options_provided
- assert_nothing_raised { Addons::CLI.run! }
- end
-
- def test_hashes_password
- password = Digest::SHA1.hexdigest('salt:bacon')
- addons_client! "provision memcache:5mb"
- assert_requested :post, /heroku:#{password}/
- end
-
- def test_client_sets_username_password_and_salt
- password = Digest::SHA1.hexdigest('salt:pass')
- client = Addons::Client.new(:username => 'test',
- :password => 'pass',
- :salt => 'salt')
+ def test_client_uses_env_var
+ ENV['ADDONS_API_URL'] = 'https://test:password@localhost:3333/api/1/resources'
+ client = Addons::Client.new
client.provision! 'foo:bar'
- target_url = "https://test:#{password}@localhost:3000/api/1/resources"
- assert_requested(:post, target_url)
- end
-
- def test_requires_api_password
- Settings.delete :api_password
- ENV['ADDONS_API_PASSWORD'] = nil
- assert_raises Addons::UserError do
- addons_client! "provision memcache:5mb"
+ assert_requested(:post, ENV['ADDONS_API_URL'])
+ end
+
+ def test_client_raises_error_on_bad_url
+ [
+ nil,
+ 'https://localhost:3333/api/1/resources',
+ 'https://foo@localhost:3333/api/1/resources',
+ ].each do |url|
+ ENV['ADDONS_API_URL'] = url
+ assert_raises Addons::UserError do
+ Addons::Client.new
+ end
end
end
-
- def test_requires_api_salt
- Settings.delete :api_salt
- ENV['ADDONS_API_SALT'] = nil
- assert_raises Addons::UserError do
- addons_client! "provision memcache:5mb"
- end
- end
-
- def test_sets_password_and_salt_from_cmd_line
- Settings.delete :api_password
- ENV['ADDONS_API_PASSWORD'] = nil
- Settings.delete :api_salt
- ENV['ADDONS_API_SALT'] = nil
- assert_nothing_raised do
- addons_client! "provision memcache:5mb --api_password=pass --api_salt=salt"
- end
- end
-
- def test_reads_url_from_env
-
- end
end
View
8 test/provision_test.rb
@@ -2,15 +2,13 @@
class ProvisionTest < Addons::Client::TestCase
def setup
- super
+ ENV["ADDONS_API_URL"] = 'https://foo:bar@heroku.com/api/1/resources'
stub_request(:any, target_url)
- @client = Addons::Client.new(:username => 'test',
- :password => 'pass',
- :salt => 'salt')
+ @client = Addons::Client.new
end
def target_url
- /resources$/
+ ENV["ADDONS_API_URL"]
end
def test_provisions_from_cmd_line
View
2  test/test_helper.rb
@@ -9,8 +9,6 @@ class Addons::Client::TestCase < Test::Unit::TestCase
include RR::Adapters::TestUnit
def setup
- ENV['ADDONS_API_SALT'] = 'salt'
- ENV['ADDONS_API_PASSWORD'] = 'bacon'
end
def teardown
Please sign in to comment.
Something went wrong with that request. Please try again.