test refactoring

This commit is contained in:
olegphenomenon 2021-12-13 10:54:09 +02:00
parent 4567e56457
commit 72f184d9b0
7 changed files with 65 additions and 73 deletions

View file

@ -17,8 +17,7 @@ module Admin
if @api_user.valid? if @api_user.valid?
@api_user.save! @api_user.save!
redirect_to admin_registrar_api_user_path(@api_user.registrar, @api_user), redirect_to admin_registrar_api_user_path(@api_user.registrar, @api_user), notice: t('.created')
notice: t('.created')
else else
render 'new' render 'new'
end end
@ -35,8 +34,7 @@ module Admin
if @api_user.valid? if @api_user.valid?
@api_user.save! @api_user.save!
redirect_to admin_registrar_api_user_path(@api_user.registrar, @api_user), redirect_to admin_registrar_api_user_path(@api_user.registrar, @api_user), notice: t('.updated')
notice: t('.updated')
else else
render 'edit' render 'edit'
end end
@ -50,7 +48,7 @@ module Admin
def set_test_date_to_api_user def set_test_date_to_api_user
user_api = User.find(params[:user_api_id]) user_api = User.find(params[:user_api_id])
uri = URI.parse((ENV['registry_demo_registrar_api_user_url'] || 'http://testapi.test') + "?username=#{user_api.username}&identity_code=#{user_api.identity_code}") uri = URI.parse((ENV['registry_demo_registrar_api_user_url']) + "?username=#{user_api.username}&identity_code=#{user_api.identity_code}")
response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port']) response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port'])
@ -58,8 +56,7 @@ module Admin
result = JSON.parse(response.body) result = JSON.parse(response.body)
demo_user_api = result['user_api'] demo_user_api = result['user_api']
Actions::RecordDateOfTest.record_result_to_api_user( Actions::RecordDateOfTest.record_result_to_api_user(api_user:user_api,
api_user:user_api,
date: demo_user_api['accreditation_date']) unless demo_user_api.empty? date: demo_user_api['accreditation_date']) unless demo_user_api.empty?
return redirect_to request.referrer, notice: 'User Api found' return redirect_to request.referrer, notice: 'User Api found'
else else

View file

@ -60,17 +60,17 @@ module Admin
def set_test_date def set_test_date
registrar = Registrar.find(params[:registrar_id]) registrar = Registrar.find(params[:registrar_id])
uri = URI.parse((ENV['registry_demo_registrar_results_url'] || 'http://testapi.test') + "?registrar_name=#{registrar.name}") uri = URI.parse((ENV['registry_demo_registrar_results_url']) + "?registrar_name=#{registrar.name}")
response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port']) response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port'])
if response.code == "200" if response.code == "200"
return record_result_for_each_api_user(response: response) return record_result_for_each_api_user(response: response)
else else
return redirect_to request.referrer, notice: 'Registrar no found' return redirect_to request.referer, notice: 'Registrar no found'
end end
redirect_to request.referrer, notice: 'Something goes wrong' redirect_to request.referer, notice: 'Something goes wrong'
end end
def remove_test_date def remove_test_date
@ -81,7 +81,7 @@ module Admin
api.save api.save
end end
redirect_to request.referrer redirect_to request.referer
end end
private private
@ -90,14 +90,14 @@ module Admin
result = JSON.parse(response.body) result = JSON.parse(response.body)
registrar_users = result['registrar_users'] registrar_users = result['registrar_users']
return redirect_to request.referrer, notice: 'Registrar found, but not accreditated yet' if registrar_users.empty? return redirect_to request.referer, notice: 'Registrar found, but not accreditated yet' if registrar_users.empty?
registrar_users.each do |api| registrar_users.each do |api|
a = ApiUser.find_by(username: api['username'], identity_code: api['identity_code']) a = ApiUser.find_by(username: api['username'], identity_code: api['identity_code'])
Actions::RecordDateOfTest.record_result_to_api_user(api_user: a, date: api['accreditation_date']) unless a.nil? Actions::RecordDateOfTest.record_result_to_api_user(api_user: a, date: api['accreditation_date']) unless a.nil?
end end
redirect_to request.referrer, notice: 'Registrar found' redirect_to request.referer, notice: 'Registrar found'
end end
def base_get_request(uri:, port:) def base_get_request(uri:, port:)

View file

@ -1,22 +1,11 @@
class SyncAccreditedUsersJob < ApplicationJob class SyncAccreditedUsersJob < ApplicationJob
def perform def perform
# apiusers_from_test = Actions::GetAccrResultsFromAnotherDb.list_of_accredated_users
# return if apiusers_from_test.nil?
# apiusers_from_test.each do |api|
# a = ApiUser.find_by(username: api.username, identity_code: api.identity_code)
# Actions::RecordDateOfTest.record_result_to_api_user(a, api.accreditation_date) unless a.nil?
# end
uri = URI.parse(ENV['registry_demo_accredited_users_url']) uri = URI.parse(ENV['registry_demo_accredited_users_url'])
response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port']) response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port'])
if response.code == "200" if response.code == '200'
result = JSON.parse(response.body) result = JSON.parse(response.body)
users = result['users'] result['users'].each do |api|
users.each do |api|
a = ApiUser.find_by(username: api.username, identity_code: api.identity_code) a = ApiUser.find_by(username: api.username, identity_code: api.identity_code)
Actions::RecordDateOfTest.record_result_to_api_user(a, api.accreditation_date) unless a.nil? Actions::RecordDateOfTest.record_result_to_api_user(a, api.accreditation_date) unless a.nil?
end end
@ -24,7 +13,7 @@ class SyncAccreditedUsersJob < ApplicationJob
logger.warn 'User not found' logger.warn 'User not found'
end end
return nil
end end
private private

View file

@ -5416,5 +5416,3 @@ INSERT INTO "schema_migrations" (version) VALUES
('20220504090512'), ('20220504090512'),
('20220524130709'), ('20220524130709'),
('20220818075833'); ('20220818075833');

View file

@ -4,6 +4,8 @@ class AdminAreaRegistrarsIntegrationTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers include Devise::Test::IntegrationHelpers
setup do setup do
ENV['registry_demo_registrar_api_user_url'] = 'http://registry.test:3000/api/v1/accreditation_center/show_api_user'
ENV['registry_demo_registrar_port'] = '3000'
@api_user = users(:api_bestnames) @api_user = users(:api_bestnames)
sign_in users(:admin) sign_in users(:admin)
end end
@ -22,14 +24,16 @@ class AdminAreaRegistrarsIntegrationTest < ActionDispatch::IntegrationTest
assert_equal api_user.accreditation_date, date assert_equal api_user.accreditation_date, date
# api_v1_accreditation_center_show_api_user_url # api_v1_accreditation_center_show_api_user_url
stub_request(:get, "http://registry.test:3000/api/v1/accreditation_center/show_api_user?identity_code=#{@api_user.identity_code}&username=#{@api_user.username}"). stub_request(:get, "http://registry.test:3000/api/v1/accreditation_center/show_api_user?username=#{@api_user.username}&identity_code=#{@api_user.identity_code}")
with( .with(
headers: { headers: {
'Accept'=>'*/*', 'Accept' => '*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent'=>'Ruby' 'User-Agent' => 'Ruby'
}).to_return(status: 200, body: { code: 200, user_api: api_user }.to_json, headers: {}) }
post set_test_date_to_api_user_admin_registrars_path, params: { user_api_id: @api_user.id }, headers: { "HTTP_REFERER" => root_path } )
.to_return(status: 200, body: { code: 200, user_api: api_user }.to_json, headers: {})
post set_test_date_to_api_user_admin_registrars_path, params: { user_api_id: @api_user.id }, headers: { 'HTTP_REFERER' => root_path }
@api_user.reload @api_user.reload
assert_equal @api_user.accreditation_date.to_date, api_user.accreditation_date.to_date assert_equal @api_user.accreditation_date.to_date, api_user.accreditation_date.to_date
assert_equal @api_user.accreditation_expire_date.to_date, api_user.accreditation_expire_date.to_date assert_equal @api_user.accreditation_expire_date.to_date, api_user.accreditation_expire_date.to_date

View file

@ -4,6 +4,8 @@ class AdminAreaRegistrarsIntegrationTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers include Devise::Test::IntegrationHelpers
setup do setup do
ENV['registry_demo_registrar_results_url'] = 'http://registry.test:3000/api/v1/accreditation_center/results'
ENV['registry_demo_registrar_port'] = '3000'
@registrar = registrars(:bestnames) @registrar = registrars(:bestnames)
sign_in users(:admin) sign_in users(:admin)
end end
@ -26,15 +28,17 @@ class AdminAreaRegistrarsIntegrationTest < ActionDispatch::IntegrationTest
assert_nil @registrar.api_users.first.accreditation_date assert_nil @registrar.api_users.first.accreditation_date
stub_request(:get, "http://registry.test:3000/api/v1/accreditation_center/results?registrar_name=#{@registrar.name}"). stub_request(:get, "http://registry.test:3000/api/v1/accreditation_center/results?registrar_name=#{@registrar.name}")
with( .with(
headers: { headers: {
'Accept'=>'*/*', 'Accept' => '*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent'=>'Ruby' 'User-Agent' => 'Ruby'
}).to_return(status: 200, body: { code: 200, registrar_users: [api_user] }.to_json, headers: {}) }
)
.to_return(status: 200, body: { code: 200, registrar_users: [api_user] }.to_json, headers: {})
post set_test_date_admin_registrars_path, params: { registrar_id: @registrar.id }, headers: { "HTTP_REFERER" => root_path } post set_test_date_admin_registrars_path, params: { registrar_id: @registrar.id }, headers: { 'HTTP_REFERER' => root_path }
@registrar.reload @registrar.reload
assert_equal @registrar.api_users.first.accreditation_date.to_date, api_user.accreditation_date.to_date assert_equal @registrar.api_users.first.accreditation_date.to_date, api_user.accreditation_date.to_date