Add Registrant/Contacts endpoint

This commit is contained in:
Maciej Szlosarczyk 2018-08-01 14:57:41 +03:00
parent fe08fccf63
commit 3673c69319
No known key found for this signature in database
GPG key ID: 41D62D42D3B0D765
3 changed files with 127 additions and 0 deletions

View file

@ -0,0 +1,47 @@
require 'rails5_api_controller_backport'
require 'auth_token/auth_token_decryptor'
module Api
module V1
module Registrant
class ContactsController < BaseController
before_action :set_contacts_pool
def index
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
@contacts = @contacts_pool.limit(limit).offset(offset)
render json: @contacts
end
def show
@contact = @contacts_pool.find_by(uuid: params[:uuid])
if @contact
render json: @contact
else
render json: { errors: ['Contact not found'] }, status: :not_found
end
end
private
def set_contacts_pool
country_code, ident = current_user.registrant_ident.to_s.split '-'
@contacts_pool = Contact.where(country_code: country_code, ident: ident)
end
end
end
end
end

View file

@ -24,6 +24,7 @@ Rails.application.routes.draw do
post 'auth/eid', to: 'auth#eid'
resources :domains, only: [:index]
resources :contacts, only: %i[index show]
end
end
end

View file

@ -0,0 +1,79 @@
require 'test_helper'
require 'auth_token/auth_token_creator'
class RegistrantApiContactsTest < ActionDispatch::IntegrationTest
def setup
super
@user = users(:registrant)
@auth_headers = { 'HTTP_AUTHORIZATION' => auth_token }
end
def test_root_returns_domain_list
get '/api/v1/registrant/contacts', {}, @auth_headers
assert_equal(200, response.status)
json_body = JSON.parse(response.body, symbolize_names: true)
assert_equal(2, json_body.count)
array_of_contact_codes = json_body.map { |x| x[:code] }
assert(array_of_contact_codes.include?('william-001'))
assert(array_of_contact_codes.include?('william-002'))
end
def test_root_accepts_limit_and_offset_parameters
get '/api/v1/registrant/contacts', { 'limit' => 1, 'offset' => 0 }, @auth_headers
response_json = JSON.parse(response.body, symbolize_names: true)
assert_equal(200, response.status)
assert_equal(1, response_json.count)
get '/api/v1/registrant/contacts', {}, @auth_headers
response_json = JSON.parse(response.body, symbolize_names: true)
assert_equal(2, response_json.count)
end
def test_root_does_not_accept_limit_higher_than_200
get '/api/v1/registrant/contacts', { '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/contacts', { '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
get '/api/v1/registrant/contacts', {}, {}
assert_equal(401, response.status)
json_body = JSON.parse(response.body, symbolize_names: true)
assert_equal({ errors: ['Not authorized'] }, json_body)
end
def test_details_returns_401_without_authorization
get '/api/v1/registrant/contacts/c0a191d5-3793-4f0b-8f85-491612d0293e', {}, {}
assert_equal(401, response.status)
json_body = JSON.parse(response.body, symbolize_names: true)
assert_equal({ errors: ['Not authorized'] }, json_body)
end
def test_details_returns_404_for_non_existent_contact
get '/api/v1/registrant/contacts/some-random-uuid', {}, @auth_headers
assert_equal(404, response.status)
json_body = JSON.parse(response.body, symbolize_names: true)
assert_equal({ errors: ['Contact not found'] }, json_body)
end
private
def auth_token
token_creator = AuthTokenCreator.create_with_defaults(@user)
hash = token_creator.token_in_hash
"Bearer #{hash[:access_token]}"
end
end