Merge pull request #1782 from internetee/fix-repp-registrar-bridge

Registrar portal: Fix REPP bridge
This commit is contained in:
Timo Võhmar 2020-12-23 12:36:58 +02:00 committed by GitHub
commit 3462d1b451
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 70 additions and 35 deletions

View file

@ -55,10 +55,12 @@ 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 '.transferred', count: parsed_response[:data].size failed = parsed_response[:data][:failed].each(&:domain_name).join(', ')
flash[:notice] = t('.transferred', count: parsed_response[:data][:success].size,
failed: failed)
redirect_to registrar_domains_url redirect_to registrar_domains_url
else else
@api_errors = parsed_response[:errors] @api_errors = parsed_response[:message]
render file: 'registrar/bulk_change/new', locals: { active_tab: :bulk_transfer } render file: 'registrar/bulk_change/new', locals: { active_tab: :bulk_transfer }
end end
else else

View file

@ -49,12 +49,13 @@ class Registrar
if response.code == '200' if response.code == '200'
notices = [t('.replaced')] notices = [t('.replaced')]
notices << "#{t('.affected_domains')}: #{parsed_response[:affected_domains].join(', ')}" notices << "#{t('.affected_domains')}: " \
"#{parsed_response[:data][:affected_domains].join(', ')}"
flash[:notice] = notices flash[:notice] = notices.join(', ')
redirect_to registrar_domains_url redirect_to registrar_domains_url
else else
@api_errors = parsed_response[:errors] @api_errors = parsed_response[:message]
render file: 'registrar/bulk_change/new', locals: { active_tab: :nameserver } render file: 'registrar/bulk_change/new', locals: { active_tab: :nameserver }
end end
end end

View file

@ -43,16 +43,18 @@ class Registrar
if response.code == '200' if response.code == '200'
notices = [t('.replaced')] notices = [t('.replaced')]
notices << "#{t('.affected_domains')}: #{parsed_response[:affected_domains].join(', ')}" notices << "#{t('.affected_domains')}: " \
"#{parsed_response[:data][:affected_domains].join(', ')}"
if parsed_response[:skipped_domains] if parsed_response[:data][:skipped_domains]
notices << "#{t('.skipped_domains')}: #{parsed_response[:skipped_domains].join(', ')}" notices << "#{t('.skipped_domains')}: " \
"#{parsed_response[:data][:skipped_domains].join(', ')}"
end end
flash[:notice] = notices flash[:notice] = notices.join(', ')
redirect_to registrar_domains_url redirect_to registrar_domains_url
else else
@error = parsed_response[:error] @error = response.code == '404' ? 'Contact(s) not found' : parsed_response[:message]
render file: 'registrar/bulk_change/new', locals: { active_tab: :technical_contact } render file: 'registrar/bulk_change/new', locals: { active_tab: :technical_contact }
end end
end end

View file

@ -3,6 +3,7 @@ module Repp
class BaseController < ActionController::API class BaseController < ActionController::API
rescue_from ActiveRecord::RecordNotFound, with: :not_found_error rescue_from ActiveRecord::RecordNotFound, with: :not_found_error
before_action :authenticate_user before_action :authenticate_user
before_action :validate_webclient_ca
before_action :check_ip_restriction before_action :check_ip_restriction
attr_reader :current_user attr_reader :current_user
@ -93,15 +94,33 @@ module Repp
end end
def check_ip_restriction def check_ip_restriction
allowed = @current_user.registrar.api_ip_white?(request.ip) return if webclient_request?
return if @current_user.registrar.api_ip_white?(request.ip)
return if allowed
@response = { code: 2202, @response = { code: 2202,
message: I18n.t('registrar.authorization.ip_not_allowed', ip: request.ip) } message: I18n.t('registrar.authorization.ip_not_allowed', ip: request.ip) }
render(json: @response, status: :unauthorized) render(json: @response, status: :unauthorized)
end end
def webclient_request?
return if Rails.env.test?
ENV['webclient_ips'].split(',').map(&:strip).include?(request.ip)
end
def validate_webclient_ca
return unless webclient_request?
request_name = request.env['HTTP_SSL_CLIENT_S_DN_CN']
webclient_cn = ENV['webclient_cert_common_name'] || 'webclient'
return if request_name == webclient_cn
@response = { code: 2202,
message: I18n.t('registrar.authorization.ip_not_allowed', ip: request.ip) }
render(json: @response, status: :unauthorized)
end
def not_found_error def not_found_error
@response = { code: 2303, message: 'Object does not exist' } @response = { code: 2303, message: 'Object does not exist' }
render(json: @response, status: :not_found) render(json: @response, status: :not_found)

View file

@ -5,19 +5,20 @@ class TechDomainContact < DomainContact
skipped_domains = [] skipped_domains = []
tech_contacts = where(contact: current_contact) tech_contacts = where(contact: current_contact)
transaction do tech_contacts.each do |tech_contact|
tech_contacts.each do |tech_contact| if tech_contact.domain.discarded?
if tech_contact.domain.discarded? skipped_domains << tech_contact.domain.name
skipped_domains << tech_contact.domain.name next
next end
end begin
tech_contact.contact = new_contact tech_contact.contact = new_contact
tech_contact.save! tech_contact.save!
affected_domains << tech_contact.domain.name affected_domains << tech_contact.domain.name
rescue ActiveRecord::RecordNotUnique
skipped_domains << tech_contact.domain.name
end end
end end
return affected_domains.sort, skipped_domains.sort [affected_domains.sort, skipped_domains.sort]
end end
end end

View file

@ -1,9 +1,13 @@
<% if @api_errors %> <% if @api_errors %>
<div class="alert alert-danger"> <div class="alert alert-danger">
<ul> <ul>
<% if @api_errors.is_a?(String) %>
<li><%= @api_errors %></li>
<% else %>
<% @api_errors.each do |error| %> <% @api_errors.each do |error| %>
<li><%= error[:title] %></li> <li><%= error[:title] %></li>
<% end %> <% end %>
<% end %>
</ul> </ul>
</div> </div>
<% end %> <% end %>

View file

@ -1,7 +1,7 @@
<%= form_tag registrar_tech_contacts_path, method: :patch, class: 'form-horizontal' do %> <%= form_tag registrar_tech_contacts_path, method: :patch, class: 'form-horizontal' do %>
<% if @error %> <% if @error %>
<div class="alert alert-danger"> <div class="alert alert-danger">
<%= @error[:message] %> <%= @error %>
</div> </div>
<% end %> <% end %>

View file

@ -1,9 +1,13 @@
<% if @api_errors %> <% if @api_errors %>
<div class="alert alert-danger"> <div class="alert alert-danger">
<ul> <ul>
<% if @api_errors.is_a?(String) %>
<li><%= @api_errors %></li>
<% else %>
<% @api_errors.each do |error| %> <% @api_errors.each do |error| %>
<li><%= error[:title] %></li> <li><%= error[:title] %></li>
<% end %> <% end %>
<% end %>
</ul> </ul>
</div> </div>
<% end %> <% end %>

View file

@ -6,7 +6,7 @@ en:
create: create:
header: Domain transfer header: Domain transfer
transferred: "%{count} domains have been successfully transferred" transferred: "%{count} domains have been successfully transferred. Failed domains: %{failed}"
form: form:
submit_btn: Transfer submit_btn: Transfer

View file

@ -11,9 +11,9 @@ class RegistrarAreaBulkTransferTest < ApplicationSystemTestCase
request_stub = stub_request(:post, /domains\/transfer/).with(body: request_body, request_stub = stub_request(:post, /domains\/transfer/).with(body: request_body,
headers: headers, headers: headers,
basic_auth: ['test_goodnames', 'testtest']) basic_auth: ['test_goodnames', 'testtest'])
.to_return(body: { data: [{ .to_return(body: { data: { success: [{ type: 'domain_transfer', domain_name: 'shop.test' }],
type: 'domain_transfer' failed: []
}] }.to_json, status: 200) } }.to_json, status: 200)
visit registrar_domains_url visit registrar_domains_url
click_link 'Bulk change' click_link 'Bulk change'
@ -27,7 +27,7 @@ class RegistrarAreaBulkTransferTest < ApplicationSystemTestCase
end end
def test_fail_gracefully def test_fail_gracefully
body = { errors: [{ title: 'epic fail' }] }.to_json body = { message: 'epic fail' }.to_json
headers = { 'Content-type' => Mime[:json] } headers = { 'Content-type' => Mime[:json] }
stub_request(:post, /domains\/transfer/).to_return(status: 400, body: body, headers: headers) stub_request(:post, /domains\/transfer/).to_return(status: 400, body: body, headers: headers)

View file

@ -15,10 +15,12 @@ class RegistrarAreaNameserverBulkChangeTest < ApplicationSystemTestCase
request_stub = stub_request(:put, /registrar\/nameservers/).with(body: request_body, request_stub = stub_request(:put, /registrar\/nameservers/).with(body: request_body,
headers: { 'Content-type' => Mime[:json] }, headers: { 'Content-type' => Mime[:json] },
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',
affected_domains: ["airport.test", "shop.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'
@ -38,7 +40,7 @@ class RegistrarAreaNameserverBulkChangeTest < ApplicationSystemTestCase
def test_fails_gracefully def test_fails_gracefully
stub_request(:put, /registrar\/nameservers/).to_return(status: 400, stub_request(:put, /registrar\/nameservers/).to_return(status: 400,
body: { errors: [{ title: 'epic fail' }] }.to_json, body: { message: 'epic fail' }.to_json,
headers: { 'Content-type' => Mime[:json] }) headers: { 'Content-type' => Mime[:json] })
visit registrar_domains_url visit registrar_domains_url

View file

@ -9,8 +9,8 @@ class RegistrarAreaTechContactBulkChangeTest < ApplicationSystemTestCase
request_stub = stub_request(:patch, /domains\/contacts/) request_stub = stub_request(:patch, /domains\/contacts/)
.with(body: { current_contact_id: 'william-001', new_contact_id: 'john-001' }, .with(body: { current_contact_id: 'william-001', new_contact_id: 'john-001' },
basic_auth: ['test_bestnames', 'testtest']) basic_auth: ['test_bestnames', 'testtest'])
.to_return(body: { affected_domains: %w[foo.test bar.test], .to_return(body: { data: { affected_domains: %w[foo.test bar.test],
skipped_domains: %w[baz.test qux.test] }.to_json, skipped_domains: %w[baz.test qux.test] } }.to_json,
status: 200) status: 200)
visit registrar_domains_url visit registrar_domains_url
@ -30,7 +30,7 @@ class RegistrarAreaTechContactBulkChangeTest < ApplicationSystemTestCase
def test_fails_gracefully def test_fails_gracefully
stub_request(:patch, /domains\/contacts/) stub_request(:patch, /domains\/contacts/)
.to_return(status: 400, .to_return(status: 400,
body: { error: { message: 'epic fail' } }.to_json, body: { message: 'epic fail' }.to_json,
headers: { 'Content-type' => Mime[:json] }) headers: { 'Content-type' => Mime[:json] })
visit registrar_domains_url visit registrar_domains_url