Merge pull request #1749 from internetee/1739-ns-bulk-change-test-and-whois-update

Registrar: Update WHOIS after bulk nameserver change
This commit is contained in:
Timo Võhmar 2021-01-06 20:14:58 +02:00 committed by GitHub
commit 18790071c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 102 additions and 23 deletions

View file

@ -48,18 +48,25 @@ class Registrar
parsed_response = JSON.parse(response.body, symbolize_names: true)
if response.code == '200'
notices = [t('.replaced')]
notices << "#{t('.affected_domains')}: " \
"#{parsed_response[:data][:affected_domains].join(', ')}"
flash[:notice] = notices.join(', ')
redirect_to registrar_domains_url
redirect_to(registrar_domains_url,
flash: { notice: compose_notice_message(parsed_response) })
else
@api_errors = parsed_response[:message]
render file: 'registrar/bulk_change/new', locals: { active_tab: :nameserver }
end
end
def compose_notice_message(res)
notices = ["#{t('.replaced')}. #{t('.affected_domains')}: " \
"#{res[:data][:affected_domains].join(', ')}"]
if res[:data][:skipped_domains]
notices << "#{t('.skipped_domains')}: #{res[:data][:skipped_domains].join(', ')}"
end
notices.join(', ')
end
def domain_list_from_csv
return [] if params[:puny_file].blank?

View file

@ -5,25 +5,31 @@ module Repp
before_action :verify_nameserver_existance, only: %i[update]
def update
domains = params[:data][:domains] || []
affected = current_user.registrar
.replace_nameservers(hostname,
hostname_params[:data][:attributes],
domains: domains)
affected, errored = current_user.registrar
.replace_nameservers(hostname,
hostname_params[:data][:attributes],
domains: domains_from_params)
render_success(data: data_format_for_success(affected))
render_success(data: data_format_for_success(affected, errored))
rescue ActiveRecord::RecordInvalid => e
handle_errors(e.record)
end
private
def data_format_for_success(affected_domains)
def domains_from_params
return [] unless params[:data][:domains]
params[:data][:domains].map(&:downcase)
end
def data_format_for_success(affected_domains, errored_domains)
{
type: 'nameserver',
id: params[:data][:attributes][:hostname],
attributes: params[:data][:attributes],
affected_domains: affected_domains,
skipped_domains: errored_domains,
}
end

View file

@ -144,22 +144,33 @@ class Registrar < ApplicationRecord
# Audit log is needed, therefore no raw SQL
def replace_nameservers(hostname, new_attributes, domains: [])
transaction do
domain_scope = domains.dup
domain_list = []
failed_list = []
nameservers.where(hostname: hostname).find_each do |original_nameserver|
next unless domains.include?(original_nameserver.domain.name_puny) || domains.empty?
nameservers.where(hostname: hostname).find_each do |origin|
idn = origin.domain.name
puny = origin.domain.name_puny
next unless domains.include?(idn) || domains.include?(puny) || domains.empty?
if origin.domain.nameservers.where(hostname: new_attributes[:hostname]).any?
failed_list << idn
next
end
new_nameserver = Nameserver.new
new_nameserver.domain = original_nameserver.domain
new_nameserver.domain = origin.domain
new_nameserver.attributes = new_attributes
new_nameserver.save!
domain_list << original_nameserver.domain.name
domain_scope.delete_if { |i| i == idn || i == puny }
domain_list << idn
original_nameserver.destroy!
origin.destroy!
end
domain_list.uniq.sort
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

View file

@ -4,3 +4,4 @@ en:
update:
replaced: Nameserver have been successfully replaced
affected_domains: Affected domains
skipped_domains: Untouched domains

View file

@ -0,0 +1,2 @@
domain_name
shop.test
1 domain_name
2 shop.test

View file

@ -67,7 +67,8 @@ class APINameserversPutTest < ApplicationIntegrationTest
attributes: { hostname: 'ns55.bestnames.test',
ipv4: ['192.0.2.55'],
ipv6: ['2001:db8::55'] },
affected_domains: ["airport.test", "shop.test"] }}),
affected_domains: ["airport.test", "shop.test"],
skipped_domains: [] }}),
JSON.parse(response.body, symbolize_names: true)
end

View file

@ -10,7 +10,7 @@ class ReplaceNameserversTest < ActiveSupport::TestCase
ipv6: '2001:db8::2' }
result = @registrar.replace_nameservers('ns1.bestnames.test', new_attributes)
assert_equal(["airport.test", "shop.test"], result)
assert_equal([["airport.test", "shop.test"], []], result)
end
def test_replace_nameservers_in_bulk_returns_empty_array_for_non_existent_base_nameserver
@ -18,6 +18,22 @@ class ReplaceNameserversTest < ActiveSupport::TestCase
ipv6: '2001:db8::2' }
result = @registrar.replace_nameservers('ns3.bestnames.test', new_attributes)
assert_equal([], result)
assert_equal([[], []], result)
end
def test_replace_nameserver_in_bulk_respects_domain_limit_scope
eligible_domain = domains(:shop)
unscoped_domain = domains(:airport)
new_attributes = { hostname: 'ns-updated1.bestnames.test', ipv4: '192.0.3.1',
ipv6: '2001:db8::2' }
result = @registrar.replace_nameservers('ns1.bestnames.test', new_attributes, domains: ['shop.test'])
assert_equal([["shop.test"], []], result)
unscoped_domain.reload
eligible_domain.reload
assert eligible_domain.nameservers.where(hostname: 'ns1.bestnames.test').empty?
assert unscoped_domain.nameservers.where(hostname: 'ns1.bestnames.test').any?
end
end

View file

@ -18,7 +18,8 @@ class RegistrarAreaNameserverBulkChangeTest < ApplicationSystemTestCase
.to_return(body: { data: {
type: 'nameserver',
id: 'new-ns.bestnames.test',
affected_domains: ["airport.test", "shop.test"]
affected_domains: ["airport.test", "shop.test"],
skipped_domains: []
}
}.to_json, status: 200)
@ -59,4 +60,38 @@ class RegistrarAreaNameserverBulkChangeTest < ApplicationSystemTestCase
assert_field 'ipv4', with: 'ipv4'
assert_field 'ipv6', with: 'ipv6'
end
def test_replaces_nameservers_only_for_scoped_domains
request_body = { data: { type: 'nameserver',
id: 'ns1.bestnames.test',
domains: ['shop.test'],
attributes: { hostname: 'new-ns.bestnames.test',
ipv4: %w[192.0.2.55 192.0.2.56],
ipv6: %w[2001:db8::55 2001:db8::56] } } }
request_stub = stub_request(:put, /registrar\/nameservers/).with(body: request_body,
headers: { 'Content-type' => Mime[:json] },
basic_auth: ['test_goodnames', 'testtest'])
.to_return(body: { data: {
type: 'nameserver',
id: 'new-ns.bestnames.test',
affected_domains: ["shop.test"],
skipped_domains: []}}.to_json, status: 200)
visit registrar_domains_url
click_link 'Bulk change'
click_link 'Nameserver'
fill_in 'Old hostname', with: 'ns1.bestnames.test'
fill_in 'New hostname', with: 'new-ns.bestnames.test'
fill_in 'ipv4', with: "192.0.2.55\n192.0.2.56"
fill_in 'ipv6', with: "2001:db8::55\n2001:db8::56"
attach_file :puny_file, Rails.root.join('test', 'fixtures', 'files', 'valid_domains_for_ns_replacement.csv').to_s
click_on 'Replace nameserver'
assert_requested request_stub
assert_current_path registrar_domains_path
assert_text 'Nameserver have been successfully replaced'
assert_text 'Affected domains: shop.test'
end
end