Skip to content

Commit

Permalink
Change error handling to more humane
Browse files Browse the repository at this point in the history
  • Loading branch information
yulgolem committed Oct 15, 2020
1 parent ce93425 commit 239b868
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
8 changes: 4 additions & 4 deletions app/controllers/api/v1/registrant/companies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def index
end

def current_user_companies
current_registrant_user.companies
[:ok, current_registrant_user.companies]
rescue CompanyRegister::NotAvailableError
[]
[:service_unavailable, []]
end

def limit
Expand All @@ -35,8 +35,8 @@ def error_result(attr_name)
end

def companies_result(limit, offset)
@companies = current_user_companies.drop(offset).first(limit)
status = @companies.present? ? :ok : :not_found
status, all_companies = current_user_companies
@companies = all_companies.drop(offset).first(limit)

serialized_companies = @companies.map do |item|
country_code = current_registrant_user.country.alpha3
Expand Down
27 changes: 27 additions & 0 deletions test/integration/api/registrant/registrant_api_companies_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$VERBOSE=nil
require 'test_helper'
require 'auth_token/auth_token_creator'

Expand All @@ -10,6 +11,11 @@ def setup
@auth_headers = { 'HTTP_AUTHORIZATION' => auth_token }
end

def teardown

CompanyRegister.const_set(:Client, CompanyRegisterClientStub)
end

def test_accepts_limit_and_offset_parameters
contacts(:william).update!(ident: '1234', ident_type: 'priv', ident_country_code: 'US')

Expand All @@ -33,6 +39,18 @@ def test_format
assert_equal(:companies, response_json.keys.first)
end

def test_status_if_nil_result
contacts(:john).update!(ident: '12344321', ident_type: 'priv', ident_country_code: 'US')

CompanyRegister.const_set(:Client, CompanyRegisterClientZeroStub)

get '/api/v1/registrant/companies', headers: @auth_headers
response_json = JSON.parse(response.body, symbolize_names: true)
assert_equal(1, response_json.count)
assert_equal(200, response.status)
assert_equal(:companies, response_json.keys.first)
end

private

def auth_token
Expand All @@ -41,3 +59,12 @@ def auth_token
"Bearer #{hash[:access_token]}"
end
end

class CompanyRegisterClientZeroStub
Company = Struct.new(:registration_number, :company_name)

def representation_rights(citizen_personal_code:, citizen_country_code:)
[]
end
end

0 comments on commit 239b868

Please sign in to comment.