From 33f6da53c12f5bc18f3ab986a40a5c3e3de0225e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Tue, 20 Oct 2020 13:58:21 +0300 Subject: [PATCH] REPP: Contact update test --- .../repp/v1/contacts_controller.rb | 16 ++-- app/models/actions/contact_update.rb | 2 +- .../repp/v1/contacts/update_test.rb | 82 +++++++++++++++++++ 3 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 test/integration/repp/v1/contacts/update_test.rb diff --git a/app/controllers/repp/v1/contacts_controller.rb b/app/controllers/repp/v1/contacts_controller.rb index 0b52505b0..1677e827f 100644 --- a/app/controllers/repp/v1/contacts_controller.rb +++ b/app/controllers/repp/v1/contacts_controller.rb @@ -41,7 +41,7 @@ module Repp ## PUT /repp/v1/contacts/1 def update - action = Actions::ContactUpdate.new(@contact, contact_params_with_address, + action = Actions::ContactUpdate.new(@contact, contact_params_with_address(required: false), params[:legal_document], contact_ident_params(required: false), current_user) @@ -84,16 +84,16 @@ module Repp @contact = Epp::Contact.find_by!(code: code, registrar: current_user.registrar) end - def contact_params_with_address - return contact_create_params unless contact_addr_params.key?(:addr) + def contact_params_with_address(required: true) + return contact_create_params(required: required) unless contact_addr_params.key?(:addr) addr = {} contact_addr_params[:addr].each_key { |k| addr[k] = contact_addr_params[:addr][k] } - contact_create_params.merge(addr) + contact_create_params(required: required).merge(addr) end - def contact_create_params - params.require(:contact).require(%i[name email phone]) + def contact_create_params(required: true) + params.require(:contact).require(%i[name email phone]) if required params.require(:contact).permit(:name, :email, :phone) end @@ -102,8 +102,10 @@ module Repp params.require(:contact).require(:ident).require(%i[ident ident_type ident_country_code]) params.require(:contact).require(:ident).permit(:ident, :ident_type, :ident_country_code) else - params.permit(ident: %i[ident ident_type ident_country_code]) + params.permit(contact: { ident: %i[ident ident_type ident_country_code] }) end + + params[:contact][:ident] end def contact_addr_params diff --git a/app/models/actions/contact_update.rb b/app/models/actions/contact_update.rb index f8b39ecb4..6400526c5 100644 --- a/app/models/actions/contact_update.rb +++ b/app/models/actions/contact_update.rb @@ -17,7 +17,7 @@ module Actions def call maybe_remove_address maybe_update_statuses - maybe_update_ident + maybe_update_ident if ident maybe_attach_legal_doc commit end diff --git a/test/integration/repp/v1/contacts/update_test.rb b/test/integration/repp/v1/contacts/update_test.rb new file mode 100644 index 000000000..816a90637 --- /dev/null +++ b/test/integration/repp/v1/contacts/update_test.rb @@ -0,0 +1,82 @@ +require 'test_helper' + +class ReppV1ContactsUpdateTest < ActionDispatch::IntegrationTest + def setup + @contact = contacts(:john) + @user = users(:api_bestnames) + token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}") + token = "Basic #{token}" + + @auth_headers = { 'Authorization' => token } + end + + def test_updates_contact + request_body = { + "contact": { + "email": "donaldtrump@yandex.ru" + } + } + + put "/repp/v1/contacts/#{@contact.code}", headers: @auth_headers, params: request_body + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal 1000, json[:code] + assert_equal 'Command completed successfully', json[:message] + + contact = Contact.find_by(code: json[:data][:contact][:id]) + assert contact.present? + + assert_equal(request_body[:contact][:email], contact.email) + end + + def test_removes_postal_info_when_updated + request_body = { + "contact": { + "addr": { + "city": "Tallinn", + "street": "Wismari 13", + "zip": "12345", + "country_code": "EE" + } + } + } + + put "/repp/v1/contacts/#{@contact.code}", headers: @auth_headers, params: request_body + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal 1100, json[:code] + assert_equal 'Command completed successfully; Postal address data discarded', json[:message] + + contact = Contact.find_by(code: json[:data][:contact][:id]) + assert contact.present? + + assert_nil contact.city + assert_nil contact.street + assert_nil contact.zip + assert_nil contact.country_code + end + + def test_can_not_change_ident_code + request_body = { + "contact": { + "name": "Donald Trumpster", + "ident": { + "ident_type": "priv", + "ident_country_code": "US", + "ident": "12345" + } + } + } + + put "/repp/v1/contacts/#{@contact.code}", headers: @auth_headers, params: request_body + json = JSON.parse(response.body, symbolize_names: true) + + @contact.reload + assert_not @contact.ident == 12345 + assert_response :bad_request + assert_equal 2308, json[:code] + assert json[:message].include? 'Ident update is not allowed. Consider creating new contact object' + end +end