mirror of
https://github.com/internetee/registry.git
synced 2025-06-13 16:14:47 +02:00
REPP: Add contact:destroy action
This commit is contained in:
parent
516b2180ca
commit
f9bef781ab
5 changed files with 104 additions and 30 deletions
|
@ -44,12 +44,13 @@ module Epp
|
|||
|
||||
def delete
|
||||
authorize! :delete, @contact, @password
|
||||
|
||||
if @contact.destroy_and_clean(params[:parsed_frame])
|
||||
render_epp_response '/epp/contacts/delete'
|
||||
else
|
||||
action = Actions::ContactDelete.new(@contact, params[:legal_document])
|
||||
unless action.call
|
||||
handle_errors(@contact)
|
||||
return
|
||||
end
|
||||
|
||||
render_epp_response '/epp/contacts/delete'
|
||||
end
|
||||
|
||||
def renew
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
module Repp
|
||||
module V1
|
||||
class ContactsController < BaseController
|
||||
before_action :find_contact, only: %i[show update]
|
||||
before_action :find_contact, only: %i[show update destroy]
|
||||
|
||||
## GET /repp/v1/contacts
|
||||
def index
|
||||
|
@ -53,6 +53,16 @@ module Repp
|
|||
render_success(create_update_success_body)
|
||||
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?
|
||||
return false unless contact_addr_params.key?(:addr)
|
||||
|
||||
|
|
41
app/models/actions/contact_delete.rb
Normal file
41
app/models/actions/contact_delete.rb
Normal 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
|
|
@ -333,31 +333,6 @@ class Contact < ApplicationRecord
|
|||
Country.new(country_code)
|
||||
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
|
||||
self.ident_country_code = ident_country_code.upcase if ident_country_code
|
||||
self.country_code = country_code.upcase if country_code
|
||||
|
|
47
test/integration/repp/v1/contacts/delete_test.rb
Normal file
47
test/integration/repp/v1/contacts/delete_test.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue