Skip to content

Commit

Permalink
raise error when non success response is returned
Browse files Browse the repository at this point in the history
  • Loading branch information
bartes committed Apr 14, 2018
1 parent 1fcc28e commit 375ca61
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master

**Enhancements:**

- [#122](github.com/castle/castle-ruby/pull/121) verify success response in `impersonate` responses

## 3.4.2 (2018-02-26)

**Features:**
Expand Down
4 changes: 3 additions & 1 deletion lib/castle/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,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::ApiError, 'Impersonate request failed' unless response[:success]
end
end

def disable_tracking
Expand Down
16 changes: 13 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,26 @@
{ 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 do
expect { client.impersonate(options) }.to raise_error(Castle::ApiError)
end
end
end

describe 'identify' do
Expand Down

0 comments on commit 375ca61

Please sign in to comment.