internetee-registry/spec/support/epp.rb
2014-10-24 17:20:34 +03:00

181 lines
5.1 KiB
Ruby

module Epp
def read_body(filename)
File.read("spec/epp/requests/#{filename}")
end
# handles connection and login automatically
def epp_request(data, *args)
server = server_zone
server = server_elkdata if args.include?(:elkdata)
return parse_response(server.request(data)) if args.include?(:xml)
return parse_response(server.request(read_body(data)))
rescue => e
e
end
def epp_plain_request(data, *args)
server = server_gitlab
server = server_elkdata if args.include?(:elkdata)
server = server_zone if args.include?(:zone)
return parse_response(server.send_request(data)) if args.include?(:xml)
return parse_response(server.send_request(read_body(data)))
rescue => e
e
end
def parse_response(raw)
res = Nokogiri::XML(raw)
obj = {
results: [],
clTRID: res.css('epp trID clTRID').text,
parsed: res.remove_namespaces!,
raw: raw
}
res.css('epp response result').each do |x|
obj[:results] << { result_code: x[:code], msg: x.css('msg').text, value: x.css('value > *').try(:first).try(:text) }
end
obj[:result_code] = obj[:results][0][:result_code]
obj[:msg] = obj[:results][0][:msg]
obj
end
# print output
def po(r)
puts r[:parsed].to_s
end
### REQUEST TEMPLATES ###
def domain_renew_xml(xml_params = {})
xml = Builder::XmlMarkup.new
xml.instruct!(:xml, standalone: 'no')
xml.epp('xmlns' => 'urn:ietf:params:xml:ns:epp-1.0') do
xml.command do
xml.renew do
xml.tag!('domain:renew', 'xmlns:domain' => 'urn:ietf:params:xml:ns:domain-1.0') do
xml.tag!('domain:name', (xml_params[:name] || 'example.ee')) if xml_params[:name] != false
xml.tag!('domain:curExpDate', (xml_params[:curExpDate] || '2014-08-07')) if xml_params[:curExpDate] != false
if xml_params[:period] != false
xml.tag!('domain:period', (xml_params[:period_value] || 1), 'unit' => (xml_params[:period_unit] || 'y'))
end
end
end
xml.clTRID 'ABC-12345'
end
end
end
def domain_update_xml(xml_params = {}, dnssec_params = false)
defaults = {
name: { value: 'example.ee' }
}
xml_params = defaults.deep_merge(xml_params)
xml = Builder::XmlMarkup.new
xml.instruct!(:xml, standalone: 'no')
xml.epp('xmlns' => 'urn:ietf:params:xml:ns:epp-1.0') do
xml.command do
xml.update do
xml.tag!('domain:update', 'xmlns:domain' => 'urn:ietf:params:xml:ns:domain-1.0') do
generate_xml_from_hash(xml_params, xml, 'domain:')
end
end
xml.extension do
xml.tag!('secDNS:create', 'xmlns:secDNS' => 'urn:ietf:params:xml:ns:secDNS-1.1') do
generate_xml_from_hash(dnssec_params, xml, 'secDNS:')
end
end if dnssec_params != false
xml.clTRID 'ABC-12345'
end
end
end
def generate_xml_from_hash(xml_params, xml, ns = '')
xml_params.each do |k, v|
# Value is a hash which has string type value
if v.is_a?(Hash) && v[:value].is_a?(String)
xml.tag!("#{ns}#{k}", v[:value], v[:attrs])
# Value is a hash which is nested
elsif v.is_a?(Hash)
xml.tag!("#{ns}#{k}") do
generate_xml_from_hash(v, xml, ns)
end
# Value is an array
elsif v.is_a?(Array)
if k.to_s.start_with?('_')
v.each do |x|
generate_xml_from_hash(x, xml, ns)
end
else
xml.tag!("#{ns}#{k}") do
v.each do |x|
generate_xml_from_hash(x, xml, ns)
end
end
end
end
end
end
def domain_transfer_xml(xml_params = {})
xml_params[:name] = xml_params[:name] || 'example.ee'
xml_params[:pw] = xml_params[:pw] || '98oiewslkfkd'
xml_params[:op] = xml_params[:op] || 'query'
xml_params[:roid] = xml_params[:roid] || 'JD1234-REP'
xml = Builder::XmlMarkup.new
xml.instruct!(:xml, standalone: 'no')
xml.epp('xmlns' => 'urn:ietf:params:xml:ns:epp-1.0') do
xml.command do
xml.transfer('op' => xml_params[:op]) do
xml.tag!('domain:transfer', 'xmlns:domain' => 'urn:ietf:params:xml:ns:domain-1.0') do
if xml_params[:name] != false
xml.tag!('domain:name', xml_params[:name])
end
xml.tag!('domain:authInfo') do
xml.tag!('domain:pw', xml_params[:pw], 'roid' => xml_params[:roid])
end if xml_params[:authInfo] != false
end
end
xml.clTRID 'ABC-12345'
end
end
end
def domain_delete_xml(xml_params = {})
xml_params[:name] = xml_params[:name] || 'example.ee'
xml = Builder::XmlMarkup.new
xml.instruct!(:xml, standalone: 'no')
xml.epp('xmlns' => 'urn:ietf:params:xml:ns:epp-1.0') do
xml.command do
xml.delete do
xml.tag!('domain:delete', 'xmlns:domain' => 'urn:ietf:params:xml:ns:domain-1.0') do
if xml_params[:name] != false
xml.tag!('domain:name', xml_params[:name])
end
end
end
xml.clTRID 'ABC-12345'
end
end
end
end
RSpec.configure do |c|
c.include Epp, epp: true
end