Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/linux_admin/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ def initialize(message, result)
super(message)
@result = result
end
end
end

class LinuxAdmin
class CredentialError < CommandResultError
def initialize(result)
super("Invalid username or password", result)
end
end
end
10 changes: 8 additions & 2 deletions lib/linux_admin/registration_system/subscription_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

class LinuxAdmin
class SubscriptionManager < RegistrationSystem
def run!(cmd, options = {})
super(cmd, options)
rescue CommandResultError => err
raise CredentialError.new(err.result) if err.result.error.downcase.include?("invalid username or password")
raise
end

def validate_credentials(options)
!!organizations(options)
Expand All @@ -23,8 +29,8 @@ def organizations(options)
params.merge!(proxy_params(options))
params["--serverurl="] = options[:server_url] if options[:server_url]

output = run!(cmd, :params => params).output
parse_output(output).index_by {|i| i[:name]}
result = run!(cmd, :params => params)
parse_output(result.output).index_by {|i| i[:name]}
end

def register(options)
Expand Down
16 changes: 12 additions & 4 deletions spec/subscription_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,17 @@
})
end

it "#organizations" do
run_options = ["subscription-manager orgs", {:params=>{"--username="=>"SomeUser", "--password="=>"SomePass", "--proxy="=>"1.2.3.4", "--proxyuser="=>"ProxyUser", "--proxypassword="=>"ProxyPass", "--serverurl="=>"192.168.1.1"}}]
described_class.any_instance.should_receive(:run!).once.with(*run_options).and_return(double(:output => sample_output("subscription_manager/output_orgs")))
expect(described_class.new.organizations({:username=>"SomeUser", :password=>"SomePass", :proxy_address=>"1.2.3.4", :proxy_username=>"ProxyUser", :proxy_password=>"ProxyPass", :server_url=>"192.168.1.1"})).to eq({"SomeOrg"=>{:name=>"SomeOrg", :key=>"1234567"}})
context "#organizations" do
it "with valid credentials" do
run_options = ["subscription-manager orgs", {:params=>{"--username="=>"SomeUser", "--password="=>"SomePass", "--proxy="=>"1.2.3.4", "--proxyuser="=>"ProxyUser", "--proxypassword="=>"ProxyPass", "--serverurl="=>"192.168.1.1"}}]
described_class.any_instance.should_receive(:run!).once.with(*run_options).and_return(double(:output => sample_output("subscription_manager/output_orgs")))
expect(described_class.new.organizations({:username=>"SomeUser", :password=>"SomePass", :proxy_address=>"1.2.3.4", :proxy_username=>"ProxyUser", :proxy_password=>"ProxyPass", :server_url=>"192.168.1.1"})).to eq({"SomeOrg"=>{:name=>"SomeOrg", :key=>"1234567"}})
end

it "with invalid credentials" do
run_options = ["subscription-manager orgs", {:params=>{"--username="=>"BadUser", "--password="=>"BadPass"}}]
described_class.any_instance.should_receive(:run).once.with(*run_options).and_return(CommandResult.new("", "Invalid username or password. To create a login, please visit https://www.redhat.com/wapps/ugc/register.html", 255))
expect { described_class.new.organizations({:username=>"BadUser", :password=>"BadPass"}) }.to raise_error(LinuxAdmin::CredentialError)
end
end
end