Add company controlled contacts to registrant API

Fix tests.
This commit is contained in:
Alex Sherman 2020-07-15 15:11:47 +05:00
parent 735a0e1642
commit c4de5655e1
8 changed files with 45 additions and 35 deletions

View file

@ -253,17 +253,8 @@ class Contact < ApplicationRecord
def registrant_user_contacts(registrant_user)
registrant_user_direct_contacts(registrant_user)
.or(registrant_user_company_contacts(registrant_user))
.or(registrant_user_indirect_contacts(registrant_user))
# .or(registrant_user_indirect_admin_registrar_contacts(registrant_user))
end
def registrant_user_indirect_admin_registrar_contacts(registrant_user)
indirect_contacts = registrant_user_indirect_contacts(registrant_user)
admin_contact_ids = admin_contact_ids(indirect_contacts)
reg_contact_ids = registrar_contact_ids(indirect_contacts)
total_ids = (admin_contact_ids + reg_contact_ids).uniq
where(id: total_ids)
end
def registrant_user_direct_contacts(registrant_user)
@ -271,9 +262,7 @@ class Contact < ApplicationRecord
.country.alpha2)
end
private
def registrant_user_indirect_contacts(registrant_user)
def registrant_user_company_contacts(registrant_user)
ident = registrant_user.companies.collect(&:registration_number)
# ident = ['12345678']
@ -282,14 +271,13 @@ class Contact < ApplicationRecord
ident_country_code: registrant_user.country.alpha2)
end
def admin_contact_ids(indirect_contacts)
domains = indirect_contacts.map(&:admin_domains).flatten
domains.map(&:contacts).flatten.collect(&:id)
end
def registrant_user_indirect_contacts(registrant_user)
company_domains = Domain.registrant_user_indirect_domains(registrant_user)
company_contact_ids = company_domains.map(&:contacts).flatten.collect(&:id)
company_ids = Contact.registrant_user_company_contacts(registrant_user).collect(&:id)
total_ids = (company_contact_ids + company_ids).uniq
def registrar_contact_ids(indirect_contacts)
registrar_domains = indirect_contacts.map(&:registrant_domains).flatten
registrar_domains.map(&:contacts).flatten.collect(&:id)
where(id: total_ids)
end
end

View file

@ -200,6 +200,7 @@ class Domain < ApplicationRecord
def registrant_user_domains(registrant_user)
from(
"(#{registrant_user_domains_by_registrant(registrant_user).to_sql} UNION " \
"#{registrant_user_indirect_domains(registrant_user).to_sql} UNION " \
"#{registrant_user_domains_by_contact(registrant_user).to_sql}) AS domains"
)
end
@ -218,6 +219,14 @@ class Domain < ApplicationRecord
)
end
def registrant_user_indirect_domains(registrant_user)
companies = Contact.registrant_user_company_contacts(registrant_user)
from(
"(#{registrant_user_company_registrant(companies).to_sql} UNION "\
"#{registrant_user_domains_company(companies).to_sql}) AS domains"
)
end
private
def registrant_user_domains_by_registrant(registrant_user)
@ -240,6 +249,14 @@ class Domain < ApplicationRecord
def registrant_user_direct_domains_by_contact(registrant_user)
joins(:domain_contacts).where(domain_contacts: { contact_id: registrant_user.direct_contacts })
end
def registrant_user_company_registrant(companies)
where(registrant: companies)
end
def registrant_user_domains_company(companies)
joins(:domain_contacts).where(domain_contacts: { contact: companies })
end
end
def name=(value)

View file

@ -12,7 +12,7 @@ class RegistrantApiContactsTest < ApplicationIntegrationTest
def test_root_accepts_limit_and_offset_parameters
contacts(:william).update!(ident: '1234', ident_type: 'priv', ident_country_code: 'US')
assert_equal 3, @user.contacts.size
assert_equal 4, @user.contacts.size
get '/api/v1/registrant/contacts', params: { 'limit' => 1, 'offset' => 0 },
headers: @auth_headers
@ -22,7 +22,7 @@ class RegistrantApiContactsTest < ApplicationIntegrationTest
get '/api/v1/registrant/contacts', headers: @auth_headers
response_json = JSON.parse(response.body, symbolize_names: true)
assert_equal(3, response_json.count)
assert_equal(@user.contacts.size, response_json.count)
end
def test_get_contact_details_by_uuid

View file

@ -75,8 +75,11 @@ class RegistrantApiV1ContactDetailsTest < ActionDispatch::IntegrationTest
end
def test_unmanaged_contact_cannot_be_accessed
assert_equal 'US-1234', @user.registrant_ident
@contact.update!(ident: '12345')
@user.update!(registrant_ident: 'US-12345')
@contact.update!(ident: '12345879')
companies = Contact.where(ident_type: 'org')
companies.update_all(ident: '78964521')
companies.reload
get api_v1_registrant_contact_path(@contact.uuid), as: :json,
headers: { 'HTTP_AUTHORIZATION' => auth_token }
@ -93,4 +96,4 @@ class RegistrantApiV1ContactDetailsTest < ActionDispatch::IntegrationTest
hash = token_creator.token_in_hash
"Bearer #{hash[:access_token]}"
end
end
end

View file

@ -35,8 +35,8 @@ class RegistrantApiV1ContactListTest < ActionDispatch::IntegrationTest
get api_v1_registrant_contacts_path, as: :json, headers: { 'HTTP_AUTHORIZATION' => auth_token }
response_json = JSON.parse(response.body, symbolize_names: true)
assert_equal 1, response_json.size
assert_equal 'acme-ltd-001', response_json.first[:code]
assert_equal @user.contacts.count, response_json.size
assert_includes response_json.map{ |hash| hash[:code] }, @contact.code
end
def test_returns_direct_contacts_when_company_register_is_unavailable
@ -70,4 +70,4 @@ class RegistrantApiV1ContactListTest < ActionDispatch::IntegrationTest
hash = token_creator.token_in_hash
"Bearer #{hash[:access_token]}"
end
end
end

View file

@ -214,6 +214,9 @@ class RegistrantApiV1ContactUpdateTest < ActionDispatch::IntegrationTest
def test_unmanaged_contact_cannot_be_updated
assert_equal 'US-1234', @user.registrant_ident
@contact.update!(ident: '12345')
companies = Contact.where(ident_type: 'org')
companies.update_all(ident: '78964521')
companies.reload
patch api_v1_registrant_contact_path(@contact.uuid), params: { name: 'new name' },
as: :json,

View file

@ -150,13 +150,12 @@ class ContactTest < ActiveSupport::TestCase
end
def test_returns_registrant_user_indirect_contacts
@contact.update!(ident_type: Contact::ORG)
assert_equal '1234', @contact.ident
@contact.update!(ident_type: Contact::ORG, ident: '1234321')
assert_equal 'US', @contact.ident_country_code
registrant_user = RegistrantUser.new(registrant_ident: 'US-1234')
registrant_user = RegistrantUser.new(registrant_ident: 'US-1234321')
registrant_user.stub(:companies, [OpenStruct.new(registration_number: '1234')]) do
assert_equal [@contact], Contact.registrant_user_contacts(registrant_user)
registrant_user.stub(:companies, [OpenStruct.new(registration_number: '1234321')]) do
assert_equal registrant_user.contacts, Contact.registrant_user_contacts(registrant_user)
end
end

View file

@ -273,7 +273,7 @@ class DomainTest < ActiveSupport::TestCase
def test_returns_registrant_user_domains_by_registrant
registrant = contacts(:john).becomes(Registrant)
assert_equal registrant, @domain.registrant
registrant_user = RegistrantUser.new
registrant_user = RegistrantUser.new(registrant_ident: 'US-12345')
registrant_user.stub(:contacts, [registrant]) do
assert_includes Domain.registrant_user_domains(registrant_user), @domain
@ -284,7 +284,7 @@ class DomainTest < ActiveSupport::TestCase
contact = contacts(:jane)
assert_not_equal contact.becomes(Registrant), @domain.registrant
assert_includes @domain.contacts, contact
registrant_user = RegistrantUser.new
registrant_user = RegistrantUser.new(registrant_ident: 'US-12345')
registrant_user.stub(:contacts, [contact]) do
assert_includes Domain.registrant_user_domains(registrant_user), @domain