From 1bb975c0ccf79ec63432fdcb6fa950754f4bbc7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 4 Feb 2021 14:17:26 +0200 Subject: [PATCH 01/10] Exclude tech from default domain query --- .../api/v1/registrant/domains_controller.rb | 2 +- app/models/domain.rb | 15 +++++++++++++-- app/models/registrant_user.rb | 4 +++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/registrant/domains_controller.rb b/app/controllers/api/v1/registrant/domains_controller.rb index 73b534598..c10789d76 100644 --- a/app/controllers/api/v1/registrant/domains_controller.rb +++ b/app/controllers/api/v1/registrant/domains_controller.rb @@ -42,7 +42,7 @@ module Api private def current_user_domains - current_registrant_user.domains + current_registrant_user.domains(admin: params[:tech] != 'true') rescue CompanyRegister::NotAvailableError current_registrant_user.direct_domains end diff --git a/app/models/domain.rb b/app/models/domain.rb index 53f0fa5b6..6837e6905 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -198,6 +198,15 @@ class Domain < ApplicationRecord Setting.nameserver_required end + def registrant_user_admin_registrant_domains(registrant_user) + companies = Contact.registrant_user_company_contacts(registrant_user) + from( + "(#{registrant_user_administered_domains(registrant_user).to_sql} UNION " \ + "#{registrant_user_company_registrant(registrant_user).to_sql} UNION " \ + "#{registrant_user_domains_company(companies, except_tech: true).to_sql}) AS domains" + ) + end + def registrant_user_domains(registrant_user) from( "(#{registrant_user_domains_by_registrant(registrant_user).to_sql} UNION " \ @@ -255,8 +264,10 @@ class Domain < ApplicationRecord where(registrant: companies) end - def registrant_user_domains_company(companies) - joins(:domain_contacts).where(domain_contacts: { contact: companies }) + def registrant_user_domains_company(companies, except_tech: false) + request = { contact: companies } + request[:type] = [AdminDomainContact.name] if except_tech + joins(:domain_contacts).where(domain_contacts: request) end end diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index aa41ccff8..92a9c61ef 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -30,7 +30,9 @@ class RegistrantUser < User Contact.registrant_user_direct_contacts(self) end - def domains + def domains(admin: false) + return Domain.registrant_user_admin_registrant_domains(self) if admin + Domain.registrant_user_domains(self) end From 419396f5b47e17ff12ae986c5db8c679f12ac474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Thu, 4 Feb 2021 15:59:37 +0200 Subject: [PATCH 02/10] Get admin/registrant domains when company registry is down --- .../api/v1/registrant/domains_controller.rb | 2 +- app/models/domain.rb | 14 ++++++++++++-- app/models/registrant_user.rb | 4 +++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/registrant/domains_controller.rb b/app/controllers/api/v1/registrant/domains_controller.rb index c10789d76..0e380eb57 100644 --- a/app/controllers/api/v1/registrant/domains_controller.rb +++ b/app/controllers/api/v1/registrant/domains_controller.rb @@ -44,7 +44,7 @@ module Api def current_user_domains current_registrant_user.domains(admin: params[:tech] != 'true') rescue CompanyRegister::NotAvailableError - current_registrant_user.direct_domains + current_registrant_user.direct_domains(admin: params[:tech] != 'true') end end end diff --git a/app/models/domain.rb b/app/models/domain.rb index 6837e6905..f2019dcb2 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -207,6 +207,14 @@ class Domain < ApplicationRecord ) end + def registrant_user_direct_admin_registrant_domains(registrant_user) + from( + "(#{registrant_user_direct_domains_by_registrant(registrant_user).to_sql} UNION " \ + "#{registrant_user_direct_domains_by_contact(registrant_user, + except_tech: true).to_sql}) AS domains" + ) + end + def registrant_user_domains(registrant_user) from( "(#{registrant_user_domains_by_registrant(registrant_user).to_sql} UNION " \ @@ -256,8 +264,10 @@ class Domain < ApplicationRecord where(registrant: registrant_user.direct_contacts) end - def registrant_user_direct_domains_by_contact(registrant_user) - joins(:domain_contacts).where(domain_contacts: { contact_id: registrant_user.direct_contacts }) + def registrant_user_direct_domains_by_contact(registrant_user, except_tech: false) + request = { contact_id: registrant_user.direct_contacts } + request[:type] = [AdminDomainContact.name] if except_tech + joins(:domain_contacts).where(domain_contacts: request) end def registrant_user_company_registrant(companies) diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index 92a9c61ef..efcb08288 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -36,7 +36,9 @@ class RegistrantUser < User Domain.registrant_user_domains(self) end - def direct_domains + def direct_domains(admin: false) + return Domain.registrant_user_direct_admin_registrant_domains(self) if admin + Domain.registrant_user_direct_domains(self) end From 9854f82669e9906136a097ea355817069eb49ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Fri, 5 Feb 2021 12:00:49 +0200 Subject: [PATCH 03/10] Registrant API: Add registrant org true/false attribute --- lib/serializers/registrant_api/domain.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/serializers/registrant_api/domain.rb b/lib/serializers/registrant_api/domain.rb index 64913a7fb..ed6eb2b9c 100644 --- a/lib/serializers/registrant_api/domain.rb +++ b/lib/serializers/registrant_api/domain.rb @@ -25,6 +25,7 @@ module Serializers registrant: { name: domain.registrant.name, id: domain.registrant.uuid, + org: domain.registrant.org?, }, tech_contacts: contacts(:tech), admin_contacts: contacts(:admin), @@ -60,7 +61,7 @@ module Serializers registrar: { name: domain.registrar.name, website: domain.registrar.website }, registrant: { name: domain.registrant.name, id: domain.registrant.uuid, phone: domain.registrant.phone, email: domain.registrant.email, - ident: domain.registrant.ident } + ident: domain.registrant.ident, org: domain.registrant.org? } } end From 84755226a381367ccb411a1687a51dcef5e855ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Fri, 5 Feb 2021 12:06:38 +0200 Subject: [PATCH 04/10] Reflect new keys in registrant API tests --- .../integration/api/registrant/registrant_api_domains_test.rb | 2 +- .../api/registrant/registrant_api_registry_locks_test.rb | 2 +- test/lib/serializers/registrant_api/domain_test.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/integration/api/registrant/registrant_api_domains_test.rb b/test/integration/api/registrant/registrant_api_domains_test.rb index 61d635e5f..a688152c6 100644 --- a/test/integration/api/registrant/registrant_api_domains_test.rb +++ b/test/integration/api/registrant/registrant_api_domains_test.rb @@ -19,7 +19,7 @@ class RegistrantApiDomainsTest < ApplicationIntegrationTest assert_equal('hospital.test', domain[:name]) assert_equal('5edda1a5-3548-41ee-8b65-6d60daf85a37', domain[:id]) - assert_equal({name: 'John', id: 'eb2f2766-b44c-4e14-9f16-32ab1a7cb957'}, domain[:registrant]) + assert_equal({name: 'John', id: 'eb2f2766-b44c-4e14-9f16-32ab1a7cb957', org: false}, domain[:registrant]) assert_equal([{name: 'John', id: 'eb2f2766-b44c-4e14-9f16-32ab1a7cb957', email: 'john@inbox.test'}], diff --git a/test/integration/api/registrant/registrant_api_registry_locks_test.rb b/test/integration/api/registrant/registrant_api_registry_locks_test.rb index fb6d13aca..d024bb6c1 100644 --- a/test/integration/api/registrant/registrant_api_registry_locks_test.rb +++ b/test/integration/api/registrant/registrant_api_registry_locks_test.rb @@ -130,7 +130,7 @@ class RegistrantApiRegistryLocksTest < ApplicationIntegrationTest response_json = JSON.parse(response.body, symbolize_names: true) assert_equal({ name: 'Best Names', website: 'https://bestnames.test' }, response_json[:registrar]) - assert_equal({name: 'John', id: 'eb2f2766-b44c-4e14-9f16-32ab1a7cb957'}, response_json[:registrant]) + assert_equal({name: 'John', id: 'eb2f2766-b44c-4e14-9f16-32ab1a7cb957', org: false}, response_json[:registrant]) assert_equal([{name: 'Jane', id: '9db3de62-2414-4487-bee2-d5c155567768', email: 'jane@mail.test' diff --git a/test/lib/serializers/registrant_api/domain_test.rb b/test/lib/serializers/registrant_api/domain_test.rb index f2623741f..d30a50ea3 100644 --- a/test/lib/serializers/registrant_api/domain_test.rb +++ b/test/lib/serializers/registrant_api/domain_test.rb @@ -30,8 +30,8 @@ class SerializersRegistrantApiDomainTest < ActiveSupport::TestCase assert_equal({name: 'Best Names', website: 'https://bestnames.test' }, @json[:registrar]) end - def test_returns_registrant_name_and_uuid - assert_equal({name: 'John', id: 'eb2f2766-b44c-4e14-9f16-32ab1a7cb957'}, + def test_returns_registrant_name_uuid_and_org + assert_equal({name: 'John', id: 'eb2f2766-b44c-4e14-9f16-32ab1a7cb957', org: false}, @json[:registrant]) end From d4312caa91bc64eed1e11a0323cfaa638cdb15de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Fri, 5 Feb 2021 16:54:42 +0200 Subject: [PATCH 05/10] Registrant API: Include unscoped domain list size --- app/controllers/api/v1/registrant/domains_controller.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/registrant/domains_controller.rb b/app/controllers/api/v1/registrant/domains_controller.rb index 0e380eb57..12b060e6c 100644 --- a/app/controllers/api/v1/registrant/domains_controller.rb +++ b/app/controllers/api/v1/registrant/domains_controller.rb @@ -25,7 +25,8 @@ module Api serializer.to_json end - render json: { count: domains.count, domains: serialized_domains } + render json: { total: current_user_domains_total_count, count: domains.count, + domains: serialized_domains } end def show @@ -41,6 +42,12 @@ module Api private + def current_user_domains_total_count + current_registrant_user.domains.count + rescue CompanyRegister::NotAvailableError + current_registrant_user.direct_domains.count + end + def current_user_domains current_registrant_user.domains(admin: params[:tech] != 'true') rescue CompanyRegister::NotAvailableError From e488c839dd4dfe26b7844e8c7891313a77d6ec29 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Tue, 9 Feb 2021 16:59:58 +0200 Subject: [PATCH 06/10] added tests for api endpoint --- .../registrant/registrant_api_domains_test.rb | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/test/integration/api/registrant/registrant_api_domains_test.rb b/test/integration/api/registrant/registrant_api_domains_test.rb index a688152c6..a919a318b 100644 --- a/test/integration/api/registrant/registrant_api_domains_test.rb +++ b/test/integration/api/registrant/registrant_api_domains_test.rb @@ -5,9 +5,10 @@ class RegistrantApiDomainsTest < ApplicationIntegrationTest def setup super - @domain = domains(:hospital) + @domain = domains(:airport) @registrant = @domain.registrant @user = users(:registrant) + domains(:metro).tech_domain_contacts.update(contact_id: @registrant.id) @auth_headers = { 'HTTP_AUTHORIZATION' => auth_token } end @@ -57,6 +58,37 @@ class RegistrantApiDomainsTest < ApplicationIntegrationTest assert(array_of_domain_registrars.include?({name: 'Good Names', website: nil})) end + def test_return_domain_list_with_registrants_and_admins + domains(:hospital).admin_domain_contacts.update(contact_id: contacts(:william).id) + domains(:hospital).update(registrant: contacts(:william).becomes(Registrant)) + + get '/api/v1/registrant/domains', headers: @auth_headers, params: { 'offset' => 0 } + assert_equal(200, response.status) + + response_json = JSON.parse(response.body, symbolize_names: true) + response_json[:domains].each do |x| + if x[:registrant][:org] == false + x[:tech_contacts].each do |s| + assert_not s[:name].include?(@registrant.name) + end + end + end + end + + def test_return_domain_list_with_registrants_and_admins_tech + get '/api/v1/registrant/domains', headers: @auth_headers, params: { 'offset' => 0, 'tech' => true } + assert_equal(200, response.status) + + response_json = JSON.parse(response.body, symbolize_names: true) + response_json[:domains].each do |x| + if x[:name] == 'metro.test' + x[:tech_contacts].each do |s| + assert s[:name].include?(@registrant.name) + end + end + end + end + def test_root_accepts_limit_and_offset_parameters get '/api/v1/registrant/domains', params: { 'limit' => 2, 'offset' => 0 }, headers: @auth_headers From d19bb69342bb29b523b4b73dbe09a471b16b7fa3 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Wed, 10 Feb 2021 09:58:22 +0200 Subject: [PATCH 07/10] added test for total count --- .../api/registrant/registrant_api_domains_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/integration/api/registrant/registrant_api_domains_test.rb b/test/integration/api/registrant/registrant_api_domains_test.rb index a919a318b..e8c68638c 100644 --- a/test/integration/api/registrant/registrant_api_domains_test.rb +++ b/test/integration/api/registrant/registrant_api_domains_test.rb @@ -89,6 +89,15 @@ class RegistrantApiDomainsTest < ApplicationIntegrationTest end end + def test_domains_total_if_an_incomplete_list_is_returned + get '/api/v1/registrant/domains', headers: @auth_headers, params: { 'offset' => 0 } + assert_equal(200, response.status) + + response_json = JSON.parse(response.body, symbolize_names: true) + assert_equal response_json[:domains].length, 4 + assert_equal response_json[:total], 5 + end + def test_root_accepts_limit_and_offset_parameters get '/api/v1/registrant/domains', params: { 'limit' => 2, 'offset' => 0 }, headers: @auth_headers From 951d95a3e8c4292223ffe227e81489240462525f Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Wed, 10 Feb 2021 10:17:50 +0200 Subject: [PATCH 08/10] added test for total count --- test/integration/api/registrant/registrant_api_domains_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/api/registrant/registrant_api_domains_test.rb b/test/integration/api/registrant/registrant_api_domains_test.rb index e8c68638c..4cfa6bd55 100644 --- a/test/integration/api/registrant/registrant_api_domains_test.rb +++ b/test/integration/api/registrant/registrant_api_domains_test.rb @@ -94,7 +94,7 @@ class RegistrantApiDomainsTest < ApplicationIntegrationTest assert_equal(200, response.status) response_json = JSON.parse(response.body, symbolize_names: true) - assert_equal response_json[:domains].length, 4 + assert_equal response_json[:domains].length, response_json[:count] assert_equal response_json[:total], 5 end From d8642657606a05244c51881a138f9ec2be619cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Tue, 23 Feb 2021 13:41:17 +0200 Subject: [PATCH 09/10] Registrant API: Expose Bearer token internally --- app/controllers/api/v1/registrant/auth_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/api/v1/registrant/auth_controller.rb b/app/controllers/api/v1/registrant/auth_controller.rb index 03dfa45f3..30f3ed973 100644 --- a/app/controllers/api/v1/registrant/auth_controller.rb +++ b/app/controllers/api/v1/registrant/auth_controller.rb @@ -19,6 +19,8 @@ module Api token = create_token(user) if token + ToStdout.msg("Bearer for #{eid_params[:first_name]} #{eid_params[:last_name]} " \ + "(#{eid_params[:ident]}) - '#{token[:access_token]}'") render json: token else render json: { errors: [{ base: ['Cannot create generate session token'] }] } From 34e71cc2c8ed42bf1dc0acc8ba1f1a0acb97b918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Erik=20=C3=95unapuu?= Date: Tue, 23 Feb 2021 15:09:50 +0200 Subject: [PATCH 10/10] Skip bearer token logging when production env --- app/controllers/api/v1/registrant/auth_controller.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/registrant/auth_controller.rb b/app/controllers/api/v1/registrant/auth_controller.rb index 30f3ed973..8c72106d0 100644 --- a/app/controllers/api/v1/registrant/auth_controller.rb +++ b/app/controllers/api/v1/registrant/auth_controller.rb @@ -19,8 +19,9 @@ module Api token = create_token(user) if token - ToStdout.msg("Bearer for #{eid_params[:first_name]} #{eid_params[:last_name]} " \ - "(#{eid_params[:ident]}) - '#{token[:access_token]}'") + msg = "Bearer for #{eid_params[:first_name]} #{eid_params[:last_name]} " \ + "(#{eid_params[:ident]}) - '#{token[:access_token]}'" + ToStdout.msg(msg) unless Rails.env.production? render json: token else render json: { errors: [{ base: ['Cannot create generate session token'] }] }