Add mock for dnsimple and fix tests for non mock mode

This commit is contained in:
Trae Robrock 2013-07-28 11:07:29 -07:00
parent 8767ee1f30
commit 25f50f761e
10 changed files with 164 additions and 27 deletions

View File

@ -29,7 +29,10 @@ module Fog
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {}
hash[key] = {
:domains => [],
:records => {}
}
end
end

View File

@ -22,6 +22,42 @@ module Fog
end
end
class Mock
def create_domain(name)
response = Excon::Response.new
response.status = 201
body = {
"domain" => {
"auto_renew" => nil,
"created_at" => Time.now.iso8601,
"expires_on" => Date.today + 365,
"id" => Fog::Mock.random_numbers(1).to_i,
"language" => nil,
"lockable" => true,
"name" => name,
"name_server_status" => "unknown",
"registrant_id" => nil,
"state" => "registered",
"token" => "4fIFYWYiJayvL2tkf_mkBkqC4L+4RtYqDA",
"unicode_name" => name,
"updated_at" => Time.now.iso8601,
"user_id" => 1,
"record_count" => 0,
"service_count" => 0,
"private_whois?" => false
}
}
self.data[:domains] << body
response.body = body
response
end
end
end
end
end

View File

@ -43,6 +43,29 @@ module Fog
end
end
class Mock
def create_record(domain, name, type, content, options = {})
response = Excon::Response.new
response.status = 201
body = {
"record" => {
"name" => name,
"record_type" => type,
"content" => content
}.merge(options)
}
self.data[:records][domain] ||= []
self.data[:records][domain] << body
response.body = body
response
end
end
end
end
end

View File

@ -21,6 +21,18 @@ module Fog
end
end
class Mock
def delete_domain(name)
self.data[:records].delete name
self.data[:domains].reject! { |domain| domain["domain"]["name"] == name }
response = Excon::Response.new
response.status = 200
response
end
end
end
end
end

View File

@ -16,6 +16,18 @@ module Fog
end
end
class Mock
def delete_record(domain, record_id)
self.data[:records][domain].reject! { |record| record["record"]["id"] == record_id }
response = Excon::Response.new
response.status = 200
response
end
end
end
end
end

View File

@ -32,6 +32,18 @@ module Fog
end
end
class Mock
def get_domain(id)
domain = self.data[:domains].detect { |domain| domain["domain"]["id"] == id }
response = Excon::Response.new
response.status = 200
response.body = domain
response
end
end
end
end
end

View File

@ -30,6 +30,18 @@ module Fog
end
end
class Mock
def list_domains
response = Excon::Response.new
response.status = 200
response.body = self.data[:domains]
response
end
end
end
end
end

View File

@ -27,6 +27,18 @@ module Fog
end
end
class Mock
def list_records(domain)
response = Excon::Response.new
response.status = 200
response.body = self.data[:records][domain]
response
end
end
end
end
end

View File

@ -37,6 +37,26 @@ module Fog
end
end
class Mock
def update_record(domain, record_id, options)
record = self.data[:records][domain].detect { |record| record["record"]["id"] == record_id }
response = Excon::Response.new
if record.nil?
response.status = 400
else
response.status = 200
record["record"].merge!(options)
record["record"]["updated_at"] = Time.now.iso8601
response.body = record
end
response
end
end
end
end
end

View File

@ -1,13 +1,11 @@
Shindo.tests('Fog::DNS[:dnsimple] | DNS requests', ['dnsimple', 'dns']) do
@domain = ''
@domain = nil
@domain_count = 0
tests("success") do
test("get current domain count") do
pending if Fog.mocking?
response = Fog::DNS[:dnsimple].list_domains()
if response.status == 200
@domain_count = response.body.size
@ -17,8 +15,6 @@ Shindo.tests('Fog::DNS[:dnsimple] | DNS requests', ['dnsimple', 'dns']) do
end
test("create domain") do
pending if Fog.mocking?
domain = generate_unique_domain
response = Fog::DNS[:dnsimple].create_domain(domain)
if response.status == 201
@ -29,15 +25,11 @@ Shindo.tests('Fog::DNS[:dnsimple] | DNS requests', ['dnsimple', 'dns']) do
end
test("get domain by id") do
pending if Fog.mocking?
response = Fog::DNS[:dnsimple].get_domain(@domain["id"])
response.status == 200
end
test("create an A resource record") do
pending if Fog.mocking?
domain = @domain["name"]
name = "www"
type = "A"
@ -53,48 +45,56 @@ Shindo.tests('Fog::DNS[:dnsimple] | DNS requests', ['dnsimple', 'dns']) do
end
test("create a MX record") do
pending if Fog.mocking?
domain = @domain["name"]
name = ""
type = "MX"
content = "mail.#{domain}"
options = { :ttl => 60, :prio => 10 }
options = { "ttl" => 60, "prio" => 10 }
response = Fog::DNS[:dnsimple].create_record(domain, name, type, content, options)
response.status == 201
test "MX record creation returns 201" do
response.status == 201
end
options.each do |key, value|
test("MX record has option #{key}") { value == response.body["record"][key.to_s] }
end
test "MX record is correct type" do
response.body["record"]["record_type"] == "MX"
end
end
test("update a record") do
pending if Fog.mocking?
domain = @domain["name"]
record_id = @record["id"]
options = { :content => "2.3.4.5", :ttl => 600 }
options = { "content" => "2.3.4.5", "ttl" => 600 }
response = Fog::DNS[:dnsimple].update_record(domain, record_id, options)
response.status == 200
end
test("list records") do
pending if Fog.mocking?
response = Fog::DNS[:dnsimple].list_records(@domain["name"])
if response.status == 200
@records = response.body
end
(response.status == 200) and (response.body.size == 2)
test "list records returns all records for domain" do
@records.reject { |record| record["record"]["system_record"] }.size == 2
end
response.status == 200
end
test("delete records") do
pending if Fog.mocking?
domain = @domain["name"]
result = true
@records.each do |record|
next if record["record"]["system_record"]
response = Fog::DNS[:dnsimple].delete_record(domain, record["record"]["id"])
if(response.status != 200)
if response.status != 200
result = false
break
end
@ -104,15 +104,10 @@ Shindo.tests('Fog::DNS[:dnsimple] | DNS requests', ['dnsimple', 'dns']) do
end
test("delete domain") do
pending if Fog.mocking?
response = Fog::DNS[:dnsimple].delete_domain(@domain["name"])
response.status == 200
end
end
tests( 'failure') do
end
end