From ece988fca1fc1afcf23da2a53703755f762288e9 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Fri, 27 Aug 2021 11:09:49 +0300 Subject: [PATCH] refactoring --- .../accreditation_center/auth_controller.rb | 84 +++++++++---------- .../accreditation_center/base_controller.rb | 2 +- .../contacts_controller.rb | 5 +- .../domains_controller.rb | 3 +- .../api/accreditation_center/auth_test.rb | 33 ++++++++ .../api/accreditation_center/contacts_test.rb | 21 +++-- 6 files changed, 95 insertions(+), 53 deletions(-) create mode 100644 test/integration/api/accreditation_center/auth_test.rb diff --git a/app/controllers/api/v1/accreditation_center/auth_controller.rb b/app/controllers/api/v1/accreditation_center/auth_controller.rb index f89c4c931..1b6f207dd 100644 --- a/app/controllers/api/v1/accreditation_center/auth_controller.rb +++ b/app/controllers/api/v1/accreditation_center/auth_controller.rb @@ -7,58 +7,58 @@ module Api before_action :authenticate_user def index - login = @current_user - registrar = @current_user.registrar + 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 authenticate_user - username, password = Base64.urlsafe_decode64(basic_token).split(':') - @current_user ||= ApiUser.find_by(username: username, plain_text_password: password) + def authenticate_user + username, password = Base64.urlsafe_decode64(basic_token).split(':') + @current_user ||= ApiUser.find_by(username: username, plain_text_password: password) - return if @current_user + return if @current_user - raise(ArgumentError) - rescue NoMethodError, ArgumentError - @response = { code: 2202, message: 'Invalid authorization information' } - render(json: @response, status: :unauthorized) - end + raise(ArgumentError) + rescue NoMethodError, ArgumentError + @response = { code: 2202, message: 'Invalid authorization information' } + render(json: @response, status: :unauthorized) + end - def basic_token - pattern = /^Basic / - header = request.headers['Authorization'] - header = header.gsub(pattern, '') if header&.match(pattern) - header.strip - end + def basic_token + pattern = /^Basic / + header = request.headers['Authorization'] + header = header.gsub(pattern, '') if header&.match(pattern) + header.strip + end - 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 - - def render_success(code: nil, message: nil, data: nil) - @response = { code: code || 1000, message: message || 'Command completed successfully', - data: 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 - render(json: @response, status: :ok) - end + 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) + end end end end diff --git a/app/controllers/api/v1/accreditation_center/base_controller.rb b/app/controllers/api/v1/accreditation_center/base_controller.rb index 75e94919a..4a3cb1fa5 100644 --- a/app/controllers/api/v1/accreditation_center/base_controller.rb +++ b/app/controllers/api/v1/accreditation_center/base_controller.rb @@ -17,7 +17,7 @@ module Api private - def check_ip_whitelist + def check_ip_whitelist allowed_ips = ENV['accr_center_api_auth_allowed_ips'].to_s.split(',').map(&:strip) return if allowed_ips.include?(request.ip) || Rails.env.development? diff --git a/app/controllers/api/v1/accreditation_center/contacts_controller.rb b/app/controllers/api/v1/accreditation_center/contacts_controller.rb index 7463aef54..0ab8665e3 100644 --- a/app/controllers/api/v1/accreditation_center/contacts_controller.rb +++ b/app/controllers/api/v1/accreditation_center/contacts_controller.rb @@ -1,4 +1,4 @@ -require 'serializers/repp/domain' +require 'serializers/repp/contact' module Api module V1 @@ -9,7 +9,8 @@ module Api if @contact render json: { contact: Serializers::Repp::Contact.new(@contact, - show_address: false).to_json }, status: :found + show_address: false).to_json }, + status: :found else render json: { errors: 'Contact not found' }, status: :not_found end diff --git a/app/controllers/api/v1/accreditation_center/domains_controller.rb b/app/controllers/api/v1/accreditation_center/domains_controller.rb index d51420568..6c6a753df 100644 --- a/app/controllers/api/v1/accreditation_center/domains_controller.rb +++ b/app/controllers/api/v1/accreditation_center/domains_controller.rb @@ -9,7 +9,8 @@ module Api if @domain render json: { domain: Serializers::Repp::Domain.new(@domain, - sponsored: true).to_json }, status: :found + sponsored: true).to_json }, + status: :found else render json: { errors: 'Domain not found' }, status: :not_found end diff --git a/test/integration/api/accreditation_center/auth_test.rb b/test/integration/api/accreditation_center/auth_test.rb new file mode 100644 index 000000000..6626762e7 --- /dev/null +++ b/test/integration/api/accreditation_center/auth_test.rb @@ -0,0 +1,33 @@ +require 'test_helper' + +class AuthTest < ApplicationIntegrationTest + def setup + super + + @user = users(:api_bestnames) + @header = { 'Authorization' => "Basic #{generate_base64}" } + end + + def test_should_return_successful + get 'https://registry.test/api/v1/accreditation_center/auth', headers: @header + + json = JSON.parse(response.body, symbolize_names: true) + assert_equal json[:code], 1000 + assert_equal json[:message], 'Command completed successfully' + end + + def test_should_return_failed + get 'https://registry.test/api/v1/accreditation_center/auth', headers: { 'Authorization' => "Basic LAHSDHDSAFSF#@" } + + json = JSON.parse(response.body, symbolize_names: true) + + assert_equal json[:code], 2202 + assert_equal json[:message], 'Invalid authorization information' + end + + private + + def generate_base64 + Base64.encode64("#{@user.username}:#{@user.plain_text_password}") + end +end diff --git a/test/integration/api/accreditation_center/contacts_test.rb b/test/integration/api/accreditation_center/contacts_test.rb index b3ca27e3f..0770d663e 100644 --- a/test/integration/api/accreditation_center/contacts_test.rb +++ b/test/integration/api/accreditation_center/contacts_test.rb @@ -4,13 +4,20 @@ class ContactsTest < ApplicationIntegrationTest def setup super - @contact = contacts(:john) + @contact = contacts(:john) end - def test_return_code_error_if_valid_domain_name - get "/api/v1/accreditation_center/contacts/?id=Alyosha" - json = JSON.parse(response.body, symbolize_names: true) + def test_return_code_error_if_valid_domain_name + get '/api/v1/accreditation_center/contacts/?id=Alyosha' + json = JSON.parse(response.body, symbolize_names: true) - assert_equal json[:errors], "Contact not found" - end -end \ No newline at end of file + assert_equal json[:errors], 'Contact not found' + end + + def test_return_code_error_if_sdfsdf + get "/api/v1/accreditation_center/contacts/?id=#{@contact.code}" + json = JSON.parse(response.body, symbolize_names: true) + + assert_equal json[:contact][:name], 'John' + end +end