From 0f16ec77f5f56b107e6428b7950fa3b2e466c51f Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Mon, 15 May 2023 14:11:46 +0300 Subject: [PATCH 1/3] Added csv export to registrar api_users and white_ips --- .../admin/registrars_controller.rb | 7 ++++- app/models/api_user.rb | 19 +++++++++++++ app/models/white_ip.rb | 14 ++++++++++ app/services/csv_generator.rb | 2 +- .../admin/registrars/show/_api_users.html.erb | 6 +++-- .../admin/registrars/show/_white_ips.html.erb | 4 ++- config/routes.rb | 7 ++++- test/fixtures/files/api_users.csv | 2 ++ test/fixtures/files/white_ips.csv | 2 ++ .../admin_area/registrars/api_users_test.rb | 20 ++++++++++++++ .../admin_area/registrars/white_ips_test.rb | 27 +++++++++++++++++++ 11 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/files/api_users.csv create mode 100644 test/fixtures/files/white_ips.csv create mode 100644 test/system/admin_area/registrars/white_ips_test.rb diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index b552a8cb3..94ca74dad 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -3,7 +3,7 @@ require 'net/http' module Admin class RegistrarsController < BaseController # rubocop:disable Metrics/ClassLength load_and_authorize_resource - before_action :set_registrar, only: [:show, :edit, :update, :destroy] + before_action :set_registrar, only: %i[show edit update destroy] before_action :set_registrar_status_filter, only: [:index] helper_method :registry_vat_rate helper_method :iban_max_length @@ -39,6 +39,11 @@ module Admin def edit; end + def show + @result = @registrar.send(params[:records]) unless params[:records].blank? + render_by_format('admin/registrars/show', "#{@registrar.name.parameterize}_#{params[:records]}") + end + def update if @registrar.update(registrar_params) redirect_to [:admin, @registrar], notice: t('.updated') diff --git a/app/models/api_user.rb b/app/models/api_user.rb index a15b12a85..289e0e8d9 100644 --- a/app/models/api_user.rb +++ b/app/models/api_user.rb @@ -91,6 +91,25 @@ class ApiUser < User another_api_user.identity_code == identity_code end + def as_csv_row + [ + username, + plain_text_password, + identity_code, + roles.join(', '), + active, + accredited?, + accreditation_expire_date, + created_at, + updated_at + ] + end + + def self.csv_header + ['Username', 'Password', 'Identity Code', 'Role', 'Active', 'Accredited', + 'Accreditation Expire Date', 'Created', 'Updated'] + end + private def machine_readable_certificate(cert) diff --git a/app/models/white_ip.rb b/app/models/white_ip.rb index 7bbd8c18f..358fa82c7 100644 --- a/app/models/white_ip.rb +++ b/app/models/white_ip.rb @@ -72,5 +72,19 @@ class WhiteIp < ApplicationRecord rescue StandardError => _e nil end + + def csv_header + %w[IPv4 IPv6 Interfaces Created Updated] + end + end + + def as_csv_row + [ + ipv4, + ipv6, + interfaces.join(', ').upcase, + created_at, + updated_at + ] end end diff --git a/app/services/csv_generator.rb b/app/services/csv_generator.rb index d92beeddc..7a8055680 100644 --- a/app/services/csv_generator.rb +++ b/app/services/csv_generator.rb @@ -22,7 +22,7 @@ class CsvGenerator def custom_csv?(class_name) [ Version::DomainVersion, Version::ContactVersion, Domain, - Contact, Invoice, Account, AccountActivity + Contact, Invoice, Account, AccountActivity, ApiUser, WhiteIp ].include?(class_name) 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 fddccd9b7..7e3cb287d 100644 --- a/app/views/admin/registrars/show/_api_users.html.erb +++ b/app/views/admin/registrars/show/_api_users.html.erb @@ -1,4 +1,4 @@ -
+
<%= t '.header' %>
@@ -15,7 +15,7 @@ <% registrar.api_users.each do |api_user| %> - <%= link_to api_user, admin_registrar_api_user_path(api_user.registrar, api_user) %> + <%= link_to api_user, admin_registrar_api_user_path(registrar, api_user) %> <%= api_user.active %> @@ -37,5 +37,7 @@
diff --git a/app/views/admin/registrars/show/_white_ips.html.erb b/app/views/admin/registrars/show/_white_ips.html.erb index c86c85eb9..5de3066c0 100644 --- a/app/views/admin/registrars/show/_white_ips.html.erb +++ b/app/views/admin/registrars/show/_white_ips.html.erb @@ -1,4 +1,4 @@ -
+
<%= t '.header' %>
@@ -34,5 +34,7 @@
diff --git a/config/routes.rb b/config/routes.rb index 7d11cb6a4..25d73043f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -326,6 +326,11 @@ Rails.application.routes.draw do 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 + + member do + get 'export/:records', to: 'registrars#show', constraints: { format: 'csv' }, + as: :export + end end resources :contacts do @@ -336,7 +341,7 @@ Rails.application.routes.draw do resources :admin_users # /admin/api_users is mainly for manual testing - resources :api_users, only: [:index, :show] do + resources :api_users, only: %i[index show] do resources :certificates do member do post 'sign' diff --git a/test/fixtures/files/api_users.csv b/test/fixtures/files/api_users.csv new file mode 100644 index 000000000..21afa2350 --- /dev/null +++ b/test/fixtures/files/api_users.csv @@ -0,0 +1,2 @@ +Username,Password,Identity Code,Role,Active,Accredited,Accreditation Expire Date,Created,Updated +test_bestnames,testtest,1234,super,true,false,,2010-07-05 10:30:00 +0300,2010-07-05 10:30:00 +0300 diff --git a/test/fixtures/files/white_ips.csv b/test/fixtures/files/white_ips.csv new file mode 100644 index 000000000..5c34cf160 --- /dev/null +++ b/test/fixtures/files/white_ips.csv @@ -0,0 +1,2 @@ +IPv4,IPv6,Interfaces,Created,Updated +127.0.0.1,,"REGISTRAR, API",2010-07-05 10:30:00 +0300,2010-07-05 10:30:00 +0300 diff --git a/test/system/admin_area/registrars/api_users_test.rb b/test/system/admin_area/registrars/api_users_test.rb index 5d833dde2..32de1451b 100644 --- a/test/system/admin_area/registrars/api_users_test.rb +++ b/test/system/admin_area/registrars/api_users_test.rb @@ -22,6 +22,26 @@ class AdminRegistrarsApiUsersSystemTest < ApplicationSystemTestCase assert_current_path admin_registrar_api_user_path(registrar, new_api_user) end + def test_downloads_api_users_list_as_csv + travel_to Time.zone.parse('2010-07-05 10:30') + registrar = registrars(:bestnames) + api_users = registrar.api_users + api_users.each do |u| + u.created_at = Time.zone.now + u.updated_at = Time.zone.now + u.save(validate: false) + end + + visit admin_registrar_path(registrar) + within('.api_users') do + click_on 'Export to CSV' + end + + assert_equal "attachment; filename=\"#{registrar.name.parameterize}_api_users_#{Time.zone.now.to_formatted_s(:number)}.csv\"; " \ + "filename*=UTF-8''#{registrar.name.parameterize}_api_users_#{Time.zone.now.to_formatted_s(:number)}.csv", response_headers['Content-Disposition'] + assert_equal file_fixture('api_users.csv').read, page.body + end + def test_shows_api_user_details api_user = users(:api_bestnames) diff --git a/test/system/admin_area/registrars/white_ips_test.rb b/test/system/admin_area/registrars/white_ips_test.rb new file mode 100644 index 000000000..f778d4dcd --- /dev/null +++ b/test/system/admin_area/registrars/white_ips_test.rb @@ -0,0 +1,27 @@ +require 'application_system_test_case' + +class AdminRegistrarsWhiteIpsSystemTest < ApplicationSystemTestCase + setup do + sign_in users(:admin) + end + + def test_downloads_whitelisted_ips_list_as_csv + travel_to Time.zone.parse('2010-07-05 10:30') + registrar = registrars(:bestnames) + white_ips = registrar.white_ips + white_ips.each do |ip| + ip.created_at = Time.zone.now + ip.updated_at = Time.zone.now + ip.save(validate: false) + end + + visit admin_registrar_path(registrar) + within('.white_ips') do + click_on 'Export to CSV' + end + + assert_equal "attachment; filename=\"#{registrar.name.parameterize}_white_ips_#{Time.zone.now.to_formatted_s(:number)}.csv\"; " \ + "filename*=UTF-8''#{registrar.name.parameterize}_white_ips_#{Time.zone.now.to_formatted_s(:number)}.csv", response_headers['Content-Disposition'] + assert_equal file_fixture('white_ips.csv').read, page.body + end +end From dc41cfb103475582f70e016e4a0179c9cdc310fc Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Mon, 15 May 2023 14:23:41 +0300 Subject: [PATCH 2/3] Fixed codeclimate issues --- app/controllers/admin/registrars_controller.rb | 5 +++-- app/models/api_user.rb | 3 +-- app/models/white_ip.rb | 13 +++++++------ 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index 94ca74dad..e4119e109 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -40,8 +40,9 @@ module Admin def edit; end def show - @result = @registrar.send(params[:records]) unless params[:records].blank? - render_by_format('admin/registrars/show', "#{@registrar.name.parameterize}_#{params[:records]}") + method = params[:records].present? ? params[:records] : 'api_users' + @result = @registrar.send(method) + render_by_format('admin/registrars/show', "#{@registrar.name.parameterize}_#{method}") end def update diff --git a/app/models/api_user.rb b/app/models/api_user.rb index 289e0e8d9..952ae09e7 100644 --- a/app/models/api_user.rb +++ b/app/models/api_user.rb @@ -100,8 +100,7 @@ class ApiUser < User active, accredited?, accreditation_expire_date, - created_at, - updated_at + created_at, updated_at, ] end diff --git a/app/models/white_ip.rb b/app/models/white_ip.rb index 358fa82c7..a90b8cdd2 100644 --- a/app/models/white_ip.rb +++ b/app/models/white_ip.rb @@ -13,6 +13,7 @@ class WhiteIp < ApplicationRecord def validate_ipv4_and_ipv6 return if ipv4.present? || ipv6.present? + errors.add(:base, I18n.t(:ipv4_or_ipv6_must_be_present)) end @@ -32,12 +33,12 @@ class WhiteIp < ApplicationRecord errors.add(:ipv6, :invalid) end - API = 'api' - REGISTRAR = 'registrar' - INTERFACES = [API, REGISTRAR] + API = 'api'.freeze + REGISTRAR = 'registrar'.freeze + INTERFACES = [API, REGISTRAR].freeze - scope :api, -> { where("interfaces @> ?::varchar[]", "{#{API}}") } - scope :registrar_area, -> { where("interfaces @> ?::varchar[]", "{#{REGISTRAR}}") } + scope :api, -> { where('interfaces @> ?::varchar[]', "{#{API}}") } + scope :registrar_area, -> { where('interfaces @> ?::varchar[]', "{#{REGISTRAR}}") } def interfaces=(interfaces) super(interfaces.reject(&:blank?)) @@ -84,7 +85,7 @@ class WhiteIp < ApplicationRecord ipv6, interfaces.join(', ').upcase, created_at, - updated_at + updated_at, ] end end From f721bc50a395c95e224edbf4f6d4a4eb57db3930 Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Mon, 15 May 2023 14:30:18 +0300 Subject: [PATCH 3/3] Fixed codeclimate issues --- app/controllers/admin/registrars_controller.rb | 12 +++++++++--- app/models/api_user.rb | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index e4119e109..ac9b407e5 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -40,9 +40,10 @@ module Admin def edit; end def show - method = params[:records].present? ? params[:records] : 'api_users' - @result = @registrar.send(method) - render_by_format('admin/registrars/show', "#{@registrar.name.parameterize}_#{method}") + method = allowed_method(params[:records]) || 'api_users' + @result = @registrar.send(method.to_sym) + partial_name = "#{@registrar.name.parameterize}_#{method}" + render_by_format('admin/registrars/show', partial_name) end def update @@ -176,5 +177,10 @@ module Admin def iban_max_length Iban.max_length end + + def allowed_method(records_param) + allowed_methods = %w[api_users white_ips] + records_param if allowed_methods.include?(records_param) + end end end diff --git a/app/models/api_user.rb b/app/models/api_user.rb index 952ae09e7..73e0f6c4e 100644 --- a/app/models/api_user.rb +++ b/app/models/api_user.rb @@ -100,7 +100,7 @@ class ApiUser < User active, accredited?, accreditation_expire_date, - created_at, updated_at, + created_at, updated_at ] end