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
2 changes: 2 additions & 0 deletions lib/xpm_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class AccessTokenExpired < Unauthorized; end

class Forbidden < Error; end

class AuthenticationUnsuccessful < Forbidden; end

class NotAvailable < Error; end

class ConnectionFailed < Error; end
Expand Down
2 changes: 2 additions & 0 deletions lib/xpm_ruby/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def handle_response(response)
end
when 403 # this can happen with a bad xero_tenant_id
detail = JSON.parse(response.body)["Detail"]
raise AuthenticationUnsuccessful.new(detail) if detail == "AuthenticationUnsuccessful"

raise Forbidden.new(detail)
when 500
raise InternalServerError.new(response.reason_phrase)
Expand Down
2 changes: 1 addition & 1 deletion lib/xpm_ruby/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module XpmRuby
VERSION = "0.4.0".freeze
VERSION = "0.4.1".freeze
end
32 changes: 28 additions & 4 deletions spec/xpm_ruby/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,28 @@ module XpmRuby
VCR.use_cassette("xpm_ruby/connection/get/bad_tenant") do
connection = Connection.new(access_token: access_token, xero_tenant_id: "bad_tenant")

expect { connection.get(endpoint: "staff.api/list") }.to raise_error(XpmRuby::Forbidden, /AuthenticationUnsuccessful/)
expect { connection.get(endpoint: "staff.api/list") }
.to raise_error(XpmRuby::AuthenticationUnsuccessful, /AuthenticationUnsuccessful/)
end
end
end

context "when XPM returns another forbidden detail" do
before(:each) do
response = instance_double(
Faraday::Response,
status: 403,
body: { Detail: "InsufficientPermissions" }.to_json
)
allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(response)
end

it "raises the generic forbidden error" do
connection = Connection.new(access_token: access_token, xero_tenant_id: xero_tenant_id)

expect { connection.get(endpoint: "staff.api/list") }.to raise_error do |error|
expect(error.class).to eq(XpmRuby::Forbidden)
expect(error.message).to eq("InsufficientPermissions")
end
end
end
Expand Down Expand Up @@ -113,7 +134,8 @@ module XpmRuby
VCR.use_cassette("xpm_ruby/connection/post/bad_tenant") do
connection = Connection.new(access_token: access_token, xero_tenant_id: "bad_tenant")

expect { connection.post(endpoint: "job.api/add", data: xml_string) }.to raise_error(XpmRuby::Forbidden, /AuthenticationUnsuccessful/)
expect { connection.post(endpoint: "job.api/add", data: xml_string) }
.to raise_error(XpmRuby::AuthenticationUnsuccessful, /AuthenticationUnsuccessful/)
end
end
end
Expand Down Expand Up @@ -149,7 +171,8 @@ module XpmRuby
VCR.use_cassette("xpm_ruby/connection/put/bad_tenant") do
connection = Connection.new(access_token: access_token, xero_tenant_id: "bad_tenant")

expect { connection.put(endpoint: "job.api/update", data: xml_string) }.to raise_error(XpmRuby::Forbidden, /AuthenticationUnsuccessful/)
expect { connection.put(endpoint: "job.api/update", data: xml_string) }
.to raise_error(XpmRuby::AuthenticationUnsuccessful, /AuthenticationUnsuccessful/)
end
end
end
Expand Down Expand Up @@ -181,7 +204,8 @@ module XpmRuby
VCR.use_cassette("xpm_ruby/connection/delete/bad_tenant") do
connection = Connection.new(access_token: access_token, xero_tenant_id: "bad_tenant")

expect { connection.delete(endpoint: "client.api/contact", id: contact_id) }.to raise_error(XpmRuby::Forbidden, /AuthenticationUnsuccessful/)
expect { connection.delete(endpoint: "client.api/contact", id: contact_id) }
.to raise_error(XpmRuby::AuthenticationUnsuccessful, /AuthenticationUnsuccessful/)
end
end
end
Expand Down
Loading