diff --git a/app/controllers/repp/v1/domains/admin_contacts_controller.rb b/app/controllers/repp/v1/domains/admin_contacts_controller.rb index 2e9a285eb..6ec0e129b 100644 --- a/app/controllers/repp/v1/domains/admin_contacts_controller.rb +++ b/app/controllers/repp/v1/domains/admin_contacts_controller.rb @@ -6,7 +6,9 @@ module Repp super unless @new_contact.identical_to?(@current_contact) - @epp_errors << { code: 2304, msg: 'Admin contacts must be identical' } + @epp_errors.add(:epp_errors, + msg: 'Admin contacts must be identical', + code: '2304') end return handle_errors if @epp_errors.any? diff --git a/app/controllers/repp/v1/domains/base_contacts_controller.rb b/app/controllers/repp/v1/domains/base_contacts_controller.rb index b601c5313..521ca4ad4 100644 --- a/app/controllers/repp/v1/domains/base_contacts_controller.rb +++ b/app/controllers/repp/v1/domains/base_contacts_controller.rb @@ -15,8 +15,12 @@ module Repp end def update - @epp_errors ||= [] - @epp_errors << { code: 2304, msg: 'New contact must be valid' } if @new_contact.invalid? + @epp_errors ||= ActiveModel::Errors.new(self) + if @new_contact.invalid? + @epp_errors.add(:epp_errors, + msg: 'New contact must be valid', + code: '2304') + end end private diff --git a/app/controllers/repp/v1/domains/contacts_controller.rb b/app/controllers/repp/v1/domains/contacts_controller.rb index 3bde107d1..4c89243c7 100644 --- a/app/controllers/repp/v1/domains/contacts_controller.rb +++ b/app/controllers/repp/v1/domains/contacts_controller.rb @@ -50,7 +50,9 @@ module Repp super if @new_contact == @current_contact - @epp_errors << { code: 2304, msg: 'New contact must be different from current' } + @epp_errors.add(:epp_errors, + msg: 'New contact must be different from current', + code: '2304') end return handle_errors if @epp_errors.any? diff --git a/app/controllers/repp/v1/domains/renews_controller.rb b/app/controllers/repp/v1/domains/renews_controller.rb index c356d799a..f8774a325 100644 --- a/app/controllers/repp/v1/domains/renews_controller.rb +++ b/app/controllers/repp/v1/domains/renews_controller.rb @@ -29,8 +29,8 @@ module Repp renew = run_bulk_renew_task(@domains, bulk_renew_params[:renew_period]) return render_success(data: { updated_domains: @domains.map(&:name) }) if renew.valid? - @epp_errors << { code: 2002, - msg: renew.errors.keys.map { |k, _v| renew.errors[k] }.join(', ') } + msg = renew.errors.keys.map { |k, _v| renew.errors[k] }.join(', ') + @epp_errors.add(:epp_errors, msg: msg , code: '2002') handle_errors end @@ -41,20 +41,20 @@ module Repp end def validate_renew_period - @epp_errors ||= [] + @epp_errors ||= ActiveModel::Errors.new(self) periods = Depp::Domain::PERIODS.map { |p| p[1] } return if periods.include? bulk_renew_params[:renew_period] - @epp_errors << { code: 2005, msg: 'Invalid renew period' } + @epp_errors.add(:epp_errors, msg: 'Invalid renew period' , code: '2005') end def select_renewable_domains - @epp_errors ||= [] + @epp_errors ||= ActiveModel::Errors.new(self) if bulk_renew_params[:domains].instance_of?(Array) @domains = bulk_renew_domains else - @epp_errors << { code: 2005, msg: 'Domains attribute must be an array' } + @epp_errors.add(:epp_errors, msg: 'Domains attribute must be an array' , code: '2005') end return handle_errors if @epp_errors.any? @@ -73,12 +73,16 @@ module Repp end def bulk_renew_domains - @epp_errors ||= [] + @epp_errors ||= ActiveModel::Errors.new(self) domains = [] bulk_renew_params[:domains].each do |idn| domain = Epp::Domain.find_by(name: idn) domains << domain if domain - @epp_errors << { code: 2304, msg: "Object does not exist: #{idn}" } unless domain + unless domain + @epp_errors.add(:epp_errors, + msg: "Object does not exist: #{idn}", + code: '2304') + end end domains diff --git a/app/controllers/repp/v1/domains_controller.rb b/app/controllers/repp/v1/domains_controller.rb index a4f518ba2..422c2295d 100644 --- a/app/controllers/repp/v1/domains_controller.rb +++ b/app/controllers/repp/v1/domains_controller.rb @@ -108,7 +108,7 @@ module Repp api :POST, '/repp/v1/domains/transfer' desc 'Transfer multiple domains' def transfer - @errors ||= [] + @errors ||= ActiveModel::Errors.new(self) @successful = [] transfer_params[:domain_transfers].each do |transfer| @@ -150,8 +150,9 @@ module Repp if action.call @successful << { type: 'domain_transfer', domain_name: domain.name } else - @errors << { type: 'domain_transfer', domain_name: domain.name, - errors: domain.errors.where(:epp_errors)[0].options } + domain.errors.where(:epp_errors).each do |domain_error| + @errors.import domain_error + end end end @@ -187,7 +188,7 @@ module Repp end def set_authorized_domain - @epp_errors ||= [] + @epp_errors ||= ActiveModel::Errors.new(self) @domain = domain_from_url_hash end diff --git a/app/interactions/domains/bulk_renew/start.rb b/app/interactions/domains/bulk_renew/start.rb index f758b52cd..88ad6a83d 100644 --- a/app/interactions/domains/bulk_renew/start.rb +++ b/app/interactions/domains/bulk_renew/start.rb @@ -41,7 +41,7 @@ module Domains end def manage_errors(task) - task.errors.each { |k, v| errors.add(k, v) } unless task.valid? + task.errors.each { |task_error| errors.import task_error } unless task.valid? errors.add(:domain, I18n.t('not_enough_funds')) unless task.result end diff --git a/app/interactions/domains/check_balance/mass.rb b/app/interactions/domains/check_balance/mass.rb index 58af25a9f..55449c8b2 100644 --- a/app/interactions/domains/check_balance/mass.rb +++ b/app/interactions/domains/check_balance/mass.rb @@ -25,7 +25,11 @@ module Domains period: period, unit: unit) - task.valid? ? @total_price += task.result : errors.merge!(task.errors) + if task.valid? + @total_price += task.result + else + task.errors.each { |task_error| errors.import task_error } + end end end end diff --git a/test/integration/repp/v1/domains/bulk_renew_test.rb b/test/integration/repp/v1/domains/bulk_renew_test.rb index 19a3e34fc..510d09f62 100644 --- a/test/integration/repp/v1/domains/bulk_renew_test.rb +++ b/test/integration/repp/v1/domains/bulk_renew_test.rb @@ -92,7 +92,10 @@ class ReppV1DomainsBulkRenewTest < ActionDispatch::IntegrationTest end def test_throws_error_when_not_enough_balance - billing_prices(:renew_one_year).update(price_cents: 99999999) + price = Billing::Price.last + price.price_cents = 99999999 + price.save(validate: false) + payload = { "domains": [ 'invalid.test', @@ -106,7 +109,7 @@ class ReppV1DomainsBulkRenewTest < ActionDispatch::IntegrationTest assert_response :bad_request assert_equal 2002, json[:code] - assert_equal 'Domain Billing failure - credit balance low', json[:message] + assert_equal 'Billing failure - credit balance low', json[:message] end end @@ -128,7 +131,7 @@ class ReppV1DomainsBulkRenewTest < ActionDispatch::IntegrationTest private - def set_status_for_domain(domain, statuses) + def set_status_for_domain(domain, statuses) domain.update(statuses: statuses) if statuses.size > 1 @@ -148,7 +151,7 @@ class ReppV1DomainsBulkRenewTest < ActionDispatch::IntegrationTest def assert_renew_prohibited_domains(domains, payload) assert_no_changes -> { Domain.where(name: domains).pluck(:valid_to) } do json = bulk_renew(payload) - + assert_response :bad_request assert_equal 2002, json[:code] assert domains.all? do |domain|