Skip to content

Commit

Permalink
refactor; fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Nov 21, 2024
1 parent e493c0c commit 02ebf69
Show file tree
Hide file tree
Showing 10 changed files with 4 additions and 130 deletions.
22 changes: 0 additions & 22 deletions app/controllers/api/v1/business_registry/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ module Api
module V1
module BusinessRegistry
class BaseController < ::Api::V1::BaseController
include Concerns::CorsHeaders
include Concerns::TokenAuthentication

before_action :authenticate

protected

def render_error(message, status, details = nil)
error_response = { error: message }
error_response[:details] = details if details
Expand All @@ -20,21 +13,6 @@ def render_success(data, status = :ok) = render(json: data, status: status)
def allowed_ips
ENV['business_registry_allowed_ips'].to_s.split(',').map(&:strip)
end

private

def extract_token_from_header = request.headers['Authorization']&.split(' ')&.last

def find_reserved_domain_status
token = extract_token_from_header
@reserved_domain_status = ReservedDomainStatus.find_by(access_token: token)

if @reserved_domain_status.nil?
render json: { error: "Invalid token" }, status: :unauthorized
elsif @reserved_domain_status.token_expired?
render json: { error: "Token expired. Please refresh the token. TODO: provide endpoint" }, status: :unauthorized
end
end
end
end
end
Expand Down
33 changes: 0 additions & 33 deletions app/controllers/api/v1/business_registry/concerns/cors_headers.rb

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
module Api
module V1
module BusinessRegistry
class DomainNamesController < ::Api::V1::BusinessRegistry::BaseController
before_action :authenticate, only: [:show]
skip_before_action :find_reserved_domain, only: [:show]

include Concerns::CorsHeaders

class DomainNamesController < BaseController
before_action :validate_organization_name

def show
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module Api
module V1
module BusinessRegistry
class LongReserveDomainsController < BaseController
before_action :authenticate, only: [:create]
skip_before_action :find_reserved_domain, only: [:create]
before_action :domain_names
before_action :validate_params
before_action :available_domains?, only: [:create]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ module Api
module V1
module BusinessRegistry
class LongReserveDomainsStatusController < BaseController
before_action :authenticate, only: [:create]
skip_before_action :find_reserved_domain, only: [:show]

before_action :set_reserved_domain_invoice, only: [:show]

def show
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,8 @@ module Api
module V1
module BusinessRegistry
class RegistrationCodeController < BaseController
before_action :authenticate, only: [:show]
before_action :find_reserved_domain_status, only: [:show]
skip_before_action :find_reserved_domain, only: [:show]

def show
puts '---'
puts @reserved_domain_status.inspect
puts '---'

if @reserved_domain_status.paid?
password = ReservedDomain.find(@reserved_domain_status.reserved_domain_id).password
render_success({ name: 'name', registration_code: password })
else
render json: { error: 'Domain not paid', linkpay_url: @reserved_domain_status.linkpay_url }, status: :unprocessable_entity
end
# TODO: get reservation code by invoice number and user unique id
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module Api
module V1
module BusinessRegistry
class ReserveDomainsController < BaseController
before_action :authenticate, only: [:create]
skip_before_action :find_reserved_domain, only: [:create]
before_action :validate_params

def create
Expand Down
Binary file modified ettevotja_rekvisiidid__lihtandmed.csv.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,23 @@
class Api::V1::BusinessRegistry::DomainNamesControllerTest < ActionDispatch::IntegrationTest
setup do
@allowed_origins = ['http://example.com', 'https://test.com']
ENV['ALLOWED_ORIGINS'] = @allowed_origins.join(',')
@valid_ip = '127.0.0.1'
ENV['auction_api_allowed_ips'] = @valid_ip
end

test "should return list of available organization domain names" do
get api_v1_business_registry_domain_names_path(organization_name: "Test Company AS"),
headers: { 'Origin' => @allowed_origins.first, 'REMOTE_ADDR' => @valid_ip }
get api_v1_business_registry_domain_names_path(organization_name: "Test Company AS")

assert_response :success
assert_equal @allowed_origins.first, response.headers['Access-Control-Allow-Origin']
json_response = JSON.parse(response.body)
assert_equal json_response['variants'], ["testcompany.test", "test-company.test"]
end

test "should handle invalid organization name" do
get api_v1_business_registry_domain_names_path(organization_name: "Invalid!@#Name"),
headers: { 'Origin' => @allowed_origins.first, 'REMOTE_ADDR' => @valid_ip }
get api_v1_business_registry_domain_names_path(organization_name: "Invalid!@#Name")

assert_response :bad_request
json_response = JSON.parse(response.body)
assert_equal 'Invalid organization name', json_response['error']
end

test "should not allow access from unauthorized IP" do
get api_v1_business_registry_domain_names_path(organization_name: "Test Company"),
headers: { 'Origin' => @allowed_origins.first, 'REMOTE_ADDR' => '192.168.1.1' }

assert_response :unauthorized
end
end

0 comments on commit 02ebf69

Please sign in to comment.