Merge pull request #926 from internetee/registry-922

Registrant API domain endpoint
This commit is contained in:
Timo Võhmar 2018-08-09 14:03:36 +03:00 committed by GitHub
commit 71c74a66f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 118 additions and 9 deletions

View file

@ -1,18 +1,47 @@
require 'rails5_api_controller_backport' require 'rails5_api_controller_backport'
require 'auth_token/auth_token_decryptor'
module Api module Api
module V1 module V1
module Registrant module Registrant
class DomainsController < BaseController class DomainsController < BaseController
def index def index
registrant = ::Registrant.find_by(ident: current_user.registrant_ident) limit = params[:limit] || 200
if registrant offset = params[:offset] || 0
domains = Domain.where(registrant_id: registrant.id)
render json: domains if limit.to_i > 200 || limit.to_i < 1
render(json: { errors: [{ limit: ['parameter is out of range'] }] },
status: :bad_request) && return
end
if offset.to_i.negative?
render(json: { errors: [{ offset: ['parameter is out of range'] }] },
status: :bad_request) && return
end
@domains = associated_domains(current_user).limit(limit).offset(offset)
render json: @domains
end
def show
domain_pool = associated_domains(current_user)
@domain = domain_pool.find_by(uuid: params[:uuid])
if @domain
render json: @domain
else else
render json: [] render json: { errors: [{ base: ['Domain not found'] }] }, status: :not_found
end end
end
private
def associated_domains(user)
country_code, ident = user.registrant_ident.split('-')
BusinessRegistryCache.fetch_associated_domains(ident, country_code)
rescue Soap::Arireg::NotAvailableError => error
Rails.logger.fatal("[EXCEPTION] #{error}")
user.domains
end end
end end
end end

View file

@ -23,7 +23,7 @@ Rails.application.routes.draw do
namespace :registrant do namespace :registrant do
post 'auth/eid', to: 'auth#eid' post 'auth/eid', to: 'auth#eid'
resources :domains, only: [:index] resources :domains, only: [:index, :show], param: :uuid
end end
end end
end end

View file

@ -42,6 +42,16 @@ metro:
period_unit: m period_unit: m
uuid: ef97cb80-333b-4893-b9df-163f2b452798 uuid: ef97cb80-333b-4893-b9df-163f2b452798
hospital:
name: hospital.test
registrar: goodnames
registrant: john
transfer_code: 23118v2
valid_to: 2010-07-05
period: 1
period_unit: m
uuid: 5edda1a5-3548-41ee-8b65-6d60daf85a37
invalid: invalid:
name: invalid.test name: invalid.test
transfer_code: 1438d6 transfer_code: 1438d6

View file

@ -5,13 +5,75 @@ class RegistrantApiDomainsTest < ApplicationIntegrationTest
def setup def setup
super super
@original_registry_time = Setting.days_to_keep_business_registry_cache
Setting.days_to_keep_business_registry_cache = 1
travel_to Time.zone.parse('2010-07-05')
@domain = domains(:hospital)
@registrant = @domain.registrant
@user = users(:registrant) @user = users(:registrant)
@auth_headers = { 'HTTP_AUTHORIZATION' => auth_token } @auth_headers = { 'HTTP_AUTHORIZATION' => auth_token }
end end
def teardown
super
Setting.days_to_keep_business_registry_cache = @original_registry_time
travel_back
end
def test_get_domain_details_by_uuid
get '/api/v1/registrant/domains/5edda1a5-3548-41ee-8b65-6d60daf85a37', {}, @auth_headers
assert_equal(200, response.status)
domain = JSON.parse(response.body, symbolize_names: true)
assert_equal('hospital.test', domain[:name])
end
def test_get_non_existent_domain_details_by_uuid
get '/api/v1/registrant/domains/random-uuid', {}, @auth_headers
assert_equal(404, response.status)
response_json = JSON.parse(response.body, symbolize_names: true)
assert_equal({ errors: [base: ['Domain not found']] }, response_json)
end
def test_root_returns_domain_list def test_root_returns_domain_list
get '/api/v1/registrant/domains', {}, @auth_headers get '/api/v1/registrant/domains', {}, @auth_headers
assert_equal(200, response.status) assert_equal(200, response.status)
response_json = JSON.parse(response.body, symbolize_names: true)
array_of_domain_names = response_json.map { |x| x[:name] }
assert(array_of_domain_names.include?('hospital.test'))
end
def test_root_accepts_limit_and_offset_parameters
get '/api/v1/registrant/domains', { 'limit' => 2, 'offset' => 0 }, @auth_headers
response_json = JSON.parse(response.body, symbolize_names: true)
assert_equal(200, response.status)
assert_equal(2, response_json.count)
get '/api/v1/registrant/domains', {}, @auth_headers
response_json = JSON.parse(response.body, symbolize_names: true)
assert_equal(5, response_json.count)
end
def test_root_does_not_accept_limit_higher_than_200
get '/api/v1/registrant/domains', { 'limit' => 400, 'offset' => 0 }, @auth_headers
assert_equal(400, response.status)
response_json = JSON.parse(response.body, symbolize_names: true)
assert_equal({ errors: [{ limit: ['parameter is out of range'] }] }, response_json)
end
def test_root_does_not_accept_offset_lower_than_0
get '/api/v1/registrant/domains', { 'limit' => 200, 'offset' => "-10" }, @auth_headers
assert_equal(400, response.status)
response_json = JSON.parse(response.body, symbolize_names: true)
assert_equal({ errors: [{ offset: ['parameter is out of range'] }] }, response_json)
end end
def test_root_returns_401_without_authorization def test_root_returns_401_without_authorization
@ -22,6 +84,14 @@ class RegistrantApiDomainsTest < ApplicationIntegrationTest
assert_equal({ errors: [base: ['Not authorized']] }, json_body) assert_equal({ errors: [base: ['Not authorized']] }, json_body)
end end
def test_details_returns_401_without_authorization
get '/api/v1/registrant/domains/5edda1a5-3548-41ee-8b65-6d60daf85a37', {}, {}
assert_equal(401, response.status)
json_body = JSON.parse(response.body, symbolize_names: true)
assert_equal({ errors: [base: ['Not authorized']] }, json_body)
end
private private
def auth_token def auth_token