Skip to content

Commit

Permalink
Merge pull request #121 from castle/impersonation_error
Browse files Browse the repository at this point in the history
impersonate  - raise error when non success response is returned
  • Loading branch information
nijikon authored Apr 18, 2018
2 parents 135d472 + 6b0007a commit 949eb9a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
**Enhancements:**

- [#122](github.com/castle/castle-ruby/pull/122) `X-Castle-Client-Id` takes precedence over `cid` from `cookies`
- [#121](github.com/castle/castle-ruby/pull/121) raise Castle::ImpersonationFailed when impersonation request failed

## 3.4.2 (2018-02-26)

Expand Down
4 changes: 3 additions & 1 deletion lib/castle/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def impersonate(options = {})
options = Castle::Utils.deep_symbolize_keys(options || {})
add_timestamp_if_necessary(options)
command = Castle::Commands::Impersonate.new(@context).build(options)
@api.request(command)
@api.request(command).tap do |response|
raise Castle::ImpersonationFailed unless response[:success]
end
end

def disable_tracking
Expand Down
3 changes: 3 additions & 0 deletions lib/castle/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ class InvalidParametersError < Castle::ApiError; end
class UnauthorizedError < Castle::ApiError; end
# all internal server errors
class InternalServerError < Castle::ApiError; end

# impersonation command failed
class ImpersonationFailed < Castle::ApiError; end
end
14 changes: 11 additions & 3 deletions spec/lib/castle/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@
let(:time_now) { Time.now }
let(:time_auto) { time_now.utc.iso8601(3) }
let(:time_user) { (Time.now - 10_000).utc.iso8601(3) }
let(:response_body) { {}.to_json }

before do
Timecop.freeze(time_now)
stub_const('Castle::VERSION', '2.2.0')
stub_request(:any, /api.castle.io/).with(
basic_auth: ['', 'secret']
).to_return(status: 200, body: '{}', headers: {})
).to_return(status: 200, body: response_body, headers: {})
end

after { Timecop.return }
Expand Down Expand Up @@ -79,17 +80,24 @@
{ user_id: '1234', timestamp: time_auto, sent_at: time_auto,
impersonator: impersonator, context: context }
end
let(:response_body) { { success: true }.to_json }
let(:options) { { user_id: '1234', impersonator: impersonator } }

before { client.impersonate(options) }

context 'when used with symbol keys' do
before { client.impersonate(options) }

it do
assert_requested :post, 'https://api.castle.io/v1/impersonate', times: 1 do |req|
JSON.parse(req.body) == JSON.parse(request_body.to_json)
end
end
end

context 'when request is not successful' do
let(:response_body) { {}.to_json }

it { expect { client.impersonate(options) }.to raise_error(Castle::ImpersonationFailed) }
end
end

describe 'identify' do
Expand Down

0 comments on commit 949eb9a

Please sign in to comment.