mirror of
https://github.com/internetee/registry.git
synced 2025-05-18 02:09:39 +02:00
parent
05bdb11ee3
commit
0751a69d83
3 changed files with 69 additions and 17 deletions
|
@ -4,13 +4,44 @@ module Repp
|
|||
|
||||
resource :domain_transfers 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']
|
||||
transfer_code = domain_transfer['transferCode']
|
||||
new_registrar = current_user.registrar
|
||||
|
||||
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
|
||||
|
|
|
@ -11,6 +11,7 @@ module Concerns::Domain::Transferable
|
|||
self.registrar = new_registrar
|
||||
regenerate_transfer_code
|
||||
|
||||
transaction do
|
||||
domain_transfers.create!(
|
||||
transfer_requested_at: Time.zone.now,
|
||||
old_registrar: old_registrar,
|
||||
|
@ -18,6 +19,8 @@ module Concerns::Domain::Transferable
|
|||
)
|
||||
|
||||
transfer_contacts(new_registrar)
|
||||
save!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -1,19 +1,37 @@
|
|||
require 'test_helper'
|
||||
|
||||
class Repp::DomainTransfersTest < ActionDispatch::IntegrationTest
|
||||
def test_post_to_domain_transfers
|
||||
request_params = { format: :json, domainTransfers: [{ domainName: domains(:shop).name, transferCode: domains(:shop).transfer_code }] }
|
||||
post '/repp/v1/domain_transfers', request_params, { 'HTTP_AUTHORIZATION' => http_auth_key }
|
||||
assert_response :created
|
||||
|
||||
assert_difference -> { domains(:shop).domain_transfers.count } do
|
||||
def test_transfers_domain
|
||||
request_params = { format: :json,
|
||||
data: { domainTransfers: [{ domainName: 'shop.test', transferCode: '65078d5' }] } }
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
private
|
||||
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue