diff --git a/app/controllers/registrar/domain_transfers_controller.rb b/app/controllers/registrar/domain_transfers_controller.rb
index 43d40e70e..cbaa5407c 100644
--- a/app/controllers/registrar/domain_transfers_controller.rb
+++ b/app/controllers/registrar/domain_transfers_controller.rb
@@ -8,10 +8,45 @@ class Registrar
end
def create
- params[:request] = true # EPP domain:transfer "op" attribute
- domain = Depp::Domain.new(current_user: depp_current_user)
- @data = domain.transfer(params)
- render :new unless response_ok?
+ if params[:batch_file].present?
+ csv = CSV.read(params[:batch_file].path, headers: true)
+ domain_transfers = []
+
+ csv.each do |row|
+ domain_name = row['Domain']
+ transfer_code = row['Transfer code']
+ domain_transfers << { 'domainName' => domain_name, 'transferCode' => transfer_code }
+ end
+
+ client_cert = File.read(ENV['cert_path'])
+ client_key = File.read(ENV['key_path'])
+
+ uri = URI.parse("#{ENV['repp_url']}domain_transfers")
+ request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
+ request.body = { data: { domainTransfers: domain_transfers } }.to_json
+ request.basic_auth(current_user.username, current_user.password)
+
+ 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
+
+ if response.code == '204'
+ flash[:notice] = t '.transferred'
+ redirect_to registrar_domains_url
+ else
+ @api_errors = JSON.parse(response.body, symbolize_names: true)[:errors]
+ render :new
+ end
+ else
+ params[:request] = true # EPP domain:transfer "op" attribute
+ domain = Depp::Domain.new(current_user: depp_current_user)
+ @data = domain.transfer(params)
+ render :new unless response_ok?
+ end
end
end
end
diff --git a/app/views/registrar/domain_transfers/form/_api_errors.html.erb b/app/views/registrar/domain_transfers/form/_api_errors.html.erb
new file mode 100644
index 000000000..56bf8c404
--- /dev/null
+++ b/app/views/registrar/domain_transfers/form/_api_errors.html.erb
@@ -0,0 +1,9 @@
+<% if @api_errors %>
+
+
+ <% @api_errors.each do |error| %>
+ - <%= error[:title] %>
+ <% end %>
+
+
+<% end %>
diff --git a/app/views/registrar/domain_transfers/form/_batch.html.erb b/app/views/registrar/domain_transfers/form/_batch.html.erb
index d6953dd05..5b415f1cd 100644
--- a/app/views/registrar/domain_transfers/form/_batch.html.erb
+++ b/app/views/registrar/domain_transfers/form/_batch.html.erb
@@ -1,10 +1,11 @@
<%= form_tag registrar_domain_transfers_path, multipart: true, class: 'form-horizontal' do %>
diff --git a/app/views/registrar/domain_transfers/new.html.erb b/app/views/registrar/domain_transfers/new.html.erb
index 28e196a83..8ba658023 100644
--- a/app/views/registrar/domain_transfers/new.html.erb
+++ b/app/views/registrar/domain_transfers/new.html.erb
@@ -2,6 +2,8 @@
<%= t '.header' %>
+<%= render 'registrar/domain_transfers/form/api_errors' %>
+
diff --git a/config/locales/registrar/domain_transfers.en.yml b/config/locales/registrar/domain_transfers.en.yml
index bf93e562f..64663b558 100644
--- a/config/locales/registrar/domain_transfers.en.yml
+++ b/config/locales/registrar/domain_transfers.en.yml
@@ -8,10 +8,12 @@ en:
create:
header: Domain transfer
+ transferred: Domains have been successfully transferred
form:
single:
transfer_btn: Transfer
batch:
- transfer_btn: Transfer
+ batch_file_help: CSV file with domain list provided by another registrar
+ transfer_btn: Transfer batch
diff --git a/test/fixtures/files/valid_domains_for_transfer.csv b/test/fixtures/files/valid_domains_for_transfer.csv
new file mode 100644
index 000000000..b04d45ba2
--- /dev/null
+++ b/test/fixtures/files/valid_domains_for_transfer.csv
@@ -0,0 +1,2 @@
+Domain,Transfer code,Registrant name,Registrant code,Date of expiry
+shop.test,65078d5,whatever,whatever,2010-07-05
diff --git a/test/api/domain_transfers_test.rb b/test/integration/api/domain_transfers_test.rb
similarity index 95%
rename from test/api/domain_transfers_test.rb
rename to test/integration/api/domain_transfers_test.rb
index 087d8b2cf..0debc37c0 100644
--- a/test/api/domain_transfers_test.rb
+++ b/test/integration/api/domain_transfers_test.rb
@@ -1,6 +1,6 @@
require 'test_helper'
-class Repp::DomainTransfersTest < ActionDispatch::IntegrationTest
+class APIDomainTransfersTest < ActionDispatch::IntegrationTest
def test_transfers_domain
request_params = { format: :json,
data: { domainTransfers: [{ domainName: 'shop.test', transferCode: '65078d5' }] } }
diff --git a/test/integration/registrar/domain_transfers_test.rb b/test/integration/registrar/domain_transfers_test.rb
new file mode 100644
index 000000000..46a92859b
--- /dev/null
+++ b/test/integration/registrar/domain_transfers_test.rb
@@ -0,0 +1,43 @@
+require 'test_helper'
+
+class RegistrarDomainTransfersTest < ActionDispatch::IntegrationTest
+ def setup
+ WebMock.reset!
+ login_as users(:api_goodnames)
+ end
+
+ def test_batch_transfer_succeeds
+ body = { data: { domainTransfers: [{ domainName: 'shop.test', transferCode: '65078d5' }] } }
+ headers = { 'Content-type' => 'application/json' }
+ request_stub = stub_request(:post, /domain_transfers/).with(body: body,
+ headers: headers,
+ basic_auth: ['test_goodnames', 'testtest'])
+ .to_return(status: 204)
+
+ visit registrar_domains_url
+ click_link 'Transfer'
+
+ click_on 'Batch'
+ attach_file 'Batch file', Rails.root.join('test', 'fixtures', 'files', 'valid_domains_for_transfer.csv').to_s
+ click_button 'Transfer batch'
+
+ assert_requested request_stub
+ assert_current_path registrar_domains_path
+ assert_text 'Domains have been successfully transferred'
+ end
+
+ def test_batch_transfer_fails_gracefully
+ body = { errors: [{ title: 'epic fail' }] }.to_json
+ headers = { 'Content-type' => 'application/json' }
+ stub_request(:post, /domain_transfers/).to_return(status: 400, body: body, headers: headers)
+
+ visit registrar_domains_url
+ click_link 'Transfer'
+
+ click_on 'Batch'
+ attach_file 'Batch file', Rails.root.join('test', 'fixtures', 'files', 'valid_domains_for_transfer.csv').to_s
+ click_button 'Transfer batch'
+
+ assert_text 'epic fail'
+ end
+end
diff --git a/test/integration/registrar/domains_test.rb b/test/integration/registrar/domains_test.rb
index c24560bad..51b76fc7c 100644
--- a/test/integration/registrar/domains_test.rb
+++ b/test/integration/registrar/domains_test.rb
@@ -1,11 +1,8 @@
require 'test_helper'
class RegistrarDomainsTest < ActionDispatch::IntegrationTest
- def setup
- login_as users(:api_bestnames)
- end
-
def test_downloads_domain_list_as_csv
+ login_as users(:api_bestnames)
travel_to Time.zone.parse('2010-07-05 10:30')
expected_csv = <<-CSV.strip_heredoc
@@ -20,22 +17,4 @@ class RegistrarDomainsTest < ActionDispatch::IntegrationTest
assert_equal 'attachment; filename="Domains_2010-07-05_10.30.csv"', response_headers['Content-Disposition']
assert_equal expected_csv, page.body
end
-
- def test_transfers_domain
- travel_to Time.zone.parse('2010-07-05 10:30:00')
-
- visit registrar_domains_url
- click_link 'Transfer'
- fill_in 'Domain name', with: 'shop.test'
- fill_in 'Transfer code', with: '65078d5'
- click_button 'Transfer'
-
- assert_text 'Transfer requested at: 2010-07-05 10:30:00'
- end
-
- def test_prefills_domain_transfer_form
- visit info_registrar_domains_url(domain_name: 'airport.test')
- click_link 'Transfer'
- assert_field 'domain_name', with: 'airport.test'
- end
end