From c43ae63521a6ff800a2894d51652c85e8cf42fa3 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Tue, 13 Oct 2020 12:52:40 +0500 Subject: [PATCH] Fix output format & format test --- .codeclimate.yml | 1 - Gemfile | 3 +- .../api/v1/registrant/companies_controller.rb | 49 ++++++++++++------- .../registrant_api_companies_test.rb | 14 ++++-- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/.codeclimate.yml b/.codeclimate.yml index f7ff3bab1..2bc90b200 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -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/" diff --git a/Gemfile b/Gemfile index 04ec5f13b..5b4091f4b 100644 --- a/Gemfile +++ b/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' diff --git a/app/controllers/api/v1/registrant/companies_controller.rb b/app/controllers/api/v1/registrant/companies_controller.rb index f8f2554c0..b11c06909 100644 --- a/app/controllers/api/v1/registrant/companies_controller.rb +++ b/app/controllers/api/v1/registrant/companies_controller.rb @@ -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 diff --git a/test/integration/api/registrant/registrant_api_companies_test.rb b/test/integration/api/registrant/registrant_api_companies_test.rb index 20b9f3896..b08383796 100644 --- a/test/integration/api/registrant/registrant_api_companies_test.rb +++ b/test/integration/api/registrant/registrant_api_companies_test.rb @@ -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