implement api endpoint for record results

This commit is contained in:
Oleg Hasjanov 2021-08-06 12:26:41 +03:00 committed by olegphenomenon
parent d56c61bf84
commit 8a1967bdb9
5 changed files with 104 additions and 57 deletions

View file

@ -6,22 +6,32 @@ module Repp
desc 'check login user and return data' desc 'check login user and return data'
def index def index
@login = current_user login = current_user
registrar = current_user.registrar registrar = current_user.registrar
# name = registrar.name
# reg_no = registrar.reg_no
# rubocop:disable Style/AndOr # rubocop:disable Style/AndOr
render_success(data: nil) and return unless @login render_success(data: nil) and return unless login
# rubocop:enable Style/AndOr # rubocop:enable Style/AndOr
data = @login.as_json(only: %i[id username name reg_no uuid roles accreditation_date accreditation_expire_date]) data = set_values_to_data(login: login, registrar: registrar)
data[:registrar_name] = registrar.name
data[:registrar_reg_no] = registrar.reg_no
render_success(data: data) render_success(data: data)
end end
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
end
end end
end end
end end

View file

@ -2,65 +2,51 @@ module Repp
module V1 module V1
module Registrar module Registrar
class AccreditationResultsController < ActionController::API class AccreditationResultsController < ActionController::API
before_action :authenticate_admin before_action :authenticate_shared_key
# api :POST, 'repp/v1/registrar/push_results' TEMPARY_SECRET_KEY = 'tempary-secret-key'.freeze
api :GET, 'repp/v1/registrar/accreditation/push_results'
api :POST, 'repp/v1/registrar/accreditation/push_results'
desc 'added datetime results' desc 'added datetime results'
def index def create
@login = @current_user username = params[:accreditation_result][:username]
result = params[:accreditation_result][:result]
# rubocop:disable Style/AndOr record_accreditation_result(username, result) if result
render_success(data: nil) and return unless @login rescue ActiveRecord::RecordNotFound
# rubocop:enable Style/AndOr record_not_found(username)
data = @login
render_success(data: data)
end end
# def create
# @login = current_user
# registrar = current_user.registrar
# rubocop:disable Style/AndOr
# render_success(data: nil) and return unless @login
# rubocop:enable Style/AndOr
# user = ApiUser.find(params[:user_id])
# user.accreditation_date = Date.now
# user.save
# data = @login.as_json(only: %i[id username name reg_no uuid roles accreditation_date accreditation_expire_date])
# data[:registrar_name] = registrar.name
# data[:registrar_reg_no] = registrar.reg_no
# render_success(data: data)
# end
private private
def authenticate_admin def record_accreditation_result(username, result)
# TODO: ADD MORE CONDITIONS FOR ACCR ADMIN REQUESTS user = ApiUser.find_by(username: username)
username, password = Base64.urlsafe_decode64(basic_token).split(':')
@current_user ||= User.find_by(username: username, plain_text_password: password)
return if @current_user raise ActiveRecord::RecordNotFound if user.nil?
# return if @current_user.roles.include? "admin"
raise(ArgumentError) user.accreditation_date = DateTime.current
rescue NoMethodError, ArgumentError
@response = { code: 2202, message: 'Invalid authorization information' } return unless user.save
render(json: @response, status: :unauthorized)
render_success(data: { user: user,
result: result,
message: 'Accreditation info successfully added' })
end end
def basic_token def authenticate_shared_key
pattern = /^Basic / api_key = "Basic #{TEMPARY_SECRET_KEY}"
header = request.headers['Authorization'] render_failed unless api_key == request.authorization
header = header.gsub(pattern, '') if header&.match(pattern) end
header.strip
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 end
def render_success(code: nil, message: nil, data: nil) def render_success(code: nil, message: nil, data: nil)

View file

@ -75,7 +75,7 @@ Rails.application.routes.draw do
resource :accreditation, only: [:index] do resource :accreditation, only: [:index] do
collection do collection do
get '/get_info', to: 'accreditation_info#index' get '/get_info', to: 'accreditation_info#index'
get '/push_results', to: 'accreditation_results#index' post '/push_results', to: 'accreditation_results#create'
end end
end end
resources :nameservers do resources :nameservers do

View file

@ -15,7 +15,9 @@ class ReppV1AccreditationInfoTest < ActionDispatch::IntegrationTest
assert_response :ok assert_response :ok
assert_equal json[:data][:username], @user.username assert_equal json[:data][:username], @user.username
assert_equal json[:data][:identity_code], @user.identity_code assert json[:data][:roles].include? 'super'
assert_equal json[:data][:registrar_name], 'Best Names'
assert_equal json[:data][:registrar_reg_no], '1234'
end end
def test_invalid_login def test_invalid_login

View file

@ -0,0 +1,49 @@
require 'test_helper'
class ReppV1AccreditationResultsTest < ActionDispatch::IntegrationTest
TEMPARY_SECRET_KEY = 'tempary-secret-key'.freeze
def setup
@user = users(:api_bestnames)
token = "Basic #{TEMPARY_SECRET_KEY}"
@auth_headers = { 'Authorization' => token }
end
def test_should_return_valid_response
post '/repp/v1/registrar/accreditation/push_results',
headers: @auth_headers,
params: {accreditation_result: {username: @user.username, result: true} }
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal json[:data][:user][:username], @user.username
assert_equal json[:data][:result], "true"
assert_equal json[:data][:message], "Accreditation info successfully added"
end
def test_should_return_valid_response_invalid_authorization
post '/repp/v1/registrar/accreditation/push_results',
headers: { 'Authorization' => 'Basic tempary-secret-ke'},
params: {accreditation_result: {username: @user.username, result: true} }
json = JSON.parse(response.body, symbolize_names: true)
assert_response :unauthorized
assert_equal json[:code], 2202
assert_equal json[:message], 'Invalid authorization information'
end
def test_should_return_valid_response_record_exception
post '/repp/v1/registrar/accreditation/push_results',
headers: @auth_headers,
params: {accreditation_result: { username: "chungachanga", result: true} }
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal json[:code], 2303
assert_equal json[:message], "Object 'chungachanga' does not exist"
end
end