From 9b77460dbfcff1cce043ab91d5f2521fcd6c744b Mon Sep 17 00:00:00 2001 From: Neer Friedman Date: Tue, 5 Jan 2016 16:43:50 +0200 Subject: [PATCH] Applicaster::Accounts.find_user_by_id will not raise on 404 --- lib/applicaster/accounts.rb | 2 ++ spec/lib/applicaster/accounts_spec.rb | 32 ++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/applicaster/accounts.rb b/lib/applicaster/accounts.rb index 2132e3f..1f89c00 100644 --- a/lib/applicaster/accounts.rb +++ b/lib/applicaster/accounts.rb @@ -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) diff --git a/spec/lib/applicaster/accounts_spec.rb b/spec/lib/applicaster/accounts_spec.rb index fe7e6c1..497d93d 100644 --- a/spec/lib/applicaster/accounts_spec.rb +++ b/spec/lib/applicaster/accounts_spec.rb @@ -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