Implement domain transfer API

#660
This commit is contained in:
Artur Beljajev 2018-01-24 13:38:51 +02:00
parent 05bdb11ee3
commit 0751a69d83
3 changed files with 69 additions and 17 deletions

View file

@ -4,13 +4,44 @@ module Repp
resource :domain_transfers do resource :domain_transfers do
post '/' do post '/' do
params['domainTransfers'].each do |domain_transfer| params do
requires :data, type: Hash do
requires :domainTransfers, type: Array do
requires :domainName, type: String, allow_blank: false
requires :transferCode, type: String, allow_blank: false
end
end
end
new_registrar = current_user.registrar
request_domain_transfers = params['data']['domainTransfers']
response_domain_transfers = []
errors = []
request_domain_transfers.each do |domain_transfer|
domain_name = domain_transfer['domainName'] domain_name = domain_transfer['domainName']
transfer_code = domain_transfer['transferCode'] transfer_code = domain_transfer['transferCode']
new_registrar = current_user.registrar
domain = Domain.find_by(name: domain_name) domain = Domain.find_by(name: domain_name)
domain.transfer(registrar: new_registrar)
if domain
if domain.transfer_code == transfer_code
domain.transfer(new_registrar)
else
errors << { title: "#{domain_name} transfer code is wrong" }
end
else
errors << { title: "#{domain_name} does not exist" }
end
response_domain_transfers << { domainName: domain_name }
end
if errors.none?
status 204
else
status 400
{ errors: errors }
end end
end end
end end

View file

@ -11,6 +11,7 @@ module Concerns::Domain::Transferable
self.registrar = new_registrar self.registrar = new_registrar
regenerate_transfer_code regenerate_transfer_code
transaction do
domain_transfers.create!( domain_transfers.create!(
transfer_requested_at: Time.zone.now, transfer_requested_at: Time.zone.now,
old_registrar: old_registrar, old_registrar: old_registrar,
@ -18,6 +19,8 @@ module Concerns::Domain::Transferable
) )
transfer_contacts(new_registrar) transfer_contacts(new_registrar)
save!
end
end end
private private

View file

@ -1,19 +1,37 @@
require 'test_helper' require 'test_helper'
class Repp::DomainTransfersTest < ActionDispatch::IntegrationTest class Repp::DomainTransfersTest < ActionDispatch::IntegrationTest
def test_post_to_domain_transfers def test_transfers_domain
request_params = { format: :json, domainTransfers: [{ domainName: domains(:shop).name, transferCode: domains(:shop).transfer_code }] } request_params = { format: :json,
post '/repp/v1/domain_transfers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key } data: { domainTransfers: [{ domainName: 'shop.test', transferCode: '65078d5' }] } }
assert_response :created
assert_difference -> { domains(:shop).domain_transfers.count } do
post '/repp/v1/domain_transfers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key } post '/repp/v1/domain_transfers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key }
assert_response 204
assert_equal registrars(:goodnames), domains(:shop).registrar
assert_empty response.body
end end
def test_fails_if_domain_does_not_exist
request_params = { format: :json,
data: { domainTransfers: [{ domainName: 'non-existent.test', transferCode: 'any' }] } }
post '/repp/v1/domain_transfers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key }
assert_response 400
assert_equal ({ errors: [{ title: 'non-existent.test does not exist' }] }),
JSON.parse(response.body, symbolize_names: true)
end
def test_fails_if_transfer_code_is_wrong
request_params = { format: :json,
data: { domainTransfers: [{ domainName: 'shop.test', transferCode: 'wrong' }] } }
post '/repp/v1/domain_transfers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key }
assert_response 400
refute_equal registrars(:goodnames), domains(:shop).registrar
assert_equal ({ errors: [{ title: 'shop.test transfer code is wrong' }] }),
JSON.parse(response.body, symbolize_names: true)
end end
private private
def http_auth_key def http_auth_key
ActionController::HttpAuthentication::Basic.encode_credentials(users(:api_bestnames).username, users(:api_bestnames).password) ActionController::HttpAuthentication::Basic.encode_credentials('test_goodnames', 'testtest')
end end
end end