Skip to content

Commit

Permalink
Merge pull request #5 from applicaster/add-404-rescue-on-find-user
Browse files Browse the repository at this point in the history
Applicaster::Accounts.find_user_by_id will not raise on 404
  • Loading branch information
vitalis committed Jan 5, 2016
2 parents f260ee5 + 9b77460 commit 45c5101
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/applicaster/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def accounts

def find_user_by_id(id)
self.class.user_by_id_and_token(id, client_credentials_token.token)
rescue Faraday::ResourceNotFound
nil
end

def connection(*args)
Expand Down
32 changes: 26 additions & 6 deletions spec/lib/applicaster/accounts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,36 @@ def return_value
end

describe "#find_user_by_id" do
let(:user_id) { 11 }

before do
stub_client_credentials_request
stub_user_show_request(user_id, "client-credentials-token")
end

it "returns User object" do
expect(accounts_service.find_user_by_id(user_id))
.to be_kind_of(Applicaster::Accounts::User)
context "when accounts service returns 200" do
let(:user_id) { 11 }

before do
stub_user_show_request(user_id, "client-credentials-token")
end

it "returns User object" do
expect(accounts_service.find_user_by_id(user_id))
.to be_kind_of(Applicaster::Accounts::User)
end
end

context "when accounts service returns 404" do
let(:user_id) { "wrong-id" }

before do
stub_request(:get, "https://#{accounts_host}/api/v1/users/#{user_id}.json").
with(query: { access_token: "client-credentials-token" }).
to_return(status: 404, body: "")
end

it "doesn't raise" do
expect{ accounts_service.find_user_by_id(user_id) }
.to_not raise_error
end
end
end

Expand Down

0 comments on commit 45c5101

Please sign in to comment.