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

View file

@ -44,7 +44,10 @@ class Registrar
parsed_response = JSON.parse(response.body, symbolize_names: true)
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
else
@api_errors = parsed_response[:errors]

View file

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

View file

@ -3,3 +3,4 @@ en:
nameservers:
update:
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",
"ipv4": ["192.0.2.1", "192.0.2.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
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',
attributes: { hostname: 'ns55.bestnames.test',
ipv4: ['192.0.2.55'],
ipv6: ['2001:db8::55'] } } }
put '/repp/v1/registrar/nameservers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key }
assert_response 200
@ -53,7 +54,8 @@ class APINameserversPutTest < ActionDispatch::IntegrationTest
id: 'ns55.bestnames.test',
attributes: { hostname: 'ns55.bestnames.test',
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)
end

View file

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