From c07abaea59b03b63b50590821dfbdfa64c2b5448 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Fri, 10 Dec 2021 16:53:25 +0200 Subject: [PATCH 1/7] added endpoints to demo registry for accr results --- app/controllers/admin/api_users_controller.rb | 38 +++++++++++ .../admin/registrars_controller.rb | 50 +++++++++++++++ .../accreditation_center/base_controller.rb | 32 +++++----- .../results_controller.rb | 40 ++++++++++++ .../accreditation_info_controller.rb | 50 ++++++++------- .../accreditation_results_controller.rb | 64 ++++++++++--------- .../actions/record_date_of_test.rb | 22 +++++++ app/jobs/sync_accredited_users_job.rb | 38 +++++++++++ app/models/api_user.rb | 10 +++ app/models/registrar.rb | 10 +++ app/views/admin/api_users/_api_user.html.erb | 12 ++++ app/views/admin/api_users/index.html.erb | 9 ++- app/views/admin/api_users/show.html.erb | 14 +++- app/views/admin/registrars/index.html.erb | 15 +++++ .../admin/registrars/show/_api_users.html.erb | 13 ++++ config/locales/admin/api_users.en.yml | 2 + config/locales/admin/registrars.en.yml | 4 ++ config/locales/en.yml | 2 + config/routes.rb | 16 ++++- test/integration/admin_area/api_users_test.rb | 38 +++++++++++ 20 files changed, 401 insertions(+), 78 deletions(-) create mode 100644 app/controllers/api/v1/accreditation_center/results_controller.rb create mode 100644 app/interactions/actions/record_date_of_test.rb create mode 100644 app/jobs/sync_accredited_users_job.rb create mode 100644 test/integration/admin_area/api_users_test.rb diff --git a/app/controllers/admin/api_users_controller.rb b/app/controllers/admin/api_users_controller.rb index cbef7def2..5021fe15a 100644 --- a/app/controllers/admin/api_users_controller.rb +++ b/app/controllers/admin/api_users_controller.rb @@ -47,8 +47,46 @@ module Admin redirect_to admin_registrar_path(@api_user.registrar), notice: t('.deleted') end + def set_test_date_to_api_user + user_api = User.find(params[:user_api_id]) + + 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']) + + if response.code == "200" + result = JSON.parse(response.body) + demo_user_api = result['user_api'] + + Actions::RecordDateOfTest.record_result_to_api_user( + api_user:user_api, + date: demo_user_api['accreditation_date']) unless demo_user_api.empty? + return redirect_to request.referrer, notice: 'User Api found' + else + return redirect_to request.referrer, notice: 'User Api no found or not accriditated yet' + end + + redirect_to request.referrer, notice: 'Something goes wrong' + end + + def remove_test_date_to_api_user + user_api = User.find(params[:user_api_id]) + user_api.accreditation_date = nil + user_api.accreditation_expire_date = nil + user_api.save + + redirect_to request.referrer + end + private + def base_get_request(uri:, port:) + http = Net::HTTP.new(uri.host, port) + req = Net::HTTP::Get.new(uri.request_uri) + + http.request(req) + end + def api_user_params params.require(:api_user).permit(:username, :plain_text_password, :active, :identity_code, { roles: [] }) diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index a803a8e7d..9c2485813 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -1,3 +1,5 @@ +require 'net/http' + module Admin class RegistrarsController < BaseController # rubocop:disable Metrics/ClassLength load_and_authorize_resource @@ -55,8 +57,56 @@ module Admin end end + def set_test_date + registrar = Registrar.find(params[:registrar_id]) + + 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']) + + if response.code == "200" + return record_result_for_each_api_user(response: response) + else + return redirect_to request.referrer, notice: 'Registrar no found' + end + + redirect_to request.referrer, notice: 'Something goes wrong' + end + + def remove_test_date + registrar = Registrar.find(params[:registrar_id]) + registrar.api_users.each do |api| + api.accreditation_date = nil + api.accreditation_expire_date = nil + api.save + end + + redirect_to request.referrer + end + private + def record_result_for_each_api_user(response:) + result = JSON.parse(response.body) + registrar_users = result['registrar_users'] + + return redirect_to request.referrer, notice: 'Registrar found, but not accreditated yet' if registrar_users.empty? + + registrar_users.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 + + redirect_to request.referrer, notice: 'Registrar found' + end + + def base_get_request(uri:, port:) + http = Net::HTTP.new(uri.host, port) + req = Net::HTTP::Get.new(uri.request_uri) + + http.request(req) + end + def filter_by_status case params[:status] when 'Active' diff --git a/app/controllers/api/v1/accreditation_center/base_controller.rb b/app/controllers/api/v1/accreditation_center/base_controller.rb index 7deb776b9..77f3b0338 100644 --- a/app/controllers/api/v1/accreditation_center/base_controller.rb +++ b/app/controllers/api/v1/accreditation_center/base_controller.rb @@ -3,24 +3,26 @@ require 'auth_token/auth_token_decryptor' module Api module V1 module AccreditationCenter - class BaseController < ActionController::API - rescue_from ActiveRecord::RecordNotFound, with: :show_not_found_error - rescue_from ActiveRecord::RecordInvalid, with: :show_invalid_record_error - rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception| - error = {} - error[parameter_missing_exception.param] = ['parameter is required'] - response = { errors: [error] } - render json: response, status: :unprocessable_entity - end + if Rails.env.development? || Rails.env.staging? || Rails.env.test? + class BaseController < ActionController::API + rescue_from ActiveRecord::RecordNotFound, with: :show_not_found_error + rescue_from ActiveRecord::RecordInvalid, with: :show_invalid_record_error + rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception| + error = {} + error[parameter_missing_exception.param] = ['parameter is required'] + response = { errors: [error] } + render json: response, status: :unprocessable_entity + end - private + private - def show_not_found_error - render json: { errors: [{ base: ['Not found'] }] }, status: :not_found - end + def show_not_found_error + render json: { errors: [{ base: ['Not found'] }] }, status: :not_found + end - def show_invalid_record_error(exception) - render json: { errors: exception.record.errors }, status: :bad_request + def show_invalid_record_error(exception) + render json: { errors: exception.record.errors }, status: :bad_request + end end end end diff --git a/app/controllers/api/v1/accreditation_center/results_controller.rb b/app/controllers/api/v1/accreditation_center/results_controller.rb new file mode 100644 index 000000000..8a6fd2d51 --- /dev/null +++ b/app/controllers/api/v1/accreditation_center/results_controller.rb @@ -0,0 +1,40 @@ +require 'serializers/repp/contact' + +module Api + module V1 + module AccreditationCenter + class ResultsController < ::Api::V1::AccreditationCenter::BaseController + def show + accr_users = [] + registrar = Registrar.find_by(name: params[:registrar_name]) + + return render json: { errors: 'Registrar not found' }, status: :not_found if registrar.nil? + + registrar.api_users.where.not(accreditation_date: nil).each do |u| + accr_users << u + end + + render json: { code: 1000, registrar_users: accr_users } + end + + def show_api_user + user_api = User.find_by(username: params[:username], identity_code: params[:identity_code]) + + return render json: { errors: 'User not found' }, status: :not_found if user_api.nil? + + return render json: { errors: 'No accreditated yet' }, status: :not_found if user_api.accreditation_date.nil? + + render json: { code: 1000, user_api: user_api } + end + + def list_accreditated_api_users + users = User.where.not(accreditation_date: nil) + + return render json: { errors: 'Accreditated users not found' }, status: :not_found if users.empty? + + render json: { code: 1000, users: users } + end + end + end + end +end diff --git a/app/controllers/repp/v1/registrar/accreditation_info_controller.rb b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb index cd86ce9ed..3157672d5 100644 --- a/app/controllers/repp/v1/registrar/accreditation_info_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb @@ -1,36 +1,38 @@ module Repp module V1 module Registrar - class AccreditationInfoController < BaseController - api :GET, 'repp/v1/registrar/accreditation/get_info' - desc 'check login user and return data' + if Rails.env.development? || Rails.env.staging? + class AccreditationInfoController < BaseController + api :GET, 'repp/v1/registrar/accreditation/get_info' + desc 'check login user and return data' - def index - login = current_user - registrar = current_user.registrar + def index + login = current_user + registrar = current_user.registrar - # rubocop:disable Style/AndOr - render_success(data: nil) and return unless login - # rubocop:enable Style/AndOr + # rubocop:disable Style/AndOr + render_success(data: nil) and return unless login + # rubocop:enable Style/AndOr - data = set_values_to_data(login: login, registrar: registrar) + data = set_values_to_data(login: login, registrar: registrar) - render_success(data: data) - end + render_success(data: data) + end - private + private - def set_values_to_data(login:, registrar:) - data = login.as_json(only: %i[id - username - name - uuid - roles - accreditation_date - accreditation_expire_date]) - data[:registrar_name] = registrar.name - data[:registrar_reg_no] = registrar.reg_no - data + def set_values_to_data(login:, registrar:) + data = login.as_json(only: %i[id + username + name + uuid + roles + accreditation_date + accreditation_expire_date]) + data[:registrar_name] = registrar.name + data[:registrar_reg_no] = registrar.reg_no + data + end end end end diff --git a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb index b58fd3895..b813754c5 100644 --- a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb @@ -1,30 +1,31 @@ module Repp module V1 module Registrar - class AccreditationResultsController < ActionController::API - before_action :authenticate_shared_key + if Rails.env.development? || Rails.env.staging? + class AccreditationResultsController < ActionController::API + before_action :authenticate_shared_key TEMPORARY_SECRET_KEY = ENV['accreditation_secret'].freeze EXPIRE_DEADLINE = 15.minutes.freeze - api :POST, 'repp/v1/registrar/accreditation/push_results' - desc 'added datetime results' + api :POST, 'repp/v1/registrar/accreditation/push_results' + desc 'added datetime results' - def create - username = params[:accreditation_result][:username] - result = params[:accreditation_result][:result] + def create + username = params[:accreditation_result][:username] + result = params[:accreditation_result][:result] - record_accreditation_result(username, result) if result - rescue ActiveRecord::RecordNotFound - record_not_found(username) - end + record_accreditation_result(username, result) if result + rescue ActiveRecord::RecordNotFound + record_not_found(username) + end - private + private - def record_accreditation_result(username, result) - user = ApiUser.find_by(username: username) + def record_accreditation_result(username, result) + user = ApiUser.find_by(username: username) - raise ActiveRecord::RecordNotFound if user.nil? + raise ActiveRecord::RecordNotFound if user.nil? user.accreditation_date = DateTime.current user.accreditation_expire_date = user.accreditation_date + EXPIRE_DEADLINE @@ -55,26 +56,27 @@ module Repp end end - def authenticate_shared_key - api_key = "Basic #{TEMPORARY_SECRET_KEY}" - render_failed unless api_key == request.authorization - end + def authenticate_shared_key + api_key = "Basic #{TEMPORARY_SECRET_KEY}" + render_failed unless api_key == request.authorization + end - def record_not_found(username) - @response = { code: 2303, message: "Object '#{username}' does not exist" } - render(json: @response) - end + def record_not_found(username) + @response = { code: 2303, message: "Object '#{username}' does not exist" } + render(json: @response) + end - def render_failed - @response = { code: 2202, message: 'Invalid authorization information' } - render(json: @response, status: :unauthorized) - end + def render_failed + @response = { code: 2202, message: 'Invalid authorization information' } + render(json: @response, status: :unauthorized) + end - def render_success(code: nil, message: nil, data: nil) - @response = { code: code || 1000, message: message || 'Command completed successfully', - data: data || {} } + def render_success(code: nil, message: nil, data: nil) + @response = { code: code || 1000, message: message || 'Command completed successfully', + data: data || {} } - render(json: @response, status: :ok) + render(json: @response, status: :ok) + end end end end diff --git a/app/interactions/actions/record_date_of_test.rb b/app/interactions/actions/record_date_of_test.rb new file mode 100644 index 000000000..53d309948 --- /dev/null +++ b/app/interactions/actions/record_date_of_test.rb @@ -0,0 +1,22 @@ +module Actions + module RecordDateOfTest + extend self + + TEST_DEADLINE = 1.year.freeze + + def record_result_to_api_user(api_user:, date:) + p "+++++++++++" + p api_user + p "-----------" + p DateTime.parse(date) + p "+++++++++++" + + api_user.accreditation_date = date + api_user.accreditation_expire_date = api_user.accreditation_date + TEST_DEADLINE + api_user.save + + # api_user.update(accreditation_date: date, + # accreditation_expire_date: DateTime.parse(date) + TEST_DEADLINE) + end + end +end diff --git a/app/jobs/sync_accredited_users_job.rb b/app/jobs/sync_accredited_users_job.rb new file mode 100644 index 000000000..8e02956e5 --- /dev/null +++ b/app/jobs/sync_accredited_users_job.rb @@ -0,0 +1,38 @@ +class SyncAccreditedUsersJob < ApplicationJob + 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']) + + response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port']) + + if response.code == "200" + result = JSON.parse(response.body) + users = result['users'] + + users.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 + else + logger.warn 'User not found' + end + + return + end + + private + + def base_get_request(uri:, port:) + http = Net::HTTP.new(uri.host, port) + req = Net::HTTP::Get.new(uri.request_uri) + + http.request(req) + end +end \ No newline at end of file diff --git a/app/models/api_user.rb b/app/models/api_user.rb index dc5cff0cc..a15b12a85 100644 --- a/app/models/api_user.rb +++ b/app/models/api_user.rb @@ -56,6 +56,16 @@ class ApiUser < User username end + def accredited? + !accreditation_date.nil? + end + + def accreditation_expired? + return false if accreditation_expire_date.nil? + + accreditation_expire_date < Time.zone.now + end + def unread_notifications registrar.notifications.unread end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 07d7d4795..5d6d7538f 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -190,6 +190,16 @@ class Registrar < ApplicationRecord # rubocop:disable Metrics/ClassLength white_ips.api.include_ip?(ip) end + def accredited? + api_users.any? do |a| + return true unless a.accreditation_date.nil? + end + end + + def accreditation_expired? + api_users.all? { |api| api.accreditation_expired? } + end + # Audit log is needed, therefore no raw SQL def replace_nameservers(hostname, new_attributes, domains: []) transaction do diff --git a/app/views/admin/api_users/_api_user.html.erb b/app/views/admin/api_users/_api_user.html.erb index d8412a519..5c443bfee 100644 --- a/app/views/admin/api_users/_api_user.html.erb +++ b/app/views/admin/api_users/_api_user.html.erb @@ -2,4 +2,16 @@ <%= link_to api_user, admin_registrar_api_user_path(api_user.registrar, api_user) %> <%= link_to api_user.registrar, admin_registrar_path(api_user.registrar) %> <%= api_user.active %> + + + <% if !api_user.accredited? || api_user.accreditation_expired? %> + <%= button_to t(:set_test_btn), + { controller: 'api_users', action: 'set_test_date_to_api_user', user_api_id: api_user.id }, + { method: :post, class: 'btn btn-primary'} %> + <% else %> + <%= button_to t(:remove_test_btn), + { controller: 'api_users', action: 'remove_test_date_to_api_user', user_api_id: api_user.id }, + { method: :post, class: 'btn btn-danger'} %> + <% end %> + diff --git a/app/views/admin/api_users/index.html.erb b/app/views/admin/api_users/index.html.erb index 40e620954..3564245be 100644 --- a/app/views/admin/api_users/index.html.erb +++ b/app/views/admin/api_users/index.html.erb @@ -10,15 +10,18 @@ - - - + diff --git a/app/views/admin/api_users/show.html.erb b/app/views/admin/api_users/show.html.erb index 05c5651ce..a85957fef 100644 --- a/app/views/admin/api_users/show.html.erb +++ b/app/views/admin/api_users/show.html.erb @@ -6,12 +6,12 @@ + + @@ -58,6 +62,17 @@ <%= content_tag(:span, x[:billing_email]) %> <% end %> + <% end %> diff --git a/app/views/admin/registrars/show/_api_users.html.erb b/app/views/admin/registrars/show/_api_users.html.erb index 2d10b1c56..fddccd9b7 100644 --- a/app/views/admin/registrars/show/_api_users.html.erb +++ b/app/views/admin/registrars/show/_api_users.html.erb @@ -8,6 +8,7 @@ + @@ -16,6 +17,18 @@ + <% end %> diff --git a/config/locales/admin/api_users.en.yml b/config/locales/admin/api_users.en.yml index 9fde2db14..c09df2ab7 100644 --- a/config/locales/admin/api_users.en.yml +++ b/config/locales/admin/api_users.en.yml @@ -3,6 +3,8 @@ en: api_users: index: header: API users + set_test_btn: Set Test + remove_test_btn: Remove Test new: header: New API user diff --git a/config/locales/admin/registrars.en.yml b/config/locales/admin/registrars.en.yml index 2f281a3b7..94225dc49 100644 --- a/config/locales/admin/registrars.en.yml +++ b/config/locales/admin/registrars.en.yml @@ -4,6 +4,8 @@ en: index: header: Registrars new_btn: New registrar + set_test_btn: Set Test + remove_test_btn: Remove Test new: header: New registrar @@ -28,6 +30,8 @@ en: api_users: header: API Users new_btn: New API user + set_test_btn: Set Test + remove_test_btn: Remove Test white_ips: header: Whitelisted IPs diff --git a/config/locales/en.yml b/config/locales/en.yml index f15a8a55d..4d1d9a52c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -635,6 +635,8 @@ en: registrant_ident: 'Registrant ident' contact_ident: 'Contact ident' results_per_page: 'Results per page' + set_test_btn: Set Test + remove_test_btn: Remove Test nameserver_hostname: 'Nameserver hostname' result_count: zero: 'No results' diff --git a/config/routes.rb b/config/routes.rb index 7ef8474f0..fd5967c9a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -176,10 +176,13 @@ Rails.application.routes.draw do namespace :accreditation_center do # At the moment invoice_status endpoint returns only cancelled invoices. But in future logic of this enpoint can change. # And it will need to return invoices of different statuses. I decided to leave the name of the endpoint "invoice_status" - resources :invoice_status, only: [:index] - resource :domains, only: [:show], param: :name - resource :contacts, only: [:show], param: :id + resources :invoice_status, only: [ :index ] + resource :domains, only: [ :show ], param: :name + resource :contacts, only: [ :show ], param: :id + resource :results, only: [ :show ], param: :name # resource :auth, only: [ :index ] + get 'show_api_user', to: 'results#show_api_user' + get 'list_accreditated_api_users', to: 'results#list_accreditated_api_users' get 'auth', to: 'auth#index' end @@ -393,6 +396,13 @@ Rails.application.routes.draw do resources :registrars do resources :api_users, except: %i[index] resources :white_ips + + collection do + post 'set_test_date', to: 'registrars#set_test_date', as: 'set_test_date' + post 'remove_test_date', to: 'registrars#remove_test_date', as: 'remove_test_date' + post 'set_test_date_to_api_user', to: 'api_users#set_test_date_to_api_user', as: 'set_test_date_to_api_user' + post 'remove_test_date_to_api_user', to: 'api_users#remove_test_date_to_api_user', as: 'remove_test_date_to_api_user' + end end resources :contacts do diff --git a/test/integration/admin_area/api_users_test.rb b/test/integration/admin_area/api_users_test.rb new file mode 100644 index 000000000..2fbdba3c1 --- /dev/null +++ b/test/integration/admin_area/api_users_test.rb @@ -0,0 +1,38 @@ +require 'test_helper' + +class AdminAreaRegistrarsIntegrationTest < ActionDispatch::IntegrationTest + include Devise::Test::IntegrationHelpers + + setup do + @api_user = users(:api_bestnames) + sign_in users(:admin) + end + + def test_set_test_date_to_api_user + date = Time.zone.now - 10.minutes + + api_user = @api_user.dup + api_user.accreditation_date = date + api_user.accreditation_expire_date = api_user.accreditation_date + 1.year + api_user.save + + assert_nil @api_user.accreditation_date + assert_equal api_user.accreditation_date, date + + # 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}"). + with( + headers: { + 'Accept'=>'*/*', + 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + '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 } + @api_user.reload + + 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 + end + end From 95f5bad64b46674cbfa86e32ccf9c6570738d6a2 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Fri, 10 Dec 2021 17:07:39 +0200 Subject: [PATCH 2/7] added registrar controller tests --- .../admin/registrars_controller.rb | 4 ++-- .../actions/record_date_of_test.rb | 9 -------- .../integration/admin_area/registrars_test.rb | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index 9c2485813..08dbbef2b 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -93,8 +93,8 @@ module Admin return redirect_to request.referrer, notice: 'Registrar found, but not accreditated yet' if registrar_users.empty? registrar_users.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? + 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? end redirect_to request.referrer, notice: 'Registrar found' diff --git a/app/interactions/actions/record_date_of_test.rb b/app/interactions/actions/record_date_of_test.rb index 53d309948..229929429 100644 --- a/app/interactions/actions/record_date_of_test.rb +++ b/app/interactions/actions/record_date_of_test.rb @@ -5,18 +5,9 @@ module Actions TEST_DEADLINE = 1.year.freeze def record_result_to_api_user(api_user:, date:) - p "+++++++++++" - p api_user - p "-----------" - p DateTime.parse(date) - p "+++++++++++" - api_user.accreditation_date = date api_user.accreditation_expire_date = api_user.accreditation_date + TEST_DEADLINE api_user.save - - # api_user.update(accreditation_date: date, - # accreditation_expire_date: DateTime.parse(date) + TEST_DEADLINE) end end end diff --git a/test/integration/admin_area/registrars_test.rb b/test/integration/admin_area/registrars_test.rb index 552650791..436769963 100644 --- a/test/integration/admin_area/registrars_test.rb +++ b/test/integration/admin_area/registrars_test.rb @@ -17,4 +17,27 @@ class AdminAreaRegistrarsIntegrationTest < ActionDispatch::IntegrationTest assert_equal new_iban, @registrar.iban end + + def test_set_test_date + api_user = @registrar.api_users.first.dup + api_user.accreditation_date = Time.zone.now - 10.minutes + api_user.accreditation_expire_date = api_user.accreditation_date + 1.year + api_user.save + + assert_nil @registrar.api_users.first.accreditation_date + + stub_request(:get, "http://registry.test:3000/api/v1/accreditation_center/results?registrar_name=#{@registrar.name}"). + with( + headers: { + 'Accept'=>'*/*', + 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'User-Agent'=>'Ruby' + }).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 } + @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_expire_date.to_date, api_user.accreditation_expire_date.to_date + end end From 7ec9253fc909349fa3b01714274c50a390d4cbad Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Mon, 13 Dec 2021 09:43:30 +0200 Subject: [PATCH 3/7] added test env for repp endpoints --- .../repp/v1/registrar/accreditation_info_controller.rb | 2 +- .../repp/v1/registrar/accreditation_results_controller.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/repp/v1/registrar/accreditation_info_controller.rb b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb index 3157672d5..5e3524c92 100644 --- a/app/controllers/repp/v1/registrar/accreditation_info_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb @@ -1,7 +1,7 @@ module Repp module V1 module Registrar - if Rails.env.development? || Rails.env.staging? + if Rails.env.development? || Rails.env.staging? || Rails.env.test? class AccreditationInfoController < BaseController api :GET, 'repp/v1/registrar/accreditation/get_info' desc 'check login user and return data' diff --git a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb index b813754c5..4a89e713a 100644 --- a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb @@ -1,7 +1,7 @@ module Repp module V1 module Registrar - if Rails.env.development? || Rails.env.staging? + if Rails.env.development? || Rails.env.staging? || Rails.env.test? class AccreditationResultsController < ActionController::API before_action :authenticate_shared_key From 97876ab1c15e32a8458a2cdbb2489376328e6328 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Mon, 13 Dec 2021 10:28:21 +0200 Subject: [PATCH 4/7] made crutch for tests --- app/controllers/admin/api_users_controller.rb | 2 +- .../admin/registrars_controller.rb | 2 +- test/integration/admin_area/api_users_test.rb | 65 +++++++++---------- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/app/controllers/admin/api_users_controller.rb b/app/controllers/admin/api_users_controller.rb index 5021fe15a..2b421cbad 100644 --- a/app/controllers/admin/api_users_controller.rb +++ b/app/controllers/admin/api_users_controller.rb @@ -50,7 +50,7 @@ module Admin def set_test_date_to_api_user user_api = User.find(params[:user_api_id]) - uri = URI.parse(ENV['registry_demo_registrar_api_user_url'] + "?username=#{user_api.username}&identity_code=#{user_api.identity_code}") + uri = URI.parse((ENV['registry_demo_registrar_api_user_url'] || 'testapi.test') + "?username=#{user_api.username}&identity_code=#{user_api.identity_code}") response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port']) diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index 08dbbef2b..1824575f1 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -60,7 +60,7 @@ module Admin def set_test_date registrar = Registrar.find(params[:registrar_id]) - uri = URI.parse(ENV['registry_demo_registrar_results_url'] + "?registrar_name=#{registrar.name}") + uri = URI.parse((ENV['registry_demo_registrar_results_url'] || 'testapi.test') + "?registrar_name=#{registrar.name}") response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port']) diff --git a/test/integration/admin_area/api_users_test.rb b/test/integration/admin_area/api_users_test.rb index 2fbdba3c1..f0ae5620a 100644 --- a/test/integration/admin_area/api_users_test.rb +++ b/test/integration/admin_area/api_users_test.rb @@ -1,38 +1,37 @@ require 'test_helper' class AdminAreaRegistrarsIntegrationTest < ActionDispatch::IntegrationTest - include Devise::Test::IntegrationHelpers - - setup do - @api_user = users(:api_bestnames) - sign_in users(:admin) - end - - def test_set_test_date_to_api_user - date = Time.zone.now - 10.minutes - - api_user = @api_user.dup - api_user.accreditation_date = date - api_user.accreditation_expire_date = api_user.accreditation_date + 1.year - api_user.save - - assert_nil @api_user.accreditation_date - assert_equal api_user.accreditation_date, date - - # 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}"). - with( - headers: { - 'Accept'=>'*/*', - 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'User-Agent'=>'Ruby' - }).to_return(status: 200, body: { code: 200, user_api: api_user }.to_json, headers: {}) + include Devise::Test::IntegrationHelpers - - 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 - - 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 - end + setup do + @api_user = users(:api_bestnames) + sign_in users(:admin) end + + def test_set_test_date_to_api_user + # ENV['registry_demo_registrar_api_user_url'] = 'http://testapi.test' + + date = Time.zone.now - 10.minutes + + api_user = @api_user.dup + api_user.accreditation_date = date + api_user.accreditation_expire_date = api_user.accreditation_date + 1.year + api_user.save + + assert_nil @api_user.accreditation_date + assert_equal api_user.accreditation_date, date + + # 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}"). + with( + headers: { + 'Accept'=>'*/*', + 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + '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 } + @api_user.reload + 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 + end +end From 4567e564570867b67b884e9edf8477e39f6b0b51 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Mon, 13 Dec 2021 10:43:16 +0200 Subject: [PATCH 5/7] added new tests --- app/controllers/admin/api_users_controller.rb | 4 +- .../admin/registrars_controller.rb | 2 +- test/interactions/record_date_of_test_test.rb | 22 ++++++ test/system/admin_area/api_users_test.rb | 68 +++++++++++++++++++ test/system/admin_area/registrars_test.rb | 66 ++++++++++++++++++ 5 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 test/interactions/record_date_of_test_test.rb diff --git a/app/controllers/admin/api_users_controller.rb b/app/controllers/admin/api_users_controller.rb index 2b421cbad..01ac6ae88 100644 --- a/app/controllers/admin/api_users_controller.rb +++ b/app/controllers/admin/api_users_controller.rb @@ -50,7 +50,7 @@ module Admin def set_test_date_to_api_user user_api = User.find(params[:user_api_id]) - uri = URI.parse((ENV['registry_demo_registrar_api_user_url'] || 'testapi.test') + "?username=#{user_api.username}&identity_code=#{user_api.identity_code}") + uri = URI.parse((ENV['registry_demo_registrar_api_user_url'] || 'http://testapi.test') + "?username=#{user_api.username}&identity_code=#{user_api.identity_code}") response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port']) @@ -74,7 +74,7 @@ module Admin user_api.accreditation_date = nil user_api.accreditation_expire_date = nil user_api.save - + redirect_to request.referrer end diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index 1824575f1..bc8c7e600 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -60,7 +60,7 @@ module Admin def set_test_date registrar = Registrar.find(params[:registrar_id]) - uri = URI.parse((ENV['registry_demo_registrar_results_url'] || 'testapi.test') + "?registrar_name=#{registrar.name}") + uri = URI.parse((ENV['registry_demo_registrar_results_url'] || 'http://testapi.test') + "?registrar_name=#{registrar.name}") response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port']) diff --git a/test/interactions/record_date_of_test_test.rb b/test/interactions/record_date_of_test_test.rb new file mode 100644 index 000000000..b8f228f62 --- /dev/null +++ b/test/interactions/record_date_of_test_test.rb @@ -0,0 +1,22 @@ +require 'test_helper' + +class RecordDateOfTestTest < ActiveSupport::TestCase + setup do + @api_bestname = users(:api_bestnames) + @api_bestname.accreditation_date = Time.zone.now - 10.minutes + @api_bestname.accreditation_expire_date = @api_bestname.accreditation_date + 1.year + @api_bestname.save + end + + def test_should_record_data_to_apiuser + api_goodname = users(:api_goodnames) + + assert_nil api_goodname.accreditation_date + assert_nil api_goodname.accreditation_expire_date + + Actions::RecordDateOfTest.record_result_to_api_user(api_user: api_goodname, date: @api_bestname.accreditation_date) + + assert_equal api_goodname.accreditation_date, @api_bestname.accreditation_date + assert_equal api_goodname.accreditation_expire_date, @api_bestname.accreditation_expire_date + end +end diff --git a/test/system/admin_area/api_users_test.rb b/test/system/admin_area/api_users_test.rb index d79434ef6..8f7b73155 100644 --- a/test/system/admin_area/api_users_test.rb +++ b/test/system/admin_area/api_users_test.rb @@ -3,6 +3,7 @@ require 'application_system_test_case' class AdminApiUsersSystemTest < ApplicationSystemTestCase setup do sign_in users(:admin) + @registrar = registrars(:bestnames) end def test_shows_api_user_list @@ -11,4 +12,71 @@ class AdminApiUsersSystemTest < ApplicationSystemTestCase api_user = users(:api_bestnames) assert_link api_user.username, href: admin_registrar_api_user_path(api_user.registrar, api_user) end + + def test_should_display_tests_button_in_api_user + visit admin_api_users_path + + assert_button 'Set Test' + assert_no_button 'Remove Test' + end + + def test_should_display_remove_test_if_there_accreditated_apiuser + date = Time.zone.now - 10.minutes + + api_user = @registrar.api_users.first + api_user.accreditation_date = date + api_user.accreditation_expire_date = api_user.accreditation_date + 1.year + api_user.save + + visit admin_api_users_path + + assert_button 'Remove Test' + end + + def test_should_not_display_remove_test_if_api_user_accreditation_date_is_expired + date = Time.zone.now - 1.year - 10.minutes + + api_user = @registrar.api_users.first + api_user.accreditation_date = date + api_user.accreditation_expire_date = api_user.accreditation_date + 1.year + api_user.save + + visit admin_api_users_path + + assert_no_button 'Remove' + end + + def test_should_display_tests_button_in_api_user_details + api_user = @registrar.api_users.first + + visit admin_api_user_path(api_user) + assert_button 'Set Test' + assert_no_button 'Remove Test' + end + + def test_should_display_remove_test_in_api_user_details_if_there_accreditated_apiuser + date = Time.zone.now - 10.minutes + + api_user = @registrar.api_users.first + api_user.accreditation_date = date + api_user.accreditation_expire_date = api_user.accreditation_date + 1.year + api_user.save + + visit admin_api_user_path(api_user) + + assert_button 'Remove Test' + end + + def test_should_not_display_remove_test_if_api_user_accreditation_date_is_expired_in_api_details + date = Time.zone.now - 1.year - 10.minutes + + api_user = @registrar.api_users.first + api_user.accreditation_date = date + api_user.accreditation_expire_date = api_user.accreditation_date + 1.year + api_user.save + + visit admin_api_user_path(api_user) + + assert_no_button 'Remove' + end end diff --git a/test/system/admin_area/registrars_test.rb b/test/system/admin_area/registrars_test.rb index f3ee31020..aa61d10e3 100644 --- a/test/system/admin_area/registrars_test.rb +++ b/test/system/admin_area/registrars_test.rb @@ -93,4 +93,70 @@ class AdminRegistrarsSystemTest < ApplicationSystemTestCase assert_text 'Language English' assert_text 'billing@bestnames.test' end + + def test_should_display_btn_for_set_test_date + visit admin_registrars_path + + assert_button 'Set Test' + assert_no_button 'Remove Test' + end + + def test_should_display_remove_test_if_there_accreditated_registrars + date = Time.zone.now - 10.minutes + + api_user = @registrar.api_users.first + api_user.accreditation_date = date + api_user.accreditation_expire_date = api_user.accreditation_date + 1.year + api_user.save + + visit admin_registrars_path + + assert_button 'Remove Test' + end + + def test_should_not_display_remove_test_if_accreditation_date_is_expired + date = Time.zone.now - 1.year - 10.minutes + + api_user = @registrar.api_users.first + api_user.accreditation_date = date + api_user.accreditation_expire_date = api_user.accreditation_date + 1.year + api_user.save + + visit admin_registrars_path + + assert_no_button 'Remove' + end + + def test_should_display_tests_button_in_registrar_deftails + visit admin_registrar_path(@registrar) + + assert_button 'Set Test' + assert_no_button 'Remove Test' + end + + def test_should_display_remove_test_if_there_accreditated_registrars_in_registrar_details + date = Time.zone.now - 10.minutes + + api_user = @registrar.api_users.first + api_user.accreditation_date = date + api_user.accreditation_expire_date = api_user.accreditation_date + 1.year + api_user.save + + visit admin_registrar_path(@registrar) + + assert_button 'Remove Test' + end + + def test_should_not_display_remove_test_if_accreditation_date_is_expired_in_registrar_details + date = Time.zone.now - 1.year - 10.minutes + + api_user = @registrar.api_users.first + api_user.accreditation_date = date + api_user.accreditation_expire_date = api_user.accreditation_date + 1.year + api_user.save + + visit admin_registrar_path(@registrar) + + assert_no_button 'Remove' + end end \ No newline at end of file From 72f184d9b0eb92bca6cbcab606e3b6af98958cd7 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Mon, 13 Dec 2021 10:54:09 +0200 Subject: [PATCH 6/7] test refactoring --- app/controllers/admin/api_users_controller.rb | 13 ++--- .../admin/registrars_controller.rb | 12 ++--- .../accreditation_results_controller.rb | 50 +++++++++---------- app/jobs/sync_accredited_users_job.rb | 19 ++----- db/structure.sql | 2 - test/integration/admin_area/api_users_test.rb | 18 ++++--- .../integration/admin_area/registrars_test.rb | 24 +++++---- 7 files changed, 65 insertions(+), 73 deletions(-) diff --git a/app/controllers/admin/api_users_controller.rb b/app/controllers/admin/api_users_controller.rb index 01ac6ae88..9e4111c9f 100644 --- a/app/controllers/admin/api_users_controller.rb +++ b/app/controllers/admin/api_users_controller.rb @@ -17,8 +17,7 @@ module Admin if @api_user.valid? @api_user.save! - redirect_to admin_registrar_api_user_path(@api_user.registrar, @api_user), - notice: t('.created') + redirect_to admin_registrar_api_user_path(@api_user.registrar, @api_user), notice: t('.created') else render 'new' end @@ -35,8 +34,7 @@ module Admin if @api_user.valid? @api_user.save! - redirect_to admin_registrar_api_user_path(@api_user.registrar, @api_user), - notice: t('.updated') + redirect_to admin_registrar_api_user_path(@api_user.registrar, @api_user), notice: t('.updated') else render 'edit' end @@ -50,7 +48,7 @@ module Admin def set_test_date_to_api_user 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']) @@ -58,9 +56,8 @@ module Admin result = JSON.parse(response.body) demo_user_api = result['user_api'] - Actions::RecordDateOfTest.record_result_to_api_user( - api_user:user_api, - date: demo_user_api['accreditation_date']) unless demo_user_api.empty? + Actions::RecordDateOfTest.record_result_to_api_user(api_user:user_api, + date: demo_user_api['accreditation_date']) unless demo_user_api.empty? return redirect_to request.referrer, notice: 'User Api found' else return redirect_to request.referrer, notice: 'User Api no found or not accriditated yet' diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index bc8c7e600..b552a8cb3 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -60,17 +60,17 @@ module Admin def set_test_date 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']) if response.code == "200" return record_result_for_each_api_user(response: response) else - return redirect_to request.referrer, notice: 'Registrar no found' + return redirect_to request.referer, notice: 'Registrar no found' end - redirect_to request.referrer, notice: 'Something goes wrong' + redirect_to request.referer, notice: 'Something goes wrong' end def remove_test_date @@ -81,7 +81,7 @@ module Admin api.save end - redirect_to request.referrer + redirect_to request.referer end private @@ -90,14 +90,14 @@ module Admin result = JSON.parse(response.body) 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| 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? end - redirect_to request.referrer, notice: 'Registrar found' + redirect_to request.referer, notice: 'Registrar found' end def base_get_request(uri:, port:) diff --git a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb index 4a89e713a..4b5c40467 100644 --- a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb @@ -26,35 +26,35 @@ module Repp user = ApiUser.find_by(username: username) raise ActiveRecord::RecordNotFound if user.nil? + + user.accreditation_date = DateTime.current + user.accreditation_expire_date = user.accreditation_date + EXPIRE_DEADLINE - user.accreditation_date = DateTime.current - user.accreditation_expire_date = user.accreditation_date + EXPIRE_DEADLINE - - if user.save - notify_registrar(user) - notify_admins - render_success(data: { user: user, - result: result, - message: 'Accreditation info successfully added' }) - else - render_failed + if user.save + notify_registrar(user) + notify_admins + render_success(data: { user: user, + result: result, + message: 'Accreditation info successfully added' }) + else + render_failed + end end - end - def notify_registrar(user) - AccreditationCenterMailer.test_was_successfully_passed_registrar(user.registrar.email).deliver_now - end - - def notify_admins - admin_users_emails = User.all.reject { |u| u.roles.nil? } - .select { |u| u.roles.include? 'admin' }.pluck(:email) - - return if admin_users_emails.empty? - - admin_users_emails.each do |email| - AccreditationCenterMailer.test_was_successfully_passed_admin(email).deliver_now + def notify_registrar(user) + AccreditationCenterMailer.test_was_successfully_passed_registrar(user.registrar.email).deliver_now + end + + def notify_admins + admin_users_emails = User.all.reject { |u| u.roles.nil? } + .select { |u| u.roles.include? 'admin' }.pluck(:email) + + return if admin_users_emails.empty? + + admin_users_emails.each do |email| + AccreditationCenterMailer.test_was_successfully_passed_admin(email).deliver_now + end end - end def authenticate_shared_key api_key = "Basic #{TEMPORARY_SECRET_KEY}" diff --git a/app/jobs/sync_accredited_users_job.rb b/app/jobs/sync_accredited_users_job.rb index 8e02956e5..de95b59c8 100644 --- a/app/jobs/sync_accredited_users_job.rb +++ b/app/jobs/sync_accredited_users_job.rb @@ -1,22 +1,11 @@ class SyncAccreditedUsersJob < ApplicationJob 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']) - 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) - users = result['users'] - - users.each do |api| + result['users'].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 @@ -24,7 +13,7 @@ class SyncAccreditedUsersJob < ApplicationJob logger.warn 'User not found' end - return + nil end private @@ -35,4 +24,4 @@ class SyncAccreditedUsersJob < ApplicationJob http.request(req) end -end \ No newline at end of file +end diff --git a/db/structure.sql b/db/structure.sql index 03fca59a8..26515ee8e 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -5416,5 +5416,3 @@ INSERT INTO "schema_migrations" (version) VALUES ('20220504090512'), ('20220524130709'), ('20220818075833'); - - diff --git a/test/integration/admin_area/api_users_test.rb b/test/integration/admin_area/api_users_test.rb index f0ae5620a..3e0af7bad 100644 --- a/test/integration/admin_area/api_users_test.rb +++ b/test/integration/admin_area/api_users_test.rb @@ -4,6 +4,8 @@ class AdminAreaRegistrarsIntegrationTest < ActionDispatch::IntegrationTest include Devise::Test::IntegrationHelpers 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) sign_in users(:admin) end @@ -22,14 +24,16 @@ class AdminAreaRegistrarsIntegrationTest < ActionDispatch::IntegrationTest assert_equal api_user.accreditation_date, date # 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}"). - with( + 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( headers: { - 'Accept'=>'*/*', - 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - '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 } + 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + '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 } @api_user.reload 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 diff --git a/test/integration/admin_area/registrars_test.rb b/test/integration/admin_area/registrars_test.rb index 436769963..c0753ebba 100644 --- a/test/integration/admin_area/registrars_test.rb +++ b/test/integration/admin_area/registrars_test.rb @@ -4,6 +4,8 @@ class AdminAreaRegistrarsIntegrationTest < ActionDispatch::IntegrationTest include Devise::Test::IntegrationHelpers 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) sign_in users(:admin) end @@ -26,18 +28,20 @@ class AdminAreaRegistrarsIntegrationTest < ActionDispatch::IntegrationTest assert_nil @registrar.api_users.first.accreditation_date - stub_request(:get, "http://registry.test:3000/api/v1/accreditation_center/results?registrar_name=#{@registrar.name}"). - with( - headers: { - 'Accept'=>'*/*', - 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', - 'User-Agent'=>'Ruby' - }).to_return(status: 200, body: { code: 200, registrar_users: [api_user] }.to_json, headers: {}) + stub_request(:get, "http://registry.test:3000/api/v1/accreditation_center/results?registrar_name=#{@registrar.name}") + .with( + headers: { + 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'User-Agent' => 'Ruby' + } + ) + .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 - assert_equal @registrar.api_users.first.accreditation_date.to_date, api_user.accreditation_date.to_date - assert_equal @registrar.api_users.first.accreditation_expire_date.to_date, api_user.accreditation_expire_date.to_date + assert_equal @registrar.api_users.first.accreditation_date.to_date, api_user.accreditation_date.to_date + assert_equal @registrar.api_users.first.accreditation_expire_date.to_date, api_user.accreditation_expire_date.to_date end end From 44296ab045e2c18568969e7679cb10dcd16c7838 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Wed, 9 Mar 2022 15:26:05 +0200 Subject: [PATCH 7/7] change condition for render endpoint in specific envinronment --- .../api/v1/accreditation_center/base_controller.rb | 2 +- .../v1/registrar/accreditation_info_controller.rb | 2 +- .../registrar/accreditation_results_controller.rb | 2 +- app/models/feature.rb | 14 ++++++++++++++ config/application.yml.sample | 1 - 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/accreditation_center/base_controller.rb b/app/controllers/api/v1/accreditation_center/base_controller.rb index 77f3b0338..05b809006 100644 --- a/app/controllers/api/v1/accreditation_center/base_controller.rb +++ b/app/controllers/api/v1/accreditation_center/base_controller.rb @@ -3,7 +3,7 @@ require 'auth_token/auth_token_decryptor' module Api module V1 module AccreditationCenter - if Rails.env.development? || Rails.env.staging? || Rails.env.test? + if Feature.allow_accr_endspoints? class BaseController < ActionController::API rescue_from ActiveRecord::RecordNotFound, with: :show_not_found_error rescue_from ActiveRecord::RecordInvalid, with: :show_invalid_record_error diff --git a/app/controllers/repp/v1/registrar/accreditation_info_controller.rb b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb index 5e3524c92..1d3065062 100644 --- a/app/controllers/repp/v1/registrar/accreditation_info_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb @@ -1,7 +1,7 @@ module Repp module V1 module Registrar - if Rails.env.development? || Rails.env.staging? || Rails.env.test? + if Feature.allow_accr_endspoints? class AccreditationInfoController < BaseController api :GET, 'repp/v1/registrar/accreditation/get_info' desc 'check login user and return data' diff --git a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb index 4b5c40467..cbc39b063 100644 --- a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb @@ -1,7 +1,7 @@ module Repp module V1 module Registrar - if Rails.env.development? || Rails.env.staging? || Rails.env.test? + if Feature.allow_accr_endspoints? class AccreditationResultsController < ActionController::API before_action :authenticate_shared_key diff --git a/app/models/feature.rb b/app/models/feature.rb index 7a0d6d78b..ed7ae12fd 100644 --- a/app/models/feature.rb +++ b/app/models/feature.rb @@ -4,4 +4,18 @@ class Feature ENV['billing_system_integrated'] || false end + # def self.obj_and_extensions_statuses_enabled? + # return false if ENV['obj_and_extensions_prohibited'] == 'false' + # + # ENV['obj_and_extensions_prohibited'] || false + # end + # + # def self.enable_lock_domain_with_new_statuses? + # return false if ENV['enable_lock_domain_with_new_statuses'] == 'false' + # + # ENV['enable_lock_domain_with_new_statuses'] || false + # end + def self.allow_accr_endspoints? + ENV['allow_accr_endspoints'] == 'true' + end end diff --git a/config/application.yml.sample b/config/application.yml.sample index b25b09fa8..acc5ab6b4 100644 --- a/config/application.yml.sample +++ b/config/application.yml.sample @@ -242,4 +242,3 @@ billing_system_integrated: 'true' secret_access_word: 'please-Give-Me-accesS' secret_word: 'this-secret-should-be-change' allow_accr_endspoints: 'true' -
+ <%= sort_link(@q, 'username') %> + <%= sort_link(@q, 'registrar_name', Registrar.model_name.human) %> + <%= sort_link(@q, 'active', ApiUser.human_attribute_name(:active)) %> + Test status +
<%= t(:emails) %> + Test status +
+ <% if !x.accredited? || x.accreditation_expired? %> + <%= button_to t('.set_test_btn'), + { controller: 'registrars', action: 'set_test_date', registrar_id: x.id}, + { method: :post, class: 'btn btn-primary'} %> + <% else %> + <%= button_to t('.remove_test_btn'), + { controller: 'registrars', action: 'remove_test_date', registrar_id: x.id}, + { method: :post, class: 'btn btn-danger'} %> + <% end %> +
<%= ApiUser.human_attribute_name :username %> <%= ApiUser.human_attribute_name :active %>Test Results
<%= link_to api_user, admin_registrar_api_user_path(api_user.registrar, api_user) %> <%= api_user.active %> + + <% if !api_user.accredited? || api_user.accreditation_expired? %> + <%= button_to t('.set_test_btn'), + { controller: 'api_users', action: 'set_test_date_to_api_user', user_api_id: api_user.id }, + { method: :post, class: 'btn btn-primary'} %> + <% else %> + <%= button_to t('.remove_test_btn'), + { controller: 'api_users', action: 'remove_test_date_to_api_user', user_api_id: api_user.id }, + { method: :post, class: 'btn btn-danger'} %> + <% end %> +