Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added init which returns definitly count of domains #1969

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion app/controllers/api/v1/registrant/domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Registrant
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
Expand Down Expand Up @@ -57,10 +59,34 @@ def current_user_domains_total_count
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
Expand Down
46 changes: 46 additions & 0 deletions test/integration/api/v1/registrant/domains_test.rb
Original file line number Diff line number Diff line change
@@ -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