Add registrar nameserver replacement UI

#661
This commit is contained in:
Artur Beljajev 2018-02-26 10:30:46 +02:00
parent 18010c6a2e
commit 2db401fd98
11 changed files with 197 additions and 11 deletions

View file

@ -2,7 +2,7 @@ module Repp
class NameserversV1 < Grape::API class NameserversV1 < Grape::API
version 'v1', using: :path version 'v1', using: :path
resource :nameservers do resource 'registrar/nameservers' do
put '/' do put '/' do
params do params do
requires :data, type: Hash do requires :data, type: Hash do

View file

@ -0,0 +1,59 @@
class Registrar
class RegistrarNameserversController < DeppController
def edit
authorize! :manage, :repp
end
def update
authorize! :manage, :repp
ipv4 = params[:ipv4].split("\r\n")
ipv6 = params[:ipv6].split("\r\n")
uri = URI.parse("#{ENV['repp_url']}registrar/nameservers")
request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
request.body = { data: { type: 'nameserver', id: params[:old_hostname],
attributes: { hostname: params[:new_hostname],
ipv4: ipv4,
ipv6: ipv6 } } }.to_json
request.basic_auth(current_user.username, current_user.password)
if Rails.env.test?
response = Net::HTTP.start(uri.hostname, uri.port,
use_ssl: (uri.scheme == 'https'),
verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
http.request(request)
end
elsif Rails.env.development?
client_cert = File.read(ENV['cert_path'])
client_key = File.read(ENV['key_path'])
response = Net::HTTP.start(uri.hostname, uri.port,
use_ssl: (uri.scheme == 'https'),
verify_mode: OpenSSL::SSL::VERIFY_NONE,
cert: OpenSSL::X509::Certificate.new(client_cert),
key: OpenSSL::PKey::RSA.new(client_key)) do |http|
http.request(request)
end
else
client_cert = File.read(ENV['cert_path'])
client_key = File.read(ENV['key_path'])
response = Net::HTTP.start(uri.hostname, uri.port,
use_ssl: (uri.scheme == 'https'),
cert: OpenSSL::X509::Certificate.new(client_cert),
key: OpenSSL::PKey::RSA.new(client_key)) do |http|
http.request(request)
end
end
parsed_response = JSON.parse(response.body, symbolize_names: true)
if response.code == '200'
flash[:notice] = t '.replaced'
redirect_to registrar_domains_url
else
@api_errors = parsed_response[:errors]
render :edit
end
end
end
end

View file

@ -1,12 +1,14 @@
<div class="page-header"> <div class="page-header">
<div class="row"> <div class="row">
<div class="col-sm-9"> <div class="col-sm-7">
<h1><%= t '.header' %></h1> <h1><%= t '.header' %></h1>
</div> </div>
<div class="col-sm-3 text-right"> <div class="col-sm-5 text-right">
<%= link_to t('.new_btn'), new_registrar_domain_path, class: 'btn btn-primary' %> <%= link_to t('.new_btn'), new_registrar_domain_path, class: 'btn btn-primary' %>
<%= link_to t('.transfer_btn'), new_registrar_domain_transfer_path, class: 'btn btn-default' %> <%= link_to t('.transfer_btn'), new_registrar_domain_transfer_path, class: 'btn btn-default' %>
<%= link_to t('.replace_nameserver_btn'), registrar_edit_registrar_nameserver_path,
class: 'btn btn-default' %>
</div> </div>
</div> </div>
</div> </div>

View file

@ -0,0 +1,50 @@
<%= form_tag registrar_update_registrar_nameserver_path, method: :put, class: 'form-horizontal' do %>
<div class="form-group">
<div class="col-md-2 control-label">
<%= label_tag :old_hostname, nil %>
</div>
<div class="col-md-5">
<%= text_field_tag :old_hostname, nil, autofocus: true, required: true,
class: 'form-control' %>
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
<%= label_tag :new_hostname, nil %>
</div>
<div class="col-md-5">
<%= text_field_tag :new_hostname, nil, required: true, class: 'form-control' %>
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
<%= label_tag :ipv4, nil %>
</div>
<div class="col-md-3">
<%= text_area_tag :ipv4, nil, class: 'form-control' %>
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
<%= label_tag :ipv6, nil %>
</div>
<div class="col-md-3">
<%= text_area_tag :ipv6, nil, class: 'form-control' %>
</div>
</div>
<div class="form-group">
<div class="col-md-5 col-md-offset-2 text-right">
<button class="btn btn-warning">
<%= t '.replace_btn' %>
</button>
</div>
</div>
<% end %>

View file

@ -0,0 +1,11 @@
<ol class="breadcrumb">
<li><%= link_to t('registrar.domains.index.header'), registrar_domains_path %></li>
</ol>
<div class="page-header">
<h1><%= t '.header' %></h1>
</div>
<%= render 'registrar/domain_transfers/form/api_errors' %>
<%= render 'form' %>

View file

@ -5,6 +5,7 @@ en:
header: Domains header: Domains
new_btn: New domain new_btn: New domain
transfer_btn: Transfer transfer_btn: Transfer
replace_nameserver_btn: Replace nameserver
csv: csv:
domain_name: Domain domain_name: Domain
transfer_code: Transfer code transfer_code: Transfer code

View file

@ -0,0 +1,12 @@
en:
registrar:
registrar_nameservers:
edit:
header: Replace nameserver
replace_btn: Replace
form:
replace_btn: Replace nameserver
update:
replaced: Nameserver have been successfully replaced

View file

@ -62,6 +62,8 @@ Rails.application.routes.draw do
end end
end end
resources :domain_transfers, only: %i[new create] resources :domain_transfers, only: %i[new create]
get 'registrar/nameservers', to: 'registrar_nameservers#edit', as: :edit_registrar_nameserver
put 'registrar/nameservers', to: 'registrar_nameservers#update', as: :update_registrar_nameserver
resources :contacts, constraints: {:id => /[^\/]+(?=#{ ActionController::Renderers::RENDERERS.map{|e| "\\.#{e}\\z"}.join("|") })|[^\/]+/} do resources :contacts, constraints: {:id => /[^\/]+(?=#{ ActionController::Renderers::RENDERERS.map{|e| "\\.#{e}\\z"}.join("|") })|[^\/]+/} do
member do member do

View file

@ -1,11 +1,11 @@
# Nameservers # Nameservers
## PATCH /repp/v1/nameservers ## PUT /repp/v1/registrar/nameservers
Replaces all name servers of current registrar domains. Replaces all name servers of current registrar domains.
#### Request #### Request
``` ```
PATCH /repp/v1/nameservers PUT /repp/v1/registrar/nameservers
Accept: application/json Accept: application/json
Content-Type: application/json Content-Type: application/json
Authorization: Basic dGVzdDp0ZXN0dGVzdA== Authorization: Basic dGVzdDp0ZXN0dGVzdA==

View file

@ -1,12 +1,12 @@
require 'test_helper' require 'test_helper'
class APINameserversPatchTest < ActionDispatch::IntegrationTest class APINameserversPutTest < ActionDispatch::IntegrationTest
def test_replaces_current_registrar_nameservers def test_replaces_current_registrar_nameservers
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/nameservers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key } put '/repp/v1/registrar/nameservers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key }
new_nameserver = registrars(:bestnames).nameservers.find_by(hostname: 'ns55.bestnames.test') new_nameserver = registrars(:bestnames).nameservers.find_by(hostname: 'ns55.bestnames.test')
assert_nil registrars(:bestnames).nameservers.find_by(hostname: 'ns1.bestnames.test') assert_nil registrars(:bestnames).nameservers.find_by(hostname: 'ns1.bestnames.test')
@ -24,14 +24,14 @@ class APINameserversPatchTest < ActionDispatch::IntegrationTest
def test_honors_optional_params def test_honors_optional_params
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' } } }
put '/repp/v1/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
end end
def test_non_existent_nameserver_hostname def test_non_existent_nameserver_hostname
request_params = { format: :json, data: { type: 'nameserver', id: 'non-existent.test', request_params = { format: :json, data: { type: 'nameserver', id: 'non-existent.test',
attributes: { hostname: 'any.bestnames.test' } } } attributes: { hostname: 'any.bestnames.test' } } }
put '/repp/v1/nameservers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key } put '/repp/v1/registrar/nameservers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key }
assert_response 404 assert_response 404
assert_equal ({ errors: [{ title: 'Hostname non-existent.test does not exist' }] }), assert_equal ({ errors: [{ title: 'Hostname non-existent.test does not exist' }] }),
@ -41,7 +41,7 @@ class APINameserversPatchTest < ActionDispatch::IntegrationTest
def test_invalid_request_params def test_invalid_request_params
request_params = { format: :json, data: { type: 'nameserver', id: 'ns1.bestnames.test', request_params = { format: :json, data: { type: 'nameserver', id: 'ns1.bestnames.test',
attributes: { hostname: '' } } } attributes: { hostname: '' } } }
put '/repp/v1/nameservers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key } put '/repp/v1/registrar/nameservers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key }
assert_response 400 assert_response 400
assert_equal ({ errors: [{ title: 'Invalid params' }] }), assert_equal ({ errors: [{ title: 'Invalid params' }] }),
@ -49,7 +49,7 @@ class APINameserversPatchTest < ActionDispatch::IntegrationTest
end end
def test_unauthenticated def test_unauthenticated
put '/repp/v1/nameservers' put '/repp/v1/registrar/nameservers'
assert_response 401 assert_response 401
end end

View file

@ -0,0 +1,49 @@
require 'test_helper'
class RegistrarNameserverReplacementTest < ActionDispatch::IntegrationTest
def setup
WebMock.reset!
login_as users(:api_goodnames)
end
def test_replaces_current_registrar_nameservers
request_body = { data: { type: 'nameserver',
id: 'ns1.bestnames.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(:post, /registrar\/nameservers/).with(body: request_body,
headers: { 'Content-type' => 'application/json' },
basic_auth: ['test_goodnames', 'testtest'])
.to_return(body: { data: [{
type: 'nameserver',
id: 'new-ns.bestnames.test'
}] }.to_json, status: 200)
visit registrar_domains_url
click_link 'Replace 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"
click_on 'Replace nameserver'
assert_requested request_stub
assert_current_path registrar_domains_path
assert_text 'Nameserver have been successfully replaced'
end
def test_fails_gracefully
stub_request(:post, /registrar\/nameservers/).to_return(status: 400,
body: { errors: [{ title: 'epic fail' }] }.to_json,
headers: { 'Content-type' => 'application/json' })
visit registrar_domains_url
click_link 'Replace nameserver'
click_on 'Replace nameserver'
assert_text 'epic fail'
end
end