From b3cf665ed501087ddb0ce0d6a60e126a32b84803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Mon, 1 Feb 2021 14:40:36 +0200 Subject: [PATCH] REPP: Domain contacts test --- app/models/actions/domain_update.rb | 12 ++- .../repp/v1/contacts/tech_replace_test.rb | 50 +++++++++ .../repp/v1/domains/contacts_test.rb | 100 ++++++++++++++++++ .../repp/v1/domains/nameservers_test.rb | 2 +- 4 files changed, 159 insertions(+), 5 deletions(-) create mode 100644 test/integration/repp/v1/contacts/tech_replace_test.rb create mode 100644 test/integration/repp/v1/domains/contacts_test.rb diff --git a/app/models/actions/domain_update.rb b/app/models/actions/domain_update.rb index 760ea644f..4131c4f51 100644 --- a/app/models/actions/domain_update.rb +++ b/app/models/actions/domain_update.rb @@ -22,7 +22,10 @@ module Actions def assign_relational_modifications assign_nameserver_modifications if params[:nameservers] assign_dnssec_modifications if params[:dns_keys] - (assign_admin_contact_changes && assign_tech_contact_changes) if params[:contacts] + return unless params[:contacts] + + assign_admin_contact_changes + assign_tech_contact_changes end def validate_domain_integrity @@ -147,10 +150,11 @@ module Actions end def contact_for_action(action:, method:, code:) - return Epp::Contact.find_by(code: code) if action == 'add' - return domain.admin_domain_contacts.find_by(contact_code_cache: code) if method == 'admin' + contact = Epp::Contact.find_by(code: code) + return contact if action == 'add' || !contact + return domain.admin_domain_contacts.find_by(contact_id: contact.id) if method == 'admin' - domain.tech_domain_contacts.find_by(contact_code_cache: code) + domain.tech_domain_contacts.find_by(contact_id: contact.id) end def assign_contact(obj, add: false, admin: true, code:) diff --git a/test/integration/repp/v1/contacts/tech_replace_test.rb b/test/integration/repp/v1/contacts/tech_replace_test.rb new file mode 100644 index 000000000..dfc61f6e2 --- /dev/null +++ b/test/integration/repp/v1/contacts/tech_replace_test.rb @@ -0,0 +1,50 @@ +require 'test_helper' + +class ReppV1ContactsTechReplaceTest < 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_replaces_tech_contacts + old_contact = contacts(:john) + new_contact = contacts(:william) + + assert DomainContact.where(contact: old_contact, type: 'TechDomainContact').any? + + payload = { current_contact_id: old_contact.code, new_contact_id: new_contact.code} + patch "/repp/v1/domains/contacts", headers: @auth_headers, params: payload + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal 1000, json[:code] + assert_empty ["hospital.test", "library.test"] - json[:data][:affected_domains] + + assert DomainContact.where(contact: old_contact, type: 'TechDomainContact').blank? + end + + def test_validates_contact_codes + payload = { current_contact_id: 'aaa', new_contact_id: 'bbb'} + patch "/repp/v1/domains/contacts", headers: @auth_headers, params: payload + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :not_found + assert_equal 2303, json[:code] + assert_equal 'Object does not exist', json[:message] + end + + def test_new_contact_must_be_different + old_contact = contacts(:john) + + payload = { current_contact_id: old_contact.code, new_contact_id: old_contact.code } + patch "/repp/v1/domains/contacts", headers: @auth_headers, params: payload + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :bad_request + assert_equal 2304, json[:code] + assert_equal 'New contact must be different from current', json[:message] + end +end diff --git a/test/integration/repp/v1/domains/contacts_test.rb b/test/integration/repp/v1/domains/contacts_test.rb new file mode 100644 index 000000000..b9b26a745 --- /dev/null +++ b/test/integration/repp/v1/domains/contacts_test.rb @@ -0,0 +1,100 @@ +require 'test_helper' + +class ReppV1DomainsContactsTest < ActionDispatch::IntegrationTest + def setup + @user = users(:api_bestnames) + @domain = domains(:shop) + token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}") + token = "Basic #{token}" + + @auth_headers = { 'Authorization' => token } + end + + def test_shows_existing_domain_contacts + get "/repp/v1/domains/#{@domain.name}/contacts", 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] + + assert_equal @domain.admin_contacts.length, json[:data][:admin_contacts].length + assert_equal @domain.tech_contacts.length, json[:data][:tech_contacts].length + end + + def test_can_add_new_admin_contacts + new_contact = contacts(:john) + refute @domain.admin_contacts.find_by(code: new_contact.code).present? + + payload = { contacts: [ { code: new_contact.code, type: 'admin' } ] } + post "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal 1000, json[:code] + + assert @domain.admin_contacts.find_by(code: new_contact.code).present? + end + + def test_can_add_new_tech_contacts + new_contact = contacts(:john) + refute @domain.tech_contacts.find_by(code: new_contact.code).present? + + payload = { contacts: [ { code: new_contact.code, type: 'tech' } ] } + post "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal 1000, json[:code] + @domain.reload + + assert @domain.tech_contacts.find_by(code: new_contact.code).present? + end + + def test_can_remove_admin_contacts + contact = contacts(:john) + payload = { contacts: [ { code: contact.code, type: 'admin' } ] } + post "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload + assert @domain.admin_contacts.find_by(code: contact.code).present? + + # Actually delete the contact + delete "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal 1000, json[:code] + + refute @domain.admin_contacts.find_by(code: contact.code).present? + end + + def test_can_remove_tech_contacts + contact = contacts(:john) + payload = { contacts: [ { code: contact.code, type: 'tech' } ] } + post "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload + assert @domain.tech_contacts.find_by(code: contact.code).present? + + # Actually delete the contact + delete "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal 1000, json[:code] + + refute @domain.tech_contacts.find_by(code: contact.code).present? + end + + def test_can_not_remove_one_and_only_contact + contact = @domain.admin_contacts.last + + payload = { contacts: [ { code: contact.code, type: 'admin' } ] } + delete "/repp/v1/domains/#{@domain.name}/contacts", headers: @auth_headers, params: payload + json = JSON.parse(response.body, symbolize_names: true) + + @domain.reload + assert_response :bad_request + assert_equal 2004, json[:code] + + assert @domain.admin_contacts.any? + end + +end diff --git a/test/integration/repp/v1/domains/nameservers_test.rb b/test/integration/repp/v1/domains/nameservers_test.rb index 9bb75ce1d..780e889c1 100644 --- a/test/integration/repp/v1/domains/nameservers_test.rb +++ b/test/integration/repp/v1/domains/nameservers_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class ReppV1DomainsStatusesTest < ActionDispatch::IntegrationTest +class ReppV1DomainsNameserversTest < ActionDispatch::IntegrationTest def setup @user = users(:api_bestnames) @domain = domains(:shop)