Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Raise UnlicensedFeatureError when we receive a HTTP 402 #65

Merged
merged 1 commit into from
Jan 31, 2017
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
1 change: 1 addition & 0 deletions lib/ansible_tower_client/base_models/job_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def survey_spec
spec_url = related['survey_spec']
return nil unless spec_url
api.get(spec_url).body
rescue AnsibleTowerClient::UnlicensedFeatureError
end

def survey_spec_hash
Expand Down
3 changes: 2 additions & 1 deletion lib/ansible_tower_client/exception.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module AnsibleTowerClient
class Error < Exception; end
class ClientError < Error; end
class ConnectionError < Error; end
class NoMethodError < Error; end
class ClientError < Error; end
class UnlicensedFeatureError < Error; end
end
2 changes: 2 additions & 0 deletions lib/ansible_tower_client/middleware/raise_tower_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class RaiseTowerError < Faraday::Response::Middleware

def on_complete(env)
case env[:status]
when 402
raise AnsibleTowerClient::UnlicensedFeatureError
when 404
raise Faraday::Error::ResourceNotFound, response_values(env)
when 407
Expand Down
7 changes: 6 additions & 1 deletion spec/middleware/raise_tower_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
describe AnsibleTowerClient::Middleware::RaiseTowerError do
context "Faraday::Error" do
let(:env_400) { MockEnv.new('missing these attributes', 400) }
let(:env_402) { MockEnv.new('missing these attributes', 402) }
let(:env_404) { MockEnv.new('missing these attributes', 404) }
let(:env_407) { MockEnv.new('missing these attributes', 407) }

let(:error) { AnsibleTowerClient::Middleware::RaiseTowerError.new }

it "raises ClientError and returns the body message with a status 404" do
it "raises ClientError and returns the body message with a status 400" do
expect { error.on_complete(env_400) }.to raise_error(AnsibleTowerClient::ClientError, "missing these attributes")
end

it "raises UnlicensedFeatureError with a status 402" do
expect { error.on_complete(env_402) }.to raise_error(AnsibleTowerClient::UnlicensedFeatureError)
end

it "raises ResourceNotFound exception with a status 404" do
expect { error.on_complete(env_404) }.to raise_error(Faraday::Error::ResourceNotFound)
end
Expand Down
7 changes: 7 additions & 0 deletions spec/support/shared_examples/job_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
expect(described_class.new(api, raw_instance_no_survey).survey_spec).to be_nil
end
end

describe "unlicensed" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdunne - This looks great. It would be great to have some testing around the middleware as well.

it "returns a survey spec" do
expect(api).to receive(:get).and_raise(AnsibleTowerClient::UnlicensedFeatureError)
expect(described_class.new(api, raw_instance).survey_spec).to be_nil
end
end
end

shared_examples_for "JobTemplate#survey_spec_hash" do
Expand Down