Merge pull request #1969 from internetee/show-all-domains-by-default-to-users-with-less-than-3000-associated-domains

added init which returns definitly count of domains
This commit is contained in:
Timo Võhmar 2021-05-19 17:31:27 +03:00 committed by GitHub
commit 670d42ea5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 1 deletions

View file

@ -6,6 +6,8 @@ module Api
class DomainsController < ::Api::V1::Registrant::BaseController
before_action :set_tech_flag, only: [:show]
LIMIT_DOMAIN_TOTAL = 3000
def index
limit = params[:limit] || 200
offset = params[:offset] || 0
@ -57,10 +59,34 @@ module Api
end
def current_user_domains
current_registrant_user.domains(admin: params[:tech] != 'true')
init_count_of_domains
rescue CompanyRegister::NotAvailableError
init_count_of_direct_domains
end
def init_count_of_direct_domains
if params[:tech] == 'init'
if current_user_domains_total_count < LIMIT_DOMAIN_TOTAL
return current_registrant_user.direct_domains(admin: false)
end
current_registrant_user.direct_domains(admin: true)
end
current_registrant_user.direct_domains(admin: params[:tech] != 'true')
end
def init_count_of_domains
if params[:tech] == 'init'
if current_user_domains_total_count < LIMIT_DOMAIN_TOTAL
return current_registrant_user.domains(admin: false)
end
current_registrant_user.domains(admin: true)
end
current_registrant_user.domains(admin: params[:tech] != 'true')
end
end
end
end

View file

@ -0,0 +1,46 @@
require 'test_helper'
require 'auth_token/auth_token_creator'
CompanyRegisterClientStub = Struct.new(:any_method) do
def representation_rights(citizen_personal_code:, citizen_country_code:)
raise CompanyRegister::NotAvailableError
end
end
class RegistrantApiV1DomainsTest < ActionDispatch::IntegrationTest
setup do
@user = users(:registrant)
@registrar = registrars(:bestnames)
@contact = contacts(:john)
end
def test_get_default_counts_of_domains
get api_v1_registrant_domains_path + "?tech=init", as: :json,
headers: { 'HTTP_AUTHORIZATION' => auth_token }
assert_response :ok
response_json = JSON.parse(response.body)
assert_equal response_json['total'], 4
assert_equal response_json['count'], 4
end
def test_get_default_counts_of_direct_domains
CompanyRegister::Client.stub(:new, CompanyRegisterClientStub.new) do
get api_v1_registrant_domains_path + "?tech=init", as: :json,
headers: { 'HTTP_AUTHORIZATION' => auth_token }
end
response_json = JSON.parse(response.body)
assert_equal response_json['total'], 4
assert_equal response_json['count'], 4
end
private
def auth_token
token_creator = AuthTokenCreator.create_with_defaults(@user)
hash = token_creator.token_in_hash
"Bearer #{hash[:access_token]}"
end
end