Refactor RegistrantUser class

* Extract frequently used country_code and ident methods
* Refactor domain query
* Add contact query method
* Add adminstrated_domains query method. Name will most likely change
  in the future developments
* Change registry locks integration test name
This commit is contained in:
Maciej Szlosarczyk 2018-08-23 16:00:44 +03:00
parent 9f7fc47f19
commit 9623e2fb97
No known key found for this signature in database
GPG key ID: 41D62D42D3B0D765
6 changed files with 95 additions and 55 deletions

View file

@ -39,7 +39,8 @@ module Api
if decryptor.valid?
sign_in decryptor.user
else
render json: { errors: [{base: ['Not authorized']}] }, status: :unauthorized
render json: { errors: [{ base: ['Not authorized'] }] },
status: :unauthorized
end
end

View file

@ -25,7 +25,7 @@ module Api
private
def set_domain
domain_pool = associated_domains(current_user)
domain_pool = current_user.administrated_domains
@domain = domain_pool.find_by(uuid: params[:domain_uuid])
return if @domain

View file

@ -8,16 +8,30 @@ class RegistrantUser < User
delegate :can?, :cannot?, to: :ability
def ident
registrant_ident.to_s.split("-").last
registrant_ident.to_s.split('-').last
end
def country_code
registrant_ident.to_s.split('-').first
end
def domains
ident_cc, ident = registrant_ident.to_s.split '-'
Domain.includes(:registrar, :registrant).where(contacts: {
ident_type: 'priv',
ident: ident, #identity_code,
ident_country_code: ident_cc #country_code
})
Domain.includes(:registrar, :registrant).where(
contacts: {
ident_type: 'priv',
ident: ident,
ident_country_code: country_code
}
)
end
def contacts
Contact.where(ident_type: 'priv', ident: ident, ident_country_code: country_code)
end
def administrated_domains
Domain.joins(:domain_contacts)
.where(domain_contacts: { contact_id: contacts, type: AdminDomainContact })
end
def to_s

View file

@ -1,7 +1,7 @@
require 'test_helper'
require 'auth_token/auth_token_creator'
class RegistrantApiDomainRegistryLockTest < ApplicationIntegrationTest
class RegistrantApiRegistryLocksTest < ApplicationIntegrationTest
def setup
super

View file

@ -0,0 +1,56 @@
require 'test_helper'
class RegistrantUserCreationTest < ActiveSupport::TestCase
def test_find_or_create_by_api_data_creates_a_user
user_data = {
ident: '37710100070',
first_name: 'JOHN',
last_name: 'SMITH'
}
RegistrantUser.find_or_create_by_api_data(user_data)
user = User.find_by(registrant_ident: 'EE-37710100070')
assert_equal('JOHN SMITH', user.username)
end
def test_find_or_create_by_api_data_creates_a_user_after_upcasing_input
user_data = {
ident: '37710100070',
first_name: 'John',
last_name: 'Smith'
}
RegistrantUser.find_or_create_by_api_data(user_data)
user = User.find_by(registrant_ident: 'EE-37710100070')
assert_equal('JOHN SMITH', user.username)
end
def test_find_or_create_by_mid_data_creates_a_user
user_data = OpenStruct.new(user_country: 'EE', user_id_code: '37710100070',
user_givenname: 'JOHN', user_surname: 'SMITH')
RegistrantUser.find_or_create_by_mid_data(user_data)
user = User.find_by(registrant_ident: 'EE-37710100070')
assert_equal('JOHN SMITH', user.username)
end
def test_find_or_create_by_idc_with_legacy_header_creates_a_user
header = '/C=EE/O=ESTEID/OU=authentication/CN=SMITH,JOHN,37710100070/SN=SMITH/GN=JOHN/serialNumber=37710100070'
RegistrantUser.find_or_create_by_idc_data(header, RegistrantUser::ACCEPTED_ISSUER)
user = User.find_by(registrant_ident: 'EE-37710100070')
assert_equal('JOHN SMITH', user.username)
end
def test_find_or_create_by_idc_with_rfc2253_header_creates_a_user
header = 'serialNumber=37710100070,GN=JOHN,SN=SMITH,CN=SMITH\\,JOHN\\,37710100070,OU=authentication,O=ESTEID,C=EE'
RegistrantUser.find_or_create_by_idc_data(header, RegistrantUser::ACCEPTED_ISSUER)
user = User.find_by(registrant_ident: 'EE-37710100070')
assert_equal('JOHN SMITH', user.username)
end
end

View file

@ -1,62 +1,31 @@
require 'test_helper'
class RegistrantUserTest < ActiveSupport::TestCase
def setup
super
@user = RegistrantUser.new(registrant_ident: 'US-1234')
end
def teardown
super
end
def test_find_or_create_by_api_data_creates_a_user
user_data = {
ident: '37710100070',
first_name: 'JOHN',
last_name: 'SMITH'
}
RegistrantUser.find_or_create_by_api_data(user_data)
user = User.find_by(registrant_ident: 'EE-37710100070')
assert_equal('JOHN SMITH', user.username)
def test_domains_returns_an_list_of_domains_associated_with_a_specific_id_code
domain_names = @user.domains.pluck(:name)
assert_equal(3, domain_names.length)
end
def test_find_or_create_by_api_data_creates_a_user_after_upcasing_input
user_data = {
ident: '37710100070',
first_name: 'John',
last_name: 'Smith'
}
RegistrantUser.find_or_create_by_api_data(user_data)
user = User.find_by(registrant_ident: 'EE-37710100070')
assert_equal('JOHN SMITH', user.username)
def test_administrated_domains_returns_a_list_of_domains_that_is_smaller_than_domains
assert_equal(2, @user.administrated_domains.count)
end
def test_find_or_create_by_mid_data_creates_a_user
user_data = OpenStruct.new(user_country: 'EE', user_id_code: '37710100070',
user_givenname: 'JOHN', user_surname: 'SMITH')
RegistrantUser.find_or_create_by_mid_data(user_data)
user = User.find_by(registrant_ident: 'EE-37710100070')
assert_equal('JOHN SMITH', user.username)
def test_contacts_returns_an_list_of_contacts_associated_with_a_specific_id_code
assert_equal(1, @user.contacts.count)
end
def test_find_or_create_by_idc_with_legacy_header_creates_a_user
header = '/C=EE/O=ESTEID/OU=authentication/CN=SMITH,JOHN,37710100070/SN=SMITH/GN=JOHN/serialNumber=37710100070'
RegistrantUser.find_or_create_by_idc_data(header, RegistrantUser::ACCEPTED_ISSUER)
user = User.find_by(registrant_ident: 'EE-37710100070')
assert_equal('JOHN SMITH', user.username)
end
def test_find_or_create_by_idc_with_rfc2253_header_creates_a_user
header = 'serialNumber=37710100070,GN=JOHN,SN=SMITH,CN=SMITH\\,JOHN\\,37710100070,OU=authentication,O=ESTEID,C=EE'
RegistrantUser.find_or_create_by_idc_data(header, RegistrantUser::ACCEPTED_ISSUER)
user = User.find_by(registrant_ident: 'EE-37710100070')
assert_equal('JOHN SMITH', user.username)
def test_ident_and_country_code_helper_methods
assert_equal('1234', @user.ident)
assert_equal('US', @user.country_code)
end
end