From 03d940a6950f55f3c64afa1d60b50bd93ed7af64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 22 Oct 2020 15:25:02 +0300 Subject: [PATCH] REPP: Fix bulk domain transfer --- app/controllers/repp/v1/domains_controller.rb | 31 ++------ app/models/actions/domain_transfer.rb | 77 +++++++++++++++++++ .../repp/v1/domains/transfer_test.rb | 15 ++-- 3 files changed, 91 insertions(+), 32 deletions(-) create mode 100644 app/models/actions/domain_transfer.rb diff --git a/app/controllers/repp/v1/domains_controller.rb b/app/controllers/repp/v1/domains_controller.rb index 7612b7d75..7e81fac55 100644 --- a/app/controllers/repp/v1/domains_controller.rb +++ b/app/controllers/repp/v1/domains_controller.rb @@ -33,37 +33,16 @@ module Repp initiate_transfer(transfer) end - if @errors.any? - render_epp_error(:bad_request, @errors) - else - render_success(data: @successful) - end + render_success(data: {success: @successful, failed: @errors}) end def initiate_transfer(transfer) - domain = transferable_domain(transfer[:domain_name], transfer[:transfer_code]) - return unless domain + domain = Epp::Domain.find_or_initialize_by(name: transfer[:domain_name]) + action = Actions::DomainTransfer.new(domain, transfer[:transfer_code], current_user.registrar) - DomainTransfer.request(domain, current_user.registrar) - @successful << { type: 'domain_transfer', attributes: { domain_name: domain.name } } - end + @successful << { type: 'domain_transfer', domain_name: domain.name } and return if action.call - def transferable_domain(domain_name, transfer_code) - domain = Domain.find_by(name: domain_name) - # rubocop:disable Style/AndOr - add_error("#{domain_name} does not exist") and return unless domain - # rubocop:enable Style/AndOr - unless domain.transfer_code.eql?(transfer_code) - add_error("#{domain_name} transfer code is wrong") - return - end - - domain - end - - def add_error(msg) - @errors ||= [] - @errors << { title: msg } + @errors << { type: 'domain_transfer', domain_name: domain.name, errors: domain.errors[:epp_errors] } end private diff --git a/app/models/actions/domain_transfer.rb b/app/models/actions/domain_transfer.rb new file mode 100644 index 000000000..620e9b4db --- /dev/null +++ b/app/models/actions/domain_transfer.rb @@ -0,0 +1,77 @@ +module Actions + class DomainTransfer + attr_reader :domain + attr_reader :transfer_code + attr_reader :legal_document + attr_reader :ident + attr_reader :user + + def initialize(domain, transfer_code, user) + @domain = domain + @transfer_code = transfer_code + @user = user + end + + def call + return unless domain_exists? + return unless run_validations + + #return domain.pending_transfer if domain.pending_transfer + #attach_legal_document(::Deserializers::Xml::LegalDocument.new(frame).call) + + return if domain.errors[:epp_errors].any? + + commit + end + + def domain_exists? + return true if domain.persisted? + + domain.add_epp_error('2303', nil, nil, 'Object does not exist') + + false + end + + def run_validations + return unless validate_transfer_code + return unless validate_registrar + return unless validate_eligilibty + return unless validate_not_discarded + + true + end + + def validate_transfer_code + return true if transfer_code == domain.transfer_code + + domain.add_epp_error('2202', nil, nil, 'Invalid authorization information') + false + end + + def validate_registrar + return true unless user == domain.registrar + + domain.add_epp_error('2002', nil, nil, I18n.t(:domain_already_belongs_to_the_querying_registrar)) + false + end + + def validate_eligilibty + return true unless domain.non_transferable? + + domain.add_epp_error('2304', nil, nil, 'Domain is not transferable??') + false + end + + def validate_not_discarded + return true unless domain.discarded? + + domain.add_epp_error('2106', nil, nil, 'Object is not eligible for transfer') + false + end + + def commit + bare_domain = Domain.find(domain.id) + ::DomainTransfer.request(bare_domain, user) + end + end +end diff --git a/test/integration/repp/v1/domains/transfer_test.rb b/test/integration/repp/v1/domains/transfer_test.rb index 4c1280b47..9d2518117 100644 --- a/test/integration/repp/v1/domains/transfer_test.rb +++ b/test/integration/repp/v1/domains/transfer_test.rb @@ -25,8 +25,11 @@ class ReppV1DomainsTransferTest < ActionDispatch::IntegrationTest assert_equal 1000, json[:code] assert_equal 'Command completed successfully', json[:message] - assert_equal @domain.name, json[:data][0][:attributes][:domain_name] - assert_equal 'domain_transfer', json[:data][0][:type] + assert_equal @domain.name, json[:data][:success][0][:domain_name] + + @domain.reload + + assert @domain.registrar = @user.registrar end def test_does_not_transfer_domain_with_invalid_auth_code @@ -40,10 +43,10 @@ class ReppV1DomainsTransferTest < ActionDispatch::IntegrationTest post "/repp/v1/domains/transfer", headers: @auth_headers, params: payload json = JSON.parse(response.body, symbolize_names: true) - assert_response :bad_request - assert_equal 2304, json[:code] - assert_equal 'Command failed', json[:message] + assert_response :ok + assert_equal 1000, json[:code] + assert_equal 'Command completed successfully', json[:message] - assert_equal "#{@domain.name} transfer code is wrong", json[:data][0][:title] + assert_equal "Invalid authorization information", json[:data][:failed][0][:errors][0][:msg] end end