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/"
|
- "bin/"
|
||||||
- "config/"
|
- "config/"
|
||||||
- "db/"
|
- "db/"
|
||||||
- "app/controllers/api/v1/registrant/companies_controller.rb"
|
|
||||||
- "lib/core_monkey_patches/"
|
- "lib/core_monkey_patches/"
|
||||||
- "lib/daemons/"
|
- "lib/daemons/"
|
||||||
- "lib/gem_monkey_patches/"
|
- "lib/gem_monkey_patches/"
|
||||||
|
|
3
Gemfile
3
Gemfile
|
@ -68,7 +68,8 @@ gem 'pdfkit'
|
||||||
gem 'jquery-ui-rails', '5.0.5'
|
gem 'jquery-ui-rails', '5.0.5'
|
||||||
gem 'airbrake'
|
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 'e_invoice', github: 'internetee/e_invoice', branch: :master
|
||||||
gem 'lhv', github: 'internetee/lhv', branch: 'master'
|
gem 'lhv', github: 'internetee/lhv', branch: 'master'
|
||||||
gem 'domain_name'
|
gem 'domain_name'
|
||||||
|
|
|
@ -4,21 +4,39 @@ module Api
|
||||||
module V1
|
module V1
|
||||||
module Registrant
|
module Registrant
|
||||||
class CompaniesController < ::Api::V1::Registrant::BaseController
|
class CompaniesController < ::Api::V1::Registrant::BaseController
|
||||||
|
MAX_LIMIT = 200
|
||||||
|
MIN_OFFSET = 0
|
||||||
|
|
||||||
def index
|
def index
|
||||||
limit = params[:limit] || 200
|
result = error_result('limit') if limit > MAX_LIMIT || limit < 1
|
||||||
offset = params[:offset] || 0
|
result = error_result('offset') if offset < MIN_OFFSET
|
||||||
|
result ||= companies_result(limit, offset)
|
||||||
|
|
||||||
if limit.to_i > 200 || limit.to_i < 1
|
render result
|
||||||
render(json: { errors: [{ limit: ['parameter is out of range'] }] },
|
end
|
||||||
status: :bad_request) && return
|
|
||||||
end
|
|
||||||
|
|
||||||
if offset.to_i.negative?
|
def current_user_companies
|
||||||
render(json: { errors: [{ offset: ['parameter is out of range'] }] },
|
current_registrant_user.companies
|
||||||
status: :bad_request) && return
|
rescue CompanyRegister::NotAvailableError
|
||||||
end
|
[]
|
||||||
|
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|
|
serialized_companies = @companies.map do |item|
|
||||||
country_code = current_registrant_user.country.alpha3
|
country_code = current_registrant_user.country.alpha3
|
||||||
|
@ -26,14 +44,7 @@ module Api
|
||||||
country_code: country_code)
|
country_code: country_code)
|
||||||
serializer.to_json
|
serializer.to_json
|
||||||
end
|
end
|
||||||
|
{ json: { companies: serialized_companies }, status: status }
|
||||||
render json: serialized_companies
|
|
||||||
end
|
|
||||||
|
|
||||||
def current_user_companies
|
|
||||||
current_registrant_user.companies
|
|
||||||
rescue CompanyRegister::NotAvailableError
|
|
||||||
nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,21 +10,29 @@ class RegistrantApiCompaniesTest < ApplicationIntegrationTest
|
||||||
@auth_headers = { 'HTTP_AUTHORIZATION' => auth_token }
|
@auth_headers = { 'HTTP_AUTHORIZATION' => auth_token }
|
||||||
end
|
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')
|
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 },
|
get '/api/v1/registrant/companies', params: { 'limit' => 1, 'offset' => 0 },
|
||||||
headers: @auth_headers
|
headers: @auth_headers
|
||||||
response_json = JSON.parse(response.body, symbolize_names: true)
|
response_json = JSON.parse(response.body, symbolize_names: true)
|
||||||
assert_equal(200, response.status)
|
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
|
get '/api/v1/registrant/companies', headers: @auth_headers
|
||||||
response_json = JSON.parse(response.body, symbolize_names: true)
|
response_json = JSON.parse(response.body, symbolize_names: true)
|
||||||
assert_equal(@user.companies.size, response_json.count)
|
assert_equal(@user.companies.size, response_json.count)
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def auth_token
|
def auth_token
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue