[PATCH aeolus-cli] Catch 401 Unauthorized response and display a more helpful error

Martyn Taylor mtaylor at redhat.com
Fri Dec 16 15:34:05 UTC 2011


Please ignore, sent this by mistake


On 12/16/2011 03:33 PM, mtaylor at redhat.com wrote:
> From: Matt Wagner<matt.wagner at redhat.com>
>
> Resolves https://bugzilla.redhat.com/show_bug.cgi?id=767342
> ---
>   lib/aeolus_cli/command/base_command.rb             |    7 +++-
>   spec/command/base_command_spec.rb                  |   16 +++++++++
>   spec/spec_helper.rb                                |    6 +---
>   .../command/base_command/invalid_credentials.yml   |   34 ++++++++++++++++++++
>   4 files changed, 56 insertions(+), 7 deletions(-)
>   create mode 100644 spec/vcr/cassettes/command/base_command/invalid_credentials.yml
>
> diff --git a/lib/aeolus_cli/command/base_command.rb b/lib/aeolus_cli/command/base_command.rb
> index d6afc6f..16573c5 100644
> --- a/lib/aeolus_cli/command/base_command.rb
> +++ b/lib/aeolus_cli/command/base_command.rb
> @@ -90,7 +90,10 @@ module Aeolus
>           code = doc.xpath("/error/code").text
>           message = doc.xpath("/error/message").text
>
> -        if message.to_s.empty?
> +        if e.is_a?(ActiveResource::UnauthorizedAccess)
> +          code = "Unauthorized"
> +          message = "Invalid Credentials, please check ~/.aeolus-cli"
> +        elsif message.to_s.empty?
>             case code
>               when "BuildDeleteFailure" : message = "An error occured when deleting the Build from the Image Warehouse"
>               when "BuildNotFound" : message = "Could not find the specified Build"
> @@ -189,4 +192,4 @@ module Aeolus
>         end
>       end
>     end
> -end
> \ No newline at end of file
> +end
> diff --git a/spec/command/base_command_spec.rb b/spec/command/base_command_spec.rb
> index 8a3b1a2..bc8a0c1 100644
> --- a/spec/command/base_command_spec.rb
> +++ b/spec/command/base_command_spec.rb
> @@ -78,6 +78,22 @@ module Aeolus
>             errors.length.should == 0
>           end
>         end
> +
> +      describe "#handle_exception" do
> +        it "should display a user message when an authorised response is returned from Conductor" do
> +          VCR.use_cassette('command/base_command/invalid_credentials') do
> +            lc = ListCommand.new
> +            Aeolus::CLI::Base.password = "wrong_password"
> +            begin
> +              lc.accounts
> +            rescue SystemExit =>  e
> +              e.status.should == 1
> +            end
> +            $stdout.string.should include("Invalid Credentials, please check ~/.aeolus-cli")
> +          end
> +        end
> +      end
> +
>       end
>     end
>   end
> diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
> index 1c20c7a..2c3ccd7 100644
> --- a/spec/spec_helper.rb
> +++ b/spec/spec_helper.rb
> @@ -64,12 +64,8 @@ RSpec.configure do |config|
>           YAML::load(File.open(File.join(File.dirname(__FILE__), "/../examples/aeolus-cli")))
>         end
>       end
> -    Aeolus::CLI::BaseCommand.class_eval do
> -      def handle_exception(e)
> -        raise e
> -      end
> -    end
>     end
> +
>     config.before(:each) do
>       @output = double('output')
>       @stdout_orig = $stdout
> diff --git a/spec/vcr/cassettes/command/base_command/invalid_credentials.yml b/spec/vcr/cassettes/command/base_command/invalid_credentials.yml
> new file mode 100644
> index 0000000..529af9b
> --- /dev/null
> +++ b/spec/vcr/cassettes/command/base_command/invalid_credentials.yml
> @@ -0,0 +1,34 @@
> +---
> +- !ruby/struct:VCR::HTTPInteraction
> +  request: !ruby/struct:VCR::Request
> +    method: :get
> +    uri: https://admin:wrong_password@localhost:443/conductor/api/provider_accounts.xml
> +    body:
> +    headers:
> +      accept-language:
> +      - en
> +      accept:
> +      - application/xml
> +  response: !ruby/struct:VCR::Response
> +    status: !ruby/struct:VCR::ResponseStatus
> +      code: 401
> +      message: Unauthorized
> +    headers:
> +      x-ua-compatible:
> +      - IE=Edge,chrome=1
> +      content-type:
> +      - application/xml; charset=utf-8
> +      server:
> +      - thin 1.2.11 codename Bat-Shit Crazy
> +      date:
> +      - Wed, 14 Dec 2011 11:03:57 GMT
> +      x-runtime:
> +      - "0.159056"
> +      set-cookie:
> +      - _session_id=BAh7ByIPc2Vzc2lvbl9pZCIlZjJkNGVmNTZiYTQ2MTRlZGQyZDdlYjliODFjYmE5NmYiEGJyZWFkY3J1bWJzWwA%3D--18809bd7ff24d210f991c9d382b9aa27804aa043; path=/; HttpOnly
> +      cache-control:
> +      - no-cache
> +      transfer-encoding:
> +      - chunked
> +    body: " "
> +    http_version: "1.1"
> \ No newline at end of file




More information about the aeolus-devel mailing list