Merge pull request #903 from internetee/registry-835

Return affected domains from NS bulk change
This commit is contained in:
Timo Võhmar 2018-07-06 14:35:18 +03:00 committed by GitHub
commit 95d3269698
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 47 additions and 9 deletions

View file

@ -29,7 +29,7 @@ module Repp
} }
begin begin
current_user.registrar.replace_nameservers(hostname, new_attributes) affected_domains = current_user.registrar.replace_nameservers(hostname, new_attributes)
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
error!({ errors: e.record.errors.full_messages.map { |error| { title: error } } }, 400) error!({ errors: e.record.errors.full_messages.map { |error| { title: error } } }, 400)
end end
@ -37,7 +37,8 @@ module Repp
status 200 status 200
@response = { data: { type: 'nameserver', @response = { data: { type: 'nameserver',
id: params[:data][:attributes][:hostname], id: params[:data][:attributes][:hostname],
attributes: params[:data][:attributes] } } attributes: params[:data][:attributes] },
affected_domains: affected_domains }
end end
end end
end end

View file

@ -44,7 +44,10 @@ class Registrar
parsed_response = JSON.parse(response.body, symbolize_names: true) parsed_response = JSON.parse(response.body, symbolize_names: true)
if response.code == '200' if response.code == '200'
flash[:notice] = t '.replaced' notices = [t('.replaced')]
notices << "#{t('.affected_domains')}: #{parsed_response[:affected_domains].join(', ')}"
flash[:notice] = notices
redirect_to registrar_domains_url redirect_to registrar_domains_url
else else
@api_errors = parsed_response[:errors] @api_errors = parsed_response[:errors]

View file

@ -132,14 +132,20 @@ class Registrar < ActiveRecord::Base
# Audit log is needed, therefore no raw SQL # Audit log is needed, therefore no raw SQL
def replace_nameservers(hostname, new_attributes) def replace_nameservers(hostname, new_attributes)
transaction do transaction do
domain_list = []
nameservers.where(hostname: hostname).find_each do |original_nameserver| nameservers.where(hostname: hostname).find_each do |original_nameserver|
new_nameserver = Nameserver.new new_nameserver = Nameserver.new
new_nameserver.domain = original_nameserver.domain new_nameserver.domain = original_nameserver.domain
new_nameserver.attributes = new_attributes new_nameserver.attributes = new_attributes
new_nameserver.save! new_nameserver.save!
domain_list << original_nameserver.domain.name
original_nameserver.destroy! original_nameserver.destroy!
end end
domain_list.uniq.sort
end end
end end

View file

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

View file

@ -34,8 +34,9 @@ Content-Type: application/json
"hostname": "new-ns1.example.com", "hostname": "new-ns1.example.com",
"ipv4": ["192.0.2.1", "192.0.2.2"], "ipv4": ["192.0.2.1", "192.0.2.2"],
"ipv6": ["2001:db8::1", "2001:db8::2"] "ipv6": ["2001:db8::1", "2001:db8::2"]
},
} }
},
"affected_domains": ["example.com", "example.org"]
} }
``` ```

View file

@ -41,11 +41,12 @@ class APINameserversPutTest < ActionDispatch::IntegrationTest
assert_equal nameserver_hash, nameservers(:metro_ns1).reload.attributes assert_equal nameserver_hash, nameservers(:metro_ns1).reload.attributes
end end
def test_returns_new_nameserver_record def test_returns_new_nameserver_record_and_affected_domain
request_params = { format: :json, data: { type: 'nameserver', id: 'ns1.bestnames.test', request_params = { format: :json, data: { type: 'nameserver', id: 'ns1.bestnames.test',
attributes: { hostname: 'ns55.bestnames.test', attributes: { hostname: 'ns55.bestnames.test',
ipv4: ['192.0.2.55'], ipv4: ['192.0.2.55'],
ipv6: ['2001:db8::55'] } } } ipv6: ['2001:db8::55'] } } }
put '/repp/v1/registrar/nameservers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key } put '/repp/v1/registrar/nameservers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key }
assert_response 200 assert_response 200
@ -53,7 +54,8 @@ class APINameserversPutTest < ActionDispatch::IntegrationTest
id: 'ns55.bestnames.test', id: 'ns55.bestnames.test',
attributes: { hostname: 'ns55.bestnames.test', attributes: { hostname: 'ns55.bestnames.test',
ipv4: ['192.0.2.55'], ipv4: ['192.0.2.55'],
ipv6: ['2001:db8::55'] } } }), ipv6: ['2001:db8::55'] }},
affected_domains: ["airport.test", "shop.test"] }),
JSON.parse(response.body, symbolize_names: true) JSON.parse(response.body, symbolize_names: true)
end end

View file

@ -16,8 +16,8 @@ class RegistrarAreaNameserverBulkChangeTest < ActionDispatch::IntegrationTest
basic_auth: ['test_goodnames', 'testtest']) basic_auth: ['test_goodnames', 'testtest'])
.to_return(body: { data: [{ .to_return(body: { data: [{
type: 'nameserver', type: 'nameserver',
id: 'new-ns.bestnames.test' id: 'new-ns.bestnames.test'}],
}] }.to_json, status: 200) affected_domains: ["airport.test", "shop.test"]}.to_json, status: 200)
visit registrar_domains_url visit registrar_domains_url
click_link 'Bulk change' click_link 'Bulk change'
@ -32,6 +32,7 @@ class RegistrarAreaNameserverBulkChangeTest < ActionDispatch::IntegrationTest
assert_requested request_stub assert_requested request_stub
assert_current_path registrar_domains_path assert_current_path registrar_domains_path
assert_text 'Nameserver have been successfully replaced' assert_text 'Nameserver have been successfully replaced'
assert_text 'Affected domains: airport.test, shop.test'
end end
def test_fails_gracefully def test_fails_gracefully

View file

@ -0,0 +1,23 @@
require 'test_helper'
class ReplaceNameserversTest < ActiveSupport::TestCase
def setup
@registrar = registrars(:bestnames)
end
def test_replace_nameservers_in_bulk_returns_sorted_domain_names
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)
assert_equal(["airport.test", "shop.test"], result)
end
def test_replace_nameservers_in_bulk_returns_empty_array_for_non_existent_base_nameserver
new_attributes = { hostname: 'ns-updated1.bestnames.test', ipv4: '192.0.3.1',
ipv6: '2001:db8::2' }
result = @registrar.replace_nameservers('ns3.bestnames.test', new_attributes)
assert_equal([], result)
end
end