diff --git a/app/controllers/api/v1/registrant/domains_controller.rb b/app/controllers/api/v1/registrant/domains_controller.rb index fdfc6872c..27b7b6125 100644 --- a/app/controllers/api/v1/registrant/domains_controller.rb +++ b/app/controllers/api/v1/registrant/domains_controller.rb @@ -1,18 +1,47 @@ require 'rails5_api_controller_backport' -require 'auth_token/auth_token_decryptor' module Api module V1 module Registrant class DomainsController < BaseController def index - registrant = ::Registrant.find_by(ident: current_user.registrant_ident) - if registrant - domains = Domain.where(registrant_id: registrant.id) - render json: domains - else - render json: [] + limit = params[:limit] || 200 + offset = params[:offset] || 0 + + 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 + render json: { errors: [{ base: ['Domain not found'] }] }, status: :not_found + 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 diff --git a/config/routes.rb b/config/routes.rb index 3ae18a7cd..74468c79b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,7 +23,7 @@ Rails.application.routes.draw do namespace :registrant do post 'auth/eid', to: 'auth#eid' - resources :domains, only: [:index] + resources :domains, only: [:index, :show], param: :uuid end end end diff --git a/test/fixtures/domains.yml b/test/fixtures/domains.yml index 59a1b8ea5..4d6468c92 100644 --- a/test/fixtures/domains.yml +++ b/test/fixtures/domains.yml @@ -42,10 +42,20 @@ metro: period_unit: m 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: name: invalid.test transfer_code: 1438d6 valid_to: <%= Time.zone.parse('2010-07-05').utc.to_s(:db) %> registrar: bestnames registrant: invalid - uuid: 3c430ead-bb17-4b5b-aaa1-caa7dde7e138 \ No newline at end of file + uuid: 3c430ead-bb17-4b5b-aaa1-caa7dde7e138 diff --git a/test/integration/api/registrant/registrant_api_domains_test.rb b/test/integration/api/registrant/registrant_api_domains_test.rb index 9fc92ce62..128d15e20 100644 --- a/test/integration/api/registrant/registrant_api_domains_test.rb +++ b/test/integration/api/registrant/registrant_api_domains_test.rb @@ -5,13 +5,75 @@ class RegistrantApiDomainsTest < ApplicationIntegrationTest def setup 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) @auth_headers = { 'HTTP_AUTHORIZATION' => auth_token } 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 get '/api/v1/registrant/domains', {}, @auth_headers 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 def test_root_returns_401_without_authorization @@ -22,6 +84,14 @@ class RegistrantApiDomainsTest < ApplicationIntegrationTest assert_equal({ errors: [base: ['Not authorized']] }, json_body) 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 def auth_token