created method for adding nameservers

This commit is contained in:
dinsmol 2021-09-15 21:55:11 +03:00
parent 5d9692784f
commit d36ef24170
3 changed files with 69 additions and 7 deletions

View file

@ -8,7 +8,7 @@ module Repp
desc 'bulk nameserver change' desc 'bulk nameserver change'
param :data, Hash, required: true, desc: 'Object holding nameserver changes' do param :data, Hash, required: true, desc: 'Object holding nameserver changes' do
param :type, String, required: true, desc: 'Always set as "nameserver"' 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 ' \ param :domains, Array, required: false, desc: 'Array of domain names qualified for ' \
'nameserver replacement' 'nameserver replacement'
param :attributes, Hash, required: true, desc: 'Object holding new nameserver values' do 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' param :ipv6, Array, of: String, required: false, desc: 'Array of fixed IPv6 addresses'
end end
end end
def update def update
affected, errored = current_user.registrar affected, errored = if hostname.present?
.replace_nameservers(hostname, current_user.registrar.replace_nameservers(hostname,
hostname_params[:data][:attributes], hostname_params[:data][:attributes],
domains: domains_from_params) 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)) render_success(data: data_format_for_success(affected, errored))
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
@ -47,7 +52,7 @@ module Repp
end end
def hostname_params def hostname_params
params.require(:data).require(%i[type id]) params.require(:data).require(%i[type])
params.require(:data).require(:attributes).require([:hostname]) params.require(:data).require(:attributes).require([:hostname])
params.permit(data: [ params.permit(data: [
@ -58,10 +63,12 @@ module Repp
end end
def hostname def hostname
hostname_params[:data][:id] hostname_params[:data][:id] || nil
end end
def verify_nameserver_existance def verify_nameserver_existance
return true if hostname.nil?
current_user.registrar.nameservers.find_by!(hostname: hostname) current_user.registrar.nameservers.find_by!(hostname: hostname)
end end
end end

View file

@ -43,6 +43,7 @@ class Registrar < ApplicationRecord
after_commit :update_whois_records after_commit :update_whois_records
def update_whois_records def update_whois_records
return true unless changed? && (changes.keys & WHOIS_TRIGGERS).present? return true unless changed? && (changes.keys & WHOIS_TRIGGERS).present?
RegenerateRegistrarWhoisesJob.perform_later id RegenerateRegistrarWhoisesJob.perform_later id
end end
@ -175,6 +176,36 @@ class Registrar < ApplicationRecord
end end
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) def vat_country=(country)
self.address_country_code = country.alpha2 self.address_country_code = country.alpha2
end end
@ -198,6 +229,7 @@ class Registrar < ApplicationRecord
def billing_email def billing_email
return contact_email if self[:billing_email].blank? return contact_email if self[:billing_email].blank?
self[:billing_email] self[:billing_email]
end end

View file

@ -34,6 +34,29 @@ class ReppV1RegistrarNameserversTest < ActionDispatch::IntegrationTest
assert json[:data][:affected_domains].include? 'shop.test' assert json[:data][:affected_domains].include? 'shop.test'
end 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 def test_fails_to_update_if_prohibited
domain = domains(:shop) domain = domains(:shop)
domain.update(statuses: [DomainStatus::CLIENT_UPDATE_PROHIBITED]) domain.update(statuses: [DomainStatus::CLIENT_UPDATE_PROHIBITED])