mirror of
https://github.com/internetee/registry.git
synced 2025-06-10 22:54:47 +02:00
Merge branch 'master' into registrant-api-contacts-controller
This commit is contained in:
commit
9dd9d4b1d6
7 changed files with 156 additions and 34 deletions
|
@ -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
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
module DomainVersionObserver
|
||||
extend ActiveSupport::Concern
|
||||
end
|
|
@ -1,21 +0,0 @@
|
|||
- content_for :actions do
|
||||
= render 'shared/title', name: t(:registrars)
|
||||
|
||||
.row
|
||||
.col-md-12
|
||||
.table-responsive
|
||||
%table.table.table-hover.table-bordered.table-condensed
|
||||
%thead
|
||||
%tr
|
||||
%th{class: 'col-xs-6'}
|
||||
= sort_link(@q, 'name')
|
||||
%th{class: 'col-xs-6'}
|
||||
= sort_link(@q, 'reg_no', Registrar.human_attribute_name(:reg_no))
|
||||
%tbody
|
||||
- @registrars.each do |x|
|
||||
%tr
|
||||
%td= link_to(x, [:registrar, x])
|
||||
%td= x.reg_no
|
||||
.row
|
||||
.col-md-12
|
||||
= paginate @registrars
|
|
@ -23,8 +23,8 @@ Rails.application.routes.draw do
|
|||
namespace :registrant do
|
||||
post 'auth/eid', to: 'auth#eid'
|
||||
|
||||
resources :domains, only: [:index]
|
||||
resources :contacts, only: %i[index show]
|
||||
resources :domains, only: %i[index, show], param: :uuid
|
||||
resources :contacts, only: %i[index show], param: :uuid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
10
test/fixtures/domains.yml
vendored
10
test/fixtures/domains.yml
vendored
|
@ -42,6 +42,16 @@ 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
|
||||
|
|
|
@ -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
|
||||
|
|
37
test/models/contact_test.rb
Normal file
37
test/models/contact_test.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
require 'test_helper'
|
||||
|
||||
class ContactTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@contact = contacts(:john)
|
||||
end
|
||||
|
||||
def test_valid_fixture
|
||||
assert @contact.valid?
|
||||
end
|
||||
|
||||
def test_invalid_without_email
|
||||
@contact.email = ''
|
||||
assert @contact.invalid?
|
||||
end
|
||||
|
||||
def test_email_format_validation
|
||||
@contact.email = 'invalid'
|
||||
assert @contact.invalid?
|
||||
|
||||
@contact.email = 'test@bestmail.test'
|
||||
assert @contact.valid?
|
||||
end
|
||||
|
||||
def test_invalid_without_phone
|
||||
@contact.email = ''
|
||||
assert @contact.invalid?
|
||||
end
|
||||
|
||||
def test_phone_format_validation
|
||||
@contact.phone = '+123.'
|
||||
assert @contact.invalid?
|
||||
|
||||
@contact.phone = '+123.4'
|
||||
assert @contact.valid?
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue