From d36ef24170d40db35b0926adba656189df215d12 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Wed, 15 Sep 2021 21:55:11 +0300 Subject: [PATCH] created method for adding nameservers --- .../v1/registrar/nameservers_controller.rb | 21 ++++++++---- app/models/registrar.rb | 32 +++++++++++++++++++ .../repp/v1/registrar/nameservers_test.rb | 23 +++++++++++++ 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/app/controllers/repp/v1/registrar/nameservers_controller.rb b/app/controllers/repp/v1/registrar/nameservers_controller.rb index 0d03bf2a9..5c53b62f2 100644 --- a/app/controllers/repp/v1/registrar/nameservers_controller.rb +++ b/app/controllers/repp/v1/registrar/nameservers_controller.rb @@ -8,7 +8,7 @@ module Repp desc 'bulk nameserver change' param :data, Hash, required: true, desc: 'Object holding nameserver changes' do param :type, String, required: true, desc: 'Always set as "nameserver"' - param :id, String, required: true, desc: 'Hostname of replacable nameserver' + param :id, String, required: false, desc: 'Hostname of replacable nameserver' param :domains, Array, required: false, desc: 'Array of domain names qualified for ' \ 'nameserver replacement' param :attributes, Hash, required: true, desc: 'Object holding new nameserver values' do @@ -17,11 +17,16 @@ module Repp param :ipv6, Array, of: String, required: false, desc: 'Array of fixed IPv6 addresses' end end + def update - affected, errored = current_user.registrar - .replace_nameservers(hostname, - hostname_params[:data][:attributes], - domains: domains_from_params) + affected, errored = if hostname.present? + current_user.registrar.replace_nameservers(hostname, + hostname_params[:data][:attributes], + domains: domains_from_params) + else + current_user.registrar.add_nameservers(hostname_params[:data][:attributes], + domains: domains_from_params) + end render_success(data: data_format_for_success(affected, errored)) rescue ActiveRecord::RecordInvalid => e @@ -47,7 +52,7 @@ module Repp end def hostname_params - params.require(:data).require(%i[type id]) + params.require(:data).require(%i[type]) params.require(:data).require(:attributes).require([:hostname]) params.permit(data: [ @@ -58,10 +63,12 @@ module Repp end def hostname - hostname_params[:data][:id] + hostname_params[:data][:id] || nil end def verify_nameserver_existance + return true if hostname.nil? + current_user.registrar.nameservers.find_by!(hostname: hostname) end end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 18c549e41..efb073e04 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -43,6 +43,7 @@ class Registrar < ApplicationRecord after_commit :update_whois_records def update_whois_records return true unless changed? && (changes.keys & WHOIS_TRIGGERS).present? + RegenerateRegistrarWhoisesJob.perform_later id end @@ -175,6 +176,36 @@ class Registrar < ApplicationRecord end end + def add_nameservers(new_attributes, domains: []) + transaction do + domain_scope = domains.dup + domain_list = [] + failed_list = [] + + return if domains.empty? + + domain_scope.each do |domain_name| + domain = self.domains.find_by('name = ? OR name_puny = ?', domain_name, domain_name) + + if !domain.present? || domain_not_updatable?(hostname: new_attributes[:hostname], domain: domain) + failed_list << domain_name + next + end + + new_nameserver = Nameserver.new + new_nameserver.domain = domain + new_nameserver.attributes = new_attributes + new_nameserver.save! + + domain_scope.delete_if { |i| i == domain.name || i == domain.name_puny } + domain_list << domain_name + end + + self.domains.where(name: domain_list).find_each(&:update_whois_record) if domain_list.any? + [domain_list.uniq.sort, (domain_scope + failed_list).uniq.sort] + end + end + def vat_country=(country) self.address_country_code = country.alpha2 end @@ -198,6 +229,7 @@ class Registrar < ApplicationRecord def billing_email return contact_email if self[:billing_email].blank? + self[:billing_email] end diff --git a/test/integration/repp/v1/registrar/nameservers_test.rb b/test/integration/repp/v1/registrar/nameservers_test.rb index 01f30c813..11216375d 100644 --- a/test/integration/repp/v1/registrar/nameservers_test.rb +++ b/test/integration/repp/v1/registrar/nameservers_test.rb @@ -34,6 +34,29 @@ class ReppV1RegistrarNameserversTest < ActionDispatch::IntegrationTest assert json[:data][:affected_domains].include? 'shop.test' end + def test_add_nameserver_values + nameserver = nameservers(:shop_ns1) + payload = { + "data": { + "type": 'nameserver', + "domains": ['shop.test'], + "attributes": { + "hostname": "#{nameserver.hostname}.testtest", + "ipv4": ['2.2.2.2'] + } + } + } + + put '/repp/v1/registrar/nameservers', headers: @auth_headers, params: payload + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal 1000, json[:code] + assert_equal 'Command completed successfully', json[:message] + assert_equal({ hostname: "#{nameserver.hostname}.testtest", ipv4: ['2.2.2.2'] }, json[:data][:attributes]) + assert json[:data][:affected_domains].include? 'shop.test' + end + def test_fails_to_update_if_prohibited domain = domains(:shop) domain.update(statuses: [DomainStatus::CLIENT_UPDATE_PROHIBITED])