mirror of
https://github.com/internetee/registry.git
synced 2025-06-10 22:54:47 +02:00
Fix output format & format test
This commit is contained in:
parent
c79042f789
commit
c43ae63521
4 changed files with 43 additions and 24 deletions
|
@ -32,7 +32,6 @@ exclude_patterns:
|
|||
- "bin/"
|
||||
- "config/"
|
||||
- "db/"
|
||||
- "app/controllers/api/v1/registrant/companies_controller.rb"
|
||||
- "lib/core_monkey_patches/"
|
||||
- "lib/daemons/"
|
||||
- "lib/gem_monkey_patches/"
|
||||
|
|
3
Gemfile
3
Gemfile
|
@ -68,7 +68,8 @@ gem 'pdfkit'
|
|||
gem 'jquery-ui-rails', '5.0.5'
|
||||
gem 'airbrake'
|
||||
|
||||
gem 'company_register', github: 'internetee/company_register', branch: '1708-registrant-companies-endpoint'
|
||||
gem 'company_register', github: 'internetee/company_register',
|
||||
branch: '1708-registrant-companies-endpoint'
|
||||
gem 'e_invoice', github: 'internetee/e_invoice', branch: :master
|
||||
gem 'lhv', github: 'internetee/lhv', branch: 'master'
|
||||
gem 'domain_name'
|
||||
|
|
|
@ -4,21 +4,39 @@ module Api
|
|||
module V1
|
||||
module Registrant
|
||||
class CompaniesController < ::Api::V1::Registrant::BaseController
|
||||
MAX_LIMIT = 200
|
||||
MIN_OFFSET = 0
|
||||
|
||||
def index
|
||||
limit = params[:limit] || 200
|
||||
offset = params[:offset] || 0
|
||||
result = error_result('limit') if limit > MAX_LIMIT || limit < 1
|
||||
result = error_result('offset') if offset < MIN_OFFSET
|
||||
result ||= companies_result(limit, offset)
|
||||
|
||||
if limit.to_i > 200 || limit.to_i < 1
|
||||
render(json: { errors: [{ limit: ['parameter is out of range'] }] },
|
||||
status: :bad_request) && return
|
||||
end
|
||||
render result
|
||||
end
|
||||
|
||||
if offset.to_i.negative?
|
||||
render(json: { errors: [{ offset: ['parameter is out of range'] }] },
|
||||
status: :bad_request) && return
|
||||
end
|
||||
def current_user_companies
|
||||
current_registrant_user.companies
|
||||
rescue CompanyRegister::NotAvailableError
|
||||
[]
|
||||
end
|
||||
|
||||
@companies = current_user_companies.drop(offset.to_i).first(limit.to_i)
|
||||
def limit
|
||||
(params[:limit] || MAX_LIMIT).to_i
|
||||
end
|
||||
|
||||
def offset
|
||||
(params[:offset] || MIN_OFFSET).to_i
|
||||
end
|
||||
|
||||
def error_result(attr_name)
|
||||
{ json: { errors: [{ attr_name.to_sym => ['parameter is out of range'] }] },
|
||||
status: :bad_request }
|
||||
end
|
||||
|
||||
def companies_result(limit, offset)
|
||||
@companies = current_user_companies.drop(offset).first(limit)
|
||||
status = @companies.present? ? :ok : :not_found
|
||||
|
||||
serialized_companies = @companies.map do |item|
|
||||
country_code = current_registrant_user.country.alpha3
|
||||
|
@ -26,14 +44,7 @@ module Api
|
|||
country_code: country_code)
|
||||
serializer.to_json
|
||||
end
|
||||
|
||||
render json: serialized_companies
|
||||
end
|
||||
|
||||
def current_user_companies
|
||||
current_registrant_user.companies
|
||||
rescue CompanyRegister::NotAvailableError
|
||||
nil
|
||||
{ json: { companies: serialized_companies }, status: status }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,21 +10,29 @@ class RegistrantApiCompaniesTest < ApplicationIntegrationTest
|
|||
@auth_headers = { 'HTTP_AUTHORIZATION' => auth_token }
|
||||
end
|
||||
|
||||
def test_root_accepts_limit_and_offset_parameters
|
||||
def test_accepts_limit_and_offset_parameters
|
||||
contacts(:william).update!(ident: '1234', ident_type: 'priv', ident_country_code: 'US')
|
||||
assert_equal 4, @user.contacts(representable: false).size
|
||||
|
||||
get '/api/v1/registrant/companies', params: { 'limit' => 1, 'offset' => 0 },
|
||||
headers: @auth_headers
|
||||
response_json = JSON.parse(response.body, symbolize_names: true)
|
||||
assert_equal(200, response.status)
|
||||
assert_equal(1, response_json.count)
|
||||
assert_equal(1, response_json.values.flatten.count)
|
||||
|
||||
get '/api/v1/registrant/companies', headers: @auth_headers
|
||||
response_json = JSON.parse(response.body, symbolize_names: true)
|
||||
assert_equal(@user.companies.size, response_json.count)
|
||||
end
|
||||
|
||||
def test_format
|
||||
contacts(:william).update!(ident: '1234', ident_type: 'priv', ident_country_code: 'US')
|
||||
get '/api/v1/registrant/companies', headers: @auth_headers
|
||||
response_json = JSON.parse(response.body, symbolize_names: true)
|
||||
assert_equal(1, response_json.count)
|
||||
assert response_json.is_a?(Hash)
|
||||
assert_equal(:companies, response_json.keys.first)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def auth_token
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue