added endpoints to demo registry for accr results

This commit is contained in:
olegphenomenon 2021-12-10 16:53:25 +02:00
parent 487cbb1774
commit c07abaea59
20 changed files with 401 additions and 78 deletions

View file

@ -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: [] })

View file

@ -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'

View file

@ -3,6 +3,7 @@ require 'auth_token/auth_token_decryptor'
module Api
module V1
module AccreditationCenter
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
@ -26,3 +27,4 @@ module Api
end
end
end
end

View file

@ -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

View file

@ -1,6 +1,7 @@
module Repp
module V1
module Registrar
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'
@ -36,3 +37,4 @@ module Repp
end
end
end
end

View file

@ -1,6 +1,7 @@
module Repp
module V1
module Registrar
if Rails.env.development? || Rails.env.staging?
class AccreditationResultsController < ActionController::API
before_action :authenticate_shared_key
@ -80,3 +81,4 @@ module Repp
end
end
end
end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -2,4 +2,16 @@
<td><%= link_to api_user, admin_registrar_api_user_path(api_user.registrar, api_user) %></td>
<td><%= link_to api_user.registrar, admin_registrar_path(api_user.registrar) %></td>
<td><%= api_user.active %></td>
<td style="text-align: center;">
<% 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 %>
</td>
</tr>

View file

@ -10,15 +10,18 @@
<table class="table table-hover table-bordered table-condensed">
<thead>
<tr>
<th class="col-xs-2">
<th class="col-xs-3">
<%= sort_link(@q, 'username') %>
</th>
<th class="col-xs-2">
<th class="col-xs-3">
<%= sort_link(@q, 'registrar_name', Registrar.model_name.human) %>
</th>
<th class="col-xs-2">
<th class="col-xs-3">
<%= sort_link(@q, 'active', ApiUser.human_attribute_name(:active)) %>
</th>
<th class="col-xs-1">
Test status
</th>
</tr>
</thead>

View file

@ -6,12 +6,12 @@
</ol>
<div class="page-header">
<div class="row">
<div class="row" style="display: flex; flex-direction: row; align-items: baseline;">
<div class="col-sm-8">
<h1><%= @api_user.username %></h1>
</div>
<div class="col-sm-4 text-right">
<div class="col-sm-4 text-right" style="display: flex; flex-direction: row; align-items: baseline; justify-content: space-evenly;">
<%= link_to t('.edit_btn'), edit_admin_registrar_api_user_path(@api_user.registrar,
@api_user),
class: 'btn btn-primary' %>
@ -20,6 +20,16 @@
method: :delete,
data: { confirm: t('.delete_btn_confirm') },
class: 'btn btn-default' %>
<% 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 %>
</div>
</div>
</div>

View file

@ -33,6 +33,10 @@
<th class="col-xs-4">
<%= t(:emails) %>
</th>
</th>
<th>
Test status
</th>
</tr>
</thead>
<tbody>
@ -58,6 +62,17 @@
<%= content_tag(:span, x[:billing_email]) %>
<% end %>
</td>
<td>
<% 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 %>
</td>
</tr>
<% end %>
</tbody>

View file

@ -8,6 +8,7 @@
<tr>
<th class="col-xs-6"><%= ApiUser.human_attribute_name :username %></th>
<th class="col-xs-6"><%= ApiUser.human_attribute_name :active %></th>
<th class="col-xs-6">Test Results</th>
</tr>
</thead>
@ -16,6 +17,18 @@
<tr>
<td><%= link_to api_user, admin_registrar_api_user_path(api_user.registrar, api_user) %></td>
<td><%= api_user.active %></td>
<td>
<% 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 %>
</td>
</tr>
<% end %>
</tbody>

View file

@ -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

View file

@ -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

View file

@ -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'

View file

@ -179,7 +179,10 @@ Rails.application.routes.draw do
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

View file

@ -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