REPP: Add contact:destroy action

This commit is contained in:
Karl Erik Õunapuu 2020-10-21 11:54:25 +03:00
parent 516b2180ca
commit f9bef781ab
No known key found for this signature in database
GPG key ID: C9DD647298A34764
5 changed files with 104 additions and 30 deletions

View file

@ -44,12 +44,13 @@ module Epp
def delete def delete
authorize! :delete, @contact, @password authorize! :delete, @contact, @password
action = Actions::ContactDelete.new(@contact, params[:legal_document])
if @contact.destroy_and_clean(params[:parsed_frame]) unless action.call
render_epp_response '/epp/contacts/delete'
else
handle_errors(@contact) handle_errors(@contact)
return
end end
render_epp_response '/epp/contacts/delete'
end end
def renew def renew

View file

@ -1,7 +1,7 @@
module Repp module Repp
module V1 module V1
class ContactsController < BaseController class ContactsController < BaseController
before_action :find_contact, only: %i[show update] before_action :find_contact, only: %i[show update destroy]
## GET /repp/v1/contacts ## GET /repp/v1/contacts
def index def index
@ -53,6 +53,16 @@ module Repp
render_success(create_update_success_body) render_success(create_update_success_body)
end end
def destroy
action = Actions::ContactDelete.new(@contact, params[:legal_document])
unless action.call
handle_errors(@contact)
return
end
render_success
end
def contact_addr_present? def contact_addr_present?
return false unless contact_addr_params.key?(:addr) return false unless contact_addr_params.key?(:addr)

View file

@ -0,0 +1,41 @@
module Actions
class ContactDelete
attr_reader :contact
attr_reader :new_attributes
attr_reader :legal_document
attr_reader :ident
attr_reader :user
def initialize(contact, legal_document = nil)
@legal_document = legal_document
@contact = contact
end
def call
maybe_attach_legal_doc
if contact.linked?
contact.errors.add(:domains, :exist)
return
end
commit
end
def maybe_attach_legal_doc
return unless legal_document
document = contact.legal_documents.create(
document_type: legal_document[:type],
body: legal_document[:body]
)
contact.legal_document_id = document.id
contact.save
end
def commit
contact.destroy
end
end
end

View file

@ -333,31 +333,6 @@ class Contact < ApplicationRecord
Country.new(country_code) Country.new(country_code)
end end
# TODO: refactor, it should not allow to destroy with normal destroy,
# no need separate method
# should use only in transaction
def destroy_and_clean frame
if linked?
errors.add(:domains, :exist)
return false
end
legal_document_data = ::Deserializers::Xml::LegalDocument.new(frame).call
if legal_document_data
doc = LegalDocument.create(
documentable_type: Contact,
document_type: legal_document_data[:type],
body: legal_document_data[:body]
)
self.legal_documents = [doc]
self.legal_document_id = doc.id
self.save
end
destroy
end
def to_upcase_country_code def to_upcase_country_code
self.ident_country_code = ident_country_code.upcase if ident_country_code self.ident_country_code = ident_country_code.upcase if ident_country_code
self.country_code = country_code.upcase if country_code self.country_code = country_code.upcase if country_code

View file

@ -0,0 +1,47 @@
require 'test_helper'
class ReppV1ContactsDeleteTest < ActionDispatch::IntegrationTest
def setup
@user = users(:api_bestnames)
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
end
def test_deletes_unassociated_contact
contact = contacts(:invalid_email)
delete "/repp/v1/contacts/#{contact.code}", headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Command completed successfully', json[:message]
end
def test_can_not_delete_associated_contact
contact = contacts(:john)
delete "/repp/v1/contacts/#{contact.code}", headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :bad_request
assert_equal 2305, json[:code]
assert_equal 'Object association prohibits operation [domains]', json[:message]
end
def test_handles_unknown_contact
delete "/repp/v1/contacts/definitely:unexistant", headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :not_found
end
def test_can_not_destroy_other_registrar_contact
contact = contacts(:jack)
delete "/repp/v1/contacts/#{contact.code}", headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :not_found
end
end